The Akwatype types allows to declare that a field corresponds to an object list.
For example a Person with a list of means of communication :
type Person { name: String communications: [Communication] }
type Communication { value : String nature: Integer activationDate:Date }
A Json corresponding to this description could be :
{
"name": "Anderson",
"communications": [
{
"value": "paul.anderson@mymail.com",
"nature": 1,
"activationDate": "2020-10-25"
},
{
"value": "p.anderson@mailbox.com",
"nature": 1,
"activationDate": "2018-02-05"
},
{
"value": "06 06 01 06 06",
"nature": 2,
"activationDate": "2012-06-17"
}
]
}
Simple use of dispatch
In this example we will use the value of the nature field to split the elements of the list into a mail list field and a phone list field.
To perform this transformation you can use Awatype's dispatch function.
This function is used following the declaration of a selected field name for a facade.
In its simplest form the dispatch function is expressed like this (for a list field named communications)
In our example we would have :
facade PersonDTO on Person{ name communications dispatch ( nature(1:mail,2:phone) ) }
{
"name": "Anderson",
"mail": [
{
"value": "paul.anderson@mymail.com",
"nature": 1,
"activationDate": "2020-10-25"
},
{
"value": "p.anderson@mailbox.com",
"nature": 1,
"activationDate": "2018-02-05"
}
],
"phone": [
{
"value": "06 06 01 06 06",
"nature": 2,
"activationDate": "2012-06-17"
}
]
}
Apply a facade on dispatched fields
As for any fields, you can declare a facade to be applied on the dispatched elements by following the dispatched field name values with :: facadeName
facade PersonDTO on Person{ name communications::CommunicationDTO dispatch (nature(1:mail,2:url)) }
{
"name": "Anderson",
"mail": [
{
"value": "paul.anderson@mymail.com",
"nature": 1
},
{
"value": "p.anderson@mailbox.com",
"nature": 1
}
],
"phone": [
{
"value": "06 06 01 06 06",
"nature": 2
}
]
}
Dispatched on scalar fields
You can declare a scalar field to be kept. To distribute only the value of a field, you can follow the list of distribution values in parenthesis with .fieldName
facade PersonDTO on Person{ name communications dispatch ( nature(1:mail,2:url).value ) }
{
"name": "Anderson",
"mail": [
"paul.anderson@mymail.com",
"p.anderson@mailbox.com"
],
"phone": [
"06 06 01 06 06"
]
}
Dispatched on additional fields
There are additional possibilities if you declare the fields on which you want to dispatch the data rather than letting them be created automatically.
In particular, it gives you the possibility to apply different facades on the different fields.
facade PersonDTO on Person{ additionalFields { mail::[mailDTO] url::[urlDTO] } name communications dispatch (nature(1:mail,2:url)) } facade mailDTO on Communication{ nature address:value } facade urlDTO on Communication{ nature number:value }
{
"name": "Anderson",
"mail": [
{
"address": "paul.anderson@mymail.com",
"nature": 1
},
{
"address": "p.anderson@mailbox.com",
"nature": 1
}
],
"phone": [
{
"number": "06 06 01 06 06",
"nature": 2
}
]
}