-
Notifications
You must be signed in to change notification settings - Fork 208
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
Comments
It seems that what you're asking for is a programmatic way to interpret the diff. Is that correct? |
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. |
This would be really nice for structured logging of diffs as well. |
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 : 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! |
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 |
Fixed by #123. |
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?
The text was updated successfully, but these errors were encountered: