From 0e7b03c3eb7cdbdae6cafd9b81bfe7f536ea06f7 Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Thu, 1 Aug 2024 17:48:03 +0530 Subject: [PATCH 01/11] Add yaml to anydata sample --- examples/yaml-to-anydata/yaml_to_anydata.bal | 51 +++++++++++++++++++ examples/yaml-to-anydata/yaml_to_anydata.md | 10 ++++ .../yaml-to-anydata/yaml_to_anydata.metatags | 2 + examples/yaml-to-anydata/yaml_to_anydata.out | 4 ++ 4 files changed, 67 insertions(+) create mode 100644 examples/yaml-to-anydata/yaml_to_anydata.bal create mode 100644 examples/yaml-to-anydata/yaml_to_anydata.md create mode 100644 examples/yaml-to-anydata/yaml_to_anydata.metatags create mode 100644 examples/yaml-to-anydata/yaml_to_anydata.out diff --git a/examples/yaml-to-anydata/yaml_to_anydata.bal b/examples/yaml-to-anydata/yaml_to_anydata.bal new file mode 100644 index 0000000000..dad5e44f6c --- /dev/null +++ b/examples/yaml-to-anydata/yaml_to_anydata.bal @@ -0,0 +1,51 @@ +import ballerina/data.yaml; +import ballerina/io; + +type ServerConfig record {| + string host; + int port; + string protocol; +|}; + +final string yamlString = "host: \"localhost\"\n" + + "port: 8080\n" + + "protocol: \"http\"\n" + + "database:\n"; + +public function main() returns error? { + // Convert the YAML string to a record type. + ServerConfig serverConfig1 = check yaml:parseString(yamlString); + io:println(serverConfig1); + + byte[] yamlByteArr = yamlString.toBytes(); + // Convert the YAML byte array to a record type. + ServerConfig serverConfig2 = check yaml:parseBytes(yamlByteArr); + io:println(serverConfig2); + + stream byteBlockStream = new (new ByteBlockGenerator(yamlString)); + // Convert the YAML byte block stream to a record type. + ServerConfig serverConfig3 = check yaml:parseStream(byteBlockStream); + io:println(serverConfig3); +} + +// Defines a class called `ByteBlockGenerator`, which implements the `next()` method. +// This will be invoked when the `next()` method of the stream gets invoked. +class ByteBlockGenerator { + private int index = 0; + private final byte[] byteArr; + private final int arraySize; + + public function init(string data) { + self.byteArr = data.toBytes(); + self.arraySize = self.byteArr.length(); + } + + public isolated function next() returns record {|byte[] value;|}|error? { + if self.index >= self.arraySize { + return; + } + int startIndex = self.index; + self.index = startIndex + 4 > self.arraySize ? self.arraySize : startIndex + 3; + return {value: self.byteArr.slice(startIndex, self.index)}; + } +} diff --git a/examples/yaml-to-anydata/yaml_to_anydata.md b/examples/yaml-to-anydata/yaml_to_anydata.md new file mode 100644 index 0000000000..e3bdcf8ca1 --- /dev/null +++ b/examples/yaml-to-anydata/yaml_to_anydata.md @@ -0,0 +1,10 @@ +# YAML to anydata conversion + +The `data.yaml` library provides multiple APIs to perform the conversion from YAML source, which can be provided as a `string`, `byte array`, or `byte block stream`, +into a subtype of Ballerina `anydata`. + +For more information on the underlying module, see the [`data.yaml` module](https://lib.ballerina.io/ballerina/data.yaml/latest/). + +::: code yaml_to_anydata.bal ::: + +::: out yaml_to_anydata.out ::: diff --git a/examples/yaml-to-anydata/yaml_to_anydata.metatags b/examples/yaml-to-anydata/yaml_to_anydata.metatags new file mode 100644 index 0000000000..61ad4615b1 --- /dev/null +++ b/examples/yaml-to-anydata/yaml_to_anydata.metatags @@ -0,0 +1,2 @@ +description: This BBE demonstrates how to convert a YAML source, which can be provided as a string, byte array, or byte block stream, into a subtype of Ballerina anydata. +keywords: ballerina, ballerina by example, BBE, yaml, record, stream, byte array diff --git a/examples/yaml-to-anydata/yaml_to_anydata.out b/examples/yaml-to-anydata/yaml_to_anydata.out new file mode 100644 index 0000000000..b99b6c4306 --- /dev/null +++ b/examples/yaml-to-anydata/yaml_to_anydata.out @@ -0,0 +1,4 @@ +$ bal run yaml_to_anydata.bal +{"host":"localhost","port":8080,"protocol":"http"} +{"host":"localhost","port":8080,"protocol":"http"} +{"host":"localhost","port":8080,"protocol":"http"} From cb299652f57585a21ced9dde3c13b45ff8d77ff7 Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Thu, 1 Aug 2024 17:58:08 +0530 Subject: [PATCH 02/11] Add yaml to anydata with projection bbe --- .../yaml_to_anydata_with_projection.bal | 31 +++++++++++++++++++ .../yaml_to_anydata_with_projection.md | 11 +++++++ .../yaml_to_anydata_with_projection.metatags | 2 ++ .../yaml_to_anydata_with_projection.out | 2 ++ 4 files changed, 46 insertions(+) create mode 100644 examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal create mode 100644 examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.md create mode 100644 examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.metatags create mode 100644 examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.out diff --git a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal new file mode 100644 index 0000000000..ffc7f71675 --- /dev/null +++ b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal @@ -0,0 +1,31 @@ +import ballerina/data.yaml; +import ballerina/io; + +type ServerConfig record {| + string host; + int port; + int[2] remotePorts; + DatabaseConfig database; +|}; + +type DatabaseConfig record {| + string dbName; + string username; +|}; + + +final string yamlString = + "host: \"localhost\"\n" + + "port: 8080\n" + + "remotePorts: [9000, 9001, 9002, 9003]\n" + + "protocol: \"http\"\n" + + "database:\n" + + " dbName: \"testdb\"\n" + + " username: \"dbuser\"\n" + + " password: \"dbpassword\"\n"; + +public function main() returns error? { + // Based on the expected type, it selectively converts the YAML string to the record type. + ServerConfig serverConfig = check yaml:parseString(yamlString); + io:println(serverConfig); +} diff --git a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.md b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.md new file mode 100644 index 0000000000..e6896b9920 --- /dev/null +++ b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.md @@ -0,0 +1,11 @@ +# YAML to anydata conversion with projection + +The `data.yaml` library provides multiple APIs to selectively convert elements and attributes from a YAML source, +which can be provided as a `string`, `byte array`, or `byte block stream`, into a Ballerina record. +In projection users can drop attributes and elements in arrays when creating the Ballerina value. + +For more information on the underlying module, see the [`data.yaml` module](https://lib.ballerina.io/ballerina/data.yaml/latest/). + +::: code yaml_to_anydata_with_projection.bal ::: + +::: out yaml_to_anydata_with_projection.out ::: diff --git a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.metatags b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.metatags new file mode 100644 index 0000000000..23dab52f21 --- /dev/null +++ b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.metatags @@ -0,0 +1,2 @@ +description: This BBE demonstrates how to selectively convert fields from a YAML source, which can be provided as a string, byte array, or byte block stream, into a Ballerina record with projection. +keywords: ballerina, ballerina by example, BBE, yaml, record, data projection diff --git a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.out b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.out new file mode 100644 index 0000000000..35b2ccf457 --- /dev/null +++ b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.out @@ -0,0 +1,2 @@ +$ bal run yaml_to_anydata_with_projection.bal +{"host":"localhost","port":8080,"remotePorts":[9000,9001],"database":{"dbName":"testdb","username":"dbuser"}} From 2170bc9b153f5b2937e0902091e0a03984eb3aa0 Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Thu, 1 Aug 2024 18:05:15 +0530 Subject: [PATCH 03/11] Add anydata to yaml string bbe --- .../any_data_to_yaml_string.metatags | 2 ++ .../anydata_to_yaml_string.bal | 26 +++++++++++++++++++ .../anydata_to_yaml_string.md | 9 +++++++ .../anydata_to_yaml_string.out | 6 +++++ 4 files changed, 43 insertions(+) create mode 100644 examples/anydata-to-yaml-string/any_data_to_yaml_string.metatags create mode 100644 examples/anydata-to-yaml-string/anydata_to_yaml_string.bal create mode 100644 examples/anydata-to-yaml-string/anydata_to_yaml_string.md create mode 100644 examples/anydata-to-yaml-string/anydata_to_yaml_string.out diff --git a/examples/anydata-to-yaml-string/any_data_to_yaml_string.metatags b/examples/anydata-to-yaml-string/any_data_to_yaml_string.metatags new file mode 100644 index 0000000000..12ecb60726 --- /dev/null +++ b/examples/anydata-to-yaml-string/any_data_to_yaml_string.metatags @@ -0,0 +1,2 @@ +description: This BBE demonstrates how to convert a subtype of `anydata` value to a YAML format string. +keywords: ballerina, ballerina by example, BBE, yaml, yaml to string, record to yaml diff --git a/examples/anydata-to-yaml-string/anydata_to_yaml_string.bal b/examples/anydata-to-yaml-string/anydata_to_yaml_string.bal new file mode 100644 index 0000000000..2f67c1bdce --- /dev/null +++ b/examples/anydata-to-yaml-string/anydata_to_yaml_string.bal @@ -0,0 +1,26 @@ +import ballerina/data.yaml; +import ballerina/io; + +type ServerConfig record {| + string host; + int port; + DatabaseConfig database; +|}; + +type DatabaseConfig record {| + string dbName; + string username; +|}; + +public function main() returns error? { + + ServerConfig serverConfig = { + database: {dbName: "userDB", username: "testUser"}, + port: 3000, + host: "localhost" + }; + + // Serialize a ballerina value to a YAML value. + string serverConfigYamlStr = check yaml:toYamlString(serverConfig); + io:println(serverConfigYamlStr); +} diff --git a/examples/anydata-to-yaml-string/anydata_to_yaml_string.md b/examples/anydata-to-yaml-string/anydata_to_yaml_string.md new file mode 100644 index 0000000000..6414e03340 --- /dev/null +++ b/examples/anydata-to-yaml-string/anydata_to_yaml_string.md @@ -0,0 +1,9 @@ +# Anydata to YAML string conversion + +The `data.yaml` library provides `toYamlString` API to convert a subtype of `anydata` value to a YAML format string. + +For more information on the underlying module, see the [`data.yaml` module](https://lib.ballerina.io/ballerina/data.yaml/latest/). + +::: code anydata_to_yaml_string.bal ::: + +::: out anydata_to_yaml_string.out ::: diff --git a/examples/anydata-to-yaml-string/anydata_to_yaml_string.out b/examples/anydata-to-yaml-string/anydata_to_yaml_string.out new file mode 100644 index 0000000000..8acdad1d86 --- /dev/null +++ b/examples/anydata-to-yaml-string/anydata_to_yaml_string.out @@ -0,0 +1,6 @@ +$ bal run anydata_to_yaml_string.bal +host: localhost +port: 3000 +database: + dbName: userDB + username: testUser From 6c4b86ce4d1713fdf030eee4b7cb851072520e33 Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Thu, 1 Aug 2024 18:08:13 +0530 Subject: [PATCH 04/11] Add yaml bbes to index.json --- examples/index.json | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/examples/index.json b/examples/index.json index 4e03aa6245..572a1a5b1e 100644 --- a/examples/index.json +++ b/examples/index.json @@ -4619,6 +4619,34 @@ } ] }, + { + "title": "YAML data", + "column": 2, + "category": "Common libraries", + "samples": [ + { + "name": "YAML to anydata", + "url": "yaml-to-anydata", + "verifyBuild": true, + "verifyOutput": true, + "isLearnByExample": true + }, + { + "name": "YAML to anydata with projection", + "url": "yaml-to-anydata-with-projection", + "verifyBuild": true, + "verifyOutput": true, + "isLearnByExample": true + }, + { + "name": "Anydata to YAML string", + "url": "anydata-to-yaml-string", + "verifyBuild": true, + "verifyOutput": true, + "isLearnByExample": true + } + ] + }, { "title": "Constraint", "column": 3, From 1eb9b57d3c38e2375b3c8271b891ab6c5aeaf45b Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Tue, 6 Aug 2024 10:12:39 +0530 Subject: [PATCH 05/11] Address review suggestions --- .../anydata_to_yaml_string.bal | 7 +++++-- .../anydata-to-yaml-string/anydata_to_yaml_string.md | 2 +- examples/index.json | 2 +- .../yaml_to_anydata_with_projection.bal | 6 +++--- .../yaml_to_anydata_with_projection.md | 7 +++++-- examples/yaml-to-anydata/yaml_to_anydata.bal | 12 ++++++------ 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/examples/anydata-to-yaml-string/anydata_to_yaml_string.bal b/examples/anydata-to-yaml-string/anydata_to_yaml_string.bal index 2f67c1bdce..851e3ab46e 100644 --- a/examples/anydata-to-yaml-string/anydata_to_yaml_string.bal +++ b/examples/anydata-to-yaml-string/anydata_to_yaml_string.bal @@ -15,12 +15,15 @@ type DatabaseConfig record {| public function main() returns error? { ServerConfig serverConfig = { - database: {dbName: "userDB", username: "testUser"}, + database: { + dbName: "userDB", + username: "testUser" + }, port: 3000, host: "localhost" }; - // Serialize a ballerina value to a YAML value. + // Serialize a Ballerina value to a string in YAML format. string serverConfigYamlStr = check yaml:toYamlString(serverConfig); io:println(serverConfigYamlStr); } diff --git a/examples/anydata-to-yaml-string/anydata_to_yaml_string.md b/examples/anydata-to-yaml-string/anydata_to_yaml_string.md index 6414e03340..aad4a18bf0 100644 --- a/examples/anydata-to-yaml-string/anydata_to_yaml_string.md +++ b/examples/anydata-to-yaml-string/anydata_to_yaml_string.md @@ -1,6 +1,6 @@ # Anydata to YAML string conversion -The `data.yaml` library provides `toYamlString` API to convert a subtype of `anydata` value to a YAML format string. +The `data.yaml` library provides the `toYamlString` function to convert a value belonging to `anydata` to a string in YAML format. For more information on the underlying module, see the [`data.yaml` module](https://lib.ballerina.io/ballerina/data.yaml/latest/). diff --git a/examples/index.json b/examples/index.json index 572a1a5b1e..ab218e9269 100644 --- a/examples/index.json +++ b/examples/index.json @@ -4621,7 +4621,7 @@ }, { "title": "YAML data", - "column": 2, + "column": 3, "category": "Common libraries", "samples": [ { diff --git a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal index ffc7f71675..2a4c8da0ef 100644 --- a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal +++ b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal @@ -13,8 +13,9 @@ type DatabaseConfig record {| string username; |}; - -final string yamlString = +public function main() returns error? { + // Can read from a file as well. + string yamlString = "host: \"localhost\"\n" + "port: 8080\n" + "remotePorts: [9000, 9001, 9002, 9003]\n" + @@ -24,7 +25,6 @@ final string yamlString = " username: \"dbuser\"\n" + " password: \"dbpassword\"\n"; -public function main() returns error? { // Based on the expected type, it selectively converts the YAML string to the record type. ServerConfig serverConfig = check yaml:parseString(yamlString); io:println(serverConfig); diff --git a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.md b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.md index e6896b9920..45ab346841 100644 --- a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.md +++ b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.md @@ -1,8 +1,11 @@ # YAML to anydata conversion with projection The `data.yaml` library provides multiple APIs to selectively convert elements and attributes from a YAML source, -which can be provided as a `string`, `byte array`, or `byte block stream`, into a Ballerina record. -In projection users can drop attributes and elements in arrays when creating the Ballerina value. +in the form of a string, byte array, or byte block stream, into a Ballerina record. +Using projection, users can selectively add attributes to records and limit the sizes of arrays. +In this example, the `password` attribute is excluded when creating the `DatabaseConfig` record, +and only the first two elements from the source are used to create the `remotePorts` array. + For more information on the underlying module, see the [`data.yaml` module](https://lib.ballerina.io/ballerina/data.yaml/latest/). diff --git a/examples/yaml-to-anydata/yaml_to_anydata.bal b/examples/yaml-to-anydata/yaml_to_anydata.bal index dad5e44f6c..a2a7d9d0a8 100644 --- a/examples/yaml-to-anydata/yaml_to_anydata.bal +++ b/examples/yaml-to-anydata/yaml_to_anydata.bal @@ -13,24 +13,24 @@ final string yamlString = "host: \"localhost\"\n" + "database:\n"; public function main() returns error? { - // Convert the YAML string to a record type. + // Parse the YAML string to a record type. ServerConfig serverConfig1 = check yaml:parseString(yamlString); io:println(serverConfig1); byte[] yamlByteArr = yamlString.toBytes(); - // Convert the YAML byte array to a record type. + // Parse the YAML byte array to a record type. ServerConfig serverConfig2 = check yaml:parseBytes(yamlByteArr); io:println(serverConfig2); - stream byteBlockStream = new (new ByteBlockGenerator(yamlString)); - // Convert the YAML byte block stream to a record type. + stream byteBlockStream = new (new StreamImplementor(yamlString)); + // Parse the YAML byte block stream to a record type. ServerConfig serverConfig3 = check yaml:parseStream(byteBlockStream); io:println(serverConfig3); } -// Defines a class called `ByteBlockGenerator`, which implements the `next()` method. +// Defines a class called `StreamImplementor`, which implements the `next()` method. // This will be invoked when the `next()` method of the stream gets invoked. -class ByteBlockGenerator { +class StreamImplementor { private int index = 0; private final byte[] byteArr; private final int arraySize; From 84a51f1f4e61968eaef3db2cc1d7d08ed0a149c6 Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Tue, 6 Aug 2024 10:51:11 +0530 Subject: [PATCH 06/11] Update samples with string templates --- .../yaml_to_anydata_with_projection.bal | 19 ++++++++++--------- examples/yaml-to-anydata/yaml_to_anydata.bal | 10 ++++++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal index 2a4c8da0ef..f522b16787 100644 --- a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal +++ b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal @@ -15,15 +15,16 @@ type DatabaseConfig record {| public function main() returns error? { // Can read from a file as well. - string yamlString = - "host: \"localhost\"\n" + - "port: 8080\n" + - "remotePorts: [9000, 9001, 9002, 9003]\n" + - "protocol: \"http\"\n" + - "database:\n" + - " dbName: \"testdb\"\n" + - " username: \"dbuser\"\n" + - " password: \"dbpassword\"\n"; + string yamlString = string + ` + host: "localhost" + port: 8080 + remotePorts: [9000, 9001, 9002, 9003] + protocol: "http" + database: + dbName: "testdb" + username: "dbuser" + password: "dbpassword"`; // Based on the expected type, it selectively converts the YAML string to the record type. ServerConfig serverConfig = check yaml:parseString(yamlString); diff --git a/examples/yaml-to-anydata/yaml_to_anydata.bal b/examples/yaml-to-anydata/yaml_to_anydata.bal index a2a7d9d0a8..8b11774ac6 100644 --- a/examples/yaml-to-anydata/yaml_to_anydata.bal +++ b/examples/yaml-to-anydata/yaml_to_anydata.bal @@ -7,10 +7,12 @@ type ServerConfig record {| string protocol; |}; -final string yamlString = "host: \"localhost\"\n" + - "port: 8080\n" + - "protocol: \"http\"\n" + - "database:\n"; + +final string yamlString = string + ` + host: "localhost" + port: 8080 + protocol: "http"`; public function main() returns error? { // Parse the YAML string to a record type. From 0513c569c859a77bb1ae4060aabd744da4c94b2a Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Thu, 15 Aug 2024 10:39:21 +0530 Subject: [PATCH 07/11] Address review suggestions --- .../any_data_to_yaml_string.metatags | 2 -- .../anydata-to-yaml-string/anydata_to_yaml_string.md | 4 ++-- .../anydata_to_yaml_string.metatags | 2 ++ examples/index.json | 4 ++-- .../yaml_to_anydata_with_projection.bal | 7 ++++--- .../yaml_to_anydata_with_projection.md | 7 ++----- examples/yaml-to-anydata/yaml_to_anydata.bal | 9 ++++----- examples/yaml-to-anydata/yaml_to_anydata.md | 3 +-- 8 files changed, 17 insertions(+), 21 deletions(-) delete mode 100644 examples/anydata-to-yaml-string/any_data_to_yaml_string.metatags create mode 100644 examples/anydata-to-yaml-string/anydata_to_yaml_string.metatags diff --git a/examples/anydata-to-yaml-string/any_data_to_yaml_string.metatags b/examples/anydata-to-yaml-string/any_data_to_yaml_string.metatags deleted file mode 100644 index 12ecb60726..0000000000 --- a/examples/anydata-to-yaml-string/any_data_to_yaml_string.metatags +++ /dev/null @@ -1,2 +0,0 @@ -description: This BBE demonstrates how to convert a subtype of `anydata` value to a YAML format string. -keywords: ballerina, ballerina by example, BBE, yaml, yaml to string, record to yaml diff --git a/examples/anydata-to-yaml-string/anydata_to_yaml_string.md b/examples/anydata-to-yaml-string/anydata_to_yaml_string.md index aad4a18bf0..34c0dbfc9d 100644 --- a/examples/anydata-to-yaml-string/anydata_to_yaml_string.md +++ b/examples/anydata-to-yaml-string/anydata_to_yaml_string.md @@ -1,6 +1,6 @@ -# Anydata to YAML string conversion +# Serialize a Ballerina value to a string in YAML format -The `data.yaml` library provides the `toYamlString` function to convert a value belonging to `anydata` to a string in YAML format. +The `data.yaml` library provides the `toYamlString` function to serialize a value belonging to `anydata` to a string in YAML format. For more information on the underlying module, see the [`data.yaml` module](https://lib.ballerina.io/ballerina/data.yaml/latest/). diff --git a/examples/anydata-to-yaml-string/anydata_to_yaml_string.metatags b/examples/anydata-to-yaml-string/anydata_to_yaml_string.metatags new file mode 100644 index 0000000000..c53a95c3f6 --- /dev/null +++ b/examples/anydata-to-yaml-string/anydata_to_yaml_string.metatags @@ -0,0 +1,2 @@ +description: This BBE demonstrates how to serialize a value belonging to `anydata` to a string in YAML format. +keywords: ballerina, ballerina by example, BBE, yaml, yaml to string, record to yaml diff --git a/examples/index.json b/examples/index.json index ab218e9269..173df2298c 100644 --- a/examples/index.json +++ b/examples/index.json @@ -790,7 +790,7 @@ }, { "title": "Tables", - "column": 2, + "column": 1, "category": "Language concepts", "samples": [ { @@ -4639,7 +4639,7 @@ "isLearnByExample": true }, { - "name": "Anydata to YAML string", + "name": "Serialize to YAML string", "url": "anydata-to-yaml-string", "verifyBuild": true, "verifyOutput": true, diff --git a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal index f522b16787..a6749fc39a 100644 --- a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal +++ b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal @@ -14,9 +14,8 @@ type DatabaseConfig record {| |}; public function main() returns error? { - // Can read from a file as well. - string yamlString = string - ` + // Similar to content read from a YAML file. + string yamlString = string ` host: "localhost" port: 8080 remotePorts: [9000, 9001, 9002, 9003] @@ -28,5 +27,7 @@ public function main() returns error? { // Based on the expected type, it selectively converts the YAML string to the record type. ServerConfig serverConfig = check yaml:parseString(yamlString); + // The `password` field is excluded in the created record value. + // Only the first two elements from the source are used to create the `remotePorts` array. io:println(serverConfig); } diff --git a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.md b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.md index 45ab346841..4c27660676 100644 --- a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.md +++ b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.md @@ -1,11 +1,8 @@ # YAML to anydata conversion with projection -The `data.yaml` library provides multiple APIs to selectively convert elements and attributes from a YAML source, -in the form of a string, byte array, or byte block stream, into a Ballerina record. -Using projection, users can selectively add attributes to records and limit the sizes of arrays. -In this example, the `password` attribute is excluded when creating the `DatabaseConfig` record, -and only the first two elements from the source are used to create the `remotePorts` array. +The `data.yaml` library provides multiple APIs to selectively parse elements and attributes from a YAML source, in the form of a string, byte array, or byte block stream, into a Ballerina record. Using projection, users can selectively add fields to records and limit the sizes of arrays. +In this example, the `password` attribute is excluded when creating the `DatabaseConfig` record, and only the first two elements from the source are used to create the `remotePorts` array. For more information on the underlying module, see the [`data.yaml` module](https://lib.ballerina.io/ballerina/data.yaml/latest/). diff --git a/examples/yaml-to-anydata/yaml_to_anydata.bal b/examples/yaml-to-anydata/yaml_to_anydata.bal index 8b11774ac6..75185e555c 100644 --- a/examples/yaml-to-anydata/yaml_to_anydata.bal +++ b/examples/yaml-to-anydata/yaml_to_anydata.bal @@ -7,7 +7,6 @@ type ServerConfig record {| string protocol; |}; - final string yamlString = string ` host: "localhost" @@ -24,15 +23,15 @@ public function main() returns error? { ServerConfig serverConfig2 = check yaml:parseBytes(yamlByteArr); io:println(serverConfig2); - stream byteBlockStream = new (new StreamImplementor(yamlString)); + stream byteBlockStream = new (new ByteBlockGenerator(yamlString)); // Parse the YAML byte block stream to a record type. ServerConfig serverConfig3 = check yaml:parseStream(byteBlockStream); io:println(serverConfig3); } -// Defines a class called `StreamImplementor`, which implements the `next()` method. -// This will be invoked when the `next()` method of the stream gets invoked. -class StreamImplementor { +// Defines a class called `ByteBlockGenerator`, which is stream implementor with a `next()` method. +// This `next()` method is called when iterating over a stream created with a `ByteBlockGenerator` value. +class ByteBlockGenerator { private int index = 0; private final byte[] byteArr; private final int arraySize; diff --git a/examples/yaml-to-anydata/yaml_to_anydata.md b/examples/yaml-to-anydata/yaml_to_anydata.md index e3bdcf8ca1..174dd70586 100644 --- a/examples/yaml-to-anydata/yaml_to_anydata.md +++ b/examples/yaml-to-anydata/yaml_to_anydata.md @@ -1,7 +1,6 @@ # YAML to anydata conversion -The `data.yaml` library provides multiple APIs to perform the conversion from YAML source, which can be provided as a `string`, `byte array`, or `byte block stream`, -into a subtype of Ballerina `anydata`. +The `data.yaml` library provides multiple functions to parse YAML source, in the form of a string, byte array, or byte block stream, as a value that belongs to a subtype of `anydata`. For more information on the underlying module, see the [`data.yaml` module](https://lib.ballerina.io/ballerina/data.yaml/latest/). From 9e5e907da1606c2cde16845a62dba41e24fbe22c Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Tue, 20 Aug 2024 14:57:05 +0530 Subject: [PATCH 08/11] Address review suggestions --- examples/index.json | 34 +++++++++---------- .../yaml_to_anydata_with_projection.bal | 2 +- examples/yaml-to-anydata/yaml_to_anydata.bal | 6 ++-- .../yaml-to-anydata/yaml_to_anydata.metatags | 2 +- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/examples/index.json b/examples/index.json index 173df2298c..3b3efb8bbc 100644 --- a/examples/index.json +++ b/examples/index.json @@ -790,7 +790,7 @@ }, { "title": "Tables", - "column": 1, + "column": 2, "category": "Language concepts", "samples": [ { @@ -4442,6 +4442,21 @@ } ] }, + { + "title": "OS", + "column": 1, + "category": "Common libraries", + "samples": [ + { + "name": "Environment variables", + "url": "environment-variables", + "verifyBuild": true, + "verifyOutput": false, + "disableVerificationReason": "Includes varying output", + "isLearnByExample": false + } + ] + }, { "title": "File", "column": 2, @@ -4535,7 +4550,7 @@ }, { "title": "UUID", - "column": 3, + "column": 2, "category": "Common libraries", "samples": [ { @@ -4569,21 +4584,6 @@ } ] }, - { - "title": "OS", - "column": 3, - "category": "Common libraries", - "samples": [ - { - "name": "Environment variables", - "url": "environment-variables", - "verifyBuild": true, - "verifyOutput": false, - "disableVerificationReason": "Includes varying output", - "isLearnByExample": false - } - ] - }, { "title": "XML data", "column": 3, diff --git a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal index a6749fc39a..7211dd38a8 100644 --- a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal +++ b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal @@ -25,7 +25,7 @@ public function main() returns error? { username: "dbuser" password: "dbpassword"`; - // Based on the expected type, it selectively converts the YAML string to the record type. + // Based on the expected type, this function selectively constructs the record type from the YAML string. ServerConfig serverConfig = check yaml:parseString(yamlString); // The `password` field is excluded in the created record value. // Only the first two elements from the source are used to create the `remotePorts` array. diff --git a/examples/yaml-to-anydata/yaml_to_anydata.bal b/examples/yaml-to-anydata/yaml_to_anydata.bal index 75185e555c..473726ef2c 100644 --- a/examples/yaml-to-anydata/yaml_to_anydata.bal +++ b/examples/yaml-to-anydata/yaml_to_anydata.bal @@ -14,17 +14,17 @@ final string yamlString = string protocol: "http"`; public function main() returns error? { - // Parse the YAML string to a record type. + // Parse the YAML string as a record. ServerConfig serverConfig1 = check yaml:parseString(yamlString); io:println(serverConfig1); byte[] yamlByteArr = yamlString.toBytes(); - // Parse the YAML byte array to a record type. + // Parse the YAML byte array as a record. ServerConfig serverConfig2 = check yaml:parseBytes(yamlByteArr); io:println(serverConfig2); stream byteBlockStream = new (new ByteBlockGenerator(yamlString)); - // Parse the YAML byte block stream to a record type. + // Parse the YAML byte block stream as a record. ServerConfig serverConfig3 = check yaml:parseStream(byteBlockStream); io:println(serverConfig3); } diff --git a/examples/yaml-to-anydata/yaml_to_anydata.metatags b/examples/yaml-to-anydata/yaml_to_anydata.metatags index 61ad4615b1..a6bad6d6a9 100644 --- a/examples/yaml-to-anydata/yaml_to_anydata.metatags +++ b/examples/yaml-to-anydata/yaml_to_anydata.metatags @@ -1,2 +1,2 @@ -description: This BBE demonstrates how to convert a YAML source, which can be provided as a string, byte array, or byte block stream, into a subtype of Ballerina anydata. +description: This BBE demonstrates how to parse a YAML source, which can be provided as a string, byte array, or byte block stream, as a value that belongs to a subtype of Ballerina anydata. keywords: ballerina, ballerina by example, BBE, yaml, record, stream, byte array From af93017841c3b733a381728fccdc4f0dff2f598c Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Tue, 20 Aug 2024 17:35:22 +0530 Subject: [PATCH 09/11] Disable output verification for xml-from-record-conversion --- examples/index.json | 2 +- .../xml-from-record-conversion/xml_from_record_conversion.bal | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/index.json b/examples/index.json index 3b3efb8bbc..9aa3f01cf1 100644 --- a/examples/index.json +++ b/examples/index.json @@ -4614,7 +4614,7 @@ "name": "Record to XML conversion", "url": "xml-from-record-conversion", "verifyBuild": true, - "verifyOutput": true, + "verifyOutput": false, "isLearnByExample": false } ] diff --git a/examples/xml-from-record-conversion/xml_from_record_conversion.bal b/examples/xml-from-record-conversion/xml_from_record_conversion.bal index 31d051c0ca..58e515e3e7 100644 --- a/examples/xml-from-record-conversion/xml_from_record_conversion.bal +++ b/examples/xml-from-record-conversion/xml_from_record_conversion.bal @@ -18,7 +18,6 @@ type Invoice record { @xmldata:Namespace { uri: "http://example1.com" } - type Item record { string itemCode; int count; From dc144476e17cfc18c969ec4e523f972edaff5d1c Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Wed, 21 Aug 2024 07:58:01 +0530 Subject: [PATCH 10/11] Address review suggestions --- .../yaml_to_anydata_with_projection.bal | 2 +- examples/yaml-to-anydata/yaml_to_anydata.metatags | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal index 7211dd38a8..937daf1b74 100644 --- a/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal +++ b/examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal @@ -25,7 +25,7 @@ public function main() returns error? { username: "dbuser" password: "dbpassword"`; - // Based on the expected type, this function selectively constructs the record type from the YAML string. + // Based on the expected type, this function selectively constructs the record from the YAML string. ServerConfig serverConfig = check yaml:parseString(yamlString); // The `password` field is excluded in the created record value. // Only the first two elements from the source are used to create the `remotePorts` array. diff --git a/examples/yaml-to-anydata/yaml_to_anydata.metatags b/examples/yaml-to-anydata/yaml_to_anydata.metatags index a6bad6d6a9..44f29c329c 100644 --- a/examples/yaml-to-anydata/yaml_to_anydata.metatags +++ b/examples/yaml-to-anydata/yaml_to_anydata.metatags @@ -1,2 +1,2 @@ -description: This BBE demonstrates how to parse a YAML source, which can be provided as a string, byte array, or byte block stream, as a value that belongs to a subtype of Ballerina anydata. +description: This BBE demonstrates how to parse a YAML source, which can be provided as a string, byte array, or byte block stream, as a value that belongs to a subtype of anydata. keywords: ballerina, ballerina by example, BBE, yaml, record, stream, byte array From d5ef26e54783db302a1e758ed1e2cb8ca0a6eb05 Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Wed, 21 Aug 2024 09:32:35 +0530 Subject: [PATCH 11/11] Update the xmldata version --- examples/index.json | 2 +- gradle.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/index.json b/examples/index.json index 9aa3f01cf1..3b3efb8bbc 100644 --- a/examples/index.json +++ b/examples/index.json @@ -4614,7 +4614,7 @@ "name": "Record to XML conversion", "url": "xml-from-record-conversion", "verifyBuild": true, - "verifyOutput": false, + "verifyOutput": true, "isLearnByExample": false } ] diff --git a/gradle.properties b/gradle.properties index dc2c3e5649..4b9f20f096 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,7 +17,7 @@ stdlibIoVersion=1.6.0 stdlibJavaArraysVersion=1.4.0 stdlibTimeVersion=2.4.0 stdlibUrlVersion=2.4.0 -stdlibXmldataVersion=2.7.0 +stdlibXmldataVersion=2.8.0 observeVersion=1.2.3 stdlibMathVectorVersion=1.0.2