How do I update an array property in a Palantir Foundry Ontology edit Function? - palantir-foundry

I want to modify an array property on an Object using an Ontology Function (a.k.a FoO), but I'm seeing the following error:
[typescript] Property 'push' does not exist on type 'readonly string[]'.
Looking at the generated TypeScript definition for my Object type, it looks like my array property has type ReadonlyArray<string> | undefined
How can I update this array from my Function?

You need to assign a new value to the property rather than manipulate the existing array in-place.
Array properties on an object type have immutable values to make the semantics for editing an array property clear: the only way to modify the values of an array property is to assign an entirely new array value.
If you want to manipulate the values of an array property, make a copy of it and update that (as described in the Foundry docs):
// Copy to a new array
let arrayCopy = [...myObject.myArrayProperty];
// Now you can modify the copied array
arrayCopy.push(newItem);
// Then overwrite the property value
myObject.myArrayProperty = arrayCopy;

Related

Extract a specified data object that resides inside a PDF form field to an external file

I am looking for a script to extract a data object nested inside a data object to a new data object. Aside from being able to easily clone the object by creating a new one, while I can also extract one of several objects nested inside an object that includes all its related properties and values, the name associated with the property and values is missing. Hence, I require a script to create a new object containing any one of several names including its related properties and values extracted from the existing object provided below.
{"J Doe Company":{"lastUpdate":"01/05/2023","website":"jdoecompany.com","userID":"jdoe1985#gmail.com","password":"igfndhsi1985","primaryCC":"Discover","secondaryCC":"Capital One","primaryBank":"Chase","secondaryBank":"","sq1":"Year Graduated HS","sa1":"1985","sq2":"","sa2":"","notes1":"Sample password record","notes2":""},"Bob The Builder":{"lastUpdate":"01/05/2023","website":"bobthebuilder.com","userID":"bobthebuilder#gmail.com","password":"bbob1985","primaryCC":"Amazon Visa","secondaryCC":"Mastercard","primaryBank":"Wells Fargo","secondaryBank":"","sq1":"First Girlfriend's Name","sa1":"Kaye","sq2":"","sa2":"","notes1":"Sample password record","notes2":""},"SpongeBob Square Pants":{"lastUpdate":"01/07/2023","website":"spongebobsquarepants.com","userID":"spongebob#gmail.com","password":"spongebob1999","primaryCC":"None/Not Applicable","secondaryCC":"","primaryBank":"None/Not Applicable","secondaryBank":"","sq1":"Year show debuted on TV","sa1":"1999","sq2":"","sa2":"","notes1":"Animated TV show for kids","notes2":""}}
You can run cpdf -output-json in.pdf -o put.json and process the result. The format is described in the cpdf manual.
Upon further trial and error after learning more about how to create and edit an object literal, the script required to add an object along with its key-value properties is as follows:
dsFld =getField("dataSrc");// dataSRC is a hidden field containing a JS object converted to a JSON string
oVendors = JSON.parse(dsFld.value); // oVendors is a JS object
oVendors[event.value]=oVendorsPropValues;//oVendorsPropValues = {key-value pairs}
dsFld.value = JSON.stringify(oVendors);// convert the JS obj back to a JSON string

What is a good way to retrieve the associated value from a struct's enum property when the associated value might have multiple possible types?

I have an array of structs that has been decoded from a JSON file. Each struct has a stored property that is a variable-dimension array stored in an enum associated value.
Some of the structs have property Enum.array2D([[Float]]) and others have Enum.array3D([[[Float]]])
Is there a simple or elegant way to extract a variable-type associated value from the struct's enum property - maybe with a getter function? Currently, the only way I know how to do this is with an external switch anytime I want to access the underlaying value. For example, somewhere in external code I have to use this anytime I want to get these values and manipulate them:
switch structArray[index].enumProperty {
case .array2D(let array2Val):
// Do stuff with the 2D array
case .array3D(let array3Val):
// Do stuff with the 3D array
}
I have considered adding each of the two possible types as optionals and setting the correct one in the init function with a switch, but that seems inefficient as I’ll have the arrays stored in two places.

jsonschema: Unique Properties in an Object Array

I have a schema with an array, and I would like to ensure that one property in the array is unique to all other of the same properties in the array. Using uniqueItems only ensures the entire object is unique, not one particular property.
An example will make this question more clear. In the arrays below, I want the key to be unique in the array, but content does not have to be. How would I make a json schema so that below good_array passes but bad_array fails?
good_array = [
{"key":1, "content":"foo"},
{"key":2, "content":"bar"},
{"key":3, "content":"foo"}
]
bad_array = [
{"key":1, "content":"foo"},
{"key":1, "content":"bar"},
{"key":3, "content":"foo"}
]
This is not possible with JSON Schema. Sorry.
Two JSON instances are said to be equal if and only if they are of
the same type and have the same value according to the data model.
Specifically, this means:
both are null; or
both are true; or
both are false; or
both are strings, and are the same codepoint-for-codepoint; or
both are numbers, and have the same mathematical value; or
both are arrays, and have an equal value item-for-item; or
both are objects, and each property in one has exactly one
property with a key equal to the other's, and that other property
has an equal value.
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-01#section-4.2.3
Since the JSON Schema standard does not support this I created a Python package (JsonVL) that supports unique constraints on values in objects in an array.
It can be installed with pip install jsonvl
And then run with:
from jsonvl import validate
validate(data, schema)
Code examples in the GitHub repo: https://github.com/gregorybchris/jsonvl

What is the difference between an ArrayItems vs SingleItems in Jackson JsonSchema module?

In the documentation, ArrayItems is described as
When this attribute value is an array of jsonSchemas and the instance value is an array, each position in the instance array MUST conform to the jsonSchema in the corresponding position for this array
and SingleItems as
When this attribute value is a jsonSchema and the instance value is an array, then all the items in the array MUST be valid according to the jsonSchema.
but I don't really understand what the attribute value and instance is referring to. If I just want to know the data types of items in the array, which one should I use?

Setting a collection through Expressions SSIS

I understand how to set simple type variables to the Expressions properties of a Task
I need help understanding, if it is possible, to set an Expression that is of type collection.
I have a custom Task that takes a parameter collection, that collection changes depending on which web method it tries to call
Example:
So how would I dynamically setup the correct collection, given I can properly identify the one to setup.
What I infer is that you have a custom task, it has a property and you want to set property value as of type collection, right?
You can create variable of type object which is used to store collection items. When you call web method, you can populate value of variable and you should assign this object variable to property. So store parameter collections to string variable and then parse this string to a collection (object variable) in your custom task and assign this object variable to property.
If you are calling web method in your custom task then you can populate collection variable and assign to property value itself in task otherwise you need to add script component for populating those values.