Skip to content

Commit

Permalink
Merge branch 'main' into jonathan/codegen_map_xmlschema
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-casey committed Nov 10, 2023
2 parents ce647a1 + 79653dd commit bc7653a
Show file tree
Hide file tree
Showing 5 changed files with 350 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/codegen/fromcto/java/javavisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class JavaVisitor {
break;
default:
if (ModelUtil.isMap(field)) {
const decl = field.getModelFile().getType(field.ast.type.name);
const decl = field.getModelFile().getType(field.getType());
parameters.fileWriter.writeLine(1, `private Map<${this.toJavaType(decl.getKey().getType())}, ${this.toJavaType(decl.getValue().getType())}> ${field.getName()} = new HashMap<>();`);
} else {
parameters.fileWriter.writeLine(1, 'private ' + fieldType + ' ' + fieldName + ';' );
Expand Down
22 changes: 18 additions & 4 deletions lib/codegen/fromcto/protobuf/protobufvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ class ProtobufVisitor {

/**
* Transform a Concerto primitive type into a Proto3 one.
* @param {Object} field - the Concerto primitive type
* @param {Object} type - the Concerto primitive type
* @return {Object} the Proto3 primitive type
* @public
*/
concertoToProto3PrimitiveType(field) {
switch (field.getType()) {
concertoToProto3PrimitiveType(type) {
switch (type) {
case 'String':
return 'string';
case 'Double':
Expand Down Expand Up @@ -394,9 +394,23 @@ class ProtobufVisitor {
debug('entering visitField', field.getName());

const preposition = this.concertoToProto3FieldRule(field);

if (ModelUtil.isMap(field)) {
const mapDeclaration = field.getModelFile().getType(field.getType());
const keyType = mapDeclaration.getKey().getType();
const valueType = mapDeclaration.getValue().getType();

let key = ModelUtil.isPrimitiveType(keyType) ? this.concertoToProto3PrimitiveType(keyType) : keyType;
let value = ModelUtil.isPrimitiveType(valueType) ? this.concertoToProto3PrimitiveType(valueType) : valueType;

parameters.fileWriter.writeLine(0, ` ${preposition ? `${preposition} ` : ''}map<${key}, ${value}> ${mapDeclaration.getName()} = ${parameters.fieldIndex ?? '0'};`);

return;
}

const type = field.isPrimitive()
// Primitive Concerto types are mapped to specific Proto3 types.
? this.concertoToProto3PrimitiveType(field)
? this.concertoToProto3PrimitiveType(field.getType())
// The rest are references to classes and enums.
: this.concertoToProto3MessageOrEnumType(field);
// Proto3 is not happy with the "$" sign, so we are replacing it with an "_".
Expand Down
32 changes: 16 additions & 16 deletions test/codegen/__snapshots__/codegen.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3681,11 +3681,11 @@ import "google/protobuf/timestamp.proto";
import "org.acme.hr.base.v.proto";

message Company {
optional CompanyProperties companyProperties = 1;
optional EmployeeDirectory employeeDirectory = 2;
optional EmployeeProfiles employeeProfiles = 3;
optional EmployeeSocialSecurityNumbers employeeSocialSecurityNumbers = 4;
optional EmployeeTShirtSizes employeeTShirtSizes = 5;
optional map<string, string> CompanyProperties = 1;
optional map<SSN, Employee> EmployeeDirectory = 2;
optional map<string, Concept> EmployeeProfiles = 3;
optional map<string, SSN> EmployeeSocialSecurityNumbers = 4;
optional map<SSN, TShirtSizeType> EmployeeTShirtSizes = 5;
Address headquarters = 6;
string name = 7;
}
Expand Down Expand Up @@ -3735,7 +3735,7 @@ message Employee {
string lastName = 9;
optional string manager = 10;
optional string middleNames = 11;
NextOfKin nextOfKin = 12;
map<KinName, KinTelephone> NextOfKin = 12;
sint64 numDependents = 13;
Address officeAddress = 14;
bool retired = 15;
Expand All @@ -3760,7 +3760,7 @@ message Contractor {
string lastName = 7;
optional string manager = 8;
optional string middleNames = 9;
NextOfKin nextOfKin = 10;
map<KinName, KinTelephone> NextOfKin = 10;
string ssn = 11;
}

Expand All @@ -3776,7 +3776,7 @@ message Manager {
string lastName = 9;
optional string manager = 10;
optional string middleNames = 11;
NextOfKin nextOfKin = 12;
map<KinName, KinTelephone> NextOfKin = 12;
sint64 numDependents = 13;
Address officeAddress = 14;
repeated string reports = 15;
Expand Down Expand Up @@ -9314,11 +9314,11 @@ import "google/protobuf/timestamp.proto";
import "org.acme.hr.base.v1_0_0.proto";

message Company {
optional CompanyProperties companyProperties = 1;
optional EmployeeDirectory employeeDirectory = 2;
optional EmployeeProfiles employeeProfiles = 3;
optional EmployeeSocialSecurityNumbers employeeSocialSecurityNumbers = 4;
optional EmployeeTShirtSizes employeeTShirtSizes = 5;
optional map<string, string> CompanyProperties = 1;
optional map<SSN, Employee> EmployeeDirectory = 2;
optional map<string, Concept> EmployeeProfiles = 3;
optional map<string, SSN> EmployeeSocialSecurityNumbers = 4;
optional map<SSN, TShirtSizeType> EmployeeTShirtSizes = 5;
Address headquarters = 6;
string name = 7;
}
Expand Down Expand Up @@ -9368,7 +9368,7 @@ message Employee {
string lastName = 9;
optional string manager = 10;
optional string middleNames = 11;
NextOfKin nextOfKin = 12;
map<KinName, KinTelephone> NextOfKin = 12;
sint64 numDependents = 13;
Address officeAddress = 14;
bool retired = 15;
Expand All @@ -9393,7 +9393,7 @@ message Contractor {
string lastName = 7;
optional string manager = 8;
optional string middleNames = 9;
NextOfKin nextOfKin = 10;
map<KinName, KinTelephone> NextOfKin = 10;
string ssn = 11;
}

Expand All @@ -9409,7 +9409,7 @@ message Manager {
string lastName = 9;
optional string manager = 10;
optional string middleNames = 11;
NextOfKin nextOfKin = 12;
map<KinName, KinTelephone> NextOfKin = 12;
sint64 numDependents = 13;
Address officeAddress = 14;
repeated string reports = 15;
Expand Down
Loading

0 comments on commit bc7653a

Please sign in to comment.