Skip to content

Array mapping resolver

Tracy Li edited this page Jun 18, 2018 · 2 revisions

Representative array mapping

Array 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"
            }
        ]
}
  1. Straight mapping trigger root field($.body.rootField) to flowRootField. $.body means get currect tirgger attribute body.
  2. Another mapping is an array mapping structure. Map trigger output $.body.triggerOutputLevel1 to flowInputLevel1, both are JSON array. foreach trigger outputLevel1 and assigin it's id1 and name1 to flow flowInputLevel1 field id1 and name1, 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.....

Accessing array parent data

Assume we have 2 level array, how to access first level data from second level? Some approaches

  • $current["FlowinputLevel1"].id
  • $current["arrayinputLevel1.arrayInputLevel2"].id

Examples.

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"
}
  1. Using $element to access current arrray context value, such as: $element.model
  2. Using $element["nodename"] to access parent array context value, such as: $element["vehicles"].make