Home  >  Article  >  Backend Development  >  Go Kazaam transformation returns unexpected results

Go Kazaam transformation returns unexpected results

WBOY
WBOYforward
2024-02-14 12:09:081248browse

Go Kazaam 转换返回意外结果

php editor Youzi recently discovered that a new conversion tool Go Kazaam had some unexpected results when users used it. This tool was originally designed to help developers quickly convert code and improve development efficiency. However, some users reported that unexpected problems occurred during use. This has attracted the attention of developers and users. In this article, we will discuss this issue and provide solutions.

Question content

I use kazaam module to define json conversion. https://github.com/qntfy/kazaam

These are my rules:

"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"
                ]
            }
        }
    ]

Apply to this json:

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

The result is like this:

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

So the first operation works, the second operation returns null_null, and the third operation does not appear.

Solution

These rules will be applied in sequence. The results produced by the first rule will serve as input to the second rule, and so on, they are chained together. So your first conversion results in an object:

{
  "Shift": "example1 value"
}

When the above is used as input to the second action - you will get a null value because the field you are referencing - does not exist.
You can try changing the first rule to:

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

See how it works.

The above is the detailed content of Go Kazaam transformation returns unexpected results. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete