Skip to content

Commit e5d7817

Browse files
Googlertwifkak
authored andcommitted
Fix generic comparisons on protobuf messages
Generated protobuf messages contain internal data structures that general purpose comparison functions (e.g., reflect.DeepEqual, pretty.Compare, etc) do not properly compare. It is already the case today that these functions may report a difference when two messages are actually semantically equivalent. Fix all usages by either calling proto.Equal directly if the top-level types are themselves proto.Message, or by calling cmp.Equal with the cmp.Comparer(proto.Equal) option specified. This option teaches cmp to use proto.Equal anytime it encounters proto.Message types. PiperOrigin-RevId: 278904435
1 parent e73b0e2 commit e5d7817

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

transformer/transformer_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"testing"
88

9+
"github.com/golang/protobuf/proto"
910
rpb "github.com/ampproject/amppackager/transformer/request"
1011
"github.com/ampproject/amppackager/transformer/transformers"
1112
"github.com/google/go-cmp/cmp"
@@ -109,7 +110,7 @@ func TestPreloads(t *testing.T) {
109110
t.Fatalf("unexpected failure: %v", err)
110111
}
111112

112-
if diff := cmp.Diff(tc.expectedPreloads, metadata.Preloads); diff != "" {
113+
if diff := cmp.Diff(tc.expectedPreloads, metadata.Preloads, cmp.Comparer(proto.Equal)); diff != "" {
113114
t.Errorf("preloads differ (-want +got):\n%s", diff)
114115
}
115116
})
@@ -118,8 +119,8 @@ func TestPreloads(t *testing.T) {
118119

119120
func TestMaxAge(t *testing.T) {
120121
tcs := []struct {
121-
html string
122-
expectedMaxAgeSecs int32
122+
html string
123+
expectedMaxAgeSecs int32
123124
}{
124125
{
125126
// No amp-scripts; no constraints on signing duration.

0 commit comments

Comments
 (0)