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

feat: respect map HashIncludeMap interface #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions hashstructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,13 @@ func (w *walker) visit(v reflect.Value, opts *visitOpts) (uint64, error) {

case reflect.Map:
var includeMap IncludableMap
if opts != nil && opts.Struct != nil {
var field string

if v, ok := v.Interface().(IncludableMap); ok {
includeMap = v
} else if opts != nil && opts.Struct != nil {
if v, ok := opts.Struct.(IncludableMap); ok {
includeMap = v
includeMap, field = v, opts.StructField
}
}

Expand All @@ -218,8 +222,7 @@ func (w *walker) visit(v reflect.Value, opts *visitOpts) (uint64, error) {
for _, k := range v.MapKeys() {
v := v.MapIndex(k)
if includeMap != nil {
incl, err := includeMap.HashIncludeMap(
opts.StructField, k.Interface(), v.Interface())
incl, err := includeMap.HashIncludeMap(field, k.Interface(), v.Interface())
if err != nil {
return 0, err
}
Expand Down
23 changes: 23 additions & 0 deletions hashstructure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,23 @@ func TestHash_includableMap(t *testing.T) {
testIncludableMap{Map: map[string]string{"bar": "baz"}},
false,
},
{
testIncludableMapMap{"foo": "bar"},
testIncludableMapMap{"foo": "bar"},
true,
},

{
testIncludableMapMap{"foo": "bar", "ignore": "true"},
testIncludableMapMap{"foo": "bar"},
true,
},

{
testIncludableMapMap{"foo": "bar", "ignore": "true"},
testIncludableMapMap{"bar": "baz"},
false,
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -728,3 +745,9 @@ func (t *testHashablePointer) Hash() (uint64, error) {

return 100, nil
}

type testIncludableMapMap map[string]string

func (t testIncludableMapMap) HashIncludeMap(_ string, k, _ interface{}) (bool, error) {
return k.(string) != "ignore", nil
}