@accordproject/concerto-tools
Advanced tools
Comparing version 3.2.1-20221216153347 to 3.2.1-20230105164345
@@ -42,10 +42,4 @@ /* | ||
return this.visitModelFile(thing, parameters); | ||
} else if (thing.isAsset?.()) { | ||
return this.visitClassDeclaration(thing, parameters); | ||
} else if (thing.isTransaction?.()) { | ||
// return this.visitTransactionDeclaration(thing, parameters); | ||
} else if (thing.isEnum?.()) { | ||
return this.visitEnumDeclaration(thing, parameters); | ||
} else if (thing.isConcept?.()) { | ||
//return this.visitConceptDeclaration(thing, parameters); | ||
} else if (thing.isClassDeclaration?.()) { | ||
@@ -57,4 +51,4 @@ return this.visitClassDeclaration(thing, parameters); | ||
return this.visitField(thing, parameters); | ||
} else if (thing.isRelationship?.()) { | ||
// return this.visitRelationshipDeclaration(thing, parameters); | ||
} else if(thing.isRelationship?.()) { | ||
return this.visitRelationship(thing, parameters); | ||
} else if (thing.isEnumValue?.()) { | ||
@@ -65,3 +59,3 @@ return this.visitEnumValueDeclaration(thing, parameters); | ||
} else { | ||
throw new Error('Unrecognised type: ' + typeof thing + ', value: ' + util.inspect(thing, { showHidden: true, depth: null })); | ||
throw new Error('Unrecognised type: ' + typeof thing + ', value: ' + util.inspect(thing, { showHidden: false, depth: 1 })); | ||
} | ||
@@ -78,16 +72,3 @@ } | ||
visitModelManager(modelManager, parameters) { | ||
parameters.fileWriter.openFile('main.go'); | ||
parameters.fileWriter.writeLine(0, 'package main'); | ||
parameters.fileWriter.writeLine(0, 'import "fmt"'); | ||
parameters.fileWriter.writeLine(0, 'type Relationship struct {' ); | ||
parameters.fileWriter.writeLine(1, 'Namespace string `json:"namespace"`' ); | ||
parameters.fileWriter.writeLine(1, 'Type string `json:"type"`' ); | ||
parameters.fileWriter.writeLine(1, 'Identifier string `json:"identifier"`' ); | ||
parameters.fileWriter.writeLine(0, '}' ); | ||
parameters.fileWriter.writeLine(0, 'func main() {'); | ||
parameters.fileWriter.writeLine(1, 'fmt.Printf("Hello, world.")'); | ||
parameters.fileWriter.writeLine(0, '}'); | ||
parameters.fileWriter.closeFile(); | ||
modelManager.getModelFiles().forEach((modelFile) => { | ||
modelManager.getModelFiles(true).forEach((modelFile) => { | ||
modelFile.accept(this,parameters); | ||
@@ -106,7 +87,6 @@ }); | ||
visitModelFile(modelFile, parameters) { | ||
// we put all the code into the main package, but we | ||
// seperate out into multiple files using the namespaces | ||
const { escapedNamespace } = ModelUtil.parseNamespace(modelFile.getNamespace()); | ||
parameters.fileWriter.openFile(this.toGoPackageName(escapedNamespace) + '.go'); | ||
parameters.fileWriter.writeLine(0, 'package main'); | ||
const packageName = this.toGoPackageName(modelFile.getNamespace()); | ||
parameters.fileWriter.openFile(`${modelFile.getNamespace()}.go`); | ||
parameters.fileWriter.writeLine(0, `// Package ${packageName} contains domain objects and was generated from Concerto namespace ${modelFile.getNamespace()}.`); | ||
parameters.fileWriter.writeLine(0, `package ${packageName}`); | ||
@@ -117,2 +97,10 @@ if(this.containsDateTimeField(modelFile)) { | ||
modelFile.getImports().map(importString => ModelUtil.getNamespace(importString)).filter(namespace => namespace !== modelFile.getNamespace()) // Skip own namespace. | ||
.filter((v, i, a) => a.indexOf(v) === i) // Remove any duplicates from direct imports | ||
.forEach(namespace => { | ||
parameters.fileWriter.writeLine(0, `import "${this.toGoPackageName(namespace)}";`); | ||
}); | ||
parameters.fileWriter.writeLine(1, ''); | ||
modelFile.getAllDeclarations().forEach((decl) => { | ||
@@ -160,3 +148,6 @@ decl.accept(this, parameters); | ||
if(classDeclaration.getSuperType()) { | ||
parameters.fileWriter.writeLine(1, ModelUtil.getShortName(classDeclaration.getSuperType())); | ||
let superPackageName = ModelUtil.getNamespace(classDeclaration.getSuperType()); | ||
let thisPackageName = ModelUtil.getNamespace(classDeclaration.getFullyQualifiedName()); | ||
let useName = superPackageName === thisPackageName ? '' : `${this.toGoPackageName(superPackageName)}.`; | ||
parameters.fileWriter.writeLine(1, `${useName}${ModelUtil.getShortName(classDeclaration.getSuperType())}`); | ||
} | ||
@@ -187,3 +178,5 @@ | ||
// we export all fields by capitalizing them | ||
parameters.fileWriter.writeLine(1, ModelUtil.capitalizeFirstLetter(field.getName()) + ' ' + array + this.toGoType(field.getType()) + ' `json:"' + field.getName() + '"`' ); | ||
// we strip $ as it is not legal in Go | ||
const name = field.getName().startsWith('$') ? field.getName().substring(1) : field.getName(); | ||
parameters.fileWriter.writeLine(1, ModelUtil.capitalizeFirstLetter(name) + ' ' + array + this.toGoType(field.getType()) + ' `json:"' + field.getName() + '"`' ); | ||
return null; | ||
@@ -194,3 +187,3 @@ } | ||
* Visitor design pattern | ||
* @param {EnumValueDeclaration} enumValueDeclaration - the object being visited | ||
* @param {Relationship} relationship - the object being visited | ||
* @param {Object} parameters - the parameter | ||
@@ -200,15 +193,12 @@ * @return {Object} the result of visiting or null | ||
*/ | ||
visitEnumValueDeclaration(enumValueDeclaration, parameters) { | ||
visitRelationship(relationship, parameters) { | ||
let array = ''; | ||
// is this the first enum value? | ||
// if yes, we need to use 'iota' to set the value to zero | ||
const isFirstValue = enumValueDeclaration.getParent().getOwnProperties()[0].getName() === enumValueDeclaration.getName(); | ||
let iota = ''; | ||
if(isFirstValue) { | ||
iota = ' ' + enumValueDeclaration.getParent().getName() + ' = 1 + iota'; | ||
if(relationship.isArray()) { | ||
array = '[]'; | ||
} | ||
// we export all fields by capitalizing them | ||
parameters.fileWriter.writeLine(1, ModelUtil.capitalizeFirstLetter(enumValueDeclaration.getName()) + iota ); | ||
// relationships become pointers to types | ||
parameters.fileWriter.writeLine(1, `${ModelUtil.capitalizeFirstLetter(relationship.getName())} ${array}*${this.toGoType(relationship.getType())} \`json:"${relationship.getName()}"\``); | ||
return null; | ||
@@ -219,3 +209,3 @@ } | ||
* Visitor design pattern | ||
* @param {Relationship} relationship - the object being visited | ||
* @param {EnumValueDeclaration} enumValueDeclaration - the object being visited | ||
* @param {Object} parameters - the parameter | ||
@@ -225,11 +215,15 @@ * @return {Object} the result of visiting or null | ||
*/ | ||
visitRelationship(relationship, parameters) { | ||
let array = ''; | ||
visitEnumValueDeclaration(enumValueDeclaration, parameters) { | ||
if(relationship.isArray()) { | ||
array = '[]'; | ||
// is this the first enum value? | ||
// if yes, we need to use 'iota' to set the value to zero | ||
const isFirstValue = enumValueDeclaration.getParent().getOwnProperties()[0].getName() === enumValueDeclaration.getName(); | ||
let iota = ''; | ||
if(isFirstValue) { | ||
iota = ' ' + enumValueDeclaration.getParent().getName() + ' = 1 + iota'; | ||
} | ||
// we export all relationships by capitalizing them | ||
parameters.fileWriter.writeLine(1, ModelUtil.capitalizeFirstLetter(relationship.getName()) + ' ' + array + 'Relationship `json:"' + relationship.getName() + '"`' ); | ||
// we export all fields by capitalizing them | ||
parameters.fileWriter.writeLine(1, ModelUtil.capitalizeFirstLetter(enumValueDeclaration.getName()) + iota ); | ||
return null; | ||
@@ -254,2 +248,5 @@ } | ||
let field = fields[i]; | ||
if(field.isTypeScalar?.()) { | ||
field = field.getScalarField(); | ||
} | ||
if(field.getType() === 'DateTime') { | ||
@@ -291,2 +288,3 @@ return true; | ||
* Converts a Concerto namespace to a Go package name. | ||
* See: https://rakyll.org/style-packages/ | ||
* @param {string} namespace - the concerto type | ||
@@ -297,3 +295,3 @@ * @return {string} the corresponding package name in Go Lang | ||
toGoPackageName(namespace) { | ||
return namespace.replace('.', ''); | ||
return namespace.replace(/@/g, '_').replace(/\./g, '_'); | ||
} | ||
@@ -300,0 +298,0 @@ } |
{ | ||
"name": "@accordproject/concerto-tools", | ||
"version": "3.2.1-20221216153347", | ||
"version": "3.2.1-20230105164345", | ||
"description": "Tools for the Concerto Modeling Language", | ||
@@ -70,4 +70,4 @@ "homepage": "https://github.com/accordproject/concerto", | ||
"dependencies": { | ||
"@accordproject/concerto-core": "3.2.1-20221216153347", | ||
"@accordproject/concerto-util": "3.2.1-20221216153347", | ||
"@accordproject/concerto-core": "3.2.1-20230105164345", | ||
"@accordproject/concerto-util": "3.2.1-20230105164345", | ||
"ajv": "8.10.0", | ||
@@ -74,0 +74,0 @@ "ajv-formats": "2.1.1", |
@@ -63,3 +63,3 @@ export = GoLangVisitor; | ||
* Visitor design pattern | ||
* @param {EnumValueDeclaration} enumValueDeclaration - the object being visited | ||
* @param {Relationship} relationship - the object being visited | ||
* @param {Object} parameters - the parameter | ||
@@ -69,6 +69,6 @@ * @return {Object} the result of visiting or null | ||
*/ | ||
private visitEnumValueDeclaration; | ||
private visitRelationship; | ||
/** | ||
* Visitor design pattern | ||
* @param {Relationship} relationship - the object being visited | ||
* @param {EnumValueDeclaration} enumValueDeclaration - the object being visited | ||
* @param {Object} parameters - the parameter | ||
@@ -78,3 +78,3 @@ * @return {Object} the result of visiting or null | ||
*/ | ||
private visitRelationship; | ||
private visitEnumValueDeclaration; | ||
/** | ||
@@ -99,2 +99,3 @@ * Returns true if the ModelFile contains a class that has a DateTime | ||
* Converts a Concerto namespace to a Go package name. | ||
* See: https://rakyll.org/style-packages/ | ||
* @param {string} namespace - the concerto type | ||
@@ -101,0 +102,0 @@ * @return {string} the corresponding package name in Go Lang |
/*! | ||
* Concerto Tools v3.2.1-20221216153347 | ||
* Concerto Tools v3.2.1-20230105164345 | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
@@ -4,0 +4,0 @@ * you may not use this file except in compliance with the License. |
Sorry, the diff of this file is too big to display
1547039
7574
+ Added@accordproject/concerto-core@3.2.1-20230105164345(transitive)
+ Added@accordproject/concerto-cto@3.2.1-20230105164345(transitive)
+ Added@accordproject/concerto-metamodel@3.2.1-20230105164345(transitive)
+ Added@accordproject/concerto-util@3.2.1-20230105164345(transitive)
- Removed@accordproject/concerto-core@3.2.1-20221216153347(transitive)
- Removed@accordproject/concerto-cto@3.2.1-20221216153347(transitive)
- Removed@accordproject/concerto-metamodel@3.2.1-20221216153347(transitive)
- Removed@accordproject/concerto-util@3.2.1-20221216153347(transitive)