Go Kazaam transformation returns unexpected results

WBOY
Release: 2024-02-14 12:09:08
forward
1254 people have browsed it

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"
                ]
            }
        }
    ]
Copy after login

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"
                }
            ]
        }
    }
}
Copy after login

The result is like this:

{
    "Shift": "example1 value",
    "Concat": "null_null"
}
Copy after login

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"
}
Copy after login

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"
            }
        },
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template