-
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 #5664 from rdulmina/master
Update on conflict clause `BBE` with more real-world sample
- Loading branch information
Showing
6 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
examples/advanced-conflict-handling/advanced_conflict_handling.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,40 @@ | ||
import ballerina/io; | ||
|
||
type Student record {| | ||
string name; | ||
int score; | ||
|}; | ||
|
||
public function main() { | ||
Student[] students = [ | ||
{name: "John", score: 100}, | ||
{name: "Jane", score: 150}, | ||
{name: "John", score: 200} | ||
]; | ||
|
||
// Create a map of the student `name` and the `score` where we replace the value of the duplicate key if the new `score` is even. | ||
// The old `score` `100` of `John` will be replaced by the new `score` 200` since `replaceIfSafe()`returns `nil` for even scores. | ||
map<int>|error studentScores = map from var {name, score} in students | ||
select [name, score] | ||
on conflict replaceIfSafe(score); | ||
io:println(studentScores); | ||
|
||
students = [ | ||
{name: "John", score: 100}, | ||
{name: "Jane", score: 150}, | ||
{name: "John", score: 211} | ||
]; | ||
|
||
// The result of this will be an error since the key `John` is duplicated and the `replaceIfSafe()`returns an error for odd `score` `211`. | ||
map<int>|error studentScores2 = map from var {name, score} in students | ||
select [name, score] | ||
on conflict replaceIfSafe(score); | ||
io:println(studentScores2); | ||
} | ||
|
||
function replaceIfSafe(int score) returns error? { | ||
if score % 2 != 0 { | ||
return error("Key Conflict", message = "Duplicate key has an odd score"); | ||
} | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
examples/advanced-conflict-handling/advanced_conflict_handling.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,20 @@ | ||
# Advanced conflict handling | ||
|
||
We can implement a custom conflict-handling policy to determine whether to replace a value or throw an error in the event of conflicting keys when constructing a map or table. | ||
|
||
::: code advanced_conflict_handling.bal ::: | ||
|
||
::: out advanced_conflict_handling ::: | ||
|
||
## Related links | ||
- [On conflict clause](/learn/by-example/on-conflict-clause) | ||
- [Query expressions](/learn/by-example/query-expressions) | ||
- [Sort iterable objects using query](/learn/by-example/sort-iterable-objects) | ||
- [Let clause in query expression](/learn/by-example/let-clause) | ||
- [Limit clause in query expression](/learn/by-example/limit-clause) | ||
- [Joining iterable objects using query](/learn/by-example/joining-iterable-objects) | ||
- [Querying tables](/learn/by-example/querying-tables) | ||
- [Create maps with query expression](/learn/by-example/create-maps-with-query) | ||
- [Create tables with query expression](/learn/by-example/create-tables-with-query) | ||
- [Create streams with query expression](/learn/by-example/create-streams-with-query) | ||
- [Nested query expressions](/learn/by-example/nested-query-expressions) |
2 changes: 2 additions & 0 deletions
2
examples/advanced-conflict-handling/advanced_conflict_handling.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 writing custom conflict handling logic when constructing a map or a table. | ||
keywords: ballerina, ballerina by example, bbe, duplicate, key, map, table, query, on conflict, error |
3 changes: 3 additions & 0 deletions
3
examples/advanced-conflict-handling/advanced_conflict_handling.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,3 @@ | ||
$ bal run advanced_conflict_handling.bal | ||
{"John":200,"Jane":150} | ||
error("Key Conflict",message="Duplicate key has an odd score") |
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