Skip to content

Commit

Permalink
Resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
prakanth97 committed Jul 25, 2024
2 parents 0bbdaa6 + b2524f0 commit 1990aeb
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 41 deletions.
37 changes: 37 additions & 0 deletions ballerina/tests/toXml_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,43 @@ isolated function testToRecordFieldNameEndsWithNameOrNamespace() returns error?
test:assertEquals(result.toString(), expected);
}

type String string;

type VersionType String;

@Name {
value: "Payload"
}
type Payload1 record {
@Attribute
string 'version?;
VersionType 'type?;
};

type Map map<json>;

@Name {
value: "Payload"
}
type Payload2 record {|
@Attribute
string 'version?;
Map value;
|};

@test:Config {
groups: ["toXml"]
}
isolated function testRecordFieldTypeAsReferenceTypeForToXml() returns error? {
Payload1 payload1 = {'version: "1.0", 'type: "example"};
xml result = check toXml(payload1);
test:assertEquals(result.toString(), "<Payload version=\"1.0\"><type>example</type></Payload>");

Payload2 payload2 = {'version: "1.0", value: {id: "243", name: "Kanth"}};
xml result2 = check toXml(payload2);
test:assertEquals(result2.toString(), "<Payload version=\"1.0\"><value><id>243</id><name>Kanth</name></value></Payload>");
}

@test:Config {
groups: ["toXml"]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,52 +473,67 @@ private static BMap<BString, Object> addFields(BMap<BString, Object> input, Type
String key = entry.getKey().getValue();
Object value = entry.getValue();
if (fields.containsKey(key)) {
Type fieldType = fields.get(key).getFieldType();
fieldType = getTypeFromUnionType(fieldType, value);
if (fieldType.getTag() == TypeTags.RECORD_TYPE_TAG) {
processRecord(key, annotations, recordValue, value, fieldType);
} else if (fieldType.getTag() == TypeTags.TYPE_REFERENCED_TYPE_TAG) {
Type referredType = TypeUtils.getReferredType(fieldType);
BMap<BString, Object> namespaceAnnotRecord = ValueCreator.createMapValue(Constants.JSON_MAP_TYPE);
boolean doesNamespaceDefinedInField = false;
if (annotations.size() > 0) {
String fieldName = key;
key = getKeyNameFromAnnotation(annotations, key);
QName qName = addFieldNamespaceAnnotation(fieldName, key, annotations, namespaceAnnotRecord);
if (!qName.getNamespaceURI().equals("")) {
doesNamespaceDefinedInField = true;
}
String localPart = qName.getLocalPart();
key = qName.getPrefix().isBlank() ? localPart : qName.getPrefix() + ":" + localPart;
}

BMap<BString, Object> annotationRecord = ValueCreator.createMapValue(Constants.JSON_MAP_TYPE);
if (!doesNamespaceDefinedInField) {
BMap<BString, Object> subRecordAnnotations = ((RecordType) referredType).getAnnotations();
key = getElementName(subRecordAnnotations, key);
processSubRecordAnnotation(subRecordAnnotations, annotationRecord);
}

BMap<BString, Object> subRecordValue = addFields(((BMap<BString, Object>) value), referredType);
addNamespaceToSubRecord(key, namespaceAnnotRecord, subRecordValue);
if (annotationRecord.size() > 0) {
subRecordValue.put(annotationRecord.getKeys()[0],
annotationRecord.get(annotationRecord.getKeys()[0]));
}
recordValue.put(StringUtils.fromString(key), subRecordValue);
} else if (fieldType.getTag() == TypeTags.ARRAY_TAG) {
processArray(fieldType, annotations, recordValue, entry);
} else {
addPrimitiveValue(addFieldNamespaceAnnotation(key, key, annotations, recordValue),
annotations, recordValue, value);
}
processRecordField(fields.get(key).getFieldType(), annotations, recordValue, entry, key, value);
} else {
recordValue.put(StringUtils.fromString(key), value);
}
}
return recordValue;
}

private static void processRecordField(Type fieldType, BMap<BString, Object> annotations,
BMap<BString, Object> recordValue, Map.Entry<BString, Object> entry,
String key, Object value) {
fieldType = getTypeFromUnionType(fieldType, value);
switch (fieldType.getTag()) {
case TypeTags.RECORD_TYPE_TAG -> processRecord(key, annotations, recordValue, value,
(RecordType) fieldType);
case TypeTags.ARRAY_TAG -> processArray(fieldType, annotations, recordValue, entry);
case TypeTags.TYPE_REFERENCED_TYPE_TAG -> {
Type referredType = TypeUtils.getReferredType(fieldType);
if (referredType.getTag() != TypeTags.RECORD_TYPE_TAG) {
processRecordField(referredType, annotations, recordValue, entry, key, value);
return;
}
processTypeReferenceType(fieldType, annotations, recordValue, key, value);
}
default -> addPrimitiveValue(addFieldNamespaceAnnotation(key, key, annotations, recordValue),
annotations, recordValue, value);
}
}

@SuppressWarnings("unchecked")
private static void processTypeReferenceType(Type fieldType, BMap<BString, Object> annotations,
BMap<BString, Object> recordValue, String key, Object value) {
BMap<BString, Object> namespaceAnnotRecord = ValueCreator.createMapValue(Constants.JSON_MAP_TYPE);
boolean doesNamespaceDefinedInField = false;
if (annotations.size() > 0) {
String fieldName = key;
key = getKeyNameFromAnnotation(annotations, key);
QName qName = addFieldNamespaceAnnotation(fieldName, key, annotations, namespaceAnnotRecord);
if (!qName.getNamespaceURI().equals("")) {
doesNamespaceDefinedInField = true;
}
String localPart = qName.getLocalPart();
key = qName.getPrefix().isBlank() ? localPart : qName.getPrefix() + ":" + localPart;
}

BMap<BString, Object> annotationRecord = ValueCreator.createMapValue(Constants.JSON_MAP_TYPE);
Type referredType = TypeUtils.getReferredType(fieldType);
if (!doesNamespaceDefinedInField) {
BMap<BString, Object> subRecordAnnotations = ((RecordType) referredType).getAnnotations();
key = getElementName(subRecordAnnotations, key);
processSubRecordAnnotation(subRecordAnnotations, annotationRecord);
}

BMap<BString, Object> subRecordValue = addFields(((BMap<BString, Object>) value), referredType);
addNamespaceToSubRecord(key, namespaceAnnotRecord, subRecordValue);
if (annotationRecord.size() > 0) {
subRecordValue.put(annotationRecord.getKeys()[0], annotationRecord.get(annotationRecord.getKeys()[0]));
}
recordValue.put(StringUtils.fromString(key), subRecordValue);
}

@SuppressWarnings("unchecked")
private static void addNamespaceToSubRecord(String key, BMap<BString, Object> namespaceAnnotRecord,
BMap<BString, Object> subRecord) {
Expand Down Expand Up @@ -593,9 +608,9 @@ private static BMap<BString, Object> getFieldNamespaceAndNameAnnotations(String

@SuppressWarnings("unchecked")
private static void processRecord(String key, BMap<BString, Object> parentAnnotations,
BMap<BString, Object> record, Object value, Type childType) {
BMap<BString, Object> record, Object value, RecordType childType) {
BMap<BString, Object> parentRecordAnnotations = ValueCreator.createMapValue(Constants.JSON_MAP_TYPE);
BMap<BString, Object> annotation = ((RecordType) childType).getAnnotations();
BMap<BString, Object> annotation = childType.getAnnotations();
if (parentAnnotations.size() > 0) {
annotation.merge(getFieldNamespaceAndNameAnnotations(key, parentAnnotations), true);
processSubRecordAnnotation(parentAnnotations, parentRecordAnnotations);
Expand Down

0 comments on commit 1990aeb

Please sign in to comment.