From ebcaecf418c084a1d4dac90cf0cae905862ad8a7 Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Wed, 17 Jul 2024 23:12:28 +0530 Subject: [PATCH 1/3] [Automated] Update the native jar versions --- ballerina/Ballerina.toml | 6 +++--- ballerina/CompilerPlugin.toml | 2 +- ballerina/Dependencies.toml | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ballerina/Ballerina.toml b/ballerina/Ballerina.toml index 89f5455..1f904e9 100644 --- a/ballerina/Ballerina.toml +++ b/ballerina/Ballerina.toml @@ -1,7 +1,7 @@ [package] org = "ballerina" name = "data.xmldata" -version = "0.1.3" +version = "0.1.4" authors = ["Ballerina"] keywords = ["xml"] repository = "https://github.com/ballerina-platform/module-ballerina-data-xmldata" @@ -12,5 +12,5 @@ export = ["data.xmldata"] [[platform.java17.dependency]] groupId = "io.ballerina.lib" artifactId = "data-native" -version = "0.1.3" -path = "../native/build/libs/data.xmldata-native-0.1.3.jar" +version = "0.1.4" +path = "../native/build/libs/data.xmldata-native-0.1.4-SNAPSHOT.jar" diff --git a/ballerina/CompilerPlugin.toml b/ballerina/CompilerPlugin.toml index edc2310..2585887 100644 --- a/ballerina/CompilerPlugin.toml +++ b/ballerina/CompilerPlugin.toml @@ -3,4 +3,4 @@ id = "constraint-compiler-plugin" class = "io.ballerina.lib.data.xmldata.compiler.XmldataCompilerPlugin" [[dependency]] -path = "../compiler-plugin/build/libs/data.xmldata-compiler-plugin-0.1.3.jar" +path = "../compiler-plugin/build/libs/data.xmldata-compiler-plugin-0.1.4-SNAPSHOT.jar" diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index d920883..e994f42 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -10,7 +10,7 @@ distribution-version = "2201.9.0" [[package]] org = "ballerina" name = "data.xmldata" -version = "0.1.3" +version = "0.1.4" dependencies = [ {org = "ballerina", name = "io"}, {org = "ballerina", name = "jballerina.java"}, From 7f3b210a7d0e9e74ca752ef5571449dd93c7b99a Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Thu, 18 Jul 2024 17:34:54 +0530 Subject: [PATCH 2/3] Fix toXml failed when reference type is not record --- ballerina/tests/toXml_test.bal | 35 +++++++ .../lib/data/xmldata/utils/DataUtils.java | 97 +++++++++++-------- 2 files changed, 91 insertions(+), 41 deletions(-) diff --git a/ballerina/tests/toXml_test.bal b/ballerina/tests/toXml_test.bal index 9dc91e9..9e80334 100644 --- a/ballerina/tests/toXml_test.bal +++ b/ballerina/tests/toXml_test.bal @@ -1330,6 +1330,41 @@ isolated function testToRecordFieldNameEndsWithNameOrNamespace() returns error? test:assertEquals(result.toString(), expected); } +type VersionType string; + +@Name { + value: "Payload" +} +type Payload1 record { + @Attribute + string 'version?; + VersionType 'type?; +}; + +type Map map; + +@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(), "example"); + + Payload2 payload2 = {'version: "1.0", value: {id: "243", name: "Kanth"}}; + xml result2 = check toXml(payload2); + test:assertEquals(result2.toString(), "243Kanth"); +} + @test:Config { groups: ["toXml"] } diff --git a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java index 3d26869..86fd06f 100644 --- a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java +++ b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java @@ -472,45 +472,7 @@ private static BMap addFields(BMap 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 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 annotationRecord = ValueCreator.createMapValue(Constants.JSON_MAP_TYPE); - if (!doesNamespaceDefinedInField) { - BMap subRecordAnnotations = ((RecordType) referredType).getAnnotations(); - key = getElementName(subRecordAnnotations, key); - processSubRecordAnnotation(subRecordAnnotations, annotationRecord); - } - - BMap subRecordValue = addFields(((BMap) 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); } @@ -518,6 +480,59 @@ private static BMap addFields(BMap input, Type return recordValue; } + private static void processRecordField(Type fieldType, BMap annotations, + BMap recordValue, Map.Entry 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 annotations, + BMap recordValue, String key, Object value) { + BMap 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 annotationRecord = ValueCreator.createMapValue(Constants.JSON_MAP_TYPE); + Type referredType = TypeUtils.getReferredType(fieldType); + if (!doesNamespaceDefinedInField) { + BMap subRecordAnnotations = ((RecordType) referredType).getAnnotations(); + key = getElementName(subRecordAnnotations, key); + processSubRecordAnnotation(subRecordAnnotations, annotationRecord); + } + + BMap subRecordValue = addFields(((BMap) 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 namespaceAnnotRecord, BMap subRecord) { @@ -592,9 +607,9 @@ private static BMap getFieldNamespaceAndNameAnnotations(String @SuppressWarnings("unchecked") private static void processRecord(String key, BMap parentAnnotations, - BMap record, Object value, Type childType) { + BMap record, Object value, RecordType childType) { BMap parentRecordAnnotations = ValueCreator.createMapValue(Constants.JSON_MAP_TYPE); - BMap annotation = ((RecordType) childType).getAnnotations(); + BMap annotation = childType.getAnnotations(); if (parentAnnotations.size() > 0) { annotation.merge(getFieldNamespaceAndNameAnnotations(key, parentAnnotations), true); processSubRecordAnnotation(parentAnnotations, parentRecordAnnotations); From 28079317a20e8b5abca9b8fb71a609c70ad1c580 Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Thu, 18 Jul 2024 21:40:41 +0530 Subject: [PATCH 3/3] Address review suggestions --- ballerina/tests/toXml_test.bal | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ballerina/tests/toXml_test.bal b/ballerina/tests/toXml_test.bal index 9e80334..b963091 100644 --- a/ballerina/tests/toXml_test.bal +++ b/ballerina/tests/toXml_test.bal @@ -1330,7 +1330,9 @@ isolated function testToRecordFieldNameEndsWithNameOrNamespace() returns error? test:assertEquals(result.toString(), expected); } -type VersionType string; +type String string; + +type VersionType String; @Name { value: "Payload"