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

Provide API to programmatically interpret differences #92

Closed
deinspanjer opened this issue Jul 18, 2018 · 7 comments
Closed

Provide API to programmatically interpret differences #92

deinspanjer opened this issue Jul 18, 2018 · 7 comments

Comments

@deinspanjer
Copy link

deinspanjer commented Jul 18, 2018

I have config objects in a DSL that are described via a series of structs. As an example:
https://play.golang.org/p/NQfRr0iJy8s

Given two different instances of the Quiz struct, I'd like to be able to find out starting at any particular level, if there are differences.

If I used the cmp.Diff() method, I think I could parse out the syntax to determine what the actual changes, adds, and deletes are, but not only does this feel fragile, it is explicitly warned against in the docs.

So, is there a better way?

@dsnet
Copy link
Collaborator

dsnet commented Aug 4, 2018

It seems that what you're asking for is a programmatic way to interpret the diff. Is that correct?

@deinspanjer
Copy link
Author

deinspanjer commented Aug 6, 2018

I believe that sounds right. What my code is attempting to return is a set of results containing any adds, removes, or changes as discrete sets so that the consumer of this result object can perform whatever operations are necessary upon the objects that had those changes.

@nhooyr
Copy link

nhooyr commented Feb 8, 2019

This would be really nice for structured logging of diffs as well.

@dsnet
Copy link
Collaborator

dsnet commented Feb 17, 2019

Would API like the following suffice?

// Reporter wraps the provided reporter as an Option. 
func Reporter(interface {
	// PushStep is called when a tree-traversal operation is performed
	// and provides the sub-values of x and y after applying the operation.
	// The PathStep is valid until the step is popped, while the reflect.Values
	// are valid while the entire tree is still being traversed.
	//
	// Equal and Diff always call PushStep at the start to provide an
	// operation-less PathStep used to report the root values.
	PushStep(ps PathStep, x, y reflect.Value)

	// Report is called at exactly once on leaf nodes to report whether the
	// comparison identified the node as equal, unequal, or ignored.
	// A leaf node is one that is immediately preceded by and followed by
	// a pair of PushStep and PopStep calls.
	Report(ReportFlags)

	// PopStep ascends back up the value tree.
	// There is always a matching pop call for every push call.
	PopStep()
}) Option

// ReportFlags is bit-set of reporting information about the comparison.
type ReportFlags uint

const (
	// ReportIgnored reports whether the node was ignored.
	ReportIgnored ReportFlags = iota
	// ReportEqual reports whether the node is equal.
	ReportEqual
	// reportUnequal reports whether the node is not equal.
	ReportUnequal
)

This API allows the user to pass in their own reporter and get a structured representation of the diff. The internal default reporter is being switched to use an API like this.

See report.go for an example of an implementation of this API that only report differences similar to what it does today.

@dsnet dsnet changed the title Can this package be used to determine adds, changes, and removes in a slice of structs? Provide API to programmatically interpret differences Feb 19, 2019
@SVenky
Copy link

SVenky commented Mar 7, 2019

@dsnet : Is this already available? If so do we have an example or test code to refer on how to use it? if not available yet, any timeline of when we can expect the same. Thanks a lot!

@dsnet
Copy link
Collaborator

dsnet commented Mar 7, 2019

It's here at #123. I wanted to seriously think through the implications of the final API before merging, but anticipate that it will happen next week.

See here for an example: https://github.com/google/go-cmp/pull/123/files#diff-723c10615c952a994128a9cf62e6ca49

@dsnet
Copy link
Collaborator

dsnet commented Mar 12, 2019

Fixed by #123.

@dsnet dsnet closed this as completed Mar 12, 2019
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

4 participants