-
Notifications
You must be signed in to change notification settings - Fork 293
Array mapping resolver
Today we using $. for trigger mapping(means current scope) and array mapping element(current array element)
It has an issue when doing an array mapping in trigger mapping, it doesn't know where it is coming from, is it a trigger or current array context?
Upon we discussed on Friday(June 1), we will continue to use $. for trigger mapping but need to find another way for array mapping
Take an example doing an array mapping inside a rest trigger.
{
"mappings": {
"input": [
{
"mapTo": "flowInputLevel1",
"type": "array",
"value": {
"fields": [
{
"from": "$.id1",
"to": "$$['id1']",
"type": "primitive"
},
{
"fields": [
{
"from": "$.id2",
"to": "$$['id2']",
"type": "primitive"
},
{
"from": "$.name2",
"to": "$$['name2']",
"type": "primitive"
}
],
"from": "$.triggerOutputLevel2",
"to": "$$['flowInputLevel2']",
"type": "foreach"
},
{
"from": "$.name1",
"to": "$$['name1']",
"type": "primitive"
}
],
"from": "$.body.triggerOutputLevel1",
"to": "flowInputLevel1",
"type": "foreach"
}
},
{
"mapTo": "flowRootField",
"type": "expression",
"value": "$.body.rootField"
}
]
}
- Straight mapping trigger root field(
$.body.rootField) toflowRootField.$.bodymeans get currect tirgger attributebody. - Another mapping is an array mapping structure. Map trigger output
$.body.triggerOutputLevel1toflowInputLevel1, both are JSON array. foreachtrigger outputLevel1and assigin it'sid1andname1to flow flowInputLevel1 fieldid1andname1, go with same same for second level.
For both 1 and 2 mapping are using prefix $. which get confuses. #1 using $. to get current data scope data but #2 using for current array context.
Here are some approaches to change array $. to
- $element
- $current --> Today the iterator feature already token $current
- $array
- $iterator --> $Iterator more like foreach or loop, the resolver should just for currect loop context.
- more.....
Assume we have 2 level array, how to access first level data from second level? Some approaches
- $current["FlowinputLevel1"].id
- $current["arrayinputLevel1.arrayInputLevel2"].id
Taking an exmaple that I want to map dealer vehicles to people's car, here are both JSON structrue.
Note: I assume we choose $element as final array resovler.
Dealers
{
"dealer": {
"address": {
"city": "",
"street": "",
"zipcode": ""
},
"name": "toyota",
"vehicles": [
{
"color": "",
"make": "",
"model": "",
"supliers": [
{
"name": "",
"part": ""
}
],
"year": ""
}
]
}
}
People
{
"people": {
"fullname": "Bob...",
"cars": [
{
"color": "",
"make": "",
"model": "",
"supliers": [
{
"name": "",
"part": "",
"vehicleMake":""
}
],
"year": ""
}
]
}
}
Mapping
{
"fields": [
{
"from": "$element.make",
"to": "$element.make",
"type": "primitive"
},
{
"from": "$element.model",
"to": "$element.model",
"type": "primitive"
},
{
"fields": [
{
"from": "$element.name",
"to": "$element.name",
"type": "primitive"
},
{
"from": "$element.part",
"to": "$element.part",
"type": "primitive"
},
{
"from": "$element["vehicles"].make",
"to": "$element.vehicleMake",
"type": "primitive"
}
],
"from": "$element.supliers",
"to": "$element.supliers",
"type": "foreach"
}
],
"from": "$.body.dealer.vehicles",
"to": "people.cars",
"type": "foreach"
}
- Using
$elementto access current arrray context value, such as:$element.model - Using
$element["nodename"]to access parent array context value, such as:$element["vehicles"].make