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

question: any way to prevent copying lock fields in proto comparison? #280

Closed
OliverCardoza opened this issue Nov 8, 2021 · 2 comments
Closed

Comments

@OliverCardoza
Copy link

I'm comparing protos from the github.com/google/fhir library. For example:
https://raw.githubusercontent.com/google/fhir/master/go/proto/google/fhir/proto/r4/core/datatypes_go_proto/datatypes.pb.go

These protos include a MessageState field with the DoNotCopy pragma (context).

I have unit test code comparing protos similar to the following:

import (
  "testing"

  dtpb "github.com/google/fhir/go/proto/google/fhir/proto/r4/core/datatypes_go_proto"
  "github.com/google/go-cmp/cmp"
  "google.golang.org/protobuf/testing/protocmp"
)

func TestMyFunc(t *testing.T) {
  expected := &dtpb.DateTime{...}
  actual := MyFunc(inputs...)
  if diff := cmp.Diff(*expected, *actual, protocmp.Transform()); diff != "" {
    t.Errorf("unexpected proto diff:\n%v", diff)
  } 
}

This builds and tests successfully but go vet complains:

[$CALLSITE] call of cmp.Diff copies lock value:
github.com/google/fhir/go/proto/google/fhir/proto/r4/core/datatypes_go_proto.DateTime
contains google.golang.org/protobuf/internal/impl.MessageState contains sync.Mutex

I've tried adding ignore/filter options to cmp.Diff but it appears that the copy may always occur and ignore just silences the field/message diff in the output.

I feel like I'm missing something. Any tips appreciated!

@dsnet
Copy link
Collaborator

dsnet commented Nov 8, 2021

Hi, I suspect that the problem is to pass the reference to the protobuf, rather than a shallow copy of it:

- if diff := cmp.Diff(*expected, *actual, protocmp.Transform()); diff != "" {
+ if diff := cmp.Diff(expected, actual, protocmp.Transform()); diff != "" {

@OliverCardoza
Copy link
Author

🤦‍♂️ I did not make the connection that pointer dereferencing creates shallow copies. thank you!

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

2 participants