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 XMLSchema Target #64

Merged
merged 22 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7cdcc56
feat(map): extend xmlschemavisitor
jonathan-casey Oct 13, 2023
2c5e4a7
test(map): adds test coverage
jonathan-casey Oct 13, 2023
98d5f33
test(map): updates snapshots
jonathan-casey Oct 13, 2023
cd7066c
feat(map): write scalars as primitives
jonathan-casey Oct 16, 2023
fc880d0
test(map): adds more test cases
jonathan-casey Oct 16, 2023
41cfb77
test(map): update snapshot
jonathan-casey Oct 16, 2023
0e45cfb
Merge branch 'main' into jonathan/codegen_map_xmlschema
jonathan-casey Nov 9, 2023
2cce59e
test(*): update snapshot
jonathan-casey Nov 9, 2023
5e2dcc5
feat(*): add type def
jonathan-casey Nov 9, 2023
3412f86
feat(*): removes superflous tags, declares element
jonathan-casey Nov 9, 2023
97f1b3f
test(*): fix test regression
jonathan-casey Nov 9, 2023
63d37c7
test(*): update snapshot
jonathan-casey Nov 9, 2023
74ce780
feat(*): get type from model file
jonathan-casey Nov 9, 2023
7619ed6
test(*): fix test regression
jonathan-casey Nov 10, 2023
ce647a1
test(*): update snapshot
jonathan-casey Nov 10, 2023
bc7653a
Merge branch 'main' into jonathan/codegen_map_xmlschema
jonathan-casey Nov 10, 2023
7dd8d4f
test(*): fix tests
jonathan-casey Nov 10, 2023
435a37f
feat(map): Extend supported Key Types for Typescript codegen (#70)
jonathan-casey Nov 13, 2023
6b5eecb
test(*): snapshot update
jonathan-casey Nov 14, 2023
025d948
feat(*): update xsd and test cov
jonathan-casey Nov 15, 2023
4239ab2
test(*): resets sandbox for test scenario
jonathan-casey Nov 15, 2023
c78ccde
test(*): fix casing
jonathan-casey Nov 16, 2023
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
35 changes: 22 additions & 13 deletions lib/codegen/fromcto/typescript/typescriptvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,24 +281,33 @@ class TypescriptVisitor {
* @private
*/
visitMapDeclaration(mapDeclaration, parameters) {
// const keyType = mapDeclaration.getKey().getType(); // for now, key is always string.
let valueType = mapDeclaration.getValue().getType();

const isScalar = ModelUtil.isScalar(mapDeclaration.getValue());
const isPrimitive = ModelUtil.isPrimitiveType(mapDeclaration.getValue().getType());
const mapKeyType = mapDeclaration.getKey().getType();
const mapValueType = mapDeclaration.getValue().getType();

let keyType;
let valueType;

// Map Key Type
if (ModelUtil.isPrimitiveType(mapKeyType)) {
keyType = this.toTsType(mapDeclaration.getKey().getType(), false, false);
} else if (ModelUtil.isScalar(mapDeclaration.getKey())) {
const scalarDeclaration = mapDeclaration.getModelFile().getType(mapDeclaration.getKey().getType());
const scalarType = scalarDeclaration.getType();
keyType = this.toTsType(scalarType, false, false);
}

if (isScalar) {
const scalar = mapDeclaration.getModelFile(mapDeclaration.getValue().getType());
const scalarType = scalar.getType();
valueType = this.toTsType(scalarType, false, false);
} else if (isPrimitive) {
// Map Value Type
if (ModelUtil.isPrimitiveType(mapValueType)) {
valueType = this.toTsType(mapDeclaration.getValue().getType(), false, false);
} else if (ModelUtil.isScalar(mapDeclaration.getValue())) {
const scalarDeclaration = mapDeclaration.getModelFile().getType(mapDeclaration.getValue().getType());
const scalarType = scalarDeclaration.getType();
valueType = this.toTsType(scalarType, false, false);
} else {
valueType = this.toTsType(valueType, true, false);

valueType = this.toTsType(mapValueType, true, false);
}

parameters.fileWriter.writeLine(0, 'export type ' + mapDeclaration.getName() + ` = Map<string, ${valueType}>;\n` );
parameters.fileWriter.writeLine(0, 'export type ' + mapDeclaration.getName() + ` = Map<${keyType}, ${valueType}>;\n` );

return null;
}
Expand Down
49 changes: 47 additions & 2 deletions lib/codegen/fromcto/xmlschema/xmlschemavisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class XmlSchemaVisitor {
} else if (thing.isClassDeclaration?.()) {
return this.visitClassDeclaration(thing, parameters);
} else if (thing.isMapDeclaration?.()) {
return;
return this.visitMapDeclaration(thing, parameters);
} else if (thing.isTypeScalar?.()) {
return this.visitField(thing.getScalarField(), parameters);
} else if (thing.isField?.()) {
Expand Down Expand Up @@ -153,6 +153,39 @@ class XmlSchemaVisitor {
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
mttrbrts marked this conversation as resolved.
Show resolved Hide resolved
* @private
*/
visitMapDeclaration(mapDeclaration, parameters) {

const key = this.toXsType(mapDeclaration.getKey().getType(), mapDeclaration.getKey(), mapDeclaration);
const value = this.toXsType(mapDeclaration.getValue().getType(), mapDeclaration.getValue(), mapDeclaration);

// write the element representing the MapDeclaration
parameters.fileWriter.writeLine(0, `<xs:element name="${mapDeclaration.getName()}">` );
parameters.fileWriter.writeLine(0, '<xs:complexType>' );
parameters.fileWriter.writeLine(1, '<xs:sequence>');
parameters.fileWriter.writeLine(2, `<xs:element name="entry" type="${mapDeclaration.getName()}EntryType" minOccurs="0" maxOccurs="unbounded"/>`);
parameters.fileWriter.writeLine(1, '</xs:sequence>');
parameters.fileWriter.writeLine(0, '</xs:complexType>');
parameters.fileWriter.writeLine(0, '</xs:element>');

// write the Entry type reprenting the schema of the MapDeclaration
parameters.fileWriter.writeLine(0, `<xs:complexType name="${mapDeclaration.getName()}EntryType">` );
parameters.fileWriter.writeLine(1, '<xs:sequence>');
parameters.fileWriter.writeLine(2, `<xs:element name="key" type="${key}"/>`);
parameters.fileWriter.writeLine(2, `<xs:element name="value" type="${value}"/>`);
parameters.fileWriter.writeLine(1, '</xs:sequence>');
parameters.fileWriter.writeLine(0, '</xs:complexType>');

return null;
}


/**
* Visitor design pattern
* @param {ClassDeclaration} classDeclaration - the object being visited
Expand Down Expand Up @@ -256,10 +289,12 @@ class XmlSchemaVisitor {
* Converts a Concerto type to a XML Schema type. Primitive types are converted
* everything else is passed through unchanged.
* @param {string} type - the fully qualified concerto type name
* @param {Object} mapElement - the mapElement representing either the Key or Value of a MapDeclaration
* @param {MapDeclaration} mapDeclaration - the object representing a MapDeclaration
* @return {string} the corresponding type in XML Schema
* @private
*/
toXsType(type) {
toXsType(type, mapElement, mapDeclaration) {
switch(type) {
case 'DateTime':
return 'xs:dateTime';
Expand All @@ -274,6 +309,16 @@ class XmlSchemaVisitor {
case 'Integer':
return 'xs:integer';
default:
if(!ModelUtil.getNamespace(type)) {
const typeDeclaration = mapDeclaration.getModelFile().getType(type);
if (typeDeclaration?.isClassDeclaration?.() && ['Concept', 'Asset', 'Transaction', 'Participant', 'Event'].includes(type)) {
return `concerto:${mapElement.getType()}`;
} else if (typeDeclaration?.isClassDeclaration?.() ) {
return `${ModelUtil.parseNamespace(mapDeclaration.getModelFile().getNamespace()).name}:${mapElement.getType()}`;
} else if (typeDeclaration?.isScalarDeclaration?.()) {
return this.toXsType(typeDeclaration.getType());
}
}
return `${ModelUtil.parseNamespace(ModelUtil.getNamespace(type)).name}:${ModelUtil.getShortName(type)}`;
}
}
Expand Down
Loading
Loading