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 15, 2024
1 parent 84a51f1 commit 0513c56
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 21 deletions.

This file was deleted.

4 changes: 2 additions & 2 deletions examples/anydata-to-yaml-string/anydata_to_yaml_string.md
Original file line number Diff line number Diff line change
@@ -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/).

Expand Down
Original file line number Diff line number Diff line change
@@ -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
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 to YAML string",
"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 All @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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/).

Expand Down
9 changes: 4 additions & 5 deletions examples/yaml-to-anydata/yaml_to_anydata.bal
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ type ServerConfig record {|
string protocol;
|};


final string yamlString = string
`
host: "localhost"
Expand All @@ -24,15 +23,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.
// 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;
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 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/).

Expand Down

0 comments on commit 0513c56

Please sign in to comment.