Skip to content

Commit

Permalink
go diff: don't panic on nil array value
Browse files Browse the repository at this point in the history
  • Loading branch information
berfarah committed Oct 10, 2018
1 parent d409814 commit c490e3b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
3 changes: 3 additions & 0 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ func diffMap(old map[string]interface{}, newAny interface{}) interface{} {

// reoderKey returns the key to use for a
func reorderKey(i interface{}) interface{} {
if i == nil {
return i
}
if object, ok := i.(map[string]interface{}); ok {
if key, ok := object["__key"]; ok {
return key
Expand Down
13 changes: 8 additions & 5 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/samsarahq/thunder/diff"
"github.com/samsarahq/thunder/internal"
"github.com/stretchr/testify/assert"
)

func TestDiffListString(t *testing.T) {
Expand Down Expand Up @@ -125,30 +126,32 @@ func TestDiffListOrder(t *testing.T) {
map[string]interface{}{"__key": "1"},
map[string]interface{}{"__key": "2"},
map[string]interface{}{"__key": "3"},
nil,
}, []interface{}{
nil,
map[string]interface{}{"__key": "3"},
map[string]interface{}{"__key": "-1"},
map[string]interface{}{"__key": "0"},
map[string]interface{}{"__key": "1"},
map[string]interface{}{"__key": "4"},
})

if !reflect.DeepEqual(internal.AsJSON(d), internal.ParseJSON(`
{"$": [3, -1, [0, 2], -1], "1": [{}], "4": [{}]}
`)) {
t.Error("bad reorder")
}
assert.Equal(t, internal.ParseJSON(`
{"$": [4, 3, -1, [0, 2], -1], "2": [{}], "5": [{}]}
`), internal.AsJSON(d))

d = diff.Diff([]interface{}{
map[string]interface{}{"__key": "0"},
map[string]interface{}{"__key": "1"},
map[string]interface{}{"__key": "2"},
map[string]interface{}{"__key": "3"},
nil,
}, []interface{}{
map[string]interface{}{"__key": "0"},
map[string]interface{}{"__key": "1"},
map[string]interface{}{"__key": "2"},
map[string]interface{}{"__key": "3"},
nil,
})
if d != nil {
t.Error("bad identical")
Expand Down

0 comments on commit c490e3b

Please sign in to comment.