import ballerina/io;
type Employee {
string name;
int age;
string address;
};
type Person {
string firstName;
string lastName;
int age;
string city;
string street;
};
transformer <Person p, Employee e> {
e.name = p.firstName + " " + p.lastName;
e.age = p.age;
e.address = p.street + "," + p.city.toUpperCase();
}
transformer <Person p, Employee e> setCityToNewYork() {
e.name = p.firstName + " " + p.lastName;
e.age = p.age;
e.address = p.street + ", " + "New York";
}
transformer <Person p, Employee e> insertCountry(string country) {
e.name = p.firstName + " " + p.lastName;
e.age = p.age;
e.address = p.street + "," + p.city.toUpperCase() + ", " + country;
}function main (string[] args) {
Person person = {firstName:"John", lastName:"Doe", age:30, city:"London"};
Employee employee = <Employee>person;
io:println(employee);
employee = <Employee, setCityToNewYork()>person;
io:println(employee);
employee = <Employee, insertCountry("UK")>person;
io:println(employee);
}
TransformersTransformers are used to convert a variable of one type to another. Additional arguments can also be passed to a transformer for the conversion. The transformer can be defined as an high level construct, and can be used within any expression using the type conversion syntax. |
|
import ballerina/io;
|
|
type Employee {
string name;
int age;
string address;
};
|
Defining Employee struct. |
type Person {
string firstName;
string lastName;
int age;
string city;
string street;
};
|
Defining Person struct. |
transformer <Person p, Employee e> {
e.name = p.firstName + " " + p.lastName;
e.age = p.age;
e.address = p.street + "," + p.city.toUpperCase();
}
|
Defining a default transformer for converting from ‘Person’ type to ‘Employee’ type. |
transformer <Person p, Employee e> setCityToNewYork() {
e.name = p.firstName + " " + p.lastName;
e.age = p.age;
e.address = p.street + ", " + "New York";
}
|
Defining a named transformer for converting from ‘Person’ type to ‘Employee’ type. |
transformer <Person p, Employee e> insertCountry(string country) {
e.name = p.firstName + " " + p.lastName;
e.age = p.age;
e.address = p.street + "," + p.city.toUpperCase() + ", " + country;
}
|
Defining a named transformer which takes input parameters for converting from ‘Person’ type to ‘Employee’ type. |
function main (string[] args) {
|
|
Person person = {firstName:"John", lastName:"Doe", age:30, city:"London"};
|
Initialize Person variable person. |
Employee employee = <Employee>person;
io:println(employee);
|
Using default transformer to convert from type Person to Employee is similar to the conversion syntax. |
employee = <Employee, setCityToNewYork()>person;
io:println(employee);
|
Named transformer can be explicitly provided inside the conversion syntax, to convert person to employee. |
employee = <Employee, insertCountry("UK")>person;
io:println(employee);
}
|
Using the named transformer to convert person to employee, by passing parameters. |
$ ballerina run transformers.bal
{name:"John Doe", age:30, address:",LONDON"}
{name:"John Doe", age:30, address:", New York"}
{name:"John Doe", age:30, address:",LONDON, UK"}
|
|