Skip to content

Commit

Permalink
Diff tracker bombed out when it reached a nil pointer - fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Raede committed May 6, 2015
1 parent 427d480 commit 4eefa4a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 2 additions & 4 deletions difftracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ func GetChangedFields(struct1 interface{}, struct2 interface{}, useBson bool) ([

var childDiffs []string
var err error
// Make sure they aren't zero-value
// Make sure they aren't zero-value. Skip if so
if isNilOrInvalid(field1) && isNilOrInvalid(field2) {
return diffs, nil
continue
} else if isNilOrInvalid(field1) || isNilOrInvalid(field2) {
childDiffs = getFields(childType)

Expand Down Expand Up @@ -229,10 +229,8 @@ func GetChangedFields(struct1 interface{}, struct2 interface{}, useBson bool) ([
}
} else {

// fmt.Println("Comparing ", fieldName, fmt.Sprint(field1.Interface()), fmt.Sprint(field2.Interface()))
if fmt.Sprint(field1.Interface()) != fmt.Sprint(field2.Interface()) {
diffs = append(diffs, fieldName)
// fmt.Println("Changed!")
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions difftracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ func TestDiffTracker(t *testing.T) {
So(diffs[2], ShouldEqual, "BarVal")
})

Convey("should get changed fields when comparing two structs with nil pointers", func() {
foobar1 := &FooBarChangeTest{
BarVal: "bar",
}

foobar2 := &FooBarChangeTest{
BarVal: "BAR",
}

diffs, err := GetChangedFields(foobar1, foobar2, false)
So(err, ShouldEqual, nil)
So(len(diffs), ShouldEqual, 1)
So(diffs[0], ShouldEqual, "BarVal")
})

Convey("should get fields modified since difftracker reset", func() {
foo1 := &FooChangeTest{
StringVal: "foo",
Expand Down

0 comments on commit 4eefa4a

Please sign in to comment.