-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5531 from LakshanWeerasinghe/data.yaml-bbes
Add BBEs related to the data.yaml module
- Loading branch information
Showing
14 changed files
with
205 additions
and
17 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
examples/anydata-to-yaml-string/anydata_to_yaml_string.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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 string in YAML format. | ||
string serverConfigYamlStr = check yaml:toYamlString(serverConfig); | ||
io:println(serverConfigYamlStr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Serialize a Ballerina value 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/). | ||
|
||
::: code anydata_to_yaml_string.bal ::: | ||
|
||
::: out anydata_to_yaml_string.out ::: |
2 changes: 2 additions & 0 deletions
2
examples/anydata-to-yaml-string/anydata_to_yaml_string.metatags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
$ bal run anydata_to_yaml_string.bal | ||
host: localhost | ||
port: 3000 | ||
database: | ||
dbName: userDB | ||
username: testUser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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; | ||
|}; | ||
|
||
public function main() returns error? { | ||
// Similar to content read from a YAML file. | ||
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, 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. | ||
io:println(serverConfig); | ||
} |
11 changes: 11 additions & 0 deletions
11
examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# YAML to anydata conversion with projection | ||
|
||
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/). | ||
|
||
::: code yaml_to_anydata_with_projection.bal ::: | ||
|
||
::: out yaml_to_anydata_with_projection.out ::: |
2 changes: 2 additions & 0 deletions
2
examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.metatags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
2 changes: 2 additions & 0 deletions
2
examples/yaml-to-anydata-with-projection/yaml_to_anydata_with_projection.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
$ bal run yaml_to_anydata_with_projection.bal | ||
{"host":"localhost","port":8080,"remotePorts":[9000,9001],"database":{"dbName":"testdb","username":"dbuser"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import ballerina/data.yaml; | ||
import ballerina/io; | ||
|
||
type ServerConfig record {| | ||
string host; | ||
int port; | ||
string protocol; | ||
|}; | ||
|
||
final string yamlString = string | ||
` | ||
host: "localhost" | ||
port: 8080 | ||
protocol: "http"`; | ||
|
||
public function main() returns error? { | ||
// 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 as a record. | ||
ServerConfig serverConfig2 = check yaml:parseBytes(yamlByteArr); | ||
io:println(serverConfig2); | ||
|
||
stream<byte[], error?> byteBlockStream = new (new ByteBlockGenerator(yamlString)); | ||
// Parse the YAML byte block stream as a record. | ||
ServerConfig serverConfig3 = check yaml:parseStream(byteBlockStream); | ||
io:println(serverConfig3); | ||
} | ||
|
||
// 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; | ||
|
||
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)}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# YAML to anydata conversion | ||
|
||
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/). | ||
|
||
::: code yaml_to_anydata.bal ::: | ||
|
||
::: out yaml_to_anydata.out ::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 anydata. | ||
keywords: ballerina, ballerina by example, BBE, yaml, record, stream, byte array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"} |