Translate values to properties in DTL in Sesam

时间:2018-01-11 08:36:40

标签: sesam

I have a list where a numerical value represent a property, i would like to be able to translate it inside the pipe.

Format matches this:

'15':'AA'
'10':'BB'
'20':'CC'

1 个答案:

答案 0 :(得分:1)

这可以通过创建使用DTL转换查找的映射数据集来解决。

首先我们创建映射数据集(在本例中我们只使用嵌入数据集):

return someAsyncFunction()
    .then(...)
    .then(...)
    .catch(error => { console.error(error) })

然后我们可以使用这种映射:

{
  "_id": "mapping",
  "type": "pipe",
  "source": {
    "type": "embedded",
    "entities": [{
      "_id": "15",
      "value": "AA"
    }, {
      "_id": "10",
      "value": "BB"
    }, {
      "_id": "20",
      "value": "CC"
    }]
  },
  "add_namespaces": false
}

结果是:

{
  "_id": "mapper",
  "type": "pipe",
  "source": {
    "type": "embedded",
    "entities": [{
      "_id": "foo",
      "value": "15"
    }]
  },
  "transform": {
    "type": "dtl",
    "rules": {
      "default": [
        ["copy", "*"],
        ["add", "::mapped_value",
          ["coalesce",
            ["hops", {
              "datasets": ["mapping m"],
              "where": ["eq", "_S.value", "m._id"],
              "return": "m.value"
            }]
          ]
        ]
      ]
    }
  },
  "add_namespaces": false
}