Skip to content

Commit 2250eba

Browse files
ArnoSendougm
ArnoSen
authored andcommitted
api: Add option to use a filter function as value of property.Match
Closes: #3785 Signed-off-by: ArnoSen <ArnoSen@noreply>
1 parent 0cb5102 commit 2250eba

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

property/match.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ func (m Match) Property(prop types.DynamicProperty) bool {
4242
return true
4343
}
4444

45+
if matchFunc, ok := match.(func(any) bool); ok {
46+
if matchFunc(prop.Val) {
47+
return true
48+
}
49+
}
50+
4551
ptype := reflect.TypeOf(prop.Val)
4652

4753
if strings.HasPrefix(ptype.Name(), "ArrayOf") {

property/match_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func TestMatchProperty(t *testing.T) {
15-
tests := []struct {
15+
for _, test := range []struct {
1616
key string
1717
val types.AnyType
1818
pass types.AnyType
@@ -33,9 +33,8 @@ func TestMatchProperty(t *testing.T) {
3333
{"float32s", float32(32.32), "32.32", "42.0"},
3434
{"float64", float64(64.64), float64(64.64), float64(42.0)},
3535
{"float64s", float64(64.64), "64.64", "42.0"},
36-
}
37-
38-
for _, test := range tests {
36+
{"matchFunc", "bar", func(s any) bool { return s.(string) == "bar" }, func(s any) bool { return s.(string) == "foo" }},
37+
} {
3938
p := types.DynamicProperty{Name: test.key, Val: test.val}
4039

4140
for match, value := range map[bool]types.AnyType{true: test.pass, false: test.fail} {

0 commit comments

Comments
 (0)