From 9f88864a3d66803cd062de5e515f58c68e5536c7 Mon Sep 17 00:00:00 2001 From: Jonathan Casey Date: Tue, 17 Oct 2023 17:56:13 +0100 Subject: [PATCH 1/4] feat(map): add graphqlvisitor Signed-off-by: Jonathan Casey --- lib/codegen/fromcto/graphql/graphqlvisitor.js | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/codegen/fromcto/graphql/graphqlvisitor.js b/lib/codegen/fromcto/graphql/graphqlvisitor.js index bb0f2323..b2d71fd0 100644 --- a/lib/codegen/fromcto/graphql/graphqlvisitor.js +++ b/lib/codegen/fromcto/graphql/graphqlvisitor.js @@ -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 @@ -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?.()) { @@ -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 From 08aa551c0b74be5748c04515450838029909415f Mon Sep 17 00:00:00 2001 From: Jonathan Casey Date: Tue, 17 Oct 2023 17:57:01 +0100 Subject: [PATCH 2/4] test(map): update snapshot Signed-off-by: Jonathan Casey --- test/codegen/__snapshots__/codegen.js.snap | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/codegen/__snapshots__/codegen.js.snap b/test/codegen/__snapshots__/codegen.js.snap index b031ca12..08e15db8 100644 --- a/test/codegen/__snapshots__/codegen.js.snap +++ b/test/codegen/__snapshots__/codegen.js.snap @@ -632,6 +632,30 @@ type Address { dictionary5: Map5! dictionary6: Map6! } +type Map1 { + key: String + value: String +} +type Map2 { + key: String + value: DateTime +} +type Map3 { + key: String + value: SSN +} +type Map4 { + key: String + value: Concept +} +type Map5 { + key: SSN + value: String +} +type Map6 { + key: SSN + value: Employee +} type Company { name: String! headquarters: Address! @@ -5758,6 +5782,30 @@ type Address { dictionary5: Map5! dictionary6: Map6! } +type Map1 { + key: String + value: String +} +type Map2 { + key: String + value: DateTime +} +type Map3 { + key: String + value: SSN +} +type Map4 { + key: String + value: Concept +} +type Map5 { + key: SSN + value: String +} +type Map6 { + key: SSN + value: Employee +} type Company { name: String! headquarters: Address! From 8a07bcd427640f89a8350d4bb67673160380599e Mon Sep 17 00:00:00 2001 From: Jonathan Casey Date: Wed, 18 Oct 2023 12:57:55 +0100 Subject: [PATCH 3/4] test(map): adds graphqlvisitor Signed-off-by: Jonathan Casey --- lib/codegen/fromcto/graphql/graphqlvisitor.js | 8 ++--- .../codegen/fromcto/graphql/graphqlvisitor.js | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/lib/codegen/fromcto/graphql/graphqlvisitor.js b/lib/codegen/fromcto/graphql/graphqlvisitor.js index b2d71fd0..a8100b3c 100644 --- a/lib/codegen/fromcto/graphql/graphqlvisitor.js +++ b/lib/codegen/fromcto/graphql/graphqlvisitor.js @@ -186,10 +186,10 @@ class GraphQLVisitor { 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, '}' ); + 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; } diff --git a/test/codegen/fromcto/graphql/graphqlvisitor.js b/test/codegen/fromcto/graphql/graphqlvisitor.js index 1bf7db3a..e608f1c6 100644 --- a/test/codegen/fromcto/graphql/graphqlvisitor.js +++ b/test/codegen/fromcto/graphql/graphqlvisitor.js @@ -23,10 +23,14 @@ const GraphQLVisitor = require('../../../../lib/codegen/fromcto/graphql/graphqlv const ModelFile = require('@accordproject/concerto-core').ModelFile; const ModelManager = require('@accordproject/concerto-core').ModelManager; const ClassDeclaration = require('@accordproject/concerto-core').ClassDeclaration; +const MapDeclaration = require('@accordproject/concerto-core').MapDeclaration; const EnumValueDeclaration = require('@accordproject/concerto-core').EnumValueDeclaration; const Field = require('@accordproject/concerto-core').Field; const RelationshipDeclaration = require('@accordproject/concerto-core').RelationshipDeclaration; const FileWriter = require('@accordproject/concerto-util').FileWriter; +const { ModelUtil } = require('@accordproject/concerto-core'); + +let sandbox = sinon.createSandbox(); const MODEL_WITH_DECORATORS = ` namespace test @@ -339,6 +343,35 @@ describe('GraphQLVisitor', function () { }); }); + describe('visitMapDeclaration', () => { + it('should write a type for a Map', () => { + let param = { + fileWriter: mockFileWriter + }; + + sandbox.stub(ModelUtil, 'isPrimitiveType').callsFake(() => { + return true; + }); + + const mockMapDeclaration = sinon.createStubInstance(MapDeclaration); + const getKeyType = sinon.stub(); + const getValueType = sinon.stub(); + + getKeyType.returns('String'); + getValueType.returns('String'); + mockMapDeclaration.getName.returns('map1'); + mockMapDeclaration.getKey.returns({ getType: getKeyType }); + mockMapDeclaration.getValue.returns({ getType: getValueType }); + + graphQLVisitor.visitMapDeclaration(mockMapDeclaration, param); + + param.fileWriter.writeLine.withArgs(0, 'type map1 {').calledOnce.should.be.ok; + param.fileWriter.writeLine.withArgs(1, 'key: String').calledOnce.should.be.ok; + param.fileWriter.writeLine.withArgs(1, 'value: String').calledOnce.should.be.ok; + param.fileWriter.writeLine.withArgs(0, '}').calledOnce.should.be.ok; + }); + }); + describe('visitRelationship', () => { it('should write a line for a relationship', () => { let param = { From d826e9a10f2abc0b05454adf136c9a563c4c512b Mon Sep 17 00:00:00 2001 From: Jonathan Casey Date: Wed, 18 Oct 2023 13:01:16 +0100 Subject: [PATCH 4/4] test(map): restores sandbox Signed-off-by: Jonathan Casey --- test/codegen/fromcto/graphql/graphqlvisitor.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/codegen/fromcto/graphql/graphqlvisitor.js b/test/codegen/fromcto/graphql/graphqlvisitor.js index e608f1c6..a944000a 100644 --- a/test/codegen/fromcto/graphql/graphqlvisitor.js +++ b/test/codegen/fromcto/graphql/graphqlvisitor.js @@ -369,6 +369,8 @@ describe('GraphQLVisitor', function () { param.fileWriter.writeLine.withArgs(1, 'key: String').calledOnce.should.be.ok; param.fileWriter.writeLine.withArgs(1, 'value: String').calledOnce.should.be.ok; param.fileWriter.writeLine.withArgs(0, '}').calledOnce.should.be.ok; + + sandbox.restore(); }); });