Skip to content

Commit

Permalink
Merge branch 'main' into jonathan/codegen_map_golang
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-casey committed Nov 7, 2023
2 parents f71bcbd + e3111a0 commit 47f2d18
Show file tree
Hide file tree
Showing 21 changed files with 2,253 additions and 3,204 deletions.
4 changes: 2 additions & 2 deletions lib/codegen/fromcto/csharp/csharpvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ class CSharpVisitor {
writeField(field, parameters, externalFieldType, isOptional = false) {

// write Map field
if ( Object.keys(field).length > 0 && ModelUtil.isMap?.(field)) {
const decl = field.getModelFile().getAllDeclarations().find(d => d.name === field.ast.type.name);
if (Object.keys(field).length > 0 && ModelUtil.isMap?.(field)) {
const decl = field.getModelFile().getType(field.getType());
parameters.fileWriter.writeLine(1, `public Dictionary<string, ${this.toCSharpType(decl.getValue().getType())}> ${field.getName()} { get; set; }`);
return null;
}
Expand Down
25 changes: 24 additions & 1 deletion lib/codegen/fromcto/graphql/graphqlvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'use strict';

const util = require('util');
const { ModelUtil } = require('@accordproject/concerto-core');

/**
* Convert the contents of a ModelManager to GraphQL types, based on
Expand Down Expand Up @@ -52,7 +53,7 @@ class GraphQLVisitor {
} else if (thing.isField?.()) {
return this.visitField(thing, parameters);
} else if (thing.isMapDeclaration?.()) {
return;
return this.visitMapDeclaration(thing, parameters);
} else if (thing.isRelationship?.()) {
return this.visitRelationship(thing, parameters);
} else if (thing.isEnumValue?.()) {
Expand Down Expand Up @@ -171,6 +172,28 @@ class GraphQLVisitor {
return null;
}

/**
* Visitor design pattern
* @param {MapDeclaration} mapDeclaration - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitMapDeclaration(mapDeclaration, parameters) {
const keyType = mapDeclaration.getKey().getType();
const valueType = mapDeclaration.getValue().getType();

let key = ModelUtil.isPrimitiveType(keyType) ? this.toGraphQLType(keyType) : keyType;
let value = ModelUtil.isPrimitiveType(valueType) ? this.toGraphQLType(valueType) : valueType;

parameters.fileWriter.writeLine(0, `type ${mapDeclaration.getName()} {`);
parameters.fileWriter.writeLine(1, `key: ${key}`);
parameters.fileWriter.writeLine(1, `value: ${value}`);
parameters.fileWriter.writeLine(0, '}');

return null;
}

/**
* Visitor design pattern
* @param {Relationship} relationship - the object being visited
Expand Down
16 changes: 15 additions & 1 deletion lib/codegen/fromcto/java/javavisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ class JavaVisitor {
parameters.fileWriter.writeLine(0, `import ${namespace}.${typeName};` );
});

let hasImportedJavaMap = false;
classDeclaration.getOwnProperties().forEach(p => {
if(ModelUtil.isMap(p) && !hasImportedJavaMap) {
parameters.fileWriter.writeLine(0, 'import java.util.HashMap;');
parameters.fileWriter.writeLine(0, 'import java.util.Map;');
hasImportedJavaMap = true;
}
});

parameters.fileWriter.writeLine(0, 'import com.fasterxml.jackson.annotation.*;');
parameters.fileWriter.writeLine(0, '');

Expand Down Expand Up @@ -278,7 +287,12 @@ class JavaVisitor {
parameters.fileWriter.writeLine(1, '}');
break;
default:
parameters.fileWriter.writeLine(1, 'private ' + fieldType + ' ' + fieldName + ';' );
if (ModelUtil.isMap(field)) {
const decl = field.getModelFile().getType(field.ast.type.name);
parameters.fileWriter.writeLine(1, `private Map<${this.toJavaType(decl.getKey().getType())}, ${this.toJavaType(decl.getValue().getType())}> ${field.getName()} = new HashMap<>();`);
} else {
parameters.fileWriter.writeLine(1, 'private ' + fieldType + ' ' + fieldName + ';' );
}
}
} else {
parameters.fileWriter.writeLine(1, 'private ' + fieldType + ' ' + fieldName + ';' );
Expand Down
Loading

0 comments on commit 47f2d18

Please sign in to comment.