通过元素列表进行映射以更改字符串值

时间:2019-06-07 07:54:36

标签: sesam

我要遍历列表中的每个元素的列表,然后在匹配给定字符串时将其转换为目标字符串。

用例: 列表操作

我尝试了不同的功能,例如case,case-eq等。尽管上述功能似乎都无法解决问题。

管道配置:

g++

结果:

{
  "_id": "jonas-testing-manipulated-properties",
  "type": "pipe",
  "source": {
    "type": "dataset",
    "dataset": "jonas-testing"
  },
  "transform": {
    "type": "dtl",
    "rules": {
      "default": [
        ["copy", "*"],
        ["add", "country",
          ["list", "Norway", "Denmark", "Sweden"]
        ],
        ["add", "Country Abbreviation",
          ["case-eq", "_T.country", "Norway", "NO", "Sweden", "SE", "Other"]
        ],
        ["add", "Country Abbreviation with mapping",
          ["map",
            ["case-eq", "_T.country", "Norway", "NO", "Sweden", "SE", "Other"], "_T.country"]
        ]
      ]
    }
  }
}

预期结果

[
  {
    "Country Abbreviation": "Other",
    "Country Abbreviation with mapping": [
      "Other",
      "Other",
      "Other"
    ],
    "_deleted": false,
    "_hash": "f2ed1ca17e97917245d6b465ca7ed7ae",
    "_id": "1",
    "_previous": 71,
    "_ts": 1559888447704105,
    "_updated": 72,
    "convert-to-int": "~f1.5",
    "country": [
      "Norway",
      "Denmark",
      "Sweden"
    ]
  }
]

2 个答案:

答案 0 :(得分:0)

使用map函数时,您需要引用_.变量而不是_T.country。这应该给您预期的结果。

答案 1 :(得分:0)

发布代码解决方案以供将来参考:

糟糕。使用相同的功能,还添加了“将字符串转换为字符串”的操作。

重构代码

["add", "_age",
   ["list", 20, 18, 14, 13, 22, 28, 12, 11, 12]
],
["add", "_country",
   ["list", "Norway", "Denmark", "Sweden"]
],
["add", "Country Abbreviation with mapping",
   ["map",
      ["case-eq", "_.", "Norway", "NO", "Sweden", "SE", "DK"], "_T._country"]
],
["add", "String Representation of int age list",
   ["map",
      ["if",
         ["gte", "_.", 18], "jedi", "padawan"], "_T._age"]
]