hashstructure icon indicating copy to clipboard operation
hashstructure copied to clipboard

Fallback hash methods if a struct is 100% ignored

Open jpalawaga opened this issue 3 years ago • 0 comments

motivation hashstructure has a slightly sharp edge in that if a struct contains only unexported fields, that it will hash to the same identity. This is undesirable because many structs contain only unexported fields, but semantically do not represent the same value to the user.

UseStringer is not preferable for two reasons:

  • we don't want to cast things to string if we can help it, because it could be unnecessary/expensive
  • UseStringer will not work correctly in some cases (e.g. if you have a map[string]interface{}, the values will not be stringed before hashing).

explanation this change keeps track of which fields are involved in computing a hash. If no fields from a struct are involved, then hashstructure will attempt to .MashalBinary() or .String() and hash that instead.

I put this behind a flag to maintain compatibility, but some default behaviour like this might be a good idea for a 3.x release.

note I apologize for the formatting updates--my IDE styled it automatically.

If this request is accepted, I will revert the styling changes.

jpalawaga avatar Oct 03 '22 19:10 jpalawaga