Skip to content

Commit

Permalink
feat(tools): Support for more dotnet built-in types (#605)
Browse files Browse the repository at this point in the history
* Support for more dotnet builtin types

Signed-off-by: [email protected] <[email protected]>

* remove redundant types

Signed-off-by: [email protected] <[email protected]>

---------

Signed-off-by: [email protected] <[email protected]>
  • Loading branch information
ragi-dayananda authored Feb 10, 2023
1 parent 58f34e5 commit 30590fc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ const util = require('util');
if (global === undefined) {
const { ModelFile } = require('@accordproject/concerto-core');
}
const reservedKeywords = ['abstract','as','base','bool','break','byte','case','catch','char','checked',
'class','const','continue','decimal','default','delegate','do','double','else',
'enum','event','explicit','extern','false','finally','fixed','float','for','foreach',
'goto','if','implicit','in','int','interface','internal','is','lock','long','namespace',
const csharpBuiltInTypes = ['bool','byte','char','decimal','double','float','int','long','nint','nuint','sbyte','short',
'string','uint','ulong','ushort'];

const reservedKeywords = csharpBuiltInTypes.concat(['abstract','as','base','break','case','catch','checked',
'class','const','continue','default','delegate','do','else',
'enum','event','explicit','extern','false','finally','fixed','for','foreach',
'goto','if','implicit','in','interface','internal','is','lock','namespace',
'new','null','object','operator','out','override','params','private','protected','public',
'readonly','ref','return','sbyte','sealed','short','sizeof','stackalloc','static',
'string','struct','switch','this','throw','true','try','typeof','uint','ulong','unchecked',
'unsafe','ushort','using','virtual','void','volatile','while'];
'readonly','ref','return','sealed','sizeof','stackalloc','static',
'struct','switch','this','throw','true','try','typeof','unchecked',
'unsafe','using','virtual','void','volatile','while']);

/**
* Convert the contents of a ModelManager to C# code. Set a
Expand Down Expand Up @@ -278,7 +281,7 @@ class CSharpVisitor {
parameters.fileWriter.writeLine(1, '[AccordProject.Concerto.Identifier()]');
}

let fieldType = externalFieldType ? externalFieldType : field.getType();
let fieldType = externalFieldType ? externalFieldType : this.getFieldType(field);

let nullableType = '';
if(field.isOptional() && fieldType !== 'String'){ //string type is nullable by default.
Expand Down Expand Up @@ -467,6 +470,9 @@ class CSharpVisitor {
case 'concerto.scalar.UUID':
return 'System.Guid';
default:
if(csharpBuiltInTypes.includes(type)) {
return type;
}
return this.toCSharpIdentifier(undefined, type, parameters);
}
}
Expand Down Expand Up @@ -498,6 +504,24 @@ class CSharpVisitor {
}
return args[0];
}

/**
* Get the field type for a given field.
* @private
* @param {Field} field - the object being visited
* @return {string} the type for the field
*/
getFieldType(field) {
const decorator = field.getDecorator('DotNetType');
if (!decorator) {
return field.getType();
}
const args = decorator.getArguments();
if (args.length !== 1 || !csharpBuiltInTypes.includes(args[0])) {
throw new Error('Malformed @DotNetType decorator');
}
return args[0];
}
}

module.exports = CSharpVisitor;
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const ModelFile = require('@accordproject/concerto-core').ModelFile;
const ModelManager = require('@accordproject/concerto-core').ModelManager;
const RelationshipDeclaration = require('@accordproject/concerto-core').RelationshipDeclaration;
const FileWriter = require('@accordproject/concerto-util').FileWriter;
const csharpBuiltInTypes = ['bool','byte','char','decimal','double','float','int','long','nint','nuint','sbyte','short',
'string','uint','ulong','ushort'];

describe('CSharpVisitor', function () {
let csharpVisitor;
Expand Down Expand Up @@ -83,6 +85,43 @@ describe('CSharpVisitor', function () {
file2.should.match(/namespace org.acme.other;/);
});

csharpBuiltInTypes.forEach(builtInType => {
it('should use the dotnet built in type '+ builtInType +' if @DotNetType if present', () => {
const modelManager = new ModelManager({ strict: true });
modelManager.addCTOModel(`
namespace [email protected]
concept Thing {
@DotNetType("`+ builtInType +`")
o String builtInTypeValue
@DotNetType("`+ builtInType +`")
o String optionalBuiltInTypeValue optional
}
`);
csharpVisitor.visit(modelManager, { fileWriter });
const files = fileWriter.getFilesInMemory();
const file = files.get('[email protected]');
let matchText = 'public '+ builtInType + ' builtInTypeValue';
file.should.match(new RegExp(matchText,'g'));
matchText = 'public '+ builtInType + '\\? optionalBuiltInTypeValue';
file.should.match(new RegExp(matchText,'g'));
});
});

it('should throw an error when an non built in @DotNetType is supplied', () => {
const modelManager = new ModelManager({ strict: true });
modelManager.addCTOModel(`
namespace [email protected]
concept Thing {
@DotNetType("nonBuiltInType")
o String builtInTypeValue
}`);
(() => {
csharpVisitor.visit(modelManager, { fileWriter });
}).should.throw('Malformed @DotNetType decorator');
});

it('should use the @DotNetNamespace decorator if present', () => {
const modelManager = new ModelManager({ strict: true });
modelManager.addCTOModel(`
Expand Down Expand Up @@ -1137,6 +1176,11 @@ describe('CSharpVisitor', function () {
it('should return Guid for Scalar type UUID', () => {
csharpVisitor.toCSharpType('concerto.scalar.UUID').should.deep.equal('System.Guid');
});
csharpBuiltInTypes.forEach(builtInType => {
it('should return ' + builtInType + ' for ' + builtInType, () => {
csharpVisitor.toCSharpType(builtInType).should.deep.equal(builtInType);
});
});
it('should return passed in type by default', () => {
csharpVisitor.toCSharpType('Penguin').should.deep.equal('Penguin');
});
Expand Down

0 comments on commit 30590fc

Please sign in to comment.