Skip to content

Commit

Permalink
Address review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
LakshanWeerasinghe committed Aug 7, 2024
1 parent 84a51f1 commit 698181c
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
description: This BBE demonstrates how to convert a subtype of `anydata` value to a YAML format string.
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
2 changes: 1 addition & 1 deletion examples/anydata-to-yaml-string/anydata_to_yaml_string.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 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.

Expand Down
4 changes: 2 additions & 2 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@
},
{
"title": "Tables",
"column": 2,
"column": 1,
"category": "Language concepts",
"samples": [
{
Expand Down Expand Up @@ -4639,7 +4639,7 @@
"isLearnByExample": true
},
{
"name": "Anydata to YAML string",
"name": "Serialize a Ballerina value to a string in YAML format",
"url": "anydata-to-yaml-string",
"verifyBuild": true,
"verifyOutput": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# YAML to anydata conversion with projection

The `data.yaml` library provides multiple APIs to selectively convert elements and attributes from a YAML source,
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 attributes to records and limit the sizes of arrays.
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/).

::: code yaml_to_anydata_with_projection.bal :::
Expand Down
6 changes: 3 additions & 3 deletions examples/yaml-to-anydata/yaml_to_anydata.bal
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public function main() returns error? {
ServerConfig serverConfig2 = check yaml:parseBytes(yamlByteArr);
io:println(serverConfig2);

stream<byte[], error?> byteBlockStream = new (new StreamImplementor(yamlString));
stream<byte[], error?> 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.
// Defines a class called `ByteBlockGenerator`, which is stream implementer with `next()` method.
// This will be invoked when the `next()` method of the stream gets invoked.
class StreamImplementor {
class ByteBlockGenerator {
private int index = 0;
private final byte[] byteArr;
private final int arraySize;
Expand Down
3 changes: 1 addition & 2 deletions examples/yaml-to-anydata/yaml_to_anydata.md
Original file line number Diff line number Diff line change
@@ -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 APIs to parse YAML source, in the form of 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/).

Expand Down

0 comments on commit 698181c

Please sign in to comment.