Is there a type that can hold objects of any types?


#1

I want to create a function to create test data. This function will take in two parameters, typeName and the object to create, for example: createTestData(“FixedAsset”, {name: “test”, description: “test”})

Which type can be used to represent the object, other than json?

I’ve looked into Obj but it converts all the fields to a map and it’s very hard to convert it to a format that can be used by create functions.


#2

so you want a type that contains all fields that are defined on all types?


#3

Not ALL fields but ANY fields. The correct type is Obj, what we really need is an api to convert Objs into their individual types.


#4

a) value type any can hold any value including primitives - e.g. type MyType { value: any } will have a field value that can have any value

b) if you define field as Obj then it can hold instance of any c3 type like User, Organization etc but not primitive values and collections. Note that primitive values can be “box” into Obj using PromitiveType.String.box("foo")

c) in theoretical createTestData example above most appropriate value type for second argument is going to be map<string, any>. So methods declaration and corresponding implementation should be:

createTestData: function(typeRef: !TypeRef, fields: map<string, any>})

function createTestData(typeRef, fields) {
  return typeRef.toType().make(fields);
}

Then user code will look like:

TestApi.createTestData(FixedAsset, {name: "test", description: "test"});

Note that FixedAsset is not a string but rather reference to actual type.

p.s. TestApi.createTestData approach is a bad idea because it erases type awareness and defeats possibility of code static analysis and refactor.

Much better approach is to use test framework before / after callbacks or at least take lambda as input to createTestData so user code is strongly typed:

TestApi.createTestData(function() { 
  return FixedAsset.make({name: "test", description: "test"}));
});

#5

How do I create instances of a type with fields that are stored calcs and thus dependent on a series of other types ? I want to avoid creating all the dependent types.

Assuming FixedAsset is defined as below -

type FixedAsset { name: string, description: string, field1: someType1, storedCalcField: string stored calc ‘field1OfSomeType1.fieldOfSomeType2.fieldofSomeType3’ }

createTestData(‘FixedAsset’, {name:‘test’,description:‘test’, storedCaclField: ‘test’}) generates an instance of FixedAsset type that populates the specified fields.

This is important to quickly simulate data to test customer analytics and dashboards when 1. Available customer data isn’t representative 2. Customers can’t share data for regulatory reasons.


#6

You would have to implement that logic yourself.

Functions like FixedAsset.fieldType(‘storedCalcField’) will allow you to inspect the definition of the ‘storedCalcField’ field, and in case its a stored calc, do some other logic. See also the types FieldType, ValueType, and Type. Note also that this area of the platform is undergoing changes as we extend the type system todescribe itself.

Consider remixing the TestApi type to add this logic.

Also note that this logic will be VERY DIFFICULT.

Good luck.