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(csharp,java): support aliasing imports #115

Merged
6 changes: 5 additions & 1 deletion lib/codegen/fromcto/csharp/csharpvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

'use strict';

const { ModelUtil } = require('@accordproject/concerto-core');
const { ModelUtil, ModelFile } = require('@accordproject/concerto-core');
const camelCase = require('camelcase');
const { throwUnrecognizedType } = require('../../../common/util');

Expand Down Expand Up @@ -383,6 +383,10 @@ class CSharpVisitor {
}
} else if (!field.isPrimitive()) {
let fqn = this.getDotNetNamespaceOfType(field.getFullyQualifiedTypeName(), field.getParent(), parameters);
const modelFile = field.getModelFile();
if (modelFile instanceof ModelFile && modelFile.isImportedType(fieldType)) {
fieldType = modelFile.getActualImportType(fieldType);
}
fieldType = `${fqn}${fieldType}`;
}

Expand Down
12 changes: 9 additions & 3 deletions lib/codegen/fromcto/java/javavisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

'use strict';

const { ModelFile } = require('@accordproject/concerto-core');
const EmptyPlugin = require('./emptyplugin');
const ModelUtil = require('@accordproject/concerto-core').ModelUtil;
const util = require('util');
Expand Down Expand Up @@ -267,9 +268,14 @@ class JavaVisitor {
if(field.isArray()) {
array = '[]';
}

const fieldType = this.toJavaType(field.getType()) + array;

let fieldType = field.getType();
if (!ModelUtil.isPrimitiveType(fieldType)) {
const modelFile = field.getModelFile();
if (modelFile instanceof ModelFile && modelFile.isImportedType(fieldType)) {
fieldType = modelFile.getActualImportType(fieldType);
}
}
fieldType = this.toJavaType(fieldType) + array;
const fieldName = field.getName();
const getterName = 'get' + this.capitalizeFirstLetter(fieldName);
const setterName = 'set' + this.capitalizeFirstLetter(fieldName);
Expand Down
59 changes: 59 additions & 0 deletions test/codegen/fromcto/csharp/csharpvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,65 @@ public class SampleModel : Concept {
file1.should.match(/public System.Guid otherThingId/);
file1.should.match(/public string someOtherThingId/);
});

it('should handle imported field which is aliased in a concept', () => {
const modelManager = new ModelManager({ enableAliasedType: true });
modelManager.addCTOModel(`
namespace org.example.basic
concept file{
o String name
}
`);
modelManager.addCTOModel(`
namespace org.example.complex
import org.example.basic.{file as f}

concept folder {
o String name
o f[] files
}
`);
csharpVisitor.visit(modelManager, { fileWriter });
const files = fileWriter.getFilesInMemory();
const file1 = files.get('org.example.basic.cs');
file1.should.match(/namespace org.example.basic;/);
file1.should.match(/class file : Concept/);
file1.should.match(/public string name { get; set; }/);

const file2 = files.get('org.example.complex.cs');
file2.should.match(/namespace org.example.complex;/);
file2.should.match(/using org.example.basic;/);
file2.should.match(/class folder : Concept/);
file2.should.match(/org.example.basic.file\[\] files/);
});

it('should handle imported field which extended in concept', () => {
const modelManager = new ModelManager({ enableAliasedType: true });
modelManager.addCTOModel(`
namespace org.example.basic
concept file{
o String name
}
`);
modelManager.addCTOModel(`
namespace org.example.complex
import org.example.basic.{file as f}

concept bigFile extends f{
}
`);
csharpVisitor.visit(modelManager, { fileWriter });
const files = fileWriter.getFilesInMemory();
const file1 = files.get('org.example.basic.cs');
file1.should.match(/namespace org.example.basic;/);
file1.should.match(/class file : Concept/);
file1.should.match(/public string name { get; set; }/);

const file2 = files.get('org.example.complex.cs');
file2.should.match(/namespace org.example.complex;/);
file2.should.match(/using org.example.basic;/);
file2.should.match(/public class bigFile : org.example.basic.file/);
});
});

describe('visit', () => {
Expand Down
Loading