fix: accept interface implementation on pointer
matfax authored 5 years ago
Mitchell Hashimoto committed 3 years ago
80 | 80 |
// field implements fmt.Stringer
|
81 | 81 |
//
|
82 | 82 |
func Hash(v interface{}, opts *HashOptions) (uint64, error) {
|
|
83 |
if impl, ok := v.(Hashable); ok {
|
|
84 |
return impl.Hash(), nil
|
|
85 |
}
|
|
86 |
|
83 | 87 |
// Create default options
|
84 | 88 |
if opts == nil {
|
85 | 89 |
opts = &HashOptions{}
|
589 | 589 |
}{
|
590 | 590 |
{
|
591 | 591 |
testHashable{Value: "foo"},
|
|
592 |
&testHashablePointer{Value: "foo"},
|
|
593 |
true,
|
|
594 |
"",
|
|
595 |
},
|
|
596 |
|
|
597 |
{
|
|
598 |
testHashable{Value: "foo1"},
|
|
599 |
&testHashablePointer{Value: "foo2"},
|
|
600 |
true,
|
|
601 |
"",
|
|
602 |
},
|
|
603 |
{
|
592 | 604 |
testHashable{Value: "foo"},
|
593 | |
true,
|
|
605 |
&testHashablePointer{Value: "bar"},
|
|
606 |
false,
|
594 | 607 |
"",
|
595 | 608 |
},
|
596 | |
|
597 | |
{
|
598 | |
testHashable{Value: "foo1"},
|
599 | |
testHashable{Value: "foo2"},
|
600 | |
true,
|
601 | |
"",
|
602 | |
},
|
603 | |
{
|
604 | |
testHashable{Value: "foo"},
|
605 | |
testHashable{Value: "bar"},
|
606 | |
false,
|
607 | |
"",
|
608 | |
},
|
609 | 609 |
{
|
610 | 610 |
testHashable{Value: "nofoo"},
|
611 | |
testHashable{Value: "bar"},
|
|
611 |
&testHashablePointer{Value: "bar"},
|
612 | 612 |
true,
|
613 | 613 |
"",
|
614 | 614 |
},
|
|
695 | 695 |
|
696 | 696 |
return 100, nil
|
697 | 697 |
}
|
|
698 |
|
|
699 |
type testHashablePointer struct {
|
|
700 |
Value string
|
|
701 |
}
|
|
702 |
|
|
703 |
func (t *testHashablePointer) Hash() uint64 {
|
|
704 |
if strings.HasPrefix(t.Value, "foo") {
|
|
705 |
return 500
|
|
706 |
}
|
|
707 |
return 100
|
|
708 |
}
|