yudai / gojsondiff

Go JSON Diff
Other
536 stars 79 forks source link

When swapping the value ​​of textLeft and textRight, I get different results #36

Open TianZhH opened 4 years ago

TianZhH commented 4 years ago

This is my code:

package main

import (
    "encoding/json"
    "fmt"
    diff "github.com/yudai/gojsondiff"
    "github.com/yudai/gojsondiff/formatter"
)

func main() {
    textLeft := `{
    "name": "text_left",
    "strategy_list": [
        {
            "percent": 100,
            "whitelist": [
                "aabb"
            ],
            "name": "策略1",
            "stra_id": 11
        }
    ]
}`

    textRight := `{
    "business_id": 3,
    "name": "text_right",
    "desc": "text_right desc",
    "strategy_list": [
        {
            "percent": 50,
            "whitelist": null,
            "name": "stra_1",
            "stra_id": 1
        },
        {
            "percent": 50,
            "whitelist": null,
            "name": "stra_2",
            "stra_id": 2
        }
    ]
}`

    // textLeft, textRight = textRight, textLeft
    differ := diff.New()
    d, err := differ.Compare([]byte(textLeft), []byte(textRight))
    if err != nil {
        return
    }

    config := formatter.AsciiFormatterConfig{ShowArrayIndex: true, Coloring: true}

    var aJson map[string]interface{}
    _ = json.Unmarshal([]byte(textLeft), &aJson)
    f := formatter.NewAsciiFormatter(aJson, config)
    result, err := f.Format(d)
    if err != nil {
        return
    }

    println(fmt.Sprintf("result=%s", result))
}

The result is like this image

But when I swapped textLeft and textRight, the result became like this

// swap textLeft and textRight before diff.New()
textLeft, textRight = textRight, textLeft

image

I think the second result is crorrect. The first result is missing the original content of leftText