Skip to content

Commit

Permalink
fix(metamodel) More explicit separation between enums and classes
Browse files Browse the repository at this point in the history
Signed-off-by: jeromesimeon <[email protected]>
  • Loading branch information
jeromesimeon committed Jul 28, 2021
1 parent 5daa569 commit 613dc5f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
3 changes: 2 additions & 1 deletion packages/concerto-cli/test/models/contract.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"$class": "concerto.metamodel.ModelFile",
"namespace": "org.accordproject.cicero.contract",
"imports": [],
"declarations": [
"enumDeclarations": [],
"classDeclarations": [
{
"$class": "concerto.metamodel.AssetDeclaration",
"name": "AccordContractState",
Expand Down
3 changes: 2 additions & 1 deletion packages/concerto-cli/test/models/contractResolved.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"$class": "concerto.metamodel.ModelFile",
"namespace": "org.accordproject.cicero.contract",
"imports": [],
"declarations": [
"enumDeclarations": [],
"classDeclarations": [
{
"$class": "concerto.metamodel.AssetDeclaration",
"isAbstract": false,
Expand Down
33 changes: 26 additions & 7 deletions packages/concerto-core/lib/introspect/metamodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ concept IdentifiedBy extends Identified {
o String name
}
@FormEditor("defaultSubclass","concerto.metamodel.ClassDeclaration")
abstract concept Declaration {
// TODO use regex /^(?!null|true|false)(\\p{Lu}|\\p{Ll}|\\p{Lt}|\\p{Lm}|\\p{Lo}|\\p{Nl}|\\$|_|\\\\u[0-9A-Fa-f]{4})(?:\\p{Lu}|\\p{Ll}|\\p{Lt}|\\p{Lm}|\\p{Lo}|\\p{Nl}|\\$|_|\\\\u[0-9A-Fa-f]{4}|\\p{Mn}|\\p{Mc}|\\p{Nd}|\\p{Pc}|\\u200C|\\u200D)*/u
@FormEditor("title", "Name")
Expand Down Expand Up @@ -217,8 +218,10 @@ concept ModelFile {
o String namespace default="my.namespace"
@FormEditor("hide", true)
o Import[] imports optional
@FormEditor("title", "Enums")
o EnumDeclaration[] enumDeclarations optional
@FormEditor("title", "Classes")
o Declaration[] declarations optional
o ClassDeclaration[] classDeclarations optional
}
`;

Expand Down Expand Up @@ -274,7 +277,10 @@ function createNameTable(modelManager, metaModel) {
});

// Then add the names local to this metaModel (overriding as we go along)
metaModel.declarations.forEach((decl) => {
metaModel.enumDeclarations.forEach((decl) => {
table[decl.name] = metaModel.namespace;
});
metaModel.classDeclarations.forEach((decl) => {
table[decl.name] = metaModel.namespace;
});

Expand Down Expand Up @@ -304,7 +310,10 @@ function resolveName(name, table) {
function resolveTypeNames(metaModel, table) {
switch (metaModel.$class) {
case 'concerto.metamodel.ModelFile': {
metaModel.declarations.forEach((decl) => {
metaModel.enumDeclarations.forEach((decl) => {
resolveTypeNames(decl, table);
});
metaModel.classDeclarations.forEach((decl) => {
resolveTypeNames(decl, table);
});
}
Expand Down Expand Up @@ -606,12 +615,17 @@ function modelToMetaModel(ast, validate = true) {
}

if (ast.body.length > 0) {
metamodel.declarations = [];
metamodel.enumDeclarations = [];
metamodel.classDeclarations = [];
}
for(let n=0; n < ast.body.length; n++ ) {
const thing = ast.body[n];
const decl = declToMetaModel(thing);
metamodel.declarations.push(decl);
if (decl.$class === 'concerto.metamodel.EnumDeclaration') {
metamodel.enumDeclarations.push(decl);
} else {
metamodel.classDeclarations.push(decl);
}
}

// Last, validate the JSON metaModel
Expand Down Expand Up @@ -780,8 +794,13 @@ function ctoFromMetaModel(metaModel, validate = true) {
}
});
}
if (mm.declarations && mm.declarations.length > 0) {
mm.declarations.forEach((decl) => {
if (mm.enumDeclarations && mm.enumDeclarations.length > 0) {
mm.enumDeclarations.forEach((decl) => {
result += `\n\n${declFromMetaModel(decl)}`;
});
}
if (mm.classDeclarations && mm.classDeclarations.length > 0) {
mm.classDeclarations.forEach((decl) => {
result += `\n\n${declFromMetaModel(decl)}`;
});
}
Expand Down
6 changes: 4 additions & 2 deletions packages/concerto-core/test/data/model/person.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"uri": "https://models.accordproject.org/[email protected]"
}
],
"declarations": [
"enumDeclarations": [
{
"$class": "concerto.metamodel.EnumDeclaration",
"fields": [
Expand All @@ -32,7 +32,9 @@
}
],
"name": "Gender"
},
}
],
"classDeclarations": [
{
"$class": "concerto.metamodel.ParticipantDeclaration",
"isAbstract": true,
Expand Down
6 changes: 4 additions & 2 deletions packages/concerto-core/test/data/model/personResolved.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"uri": "https://models.accordproject.org/[email protected]"
}
],
"declarations": [
"enumDeclarations": [
{
"$class": "concerto.metamodel.EnumDeclaration",
"fields": [
Expand All @@ -32,7 +32,9 @@
}
],
"name": "Gender"
},
}
],
"classDeclarations": [
{
"$class": "concerto.metamodel.ParticipantDeclaration",
"isAbstract": true,
Expand Down

0 comments on commit 613dc5f

Please sign in to comment.