Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(map): Codegen for Go Target #62

Merged
merged 13 commits into from
Nov 8, 2023
16 changes: 16 additions & 0 deletions lib/codegen/fromcto/golang/golangvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ class GoLangVisitor {
array = '[]';
}

if(this.isMap(field)) {
const decl = field.getModelFile().getAllDeclarations().find(d => d.name === field.ast.type.name);
parameters.fileWriter.writeLine(1, `${field.getName()} := make(map[${this.toGoType(decl.getKey().getType())}]${this.toGoType(decl.getValue().getType())})`);
return null;
}

// we export all fields by capitalizing them
// we strip $ as it is not legal in Go
const name = field.getName().startsWith('$') ? field.getName().substring(1) : field.getName();
Expand Down Expand Up @@ -283,6 +289,16 @@ class GoLangVisitor {
toGoPackageName(namespace) {
return namespace.replace(/@/g, '_').replace(/\./g, '_');
}

/**
* Check if field is a Map Declaration
* @param {Field} field - the field being visited
* @return {boolean} true if field is a Map Declaration
* @private
*/
isMap(field) {
return (Object.keys(field).length > 0 && ModelUtil.isMap?.(field));
}
}

module.exports = GoLangVisitor;
24 changes: 12 additions & 12 deletions test/codegen/__snapshots__/codegen.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,12 @@ type Address struct {
State State \`json:"state"\`
ZipCode string \`json:"zipCode"\`
Country string \`json:"country"\`
Dictionary1 Map1 \`json:"dictionary1"\`
Dictionary2 Map2 \`json:"dictionary2"\`
Dictionary3 Map3 \`json:"dictionary3"\`
Dictionary4 Map4 \`json:"dictionary4"\`
Dictionary5 Map5 \`json:"dictionary5"\`
Dictionary6 Map6 \`json:"dictionary6"\`
dictionary1 := make(map[string]string)
dictionary2 := make(map[string]time.Time)
dictionary3 := make(map[string]SSN)
dictionary4 := make(map[string]Concept)
dictionary5 := make(map[SSN]string)
dictionary6 := make(map[SSN]Employee)
}
type Company struct {
concerto_1_0_0.Concept
Expand Down Expand Up @@ -5650,12 +5650,12 @@ type Address struct {
State State \`json:"state"\`
ZipCode string \`json:"zipCode"\`
Country string \`json:"country"\`
Dictionary1 Map1 \`json:"dictionary1"\`
Dictionary2 Map2 \`json:"dictionary2"\`
Dictionary3 Map3 \`json:"dictionary3"\`
Dictionary4 Map4 \`json:"dictionary4"\`
Dictionary5 Map5 \`json:"dictionary5"\`
Dictionary6 Map6 \`json:"dictionary6"\`
dictionary1 := make(map[string]string)
dictionary2 := make(map[string]time.Time)
dictionary3 := make(map[string]SSN)
dictionary4 := make(map[string]Concept)
dictionary5 := make(map[SSN]string)
dictionary6 := make(map[SSN]Employee)
}
type Company struct {
concerto_1_0_0.Concept
Expand Down
47 changes: 47 additions & 0 deletions test/codegen/fromcto/golang/golangvisitor.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-vars */
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,11 +25,14 @@ const GoLangVisitor = require('../../../../lib/codegen/fromcto/golang/golangvisi
const AssetDeclaration = require('@accordproject/concerto-core').AssetDeclaration;
const ClassDeclaration = require('@accordproject/concerto-core').ClassDeclaration;
const EnumDeclaration = require('@accordproject/concerto-core').EnumDeclaration;
const MapDeclaration = require('@accordproject/concerto-core').MapDeclaration;
const ModelUtil = require('@accordproject/concerto-core').ModelUtil;
const EnumValueDeclaration = require('@accordproject/concerto-core').EnumValueDeclaration;
const Field = require('@accordproject/concerto-core').Field;
const ModelFile = require('@accordproject/concerto-core').ModelFile;
const ModelManager = require('@accordproject/concerto-core').ModelManager;
const FileWriter = require('@accordproject/concerto-util').FileWriter;
let sandbox = sinon.createSandbox();

describe('GoLangVisitor', function () {
let goVisit;
Expand All @@ -43,6 +47,10 @@ describe('GoLangVisitor', function () {
mockFileWriter = sinon.createStubInstance(FileWriter);
});

afterEach(function() {
sandbox.restore();
});

describe('visit', () => {
let param;
beforeEach(() => {
Expand Down Expand Up @@ -334,6 +342,45 @@ describe('GoLangVisitor', function () {

param.fileWriter.writeLine.withArgs(1, 'Bob []string `json:"bob"`').calledOnce.should.be.ok;
});

it('should write a line defining a map field ', () => {
let param = {
fileWriter: mockFileWriter,
};

sandbox.stub(ModelUtil, 'isMap').callsFake(() => {
return true;
});

const mockField = sinon.createStubInstance(Field);
const getAllDeclarations = sinon.stub();

mockField.dummy = 'Dummy Value';
mockField.getModelFile.returns({ getAllDeclarations: getAllDeclarations });

const mockMapDeclaration = sinon.createStubInstance(MapDeclaration);
const findStub = sinon.stub();
const getKeyType = sinon.stub();
const getValueType = sinon.stub();

mockField.getName.returns('Bob');
mockField.getType.returns('SpecialType');

getAllDeclarations.returns({ find: findStub });
findStub.returns(mockMapDeclaration);
getKeyType.returns('String');
getValueType.returns('String');
mockField.getName.returns('dictionary');
mockMapDeclaration.getName.returns('dictionary');
mockMapDeclaration.isMapDeclaration.returns(true);
mockMapDeclaration.getKey.returns({ getType: getKeyType });
mockMapDeclaration.getValue.returns({ getType: getValueType });

goVisit.visitField(mockField,param);

param.fileWriter.writeLine.withArgs(1, 'dictionary := make(map[string]string)').calledOnce.should.be.ok;
});

});

describe('visitEnumValueDeclaration', () => {
Expand Down
7 changes: 7 additions & 0 deletions types/lib/codegen/fromcto/golang/golangvisitor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,11 @@ declare class GoLangVisitor {
* @private
*/
private toGoPackageName;
/**
* Check if field is a Map Declaration
* @param {Field} field - the field being visited
* @return {boolean} true if field is a Map Declaration
* @private
*/
private isMap;
}