Skip to content

Commit

Permalink
feat: respect map HashIncludeMap
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 authored and bep committed Jan 13, 2025
1 parent d83ece6 commit c72fe04
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
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 @@ -221,8 +225,7 @@ func (w *walker) visit(v reflect.Value, opts *visitOpts) (uint64, error) {
k := iter.Key()
v := iter.Value()
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 @@ -759,3 +776,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
}

0 comments on commit c72fe04

Please sign in to comment.