From 8e98ad193457f88024c4edf51b137af6ecb9dc1c Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Fri, 11 Oct 2024 13:44:08 +0530 Subject: [PATCH 1/8] [Automated] Update the native jar versions --- ballerina/Dependencies.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index 68c32a0..6f3c777 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0" +distribution-version = "2201.10.1" [[package]] org = "ballerina" From a9b93ccc2c64eb52d4929faf86895db3f103f41a Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Fri, 11 Oct 2024 13:48:22 +0530 Subject: [PATCH 2/8] [Automated] Update the native jar versions --- ballerina/Dependencies.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index 6f3c777..68c32a0 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.1" +distribution-version = "2201.10.0" [[package]] org = "ballerina" From de25263515755ce0567f2ae98ec7df2043db9f55 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Fri, 11 Oct 2024 13:57:22 +0530 Subject: [PATCH 3/8] Fix the invalid error for readonly record types --- ballerina/tests/from_json_string_test.bal | 11 +++++++++++ ballerina/tests/from_json_test.bal | 11 +++++++++++ .../ballerina/lib/data/jsondata/json/JsonParser.java | 2 +- .../lib/data/jsondata/json/JsonTraverse.java | 2 +- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ballerina/tests/from_json_string_test.bal b/ballerina/tests/from_json_string_test.bal index bb4902e..2eda5f3 100644 --- a/ballerina/tests/from_json_string_test.bal +++ b/ballerina/tests/from_json_string_test.bal @@ -60,6 +60,17 @@ isolated function testSimpleJsonStringToRecord() returns Error? { test:assertEquals(recC.get("b"), 1); } +type ReadOnlyUser readonly & record {| + int id; +|}; + +@test:Config +isolated function testSimpleJsonStringToRecord2() returns Error? { + string user = string `{"id": 4012}`; + ReadOnlyUser r = check parseString(user); + test:assertEquals(r, {id: 4012}); +} + @test:Config isolated function testSimpleJsonStringToRecordWithProjection() returns Error? { string str = string `{"a": "hello", "b": 1}`; diff --git a/ballerina/tests/from_json_test.bal b/ballerina/tests/from_json_test.bal index c2fc4cc..353edfd 100644 --- a/ballerina/tests/from_json_test.bal +++ b/ballerina/tests/from_json_test.bal @@ -67,6 +67,17 @@ isolated function testSimpleJsonToRecord() returns Error? { test:assertEquals(recC.get("b"), 1); } +type ReadOnlyUserRecord readonly & record {| + int id; +|}; + +@test:Config +isolated function testSimpleJsonToRecord2() returns Error? { + json user = {id: 4012}; + ReadOnlyUserRecord r = check parseAsType(user); + test:assertEquals(r, {id: 4012}); +} + @test:Config isolated function testSimpleJsonToRecordWithProjection() returns Error? { json j = {"a": "hello", "b": 1}; diff --git a/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonParser.java b/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonParser.java index ad911a2..a3c4de7 100644 --- a/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonParser.java +++ b/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonParser.java @@ -249,7 +249,7 @@ public Object execute(Reader reader, BMap options, Type type) t } case TypeTags.INTERSECTION_TAG -> { Type effectiveType = ((IntersectionType) type).getEffectiveType(); - if (!SymbolFlags.isFlagOn(SymbolFlags.READONLY, effectiveType.getFlags())) { + if (!effectiveType.isReadOnly()) { throw DiagnosticLog.error(DiagnosticErrorCode.UNSUPPORTED_TYPE, type); } diff --git a/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonTraverse.java b/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonTraverse.java index fc4e704..3008574 100644 --- a/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonTraverse.java +++ b/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonTraverse.java @@ -158,7 +158,7 @@ private Object traverseJson(Object json, Type type) { } case TypeTags.INTERSECTION_TAG -> { Type effectiveType = ((IntersectionType) referredType).getEffectiveType(); - if (!SymbolFlags.isFlagOn(SymbolFlags.READONLY, effectiveType.getFlags())) { + if (!effectiveType.isReadOnly()) { throw DiagnosticLog.error(DiagnosticErrorCode.UNSUPPORTED_TYPE, type); } for (Type constituentType : ((IntersectionType) referredType).getConstituentTypes()) { From b540cf83b34edb155c6aa28cf73cc0994c54ad76 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Fri, 11 Oct 2024 17:03:58 +0530 Subject: [PATCH 4/8] Added tests for readonly record fields --- ballerina/tests/from_json_string_test.bal | 20 ++++++++++++++++++++ ballerina/tests/from_json_test.bal | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/ballerina/tests/from_json_string_test.bal b/ballerina/tests/from_json_string_test.bal index 2eda5f3..cfce966 100644 --- a/ballerina/tests/from_json_string_test.bal +++ b/ballerina/tests/from_json_string_test.bal @@ -71,6 +71,26 @@ isolated function testSimpleJsonStringToRecord2() returns Error? { test:assertEquals(r, {id: 4012}); } +public type UserId readonly & int; + +public type UserName readonly & record { + string firstname; + string lastname; +}; + +type ReadOnlyUser2 readonly & record {| + UserId id; + UserName name; + int age; +|}; + +@test:Config +isolated function testSimpleJsonStringToRecord3() returns Error? { + string user = string `{"id": 4012, "name": {"firstname": "John", "lastname": "Doe"}, "age": 27}`; + ReadOnlyUserRecord2 r = check parseString(user); + test:assertEquals(r, {id: 4012, age: 27, name: {firstname: "John", lastname: "Doe"}}); +} + @test:Config isolated function testSimpleJsonStringToRecordWithProjection() returns Error? { string str = string `{"a": "hello", "b": 1}`; diff --git a/ballerina/tests/from_json_test.bal b/ballerina/tests/from_json_test.bal index 353edfd..14325e7 100644 --- a/ballerina/tests/from_json_test.bal +++ b/ballerina/tests/from_json_test.bal @@ -78,6 +78,26 @@ isolated function testSimpleJsonToRecord2() returns Error? { test:assertEquals(r, {id: 4012}); } +public type ReadonlyUserId readonly & int; + +public type ReadonlyUserName readonly & record { + string firstname; + string lastname; +}; + +type ReadOnlyUserRecord2 readonly & record {| + ReadonlyUserId id; + ReadonlyUserName name; + int age; +|}; + +@test:Config +isolated function testSimpleJsonToRecord3() returns Error? { + json user = {id: 4012, name: {firstname: "John", lastname: "Doe"}, age: 27}; + ReadOnlyUserRecord2 r = check parseAsType(user); + test:assertEquals(r, {id: 4012, age: 27, name: {firstname: "John", lastname: "Doe"}}); +} + @test:Config isolated function testSimpleJsonToRecordWithProjection() returns Error? { json j = {"a": "hello", "b": 1}; From 900ac32a51b41eb083621b929ccbc27bb2f58988 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Fri, 11 Oct 2024 18:15:04 +0530 Subject: [PATCH 5/8] Add tests for immutable types --- ballerina/tests/from_json_test.bal | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ballerina/tests/from_json_test.bal b/ballerina/tests/from_json_test.bal index 14325e7..a47abf0 100644 --- a/ballerina/tests/from_json_test.bal +++ b/ballerina/tests/from_json_test.bal @@ -98,6 +98,15 @@ isolated function testSimpleJsonToRecord3() returns Error? { test:assertEquals(r, {id: 4012, age: 27, name: {firstname: "John", lastname: "Doe"}}); } +type UserRecord3 ReadOnlyUserRecord2; + +@test:Config +isolated function testSimpleJsonToRecord4() returns Error? { + json user = {id: 4012, name: {firstname: "John", lastname: "Doe"}, age: 27}; + UserRecord3 r = check parseAsType(user); + test:assertEquals(r, {id: 4012, age: 27, name: {firstname: "John", lastname: "Doe"}}); +} + @test:Config isolated function testSimpleJsonToRecordWithProjection() returns Error? { json j = {"a": "hello", "b": 1}; From 3dce5907625aef03a7de335de3349f691c0dabe9 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Fri, 11 Oct 2024 18:26:29 +0530 Subject: [PATCH 6/8] Add tests for readonly fields --- ballerina/tests/from_json_test.bal | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ballerina/tests/from_json_test.bal b/ballerina/tests/from_json_test.bal index a47abf0..7740690 100644 --- a/ballerina/tests/from_json_test.bal +++ b/ballerina/tests/from_json_test.bal @@ -107,6 +107,15 @@ isolated function testSimpleJsonToRecord4() returns Error? { test:assertEquals(r, {id: 4012, age: 27, name: {firstname: "John", lastname: "Doe"}}); } +@test:Config +isolated function testSimpleJsonToRecord5() returns Error? { + ReadonlyUserName username = {firstname: "John", lastname: "Doe"}; + record{string firstname; string lastname;} nameRec = username; + json user = {id: 4012, name: nameRec.toJson(), age: 27}; + ReadOnlyUserRecord2 r = check parseAsType(user); + test:assertEquals(r, {id: 4012, age: 27, name: {firstname: "John", lastname: "Doe"}}); +} + @test:Config isolated function testSimpleJsonToRecordWithProjection() returns Error? { json j = {"a": "hello", "b": 1}; From 9019906c3c301684b0ef37a14d32d67f281fc382 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Fri, 11 Oct 2024 18:41:05 +0530 Subject: [PATCH 7/8] Update the codeowner file --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 4c92918..404b558 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -4,4 +4,4 @@ # See: https://help.github.com/articles/about-codeowners/ # These owners will be the default owners for everything in the repo. -* @gimantha @MaryamZi @hasithaa +* @gimantha @MaryamZi @hasithaa @SasinduDilshara From 28c9eb6fa2db45db4a9876183ff90a5cc7f67618 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Mon, 21 Oct 2024 14:08:20 +0530 Subject: [PATCH 8/8] Add tests for selectively immutable types --- ballerina/tests/from_json_string_test.bal | 40 +++++++++++++---------- ballerina/tests/from_json_test.bal | 14 ++++++++ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/ballerina/tests/from_json_string_test.bal b/ballerina/tests/from_json_string_test.bal index cfce966..50c4f3d 100644 --- a/ballerina/tests/from_json_string_test.bal +++ b/ballerina/tests/from_json_string_test.bal @@ -60,37 +60,43 @@ isolated function testSimpleJsonStringToRecord() returns Error? { test:assertEquals(recC.get("b"), 1); } -type ReadOnlyUser readonly & record {| - int id; -|}; - @test:Config isolated function testSimpleJsonStringToRecord2() returns Error? { string user = string `{"id": 4012}`; - ReadOnlyUser r = check parseString(user); + ReadOnlyUserRecord r = check parseString(user); test:assertEquals(r, {id: 4012}); } -public type UserId readonly & int; - -public type UserName readonly & record { - string firstname; - string lastname; -}; +@test:Config +isolated function testSimpleJsonStringToRecord3() returns Error? { + string user = string `{"id": 4012, "name": {"firstname": "John", "lastname": "Doe"}, "age": 27}`; + ReadOnlyUserRecord2 r = check parseString(user); + test:assertEquals(r, {id: 4012, age: 27, name: {firstname: "John", lastname: "Doe"}}); +} -type ReadOnlyUser2 readonly & record {| - UserId id; - UserName name; - int age; -|}; +@test:Config +isolated function testSimpleJsonStringToRecord4() returns Error? { + string user = string `{"id": 4012, "name": {"firstname": "John", "lastname": "Doe"}, "age": 27}`; + UserRecord3 r = check parseString(user); + test:assertEquals(r, {id: 4012, age: 27, name: {firstname: "John", lastname: "Doe"}}); +} @test:Config -isolated function testSimpleJsonStringToRecord3() returns Error? { +isolated function testSimpleJsonStringToRecord5() returns Error? { string user = string `{"id": 4012, "name": {"firstname": "John", "lastname": "Doe"}, "age": 27}`; ReadOnlyUserRecord2 r = check parseString(user); test:assertEquals(r, {id: 4012, age: 27, name: {firstname: "John", lastname: "Doe"}}); } +@test:Config +isolated function testSimpleJsonStringToRecord6() returns Error? { + string user = string `{"ids": [4012, 4013], "names": [{"firstname": "John", "lastname": "Doe"}, + {"firstname": "Jane", "lastname": "Doe"}], "age": 27}`; + ReadOnlyUsersRecord3 r = check parseString(user); + test:assertEquals(r, {ids: [4012, 4013], names: [{firstname: "John", lastname: "Doe"}, + {firstname: "Jane", lastname: "Doe"}]}); +} + @test:Config isolated function testSimpleJsonStringToRecordWithProjection() returns Error? { string str = string `{"a": "hello", "b": 1}`; diff --git a/ballerina/tests/from_json_test.bal b/ballerina/tests/from_json_test.bal index 7740690..131e83a 100644 --- a/ballerina/tests/from_json_test.bal +++ b/ballerina/tests/from_json_test.bal @@ -116,6 +116,20 @@ isolated function testSimpleJsonToRecord5() returns Error? { test:assertEquals(r, {id: 4012, age: 27, name: {firstname: "John", lastname: "Doe"}}); } +type ReadOnlyUsersRecord3 readonly & record {| + ReadonlyUserId[] ids; + ReadonlyUserName[] names; +|}; + +@test:Config +isolated function testSimpleJsonToRecord6() returns Error? { + json user = {ids: [4012, 4013], names: [{firstname: "John", lastname: "Doe"}, + {firstname: "Jane", lastname: "Doe"}], age: 27}; + ReadOnlyUsersRecord3 r = check parseAsType(user); + test:assertEquals(r, {ids: [4012, 4013], names: [{firstname: "John", lastname: "Doe"}, + {firstname: "Jane", lastname: "Doe"}]}); +} + @test:Config isolated function testSimpleJsonToRecordWithProjection() returns Error? { json j = {"a": "hello", "b": 1};