add protorequire.ProtoEqualIgnoreFields helper#9937
add protorequire.ProtoEqualIgnoreFields helper#9937fretz12 wants to merge 3 commits intotemporalio:mainfrom
Conversation
|
|
||
| // ProtoEqualIgnoreFields compares two proto messages for equality, ignoring the specified fields on the given message | ||
| // type. Fields are specified by their proto name (snake_case). | ||
| func ProtoEqualIgnoreFields(t require.TestingT, a proto.Message, b proto.Message, msgType proto.Message, fields ...protoreflect.Name) { |
There was a problem hiding this comment.
Food for thought.
There's some other interesting options we can potentially expose:
protocmp.SortRepeatedFields
protocmp.IgnoreEnums
cmpopts.EquateApproxTime
Also another approach is to make this a really generic function:
func ProtoEqualWithOptions(t assert.TestingT, a proto.Message, b proto.Message, opts ...cmp.Option) bool
and it can be used as such:
protorequire.ProtoEqualWithOptions(t, expected, actual, protocmp.IgnoreFields(&activitypb.ActivityExecutionInfo{}, "state_transition_count"), cmpopts.EquateApproxTime(5*time.Second), )
However, that leaks proto/cmp abstractions making the helper harder to use. I decided to make the helper focused on ignore fields, as that's our majority use case and the interface is very clean. I propose for other useful options, like EquateApproxTime, we create separate helpers. But open to thoughts
What changed?
Added ProtoEqualIgnoreFields to protoassert and protorequire packages. Refactored 3 call sites in standalone_activity_test.go to use the new helper.
Why?
Comparing proto messages while ignoring specific fields required a verbose 4-line pattern (cmp.Diff + protocmp.Transform() + protocmp.IgnoreFields() + require.Empty). The new helper provides a clean one-liner that's consistent with the existing protorequire.ProtoEqual API.
How did you test it?