protobuf
protobuf copied to clipboard
I want to partially compare protos
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?
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?
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.
Oups. I guess I should have known that :)
Seems like something easy enough to extend out of protocmp.IgnoreDefaultScalars, literally just a copy-paste and delete maybe three lines.
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))
.
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.
Just checked: cmp.FilterValues, which I think is used underneath, documents that the provided filter function has to be symmetric.
cmp.FilerPath also requires it to be symmetric. So, yeah. I think we would have to use a IgnoreUnsetFieldsFrom(want)
form.
Thanks for checking! We're not sure if and when we get to this, but we're definitely happy to review contributions. :)
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
.