protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

I want to partially compare protos

Open hickford opened this issue 2 years ago • 12 comments

In C++ unit tests with GoogleTest, I can partially compare protos with Partially(EqualsProto(...)) (Google-internal docs, open-source fork).

only fields present in the expected protobuf are considered

Is there any similar library for Golang?

hickford avatar Apr 19 '22 15:04 hickford

protocmp is the recommended library to use for proto comparisons. I don't think the same matching functionality exists there though. Maybe create a feature request for that library?

znkr avatar Jun 02 '22 13:06 znkr

Maybe create a feature request for that library?

package https://pkg.go.dev/google.golang.org/protobuf/testing/protocmp links to repo https://github.com/protocolbuffers/protobuf-go but that repo has issues disabled and encourages opening issues here instead

The issue tracker for this project is currently located at golang/protobuf.

hickford avatar Jun 02 '22 13:06 hickford

Oups. I guess I should have known that :)

znkr avatar Jun 02 '22 14:06 znkr

Seems like something easy enough to extend out of protocmp.IgnoreDefaultScalars, literally just a copy-paste and delete maybe three lines.

puellanivis avatar Jun 02 '22 14:06 puellanivis

Mhmm, that sounds like it's not too much work indeed. I think the main difference would be that would have to work asymmetrically (at least that's how I understand how partial matching works in C++). I am not sure if that fits well into cmp. An alternative would be to create a filter based on fields set in the message to compare against. Something like protocmp.IgnoreUnsetFieldsIn(want)).

znkr avatar Jun 02 '22 14:06 znkr

We shouldn’t need to include a pointer to a “unset fields from.” Well, that is unless the Cmp is itself intentionally unordered (it does not recommend which is supposed to be the got or expected). In which case, we could also just have IgnoreFieldsUnsetInLeft() vs IgnoreFieldsUnsetInRight(). This would avoid the need for a duplicated reference to one of the parameters.

puellanivis avatar Jun 02 '22 15:06 puellanivis

Just checked: cmp.FilterValues, which I think is used underneath, documents that the provided filter function has to be symmetric.

znkr avatar Jun 02 '22 15:06 znkr

cmp.FilerPath also requires it to be symmetric. So, yeah. I think we would have to use a IgnoreUnsetFieldsFrom(want) form.

puellanivis avatar Jun 02 '22 15:06 puellanivis

Thanks for checking! We're not sure if and when we get to this, but we're definitely happy to review contributions. :)

znkr avatar Jun 13 '22 10:06 znkr

cmp.FilterPath also requires it to be symmetric. So, yeah. I think we would have to use a IgnoreUnsetFieldsFrom(want) form.

Correct, the cmp functionality needs to be symmetric, but it would be permissible to do something like:

cmp.Diff(x, y, protocmp.FilterTemplate(y))

where the comparison for x and y is symmetric, but what parts of the tree that is examine is determined based on the argument y passed to a hypothetical protocmp.FilterTemplate.

dsnet avatar Jun 13 '22 16:06 dsnet