Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
TianZhH opened this issue May 28, 2020 · 0 comments

Comments

@TianZhH
Copy link

TianZhH commented May 28, 2020

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

@TianZhH TianZhH changed the title I get different results when I swap the position of the textLeft and textRight When swapping the value ​​of textLeft and textRight, I get different results May 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant