Skip to content

Commit

Permalink
Add conversion for nested fields with namespaces/name
Browse files Browse the repository at this point in the history
  • Loading branch information
SasinduDilshara committed Dec 11, 2024
1 parent 8fe7203 commit 3004906
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
101 changes: 101 additions & 0 deletions ballerina/tests/test_ref_types_with_annotations.bal
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,104 @@ function testTypeRefAnnotations2() returns error? {
test:assertTrue(xmlResult is xml:Element);
test:assertEquals(xmlResult.toString(), string `<a1:root xmlns:a1="http://ballerina.io"><nestedA:nestedA xmlns:nestedA="http://nested-ballerina.io"><A>1</A><B>2</B></nestedA:nestedA><nestedB:nestedB xmlns:nestedB="http://nested-ballerina.io"><a:a xmlns:a="http://nested-ballerina.io/a">3</a:a><B>4</B></nestedB:nestedB></a1:root>`);
}

@Name {
value: "root"
}
type TypeRefType3 record {
@Namespace {
uri: "http://www.example.com/",
prefix: "ex"
}
@Name {
value: "A"
}
int a;
};

@test:Config
function testTypeRefAnnotations3() returns error? {
TypeRefType3 t = {a: 1};
xml xmlResult = check toXml(t);
test:assertTrue(xmlResult is xml:Element);
test:assertEquals(xmlResult.toString(), string `<root><ex:A xmlns:ex="http://www.example.com/">1</ex:A></root>`);
}

@Namespace {
uri: "http://ballerina.io",
prefix: "a1"
}
@Name {
value: "root"
}
type TypeRefType4 record {
@Name {
value: "nestedA"
}
NestedTypeRefType5 a;

@Name {
value: "nestedB"
}
NestedTypeRefType6 b;
};

@Namespace {
uri: "http://nested-ballerina.io",
prefix: "nestedA"
}
type NestedTypeRefType5 record {
@Name {
value: "A"
}
@Namespace {
uri: "http://nested-ballerina.io/a",
prefix: "a"
}
int a;

@Namespace {
uri: "http://nested-ballerina.io/a",
prefix: "a"
}
@Name {
value: "B"
}
int b;
};

@Namespace {
uri: "http://nested-ballerina.io",
prefix: "nestedB"
}
type NestedTypeRefType6 record {
@Name {
value: "A"
}
@Namespace {
uri: "http://nested-ballerina.io/a",
prefix: "a"
}
int a;

@Name {
value: "B"
}
@Namespace {
uri: "http://nested-ballerina.io/a",
prefix: "a"
}
int b;
};

@test:Config
function testTypeRefAnnotations4() returns error? {
TypeRefType4 t = {
a: {a: 1, b: 2},
b: {a: 3, b: 4}
};

xml xmlResult = check toXml(t);
test:assertTrue(xmlResult is xml:Element);
test:assertEquals(xmlResult.toString(), string `<a1:root xmlns:a1="http://ballerina.io"><nestedA:nestedA xmlns:nestedA="http://nested-ballerina.io"><a:A xmlns:a="http://nested-ballerina.io/a">1</a:A><a:B xmlns:a="http://nested-ballerina.io/a">2</a:B></nestedA:nestedA><nestedB:nestedB xmlns:nestedB="http://nested-ballerina.io"><a:A xmlns:a="http://nested-ballerina.io/a">3</a:A><a:B xmlns:a="http://nested-ballerina.io/a">4</a:B></nestedB:nestedB></a1:root>`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -738,16 +738,26 @@ private static void addPrimitiveValue(QName qName, BMap<BString, Object> annotat
BString annotationKey = StringUtils.fromString(Constants.FIELD
+ (localPart.getValue().replaceAll(Constants.RECORD_FIELD_NAME_ESCAPE_CHAR_REGEX, "\\\\$0")));
BMap<BString, Object> currentValue;
BString prevKey = key;
if (record.containsKey(key)) {
currentValue = (BMap<BString, Object>) record.get(key);
prevKey = key;
key = StringUtils.fromString(contentFieldName);
} else {
currentValue = record;
}

if (annotations.containsKey(annotationKey)) {
BMap<BString, Object> annotationValue = (BMap<BString, Object>) annotations.get(annotationKey);
currentValue.put(StringUtils.fromString(processFieldAnnotation(annotationValue, key.getValue())), value);
String keyName = processFieldAnnotation(annotationValue, prevKey.getValue());
if (key.getValue().equals(contentFieldName)) {
currentValue.put(StringUtils.fromString(contentFieldName), value);
if (!keyName.equals(prevKey.getValue())) {
record.put(StringUtils.fromString(keyName), record.remove(prevKey));
}
} else {
currentValue.put(StringUtils.fromString(keyName), value);
}
} else {
currentValue.put(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ value, allNamespaces, getEmptyStringMap(), options, key, getChildElementType(
return CreateElement.createElement(rootTagBstring, getEmptyStringMap(), output);
}
return output;
} catch (BError e) {
} catch (Exception e) {
return DiagnosticLog.createXmlError(e.getMessage());
}
}
Expand Down

0 comments on commit 3004906

Please sign in to comment.