diff --git a/lib/codegen/fromcto/avro/avrovisitor.js b/lib/codegen/fromcto/avro/avrovisitor.js index d44ce0cd..a17f13eb 100644 --- a/lib/codegen/fromcto/avro/avrovisitor.js +++ b/lib/codegen/fromcto/avro/avrovisitor.js @@ -181,6 +181,22 @@ class AvroVisitor { avroType = `union { null, ${avroType} }`; } + if (ModelUtil.isMap(field)) { + const mapDeclaration = field.getModelFile().getType(field.getType()); + let mapValueType = mapDeclaration.getValue().getType(); + + if (ModelUtil.isScalar(mapDeclaration.getValue())) { + const scalarDeclaration = mapDeclaration.getModelFile().getType(mapValueType); + mapValueType = scalarDeclaration.getType(); + } + + const avroValueType = this.toAvroType(mapValueType); + + parameters.fileWriter.writeLine(2, `map<${avroValueType}> ${field.getName()};`); // key is always inferred to be string. + + return null; + } + const fieldName = this.toAvroName(field.getName()); /** diff --git a/test/codegen/__snapshots__/codegen.js.snap b/test/codegen/__snapshots__/codegen.js.snap index dd92e2f9..2b0b19ea 100644 --- a/test/codegen/__snapshots__/codegen.js.snap +++ b/test/codegen/__snapshots__/codegen.js.snap @@ -110,11 +110,11 @@ protocol MyProtocol { record Company { string name; Address headquarters; - union { null, CompanyProperties } companyProperties; - union { null, EmployeeDirectory } employeeDirectory; - union { null, EmployeeTShirtSizes } employeeTShirtSizes; - union { null, EmployeeProfiles } employeeProfiles; - union { null, EmployeeSocialSecurityNumbers } employeeSocialSecurityNumbers; + map companyProperties; + map employeeDirectory; + map employeeTShirtSizes; + map employeeProfiles; + map employeeSocialSecurityNumbers; } enum Department { @@ -150,7 +150,7 @@ protocol MyProtocol { double height; @logicalType("timestamp-micros") long dob; - NextOfKin nextOfKin; + map nextOfKin; } record Employee { @@ -171,7 +171,7 @@ protocol MyProtocol { double height; @logicalType("timestamp-micros") long dob; - NextOfKin nextOfKin; + map nextOfKin; } record Contractor { @@ -186,7 +186,7 @@ protocol MyProtocol { double height; @logicalType("timestamp-micros") long dob; - NextOfKin nextOfKin; + map nextOfKin; } record Manager { @@ -208,7 +208,7 @@ protocol MyProtocol { double height; @logicalType("timestamp-micros") long dob; - NextOfKin nextOfKin; + map nextOfKin; } record CompanyEvent { @@ -6012,11 +6012,11 @@ protocol MyProtocol { record Company { string name; Address headquarters; - union { null, CompanyProperties } companyProperties; - union { null, EmployeeDirectory } employeeDirectory; - union { null, EmployeeTShirtSizes } employeeTShirtSizes; - union { null, EmployeeProfiles } employeeProfiles; - union { null, EmployeeSocialSecurityNumbers } employeeSocialSecurityNumbers; + map companyProperties; + map employeeDirectory; + map employeeTShirtSizes; + map employeeProfiles; + map employeeSocialSecurityNumbers; } enum Department { @@ -6052,7 +6052,7 @@ protocol MyProtocol { double height; @logicalType("timestamp-micros") long dob; - NextOfKin nextOfKin; + map nextOfKin; } record Employee { @@ -6073,7 +6073,7 @@ protocol MyProtocol { double height; @logicalType("timestamp-micros") long dob; - NextOfKin nextOfKin; + map nextOfKin; } record Contractor { @@ -6088,7 +6088,7 @@ protocol MyProtocol { double height; @logicalType("timestamp-micros") long dob; - NextOfKin nextOfKin; + map nextOfKin; } record Manager { @@ -6110,7 +6110,7 @@ protocol MyProtocol { double height; @logicalType("timestamp-micros") long dob; - NextOfKin nextOfKin; + map nextOfKin; } record CompanyEvent {