Skip to content

Commit c72fe04

Browse files
ilyam8bep
authored andcommitted
feat: respect map HashIncludeMap
1 parent d83ece6 commit c72fe04

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

hashstructure.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,13 @@ func (w *walker) visit(v reflect.Value, opts *visitOpts) (uint64, error) {
206206

207207
case reflect.Map:
208208
var includeMap IncludableMap
209-
if opts != nil && opts.Struct != nil {
209+
var field string
210+
211+
if v, ok := v.Interface().(IncludableMap); ok {
212+
includeMap = v
213+
} else if opts != nil && opts.Struct != nil {
210214
if v, ok := opts.Struct.(IncludableMap); ok {
211-
includeMap = v
215+
includeMap, field = v, opts.StructField
212216
}
213217
}
214218

@@ -221,8 +225,7 @@ func (w *walker) visit(v reflect.Value, opts *visitOpts) (uint64, error) {
221225
k := iter.Key()
222226
v := iter.Value()
223227
if includeMap != nil {
224-
incl, err := includeMap.HashIncludeMap(
225-
opts.StructField, k.Interface(), v.Interface())
228+
incl, err := includeMap.HashIncludeMap(field, k.Interface(), v.Interface())
226229
if err != nil {
227230
return 0, err
228231
}

hashstructure_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,23 @@ func TestHash_includableMap(t *testing.T) {
576576
testIncludableMap{Map: map[string]string{"bar": "baz"}},
577577
false,
578578
},
579+
{
580+
testIncludableMapMap{"foo": "bar"},
581+
testIncludableMapMap{"foo": "bar"},
582+
true,
583+
},
584+
585+
{
586+
testIncludableMapMap{"foo": "bar", "ignore": "true"},
587+
testIncludableMapMap{"foo": "bar"},
588+
true,
589+
},
590+
591+
{
592+
testIncludableMapMap{"foo": "bar", "ignore": "true"},
593+
testIncludableMapMap{"bar": "baz"},
594+
false,
595+
},
579596
}
580597

581598
for _, tc := range cases {
@@ -759,3 +776,9 @@ func (t *testHashablePointer) Hash() (uint64, error) {
759776

760777
return 100, nil
761778
}
779+
780+
type testIncludableMapMap map[string]string
781+
782+
func (t testIncludableMapMap) HashIncludeMap(_ string, k, _ interface{}) (bool, error) {
783+
return k.(string) != "ignore", nil
784+
}

0 commit comments

Comments
 (0)