Skip to content

Commit

Permalink
Fix basic type handling and add a sample
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed Dec 5, 2024
1 parent ccf8e21 commit fd3ef9b
Show file tree
Hide file tree
Showing 11 changed files with 611 additions and 5 deletions.
56 changes: 56 additions & 0 deletions examples/type-processing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
## Overview

This sample demonstrates the supported types of the Ballerina MI connector SDK. A Ballerina function with parameters and return type of `boolean`, `string`, `int`, `float`, `decimal`, `xml`, and `json` is supported by the MI SDK.
This scenario illustrates a content-based routing use case where a JSON payload containing a `type` property is sent to an API. Based on the value of this `type` property, a corresponding Ballerina function is invoked.

A json payload of following format is sent to the endpoint

```json
{
"type": "integer",
"val": 21
}
```

For the type field it is possible to send `boolean`, `string`, `int`, `float`, `decimal`, `xml`, and `json`.

This would output a result in the following format.

```json
{
"result": 42
}
```

## Steps to Invoke the Sample

Follow these steps to invoke the sample using the connector:

1. The `bal-type-processing` folder contains the Ballerina code for the connector. Invoke the following command to generate the connector:

```bash
bal mi -i <ballerina-project>
```

2. A ZIP file of the connector will be generated. Add this ZIP file to the MI project inside the folder `mi-type-processing` following the approach described [here](https://mi.docs.wso2.com/en/latest/develop/creating-artifacts/adding-connectors/).

3. Once the connector is added, run the MI project.

4. Send an HTTP POST request to the following resource with a payload as specified:

```bash
curl --location 'http://localhost:8290/type-processing' \
--header 'Content-Type: application/json' \
--data '{
"type": "decimal",
"val": 5.3
}'
```

Output:

```json
{
"result":15.3
}
```
14 changes: 14 additions & 0 deletions examples/type-processing/bal-type-processing/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
org = "testOrg"
name = "bal_type_processing"
version = "0.1.0"
distribution = "2201.9.2"

[build-options]
observabilityIncluded = true

[[dependency]]
org = "ballerinax"
name = "mi"
version = "0.1.3"
repository = "local"
44 changes: 44 additions & 0 deletions examples/type-processing/bal-type-processing/main.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import ballerinax/mi;

@mi:ConnectorInfo
public function invertBoolean(boolean b) returns boolean => !b;

@mi:ConnectorInfo
public function doubleInt(int n) returns int => n * 2;

@mi:ConnectorInfo
public function reciprocalFloat(float f) returns float {
if f == 0.0 {
return 1;
}
return 1.0 / f;
}

@mi:ConnectorInfo
public function addConstantToDecimal(decimal d) returns decimal => d + 10;

@mi:ConnectorInfo
public function doubleString(string s) returns string => s + s;

@mi:ConnectorInfo
public function getJsonNameProperty(json j) returns json {
json jsn = j;
if jsn is string {
json|error je = jsn.fromJsonString();
if je is error {
return {err: je.message()};
}
jsn = je;
}
json|error val = jsn.name;
if val is error {
return {err: val.message()};
}
return {val};
}

@mi:ConnectorInfo
public function getXmlNameElement(xml x) returns xml {
xml y = x/<name>;
return xml `<result>${y}</result>`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[server]
hostname = "localhost"
hot_deployment = false

[user_store]
type = "read_only_ldap"

[keystore.primary]
file_name = "repository/resources/security/wso2carbon.jks"
password = "wso2carbon"
alias = "wso2carbon"
key_password = "wso2carbon"

[truststore]
file_name = "repository/resources/security/client-truststore.jks"
password = "wso2carbon"
alias = "symmetric.key.value"
algorithm = "AES"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ARG BASE_IMAGE
FROM ${BASE_IMAGE}
COPY CompositeApps/*.car ${WSO2_SERVER_HOME}/repository/deployment/server/carbonapps/
COPY resources/wso2carbon.jks ${WSO2_SERVER_HOME}/repository/resources/security/wso2carbon.jks
COPY resources/client-truststore.jks ${WSO2_SERVER_HOME}/repository/resources/security/client-truststore.jks
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit fd3ef9b

Please sign in to comment.