yudai/gojsondiff

Confusing Array Diff Output

blaskovicz opened this issue · 1 comments

Given two arrays of objects, if the objects appear in a different order, reflect.DeepEqual(expected, actual) will return false, whereas differ.CompareArrays(expected, actual) will contain an unexpected diff.

Reproducer:

package main

import (
  "fmt"

  "github.com/yudai/gojsondiff"
  "github.com/yudai/gojsondiff/formatter"
)

func main() {
  a := map[string]interface{}{"key": "bobby", "val": "tables", "foo": false}
  b := map[string]interface{}{"key": "how", "val": "outage", "foo": true}
  c := map[string]interface{}{"key": "stuffs", "val": "sample", "foo": true}

  expected := []interface{}{a, b, c}
  //actual := []interface{}{a, b, c}
  actual := []interface{}{b, c, a}

  differ := gojsondiff.New()
  diff := differ.CompareArrays(expected, actual)
  //diff := differ.CompareObjects(map[string]interface{}{"_": expected}, map[string]interface{}{"_": actual})
  //formatter := formatter.NewDeltaFormatter()
  formatter := formatter.NewAsciiFormatter(expected, formatter.AsciiFormatterConfig{ShowArrayIndex: true})
  str, err := formatter.Format(diff)

  if err != nil {
    panic(err)
  }
  fmt.Printf("%s\n", str)
}

Diff Results:

$ go run diff.go
 [
   0: {
     "foo": false,
     "key": "bobby",
     "val": "tables"
   },
   1: {
     "foo": true,
     "key": "how",
     "val": "outage"
   },
 ]

Diff Results from jsondiff.com:
image

yudai commented

@blaskovicz Thank you for the report.

It looks a bug. I'll take a look more when I have time.