-
Notifications
You must be signed in to change notification settings - Fork 4
Add new examples and refactor response aggregator #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
warunalakshitha
merged 3 commits into
ballerina-platform:main
from
snelusha:chore/update-examples
Apr 7, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 @@ | ||
| [package] | ||
| org = "playground" | ||
| name = "response_aggregator" | ||
| version = "0.1.0" |
This file contains hidden or 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 @@ | ||
| import response_aggregator.handler; | ||
| import response_aggregator.types; | ||
|
|
||
| import ballerina/io; | ||
|
|
||
| public function main() { | ||
| types:ApiResponse[] responses = [ | ||
| {ep: "/users", status: 200, flags: handler:CACHED, result: 150}, | ||
| {ep: "/orders", status: 200, flags: handler:CACHED + handler:PAGINATED, result: "paginated"}, | ||
| {ep: "/products", status: 206, flags: handler:PARTIAL, result: 42} | ||
| ]; | ||
| [string, int] healthCheck = ["/health", 200]; | ||
|
|
||
| io:println("Health Check: ", healthCheck[0], " (", healthCheck[1], ")"); | ||
|
|
||
| foreach types:ApiResponse res in responses { | ||
| handler:processResponse(res); | ||
| } | ||
| } | ||
|
|
30 changes: 30 additions & 0 deletions
30
examples/01-response-aggregator/modules/handler/handler.bal
This file contains hidden or 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,30 @@ | ||
| import ballerina/io; | ||
|
|
||
| import playground/response_aggregator.types; | ||
|
|
||
| // Response flags | ||
| public const int CACHED = 1 << 0; | ||
| public const int PARTIAL = 1 << 1; | ||
| public const int PAGINATED = 1 << 2; | ||
|
|
||
| // Processes and displays an API response with its flags and result. | ||
| public function processResponse(types:ApiResponse res) { | ||
| io:println("\nEndpoint: ", res.ep, ",\n Status: ", res.status); | ||
|
|
||
| if res.flags >= PAGINATED { | ||
| io:println(" [Paginated]"); | ||
| } else if res.flags == CACHED { | ||
| io:println(" [Cached]"); | ||
| } else { | ||
| io:println(" [Partial]"); | ||
| } | ||
|
|
||
| any result = res.result; | ||
| if result is int { | ||
| int count = <int>result; | ||
| io:println(" Count: ", count); | ||
| } else if result is string { | ||
| io:println(" Info: ", result); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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,8 @@ | ||
| // API response record type definition. | ||
| public type ApiResponse record {| | ||
| string ep; | ||
| int status; | ||
| int flags; | ||
| any result; | ||
| |}; | ||
|
|
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,62 @@ | ||
| import ballerina/io; | ||
|
|
||
| function validateFahrenheit(int f) returns int|error { | ||
| if f < -459 { | ||
| return error("invalid fahrenheit: below absolute zero"); | ||
| } | ||
| return f; | ||
| } | ||
|
|
||
| function toCelsius(int f) returns int|error { | ||
| int valid = check validateFahrenheit(f); | ||
| return ((valid - 32) * 5) / 9; | ||
| } | ||
|
|
||
| function averageCelsius(int f1, int f2) returns int|error { | ||
| int c1 = check toCelsius(f1); | ||
| int c2 = check toCelsius(f2); | ||
| return (c1 + c2) / 2; | ||
| } | ||
|
snelusha marked this conversation as resolved.
|
||
|
|
||
| function externalCalibrator(int c) returns int { | ||
| // Simulate a dependency that may panic for suspicious values. | ||
| if c > 100 { | ||
| panic error("calibrator overflow"); | ||
| } | ||
| return c + 1; | ||
| } | ||
|
|
||
| function safeConvertedValue(int f) returns int|error { | ||
| int c = check toCelsius(f); | ||
| var calibrated = trap externalCalibrator(c); | ||
| if calibrated is error { | ||
| return calibrated; | ||
| } | ||
| return calibrated; | ||
| } | ||
|
|
||
| function leaf(int f1, int f2) { | ||
| _ = checkpanic averageCelsius(f1, f2); | ||
| } | ||
|
|
||
| function middle(int f1, int f2) { | ||
| leaf(f1, f2); | ||
| } | ||
|
|
||
| function top(int f1, int f2) { | ||
| middle(f1, f2); | ||
| } | ||
|
|
||
| public function main() { | ||
| io:println("=== Temperature Converter ==="); | ||
| io:println("Safe Convert | Input: 98F"); | ||
| io:println("Result | ", safeConvertedValue(98)); | ||
| io:println("\nSafe Convert | Input: -500F"); | ||
| io:println("Result | ", safeConvertedValue(-500)); | ||
| io:println("\nAverage | Inputs: 98F, 32F"); | ||
| io:println("Result | ", averageCelsius(98, 32)); | ||
| io:println("\nStack Trace | top(-500, 32)"); | ||
|
|
||
| top(-500, 32); | ||
| } | ||
|
|
||
This file contains hidden or 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,79 @@ | ||
| import ballerina/io; | ||
|
|
||
| type Student record {| | ||
| int id; | ||
| string name; | ||
| int grade; | ||
| |}; | ||
|
|
||
| function findById(Student[] students, int studentId) returns Student|error { | ||
| Student[] rows = from var s in students | ||
| where s.id == studentId | ||
| select s; | ||
|
|
||
| if rows.length() == 0 { | ||
| return error("student not found"); | ||
| } | ||
|
|
||
| return rows[0]; | ||
| } | ||
|
|
||
| function maxGrade(Student[] students) returns int { | ||
| int best = students[0].grade; | ||
| foreach Student s in students { | ||
| if s.grade > best { | ||
| best = s.grade; | ||
| } | ||
| } | ||
| return best; | ||
| } | ||
|
snelusha marked this conversation as resolved.
|
||
|
|
||
| function averageGrade(Student[] students) returns int { | ||
| int sum = 0; | ||
| foreach Student s in students { | ||
| sum += s.grade; | ||
| } | ||
| return sum / students.length(); | ||
| } | ||
|
snelusha marked this conversation as resolved.
|
||
|
|
||
| function topStudents(Student[] students) returns Student[] { | ||
| int best = maxGrade(students); | ||
| Student[] rows = from var s in students | ||
| where s.grade == best | ||
| select s; | ||
| return rows; | ||
| } | ||
|
|
||
| public function main() { | ||
| Student[] students = [ | ||
| {id: 101, name: "Asha", grade: 78}, | ||
| {id: 102, name: "Nimal", grade: 92}, | ||
| {id: 103, name: "Ravi", grade: 85}, | ||
| {id: 104, name: "Sara", grade: 92} | ||
| ]; | ||
|
|
||
| io:println("=== Student Grades ==="); | ||
| io:println("Average Grade | ", averageGrade(students)); | ||
| io:println("\nTop Students"); | ||
| Student[] tops = topStudents(students); | ||
| foreach Student s in tops { | ||
| io:println("- ", s.id, " | ", s.name, " | ", s.grade); | ||
| } | ||
|
|
||
| io:println("\nLookup | ID 102"); | ||
| var row1 = findById(students, 102); | ||
| if row1 is Student { | ||
| io:println("Found | ", row1.id, " | ", row1.name, " | ", row1.grade); | ||
| } else { | ||
| io:println("Error | ", row1); | ||
| } | ||
|
|
||
| io:println("\nLookup | ID 999"); | ||
| var row2 = findById(students, 999); | ||
| if row2 is Student { | ||
| io:println("Found | ", row2.id, " | ", row2.name, " | ", row2.grade); | ||
| } else { | ||
| io:println("Error | ", row2); | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.