Skip to content
This repository has been archived by the owner on May 17, 2023. It is now read-only.

Commit

Permalink
Make ObjectHash-Proto compatible with the latest protoc-gen-go.
Browse files Browse the repository at this point in the history
This is done by ignoring the additional internal non-content fields that
were added in the latest version.

Specifically, the following fields are now ignored:

- "XXX_NoUnkeyedLiteral": This is an empty struct whose presence is used
  by the proto library to force users to use keyed literals to ensure
  forward compatible usage of messages.

- "XXX_sizecache": This is used in serialization. Its value is
  irrelevant to the actual content of the proto and should not be used
  in comparisons or for generating hashes.
  • Loading branch information
maljub01 committed May 15, 2018
1 parent de1e7c2 commit 44bb650
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions detection.go
Expand Up @@ -119,6 +119,21 @@ func isAProto2BytesField(v reflect.Value, sf reflect.StructField) bool {
return true
}

// isContentIndependentField checks if a proto field is not related to its
// contents.
//
// Content independent fields include:
// - "XXX_NoUnkeyedLiteral": This is an empty struct whose presence is used by
// the proto library to force users to use keyed literals to ensure forward
// compatible usage of messages.
// - "XXX_sizecache": This is used in serialization. Its value is irrelevant to
// the actual content of the proto and should not be used in comparisons or
// for generating hashes.
func isContentIndependentField(v reflect.Value, sf reflect.StructField) bool {
name := sf.Name
return name == "XXX_NoUnkeyedLiteral" || name == "XXX_sizecache"
}

// isUnset checks if the proto field has not been set.
//
// This also includes empty proto3 scalar values.
Expand Down
5 changes: 5 additions & 0 deletions object_hasher.go
Expand Up @@ -163,6 +163,11 @@ func (hasher *objectHasher) hashStruct(sv reflect.Value) ([]byte, error) {
v := sv.Field(i)
sf := st.Field(i)

// Ignore content-independent "XXX_" fields.
if isContentIndependentField(v, sf) {
continue
}

// Ignore unset fields (and empty proto3 scalar fields).
unset, err := isUnset(v, sf)
if err != nil {
Expand Down

0 comments on commit 44bb650

Please sign in to comment.