Go Kazaam转换返回意外结果

23c0lvtd  于 5个月前  发布在  Go
关注(0)|答案(1)|浏览(54)

我使用kazaam模块来定义json转换。https://github.com/qntfy/kazaam
这是我的规则:

"rules": [
        {
            "operation": "shift",
            "spec": {
                "Shift": "instruction.context.example1"
            }
        },
        {
            "operation": "concat",
            "spec": {
                "sources": [
                    {
                        "path": "instruction.context.example1"
                    },
                    {
                        "path": "instruction.context.example2"
                    }
                ],
                "targetPath": "Concat",
                "delim": "_"
            }
        },
        {
            "operation": "coalesce",
            "spec": {
                "Coalesce": [
                    "instruction.context.example3[0].id"
                ]
            }
        }
    ]

字符串
适用于此JSON:

{
    "instruction": {
        "context": {
            "example1": "example1 value",
            "example2": "example2 value",
            "example3": [
                {
                    "id": "example3_1_value"
                },
                {
                    "id": "example3_2_value"
                },
                {
                    "id": "example3_3_value"
                }
            ]
        }
    }
}


结果是这样的:

{
    "Shift": "example1 value",
    "Concat": "null_null"
}


所以第一个操作有效,第二个返回null_null,第三个不出现。

avwztpqn

avwztpqn1#

规则一个接一个地被应用。你的第一个规则产生的东西作为输入被输入到你的第二个规则,依此类推,它们被链接起来。所以,你的第一个转换产生了一个对象:

{
  "Shift": "example1 value"
}

字符串
当上面的内容作为第二个操作的输入时,你得到的是null值,因为你引用的字段不存在。
您可以尝试将第一条规则更改为:

{
            "operation": "shift",
            "spec": {
                "Shift": "instruction.context.example1",
                "instruction.context.example1": "instruction.context.example1",
                "instruction.context.example2": "instruction.context.example2"
            }
        },


看看它的效果

相关问题