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

Support cyclic types #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type Differ struct {
FlattenEmbeddedStructs bool
ConvertCompatibleTypes bool
Filter FilterFunc
pointersSeen map[uintptr]map[uintptr]struct{}
}

// Changelog stores a list of changed items
Expand Down Expand Up @@ -127,6 +128,7 @@ func NewDiffer(opts ...func(d *Differ) error) (*Differ, error) {
d := Differ{
TagName: "diff",
DiscardParent: false,
pointersSeen: make(map[uintptr]map[uintptr]struct{}),
}

for _, opt := range opts {
Expand Down Expand Up @@ -222,7 +224,6 @@ func (d *Differ) Diff(a, b interface{}) (Changelog, error) {
}

func (d *Differ) diff(path []string, a, b reflect.Value, parent interface{}) error {

//look and see if we need to discard the parent
if parent != nil {
if d.DiscardParent || reflect.TypeOf(parent).Kind() != reflect.Struct {
Expand Down
20 changes: 20 additions & 0 deletions diff_pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ func (d *Differ) diffPtr(path []string, a, b reflect.Value, parent interface{})
return nil
}

// If two pointers have already been compared, assume they have no changes
// This mirrors how reflect.DeepEqual works, and guarantees termination
aSeen, ok := d.pointersSeen[a.Pointer()]
if !ok {
aSeen = make(map[uintptr]struct{})
d.pointersSeen[a.Pointer()] = aSeen
}
bSeen, ok := d.pointersSeen[b.Pointer()]
if !ok {
bSeen = make(map[uintptr]struct{})
d.pointersSeen[b.Pointer()] = bSeen
}
_, aok := aSeen[b.Pointer()]
_, bok := aSeen[b.Pointer()]
if aok || bok {
return nil
}
aSeen[b.Pointer()] = struct{}{}
bSeen[a.Pointer()] = struct{}{}

return d.diff(path, reflect.Indirect(a), reflect.Indirect(b), parent)
}

Expand Down
1 change: 1 addition & 0 deletions diff_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (st *sliceTracker) has(s, v reflect.Value, d *Differ) bool {
var nd Differ
nd.Filter = d.Filter
nd.customValueDiffers = d.customValueDiffers
nd.pointersSeen = d.pointersSeen

err := nd.diff([]string{}, x, v, nil)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions diff_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (d *Differ) structValues(t string, path []string, a reflect.Value) error {
var nd Differ
nd.Filter = d.Filter
nd.customValueDiffers = d.customValueDiffers
nd.pointersSeen = d.pointersSeen

if t != CREATE && t != DELETE {
return ErrInvalidChangeType
Expand Down