From 98962419f0734fcb467af75d615fdb2b378f44fa Mon Sep 17 00:00:00 2001 From: hindujaB Date: Fri, 3 Nov 2023 15:58:13 +0530 Subject: [PATCH 1/5] Fix wrong record value generation --- .../ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java index 58de262b3ae5..4a41ce0ce741 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java @@ -34,6 +34,7 @@ import org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableTypeSymbol; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BObjectTypeSymbol; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol; +import org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols; import org.wso2.ballerinalang.compiler.semantics.model.types.BField; import org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType; import org.wso2.ballerinalang.compiler.semantics.model.types.BObjectType; @@ -42,6 +43,7 @@ import org.wso2.ballerinalang.compiler.util.Name; import org.wso2.ballerinalang.compiler.util.Names; import org.wso2.ballerinalang.compiler.util.TypeTags; +import org.wso2.ballerinalang.util.Flags; import org.wso2.ballerinalang.util.Lists; import java.util.ArrayList; @@ -190,6 +192,9 @@ private static void encodePackageIdentifiers(PackageID packageID, HashMap typeDefs, HashMap encodedVsInitialIds) { for (BIRTypeDefinition typeDefinition : typeDefs) { + if (Symbols.isFlagOn(typeDefinition.type.flags, Flags.ANONYMOUS)) { + typeDefinition.name = Names.fromString(Utils.unescapeBallerina(typeDefinition.name.value)); + } typeDefinition.type.tsymbol.name = Names.fromString(encodeNonFunctionIdentifier( typeDefinition.type.tsymbol.name.value, encodedVsInitialIds)); typeDefinition.internalName = From fb6c61c5fe7660bd199f916ffdc19dd721c3a53f Mon Sep 17 00:00:00 2001 From: hindujaB Date: Fri, 3 Nov 2023 16:11:49 +0530 Subject: [PATCH 2/5] Add test case with records --- .../langlib/test/LangLibValueTest.java | 2 +- .../test/resources/test-src/valuelib_test.bal | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/langlib/langlib-test/src/test/java/org/ballerinalang/langlib/test/LangLibValueTest.java b/langlib/langlib-test/src/test/java/org/ballerinalang/langlib/test/LangLibValueTest.java index d44f848941ac..cec4adb5d0bd 100644 --- a/langlib/langlib-test/src/test/java/org/ballerinalang/langlib/test/LangLibValueTest.java +++ b/langlib/langlib-test/src/test/java/org/ballerinalang/langlib/test/LangLibValueTest.java @@ -379,7 +379,7 @@ public Object[] cloneWithTypeFunctions() { "testConvertToUnionWithAmbiguousMemberTypes", "testConvertingToReferenceTypes", "testCloneWithTypeTableToAnydata", "testUnionNestedTypeConversionErrors", "testCloneWithTypeToUnionOfTypeReference", "testCloneWithTypeToTableNegative", - "testCloneWithTypeToRecordWithIntersectingUnionMembers" + "testCloneWithTypeToRecordWithIntersectingUnionMembers", "testCloneWithTypeToRecordWithSpecialChars" }; } diff --git a/langlib/langlib-test/src/test/resources/test-src/valuelib_test.bal b/langlib/langlib-test/src/test/resources/test-src/valuelib_test.bal index 7ca8e2352c1f..6de1e730c105 100644 --- a/langlib/langlib-test/src/test/resources/test-src/valuelib_test.bal +++ b/langlib/langlib-test/src/test/resources/test-src/valuelib_test.bal @@ -4645,6 +4645,40 @@ function testLast() { assertTrue([] == value:last(ar1, ar)); } + +public type Copybook record { + DFHCOMMAREA DFHCOMMAREA?; +}; + +public type DFHCOMMAREA record { + record { + string MI\-HDR\-VERSION?; + string MI\-HDR\-MSGID?; + string MI\-HDR\-LOGGINGID?; + record { + string MI\-HDR\-REPLYQMGR?; + }[2] MI\-HDR\-REPLYSTACK?; + } BROKER\-MESSAGE\-AREA?; +}; + +function testCloneWithTypeToRecordWithSpecialChars() { + string s = string `{ + "DFHCOMMAREA": { + "BROKER-MESSAGE-AREA": { + "MI-HDR-VERSION": "2", + "MI-HDR-MSGID":"3238763233323598798798712321187612", + "MI-HDR-LOGGINGID": "Z5118761-Z" + } + } + }`; + json rec = checkpanic value:fromJsonString(s); + map mapJson = checkpanic rec.ensureType(); + Copybook|error dfhcommarea = mapJson.cloneWithType(); + assertTrue(dfhcommarea is Copybook); + Copybook cb = checkpanic dfhcommarea.ensureType(); + assertEquality(cb.DFHCOMMAREA?.BROKER\-MESSAGE\-AREA.toString(), string `{"MI-HDR-VERSION":"2","MI-HDR-MSGID":"3238763233323598798798712321187612","MI-HDR-LOGGINGID":"Z5118761-Z"}`); +} + type AssertionError distinct error; const ASSERTION_ERROR_REASON = "AssertionError"; From d30752f76c90fa6ad75af0b9ab47391f304c3b51 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Thu, 16 Nov 2023 13:40:37 +0530 Subject: [PATCH 3/5] Remove unescape logic from JvmDesugarPhase --- .../java/org/wso2/ballerinalang/compiler/bir/BIRGen.java | 3 ++- .../ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java | 5 ----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index da3c31f9d93b..594705c1aa9a 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -18,6 +18,7 @@ package org.wso2.ballerinalang.compiler.bir; +import io.ballerina.identifier.Utils; import io.ballerina.tools.diagnostics.Location; import io.ballerina.tools.text.LinePosition; import io.ballerina.tools.text.LineRange; @@ -367,7 +368,7 @@ public void visit(BLangTypeDefinition astTypeDefinition) { if (referredType.tag == TypeTags.RECORD) { BRecordType recordType = (BRecordType) referredType; if (recordType.shouldPrintShape()) { - displayName = new Name(recordType.toString()); + displayName = new Name(Utils.unescapeBallerina(recordType.toString())); } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java index 4a41ce0ce741..58de262b3ae5 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java @@ -34,7 +34,6 @@ import org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableTypeSymbol; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BObjectTypeSymbol; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol; -import org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols; import org.wso2.ballerinalang.compiler.semantics.model.types.BField; import org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType; import org.wso2.ballerinalang.compiler.semantics.model.types.BObjectType; @@ -43,7 +42,6 @@ import org.wso2.ballerinalang.compiler.util.Name; import org.wso2.ballerinalang.compiler.util.Names; import org.wso2.ballerinalang.compiler.util.TypeTags; -import org.wso2.ballerinalang.util.Flags; import org.wso2.ballerinalang.util.Lists; import java.util.ArrayList; @@ -192,9 +190,6 @@ private static void encodePackageIdentifiers(PackageID packageID, HashMap typeDefs, HashMap encodedVsInitialIds) { for (BIRTypeDefinition typeDefinition : typeDefs) { - if (Symbols.isFlagOn(typeDefinition.type.flags, Flags.ANONYMOUS)) { - typeDefinition.name = Names.fromString(Utils.unescapeBallerina(typeDefinition.name.value)); - } typeDefinition.type.tsymbol.name = Names.fromString(encodeNonFunctionIdentifier( typeDefinition.type.tsymbol.name.value, encodedVsInitialIds)); typeDefinition.internalName = From 0974f3f1aa9d4b03e1585d91c77db1bd3a22546a Mon Sep 17 00:00:00 2001 From: hindujaB Date: Wed, 22 Nov 2023 13:32:21 +0530 Subject: [PATCH 4/5] Fix failing non-anon record value creation --- .../java/org/wso2/ballerinalang/compiler/bir/BIRGen.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 594705c1aa9a..41e0949ef222 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -364,11 +364,11 @@ public void visit(BLangTypeDefinition astTypeDefinition) { BType type = getDefinedType(astTypeDefinition); BType referredType = Types.getImpliedType(type); BSymbol symbol = astTypeDefinition.symbol; - Name displayName = symbol.name; + String displayName = symbol.name.value; if (referredType.tag == TypeTags.RECORD) { BRecordType recordType = (BRecordType) referredType; if (recordType.shouldPrintShape()) { - displayName = new Name(Utils.unescapeBallerina(recordType.toString())); + displayName = recordType.toString(); } } @@ -379,7 +379,7 @@ public void visit(BLangTypeDefinition astTypeDefinition) { type, new ArrayList<>(), symbol.origin.toBIROrigin(), - displayName, + new Name(Utils.unescapeBallerina(displayName)), symbol.originalName); if (symbol.tag == SymTag.TYPE_DEF) { BTypeReferenceType referenceType = ((BTypeDefinitionSymbol) symbol).referenceType; From 4c81b9e2bb92699f1c56914d1a3a69527a6bd7ad Mon Sep 17 00:00:00 2001 From: hindujaB Date: Wed, 22 Nov 2023 13:43:57 +0530 Subject: [PATCH 5/5] Improve test case --- .../src/test/resources/test-src/valuelib_test.bal | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/langlib/langlib-test/src/test/resources/test-src/valuelib_test.bal b/langlib/langlib-test/src/test/resources/test-src/valuelib_test.bal index 6de1e730c105..d004983917af 100644 --- a/langlib/langlib-test/src/test/resources/test-src/valuelib_test.bal +++ b/langlib/langlib-test/src/test/resources/test-src/valuelib_test.bal @@ -4645,12 +4645,11 @@ function testLast() { assertTrue([] == value:last(ar1, ar)); } - public type Copybook record { - DFHCOMMAREA DFHCOMMAREA?; + DFH\-COMMAREA DFH\-COMMAREA?; }; -public type DFHCOMMAREA record { +public type DFH\-COMMAREA record { record { string MI\-HDR\-VERSION?; string MI\-HDR\-MSGID?; @@ -4663,7 +4662,7 @@ public type DFHCOMMAREA record { function testCloneWithTypeToRecordWithSpecialChars() { string s = string `{ - "DFHCOMMAREA": { + "DFH-COMMAREA": { "BROKER-MESSAGE-AREA": { "MI-HDR-VERSION": "2", "MI-HDR-MSGID":"3238763233323598798798712321187612", @@ -4673,10 +4672,10 @@ function testCloneWithTypeToRecordWithSpecialChars() { }`; json rec = checkpanic value:fromJsonString(s); map mapJson = checkpanic rec.ensureType(); - Copybook|error dfhcommarea = mapJson.cloneWithType(); - assertTrue(dfhcommarea is Copybook); - Copybook cb = checkpanic dfhcommarea.ensureType(); - assertEquality(cb.DFHCOMMAREA?.BROKER\-MESSAGE\-AREA.toString(), string `{"MI-HDR-VERSION":"2","MI-HDR-MSGID":"3238763233323598798798712321187612","MI-HDR-LOGGINGID":"Z5118761-Z"}`); + Copybook|error dfh\-commarea = mapJson.cloneWithType(); + assertTrue(dfh\-commarea is Copybook); + Copybook cb = checkpanic dfh\-commarea.ensureType(); + assertEquality(cb.DFH\-COMMAREA?.BROKER\-MESSAGE\-AREA.toString(), string `{"MI-HDR-VERSION":"2","MI-HDR-MSGID":"3238763233323598798798712321187612","MI-HDR-LOGGINGID":"Z5118761-Z"}`); } type AssertionError distinct error;