Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/codegen/fromcto/golang/golangvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,20 @@ class GoLangVisitor {
let mapKeyType = mapDeclaration.getKey().getType();
let mapValueType = mapDeclaration.getValue().getType();

if (mapDeclaration.getModelFile().getType(mapKeyType).isScalarDeclaration?.()) {
if (mapDeclaration.getModelFile().getType(mapKeyType)?.isScalarDeclaration?.()) {
mapKeyType = mapDeclaration.getModelFile().getType(mapKeyType).getType();
}

const goKeyType = this.toGoType(mapKeyType);

if (mapDeclaration.getModelFile().getType(mapValueType).isScalarDeclaration?.()) {
if (mapDeclaration.getModelFile().getType(mapValueType)?.isScalarDeclaration?.()) {
mapValueType = mapDeclaration.getModelFile().getType(mapValueType).getType();
}

const goValueType = this.toGoType(mapValueType);

parameters.fileWriter.writeLine(1, `${field.getName()} := make(map[${goKeyType}]${goValueType})` + ' `json:"' + field.getName() + '"`');
const name = field.getName().startsWith('$') ? field.getName().substring(1) : field.getName();
Comment thread
chaitijain593-prog marked this conversation as resolved.
Resolved
parameters.fileWriter.writeLine(1, `${ModelUtil.capitalizeFirstLetter(name)} map[${goKeyType}]${goValueType}` + ' `json:"' + field.getName() + '"`');
Comment thread
chaitijain593-prog marked this conversation as resolved.
Resolved
return null;
}

Expand Down
12 changes: 6 additions & 6 deletions test/codegen/__snapshots__/codegen.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1059,11 +1059,11 @@ type Company struct {
concerto_1_0_0.Concept
Name string \`json:"name"\`
Headquarters Address \`json:"headquarters"\`
companyProperties := make(map[string]string) \`json:"companyProperties"\`
employeeDirectory := make(map[string]Employee) \`json:"employeeDirectory"\`
employeeTShirtSizes := make(map[string]TShirtSizeType) \`json:"employeeTShirtSizes"\`
employeeProfiles := make(map[string]Concept) \`json:"employeeProfiles"\`
employeeSocialSecurityNumbers := make(map[string]string) \`json:"employeeSocialSecurityNumbers"\`
CompanyProperties map[string]string \`json:"companyProperties"\`
EmployeeDirectory map[string]Employee \`json:"employeeDirectory"\`
EmployeeTShirtSizes map[string]TShirtSizeType \`json:"employeeTShirtSizes"\`
EmployeeProfiles map[string]Concept \`json:"employeeProfiles"\`
EmployeeSocialSecurityNumbers map[string]string \`json:"employeeSocialSecurityNumbers"\`
}
type Department int
const (
Expand Down Expand Up @@ -1097,7 +1097,7 @@ type Person struct {
Ssn string \`json:"ssn"\`
Height float64 \`json:"height"\`
Dob time.Time \`json:"dob"\`
nextOfKin := make(map[string]string) \`json:"nextOfKin"\`
NextOfKin map[string]string \`json:"nextOfKin"\`
}
type Employee struct {
Person
Expand Down
34 changes: 33 additions & 1 deletion test/codegen/fromcto/golang/golangvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,39 @@ describe('GoLangVisitor', function () {

goVisit.visitField(mockField,param);

param.fileWriter.writeLine.withArgs(1, 'dictionary := make(map[string]string) `json:"dictionary"`').calledOnce.should.be.ok;
param.fileWriter.writeLine.withArgs(1, 'Dictionary map[string]string `json:"dictionary"`').calledOnce.should.be.ok;
Comment thread
chaitijain593-prog marked this conversation as resolved.
Resolved
});

it('should handle map field when getType returns null for external or primitive types', () => {
let param = {
fileWriter: mockFileWriter,
};

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

const mockField = sinon.createStubInstance(Field);
const mockMapDeclaration = sinon.createStubInstance(MapDeclaration);

mockField.getType.returns('Map1');
mockField.getName.returns('properties');
mockField.getModelFile.returns({
getType: sinon.stub().returns(mockMapDeclaration),
});

mockMapDeclaration.getName.returns('Map1');
mockMapDeclaration.getKey.returns({ getType: () => 'String' });
mockMapDeclaration.getValue.returns({ getType: () => 'ExternalType' });
// getType returns null (e.g. cross-namespace or unresolved type)
mockMapDeclaration.getModelFile.returns({
getType: sinon.stub().returns(null),
});

goVisit.visitField(mockField, param);

param.fileWriter.writeLine.withArgs(1, 'Properties map[string]ExternalType `json:"properties"`').calledOnce.should.be.ok;
});

});
Expand Down