-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathop_binary.go
More file actions
56 lines (54 loc) · 1.62 KB
/
op_binary.go
File metadata and controls
56 lines (54 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package filter
import (
"fmt"
"github.com/scim2/filter-parser/v2"
"strings"
)
// cmpBinary returns a compare function that compares a given value to the reference string based on the given attribute
// expression and binary attribute. The filter operators gt, lt, ge and le are not supported on binary attributes.
//
// Expects a binary attribute. Will panic on unknown filter operator.
// Known operators: eq, ne, co, sw, ew, gt, lt, ge and le.
func cmpBinary(e *filter.AttributeExpression, ref string) (func(any) error, error) {
switch op := e.Operator; op {
case filter.EQ:
return cmpStr(ref, true, func(v, ref string) error {
if v != ref {
return fmt.Errorf("%s is not equal to %s", v, ref)
}
return nil
})
case filter.NE:
return cmpStr(ref, true, func(v, ref string) error {
if v == ref {
return fmt.Errorf("%s is equal to %s", v, ref)
}
return nil
})
case filter.CO:
return cmpStr(ref, true, func(v, ref string) error {
if !strings.Contains(v, ref) {
return fmt.Errorf("%s does not contain %s", v, ref)
}
return nil
})
case filter.SW:
return cmpStr(ref, true, func(v, ref string) error {
if !strings.HasPrefix(v, ref) {
return fmt.Errorf("%s does not start with %s", v, ref)
}
return nil
})
case filter.EW:
return cmpStr(ref, true, func(v, ref string) error {
if !strings.HasSuffix(v, ref) {
return fmt.Errorf("%s does not end with %s", v, ref)
}
return nil
})
case filter.GT, filter.LT, filter.GE, filter.LE:
return nil, fmt.Errorf("can not use op %q on binary values", op)
default:
panic(fmt.Sprintf("unknown operator in expression: %s", e))
}
}