Skip to content

Commit

Permalink
Merge pull request #7 from MadhukaHarith92/main
Browse files Browse the repository at this point in the history
Update YAML module APIs as isolated and update variable name
  • Loading branch information
shafreenAnfar authored May 22, 2023
2 parents 2141d0e + 5f316a0 commit f9c26d5
Show file tree
Hide file tree
Showing 42 changed files with 148 additions and 135 deletions.
2 changes: 1 addition & 1 deletion ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "yaml"
version = "0.1.0"
version = "0.2.0"
authors = ["Ballerina"]
keywords = ["yaml"]
repository = "https://github.com/ballerina-platform/module-ballerina-yaml"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "yaml"
version = "0.1.0"
version = "0.2.0"
dependencies = [
{org = "ballerina", name = "file"},
{org = "ballerina", name = "io"},
Expand Down
6 changes: 3 additions & 3 deletions ballerina/modules/common/utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
#
# + message - Cause of the error message
# + return - Formatted error message
public function generateConversionError(string message) returns ConversionError => error(message);
public isolated function generateConversionError(string message) returns ConversionError => error(message);

# Check errors during type casting to Ballerina types.
#
# + value - Value to be type casted.
# + return - Value as a Ballerina data type
public function processTypeCastingError(json|error value) returns json|ConversionError {
public isolated function processTypeCastingError(json|error value) returns json|ConversionError {
// Check if the type casting has any errors
if value is error {
return generateConversionError(value.message());
Expand All @@ -38,5 +38,5 @@ public function processTypeCastingError(json|error value) returns json|Conversio
# + actualEvent - Obtained invalid event
# + expectedEvent - Next expected event of the stream
# + return - Formatted error message as a string
public function generateExpectedEndEventErrorMessage(string actualEvent, string expectedEvent) returns string
public isolated function generateExpectedEndEventErrorMessage(string actualEvent, string expectedEvent) returns string
=> string `Expected '-${expectedEvent}' before '-${actualEvent}'`;
4 changes: 2 additions & 2 deletions ballerina/modules/composer/collection.bal
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import yaml.schema;
# + state - Current composer state
# + flowStyle - If a collection is flow sequence
# + return - Constructed Ballerina array on success
function composeSequence(ComposerState state, boolean flowStyle) returns json[]|lexer:LexicalError|parser:ParsingError|ComposingError|schema:SchemaError {
isolated function composeSequence(ComposerState state, boolean flowStyle) returns json[]|lexer:LexicalError|parser:ParsingError|ComposingError|schema:SchemaError {
json[] sequence = [];
common:Event event = check checkEvent(state, parser:EXPECT_SEQUENCE_VALUE);

Expand Down Expand Up @@ -65,7 +65,7 @@ function composeSequence(ComposerState state, boolean flowStyle) returns json[]|
# + flowStyle - If a collection is flow mapping
# + implicitMapping - Flag is set if there can only be one key-value pair
# + return - Constructed Ballerina array on success
function composeMapping(ComposerState state, boolean flowStyle, boolean implicitMapping) returns map<json>|lexer:LexicalError|parser:ParsingError|ComposingError|schema:SchemaError {
isolated function composeMapping(ComposerState state, boolean flowStyle, boolean implicitMapping) returns map<json>|lexer:LexicalError|parser:ParsingError|ComposingError|schema:SchemaError {
map<json> structure = {};
common:Event event = check checkEvent(state, parser:EXPECT_MAP_KEY);

Expand Down
4 changes: 2 additions & 2 deletions ballerina/modules/composer/composer.bal
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import yaml.common;
# + state - Initiated composer state
# + eventParam - Passed root event if already fetched
# + return - Ballerina data structure on success
public function composeDocument(ComposerState state, common:Event? eventParam = ()) returns json|ComposingError {
public isolated function composeDocument(ComposerState state, common:Event? eventParam = ()) returns json|ComposingError {
// Obtain the root event
common:Event event = eventParam is () ? check checkEvent(state, docType = parser:ANY_DOCUMENT) : eventParam;

Expand Down Expand Up @@ -48,7 +48,7 @@ public function composeDocument(ComposerState state, common:Event? eventParam =
#
# + state - Initiated composer state
# + return - Native Ballerina data structure on success
public function composeStream(ComposerState state) returns json[]|ComposingError {
public isolated function composeStream(ComposerState state) returns json[]|ComposingError {
json[] output = [];
common:Event event = check checkEvent(state, docType = parser:ANY_DOCUMENT);

Expand Down
8 changes: 4 additions & 4 deletions ballerina/modules/composer/error.bal
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public type ComposeError distinct error<common:ReadErrorDetails>;
# + actualEvent - Obtained invalid event
# + expectedEvent - Next expected event of the stream
# + return - Formatted error message
function generateExpectedEndEventError(ComposerState state,
isolated function generateExpectedEndEventError(ComposerState state,
string actualEvent, string expectedEvent) returns ComposeError =>
generateComposeError(state, common:generateExpectedEndEventErrorMessage(actualEvent, expectedEvent),
actualEvent, expectedEvent);
Expand All @@ -44,14 +44,14 @@ function generateExpectedEndEventError(ComposerState state,
# + expectedKind - Expected core schema kind of the data
# + tag - Tag of the data
# + return - Formatted error message
function generateExpectedKindError(ComposerState state, string actualKind, string expectedKind, string tag)
isolated function generateExpectedKindError(ComposerState state, string actualKind, string expectedKind, string tag)
returns ComposeError => generateComposeError(
state,
string `Expected '${expectedKind}' kind for the '${tag}' tag but found '${actualKind}'`,
actualKind,
expectedKind);

function generateAliasingError(ComposerState state, string message, common:Event actualEvent)
isolated function generateAliasingError(ComposerState state, string message, common:Event actualEvent)
returns common:AliasingError =>
error(
message,
Expand All @@ -60,7 +60,7 @@ function generateAliasingError(ComposerState state, string message, common:Event
actual = actualEvent
);

function generateComposeError(ComposerState state, string message, json actualEvent, json? expectedEvent = ()) returns ComposeError =>
isolated function generateComposeError(ComposerState state, string message, json actualEvent, json? expectedEvent = ()) returns ComposeError =>
error(
message,
line = state.parserState.getLineNumber(),
Expand Down
6 changes: 3 additions & 3 deletions ballerina/modules/composer/scalar.bal
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import yaml.lexer;
# + state - Current composer state
# + event - Node event to be composed
# + return - Native Ballerina data on success
function composeNode(ComposerState state, common:Event event) returns json|lexer:LexicalError|parser:ParsingError|ComposingError|schema:SchemaError {
isolated function composeNode(ComposerState state, common:Event event) returns json|lexer:LexicalError|parser:ParsingError|ComposingError|schema:SchemaError {
json output;

// Check for aliases
Expand Down Expand Up @@ -73,7 +73,7 @@ function composeNode(ComposerState state, common:Event event) returns json|lexer
# + event - The event representing the alias name
# + assignedValue - Anchored value to to the alias
# + return - An error on failure
function checkAnchor(ComposerState state, common:StartEvent|common:ScalarEvent event, json assignedValue) returns ComposingError? {
isolated function checkAnchor(ComposerState state, common:StartEvent|common:ScalarEvent event, json assignedValue) returns ComposingError? {
if event.anchor != () {
if state.anchorBuffer.hasKey(<string>event.anchor) && !state.allowAnchorRedefinition {
return generateAliasingError(state, string `Duplicate anchor definition of '${<string>event.anchor}'`, event);
Expand All @@ -89,7 +89,7 @@ function checkAnchor(ComposerState state, common:StartEvent|common:ScalarEvent e
# + kind - Fail safe schema type
# + tag - Tag of the data if exists
# + return - Constructed ballerina data
function castData(ComposerState state, json data,
isolated function castData(ComposerState state, json data,
schema:FailSafeSchema kind, string? tag) returns json|ComposingError|schema:SchemaError {
// Check for explicit keys
if tag != () {
Expand Down
2 changes: 1 addition & 1 deletion ballerina/modules/composer/state.bal
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ComposerState {
# Flag is set if same map keys are allowed in a mapping
readonly & boolean allowMapEntryRedefinition;

public function init(string[] lines, map<schema:YAMLTypeConstructor> tagSchema,
public isolated function init(string[] lines, map<schema:YAMLTypeConstructor> tagSchema,
boolean allowAnchorRedefinition, boolean allowMapEntryRedefinition) returns parser:ParsingError? {

self.parserState = check new (lines);
Expand Down
2 changes: 1 addition & 1 deletion ballerina/modules/composer/utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import yaml.lexer;
# + option - Expected parser option
# + docType - Expected YAML document
# + return - Next event on success
function checkEvent(ComposerState state, parser:ParserOption option = parser:DEFAULT,
isolated function checkEvent(ComposerState state, parser:ParserOption option = parser:DEFAULT,
parser:DocumentType docType = parser:BARE_DOCUMENT) returns common:Event|lexer:LexicalError|parser:ParsingError {

// Return the terminated document event until the function stack is exited.
Expand Down
8 changes: 5 additions & 3 deletions ballerina/modules/emitter/emitter.bal
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import yaml.common;
# + state - Parameter Description
# + isStream - Whether the event tree is a stream
# + return - Output YAML content as an array of strings.
public function emit(EmitterState state, boolean isStream) returns string[]|EmittingError {
public isolated function emit(EmitterState state, boolean isStream) returns string[]|EmittingError {
if isStream { // Write YAML stream
string[] output = [];
while state.events.length() > 0 {
check write(state);
state.getDocument(true).forEach(line => output.push(line));
foreach var line in state.getDocument(true) {
output.push(line);
}
}
return output;
} else { // Write a single YAML document
Expand All @@ -40,7 +42,7 @@ public function emit(EmitterState state, boolean isStream) returns string[]|Emit
#
# + state - Current emitter state
# + return - An error on failure
function write(EmitterState state) returns EmittingError? {
isolated function write(EmitterState state) returns EmittingError? {
common:Event event = getEvent(state);

// Convert sequence collection
Expand Down
4 changes: 2 additions & 2 deletions ballerina/modules/emitter/error.bal
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public type EmittingError distinct error<common:WriteErrorDetails>;
# + actualEvent - Obtained invalid event
# + expectedEvent - Next expected event of the stream
# + return - Formatted error message
function generateExpectedEndEventError(string actualEvent, string expectedEvent) returns EmittingError =>
isolated function generateExpectedEndEventError(string actualEvent, string expectedEvent) returns EmittingError =>
generateEmittingError(common:generateExpectedEndEventErrorMessage(actualEvent, expectedEvent),
actualEvent, expectedEvent);

function generateEmittingError(string message, json actualValue, json? expectedValue = ())
isolated function generateEmittingError(string message, json actualValue, json? expectedValue = ())
returns EmittingError =>
error(
message,
Expand Down
4 changes: 2 additions & 2 deletions ballerina/modules/emitter/mapping.bal
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import yaml.common;
# + state - Current emitter state
# + tag - Tag of the start event if exists
# + return - YAML string of the flow mapping.
function writeFlowMapping(EmitterState state, string? tag) returns string|EmittingError {
isolated function writeFlowMapping(EmitterState state, string? tag) returns string|EmittingError {
string line = writeNode(state, "{", tag);
common:Event event = getEvent(state);
boolean firstValue = true;
Expand Down Expand Up @@ -80,7 +80,7 @@ function writeFlowMapping(EmitterState state, string? tag) returns string|Emitti
# + whitespace - Whitespace at the start of it
# + tag - Tag of the start event if exists
# + return - YAML string of the block mapping.
function writeBlockMapping(EmitterState state, string whitespace, string? tag) returns EmittingError? {
isolated function writeBlockMapping(EmitterState state, string whitespace, string? tag) returns EmittingError? {
common:Event event = getEvent(state);
string line;

Expand Down
4 changes: 2 additions & 2 deletions ballerina/modules/emitter/sequence.bal
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import yaml.common;
# + state - Current emitter state
# + tag - Tag of the start event if exists
# + return - YAML string of the flow sequence.
function writeFlowSequence(EmitterState state, string? tag) returns string|EmittingError {
isolated function writeFlowSequence(EmitterState state, string? tag) returns string|EmittingError {
string line = writeNode(state, "[", tag);
common:Event event = getEvent(state);
boolean firstValue = true;
Expand Down Expand Up @@ -72,7 +72,7 @@ function writeFlowSequence(EmitterState state, string? tag) returns string|Emitt
# + whitespace - Whitespace at the start of it
# + tag - Tag of the start event if exists
# + return - YAML string of the block sequence.
function writeBlockSequence(EmitterState state, string whitespace, string? tag) returns EmittingError? {
isolated function writeBlockSequence(EmitterState state, string whitespace, string? tag) returns EmittingError? {
common:Event event = getEvent(state);
boolean emptySequence = true;

Expand Down
13 changes: 7 additions & 6 deletions ballerina/modules/emitter/state.bal
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class EmitterState {
# Event tree to be converted
common:Event[] events;

public function init(common:Event[] events, map<string> customTagHandles,
public isolated function init(common:Event[] events, map<string> customTagHandles,
int indentationPolicy, boolean canonical) {

self.events = events;
Expand All @@ -52,21 +52,22 @@ public class EmitterState {
self.indent = indent;
}

function addLine(string line) => self.document.push(line);
isolated function addLine(string line) => self.document.push(line);

function addTagHandle(string tagHandle) {
isolated function addTagHandle(string tagHandle) {
if self.documentTags.indexOf(tagHandle) == () {
self.documentTags.push(tagHandle);
}
}

function getDocument(boolean isStream = false) returns string[] {
isolated function getDocument(boolean isStream = false) returns string[] {
string[] output = self.document.clone();

if self.documentTags.length() > 0 {
output.unshift("---");
self.documentTags.sort(array:DESCENDING).forEach(tagHandle =>
output.unshift(string `%TAG ${tagHandle} ${self.customTagHandles.get(tagHandle)}`));
foreach var tagHandle in self.documentTags.sort(array:DESCENDING) {
output.unshift(string `%TAG ${tagHandle} ${self.customTagHandles.get(tagHandle)}`);
}
if self.lastBareDoc {
output.unshift("...");
self.lastBareDoc = false;
Expand Down
6 changes: 3 additions & 3 deletions ballerina/modules/emitter/utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import yaml.schema;
#
# + state - Current emitter state
# + return - The topmost event from the current tree.
function getEvent(EmitterState state) returns common:Event {
isolated function getEvent(EmitterState state) returns common:Event {
if state.events.length() < 1 {
return {endType: common:STREAM};
}
Expand All @@ -33,7 +33,7 @@ function getEvent(EmitterState state) returns common:Event {
# + tag - Tag of the node
# + tagAsSuffix - If set, the tag is written after the value
# + return - YAML string representing the node
function writeNode(EmitterState state, string? value, string? tag, boolean tagAsSuffix = false) returns string {
isolated function writeNode(EmitterState state, string? value, string? tag, boolean tagAsSuffix = false) returns string {
if tag == () {
return value.toString();
}
Expand Down Expand Up @@ -71,5 +71,5 @@ function writeNode(EmitterState state, string? value, string? tag, boolean tagAs
# + tag - Tag of the node
# + value - Value of the node to be written
# + return - String with tag appended to the value
function appendTagToValue(boolean tagAsSuffix, string tag, string? value) returns string
isolated function appendTagToValue(boolean tagAsSuffix, string tag, string? value) returns string
=> tagAsSuffix ? value.toString() + " " + tag : tag + " " + value.toString();
Loading

0 comments on commit f9c26d5

Please sign in to comment.