From afc6abb9fbe1d9b63aa15aa975ee0edf5f4fd21e Mon Sep 17 00:00:00 2001 From: Kevin Knight <57677197+kevin-m-knight-gs@users.noreply.github.com> Date: Sat, 22 Jun 2024 10:25:28 -0400 Subject: [PATCH] Fix toJSON for constraints (#2927) * Remove PackageableElement and ModelElement from constraints model diagram * Add JSON serialization for constraints --- .../format/json/modelSerialization.pure | 9 ++++ .../format/json/tests/testToJson.pure | 18 ++++++++ .../core/external/format/json/toJSON.pure | 4 +- .../constraints/constraintsModelDiagram.pure | 44 +------------------ 4 files changed, 31 insertions(+), 44 deletions(-) diff --git a/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/external/format/json/modelSerialization.pure b/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/external/format/json/modelSerialization.pure index 47ae9dd5c31..c0fc53980d6 100644 --- a/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/external/format/json/modelSerialization.pure +++ b/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/external/format/json/modelSerialization.pure @@ -13,6 +13,7 @@ // limitations under the License. import meta::json::*; +import meta::pure::metamodel::constraint::*; import meta::pure::metamodel::serialization::json::*; import meta::pure::metamodel::serialization::grammar::*; import meta::pure::milestoning::*; @@ -76,6 +77,14 @@ function meta::pure::metamodel::serialization::json::qualifiedPropertyToJSON(pro ]) } +function meta::pure::metamodel::serialization::json::constraintToJSON(constraint:Constraint[1]):JSONElement[1] +{ + newJSONObject(newJSONKeyValue('name', ^JSONString(value=$constraint.name->toOne())) + ->concatenate(if($constraint.owner->isEmpty(), |[], |newJSONKeyValue('owner', ^JSONString(value=$constraint.owner->toOne())))) + ->concatenate(if($constraint.externalId->isEmpty(), |[], |newJSONKeyValue('externalId', ^JSONString(value=$constraint.externalId->toOne())))) + ->concatenate(if($constraint.enforcementLevel->isEmpty(), |[], |newJSONKeyValue('enforcementLevel', ^JSONString(value=$constraint.enforcementLevel->toOne()))))) +} + function meta::pure::metamodel::serialization::json::parameterToJSON(p:VariableExpression[1]):JSONElement[1] { newJSONObject([newJSONKeyValue('name', ^JSONString(value=$p.name)), diff --git a/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/external/format/json/tests/testToJson.pure b/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/external/format/json/tests/testToJson.pure index e1339254975..b6ec5aa133a 100644 --- a/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/external/format/json/tests/testToJson.pure +++ b/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/external/format/json/tests/testToJson.pure @@ -452,3 +452,21 @@ function <> meta::json::tests::testSerializeWithTreeAndConfig():Boole let expected = '{"@type":"meta::json::tests::TestLevel1","a":"A","l2":[{"@type":"meta::json::tests::TestLevel2_A","ba":"BA","b":"BA","l3a":[{"@type":"meta::json::tests::TestLevel3_A","ca":"CA"}]}]}'; assertEq($expected, $plainJson); } + +Class meta::json::tests::TestClassWithConstraints +[ + $this.a != $this.b, + aA: $this.a->startsWith('A'), + bNotB( ~owner: Bee ~externalId: 'Bees b not B' ~function: !$this.b->startsWith('B') ~enforcementLevel: Warn ~message: 'Look out for the B!') +] +{ + a: String[1]; + b: String[1]; +} + +function <> meta::json::tests::testToJsonHandlesConstraints():Boolean[1] +{ + let actual = meta::json::tests::TestClassWithConstraints.constraints->toJSON(); + let expected = '[{"name":"0"},{"name":"aA"},{"name":"bNotB","owner":"Bee","externalId":"Bees b not B","enforcementLevel":"Warn"}]'; + assertEq($expected, $actual); +} diff --git a/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/external/format/json/toJSON.pure b/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/external/format/json/toJSON.pure index 5c4f39b319d..b73da7b9c51 100644 --- a/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/external/format/json/toJSON.pure +++ b/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/external/format/json/toJSON.pure @@ -15,6 +15,7 @@ import meta::pure::functions::cipher::*; import meta::pure::graphFetch::routing::*; import meta::pure::graphFetch::*; +import meta::pure::metamodel::constraint::*; import meta::pure::metamodel::treepath::*; import meta::pure::metamodel::serialization::json::*; import meta::pure::mapping::*; @@ -341,6 +342,7 @@ function <> meta::json::buildLambdas(extraSerializers:Function<{ {g:GenericType[1], state:JSONState[1] | $g->genericTypeToJSON()}, {p:Property[1], state:JSONState[1] | $p->propertyToJSON()}, {q:QualifiedProperty[1], state:JSONState[1] | $q->qualifiedPropertyToJSON()}, + {c:Constraint[1], state:JSONState[1] | $c->constraintToJSON()}, {s:Stereotype[1], state:JSONState[1] | $s->stereotypeToJSON()}, {t:TaggedValue[1], state:JSONState[1] | $t->taggedValueToJSON()}, {t:Tag[1], state:JSONState[1] | $t->tagToJSON()}, @@ -466,4 +468,4 @@ function meta::json::toJsonNative(instance: Any[*], config: JSONSerializationCon function meta::json::toJsonNative(instance: Any[*]):String[1] { meta::json::toJsonBeta($instance); -} \ No newline at end of file +} diff --git a/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/pure/constraints/constraintsModelDiagram.pure b/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/pure/constraints/constraintsModelDiagram.pure index 74ec3426489..7349bca3700 100644 --- a/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/pure/constraints/constraintsModelDiagram.pure +++ b/legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/pure/constraints/constraintsModelDiagram.pure @@ -51,18 +51,6 @@ Diagram meta::pure::metamodel::constraint::ConstraintModelDiagram(width=875.2690 width=80.00000, height=36.00000) - TypeView PackageableElement_$_3( - type=meta::pure::metamodel::PackageableElement, - stereotypesVisible=true, - attributesVisible=true, - attributeStereotypesVisible=true, - attributeTypesVisible=true, - color=#FFFFCC, - lineWidth=1.0, - position=(1115.00000, 190.00000), - width=139.39648, - height=36.00000) - TypeView Type_$_4( type=meta::pure::metamodel::type::Type, stereotypesVisible=true, @@ -75,18 +63,6 @@ Diagram meta::pure::metamodel::constraint::ConstraintModelDiagram(width=875.2690 width=47.11719, height=36.00000) - TypeView ModelElement_$_5( - type=meta::pure::metamodel::ModelElement, - stereotypesVisible=true, - attributesVisible=true, - attributeStereotypesVisible=true, - attributeTypesVisible=true, - color=#FFFFCC, - lineWidth=1.0, - position=(1127.00000, 88.00000), - width=112.93604, - height=48.00000) - TypeView ValidationResult_$_6( type=meta::pure::metamodel::constraint::ValidationResult, stereotypesVisible=true, @@ -192,22 +168,4 @@ Diagram meta::pure::metamodel::constraint::ConstraintModelDiagram(width=875.2690 label='', source=Class_$_0, target=Type_$_4) - - - GeneralizationView gview_16(color=#000000, - lineWidth=-1.0, - lineStyle=SIMPLE, - points=[(1184.69824,208.00000),(800.00000,388.00000)], - label='', - source=Constraint_$_2, - target=PackageableElement_$_3) - - GeneralizationView gview_17(color=#000000, - lineWidth=-1.0, - lineStyle=SIMPLE, - points=[(1183.46802,112.00000),(1184.69824,208.00000)], - label='', - source=PackageableElement_$_3, - target=ModelElement_$_5) - -} \ No newline at end of file +}