eclipse-langium / langium

Next-gen language engineering / DSL framework
https://langium.org/
MIT License
754 stars 68 forks source link

provide optional key with reflection #1206

Closed JoergStahnke closed 11 months ago

JoergStahnke commented 1 year ago

At runtime I need to iterate over all possible keys of a given type, defined in the grammar. The background is, that I generate SQL Scripts to migrate database object. I need to compare two different versions of the modell created with langium. I loop over all possible keys. When I find a difference I generate the corresponding SQL commands. This is equivalent to the former Xtext method EClass.EAllStructuralFeatures.

Now a time there is in generated/ast.ts an object reflectionwith a method getTypeMetaData(type: string): TypeMetaData This method returns all mandantory properties of type boolean and array. Can you add here a component to get all possible keys of the provided type? I would like to add a property allKeys: TypeAllProperty[] in the interface TypeMetaData. The interface TypeAllPropertyshould look like this:

export interface TypeAllProperty{
    name: string // the name of the provided type
    type: 'array' | 'boolean' | 'string' | 'object' | 'number' //all possible primitive types similar to TypeMandatoryProperty
    objectType: string // the name of the object type if the key returns an object or an array of objects)
}

Kind regards Jörg

msujew commented 1 year ago

Hey @JoergStahnke,

we didn't choose to provide any additional reflection data beyond the mandatory properties yet, since you can easily find all key/values of an object in JavaScript via the Object.keys, Object.values and Object.entries methods. You just need to filter out the framework properties that start with $. Does that work for you?

JoergStahnke commented 1 year ago

Hey @msujew , thank you for the response. But there is a little difference. With Object.keys I get only the keys set during runtime. If I have an optional property and the property is not set I will not get this key with Objects.keys. Im interested in a full list of all possible keys of an object.

msujew commented 1 year ago

Ok, I guess that makes sense 👍