Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Fallback hash methods if a struct is 100% ignored #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jpalawaga
Copy link

@jpalawaga jpalawaga commented Oct 3, 2022

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 jpalawaga changed the title Fallback to stringer if a struct is 100% ignored Fallback hash methods if a struct is 100% ignored Oct 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant