-
Notifications
You must be signed in to change notification settings - Fork 3
/
matches.go
151 lines (146 loc) · 4.31 KB
/
matches.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"log"
"regexp"
"strings"
"github.com/Jeffail/gabs"
"github.com/lor00x/goldap/message"
)
func matchesFilterAnd(node *gabs.Container, f message.FilterAnd) bool {
//log.Printf("& filter %+v", f)
for _, filter := range f {
if !matches(node, filter) {
return false
}
}
return true
}
func matchesFilterOr(node *gabs.Container, f message.FilterOr) bool {
//log.Printf("| filter %+v", f)
for _, filter := range f {
if matches(node, filter) {
return true
}
}
return false
}
func matchesFilterNot(node *gabs.Container, f message.FilterNot) bool {
log.Printf("! filter %+v NEEDS IMPLEMENTING", f) // TODO not yet implemented
return false
}
func matchesFilterEqualityMatch(node *gabs.Container, f message.FilterEqualityMatch) bool {
n := node.Search(strings.ToLower(string(f.AttributeDesc())))
// is it an array?
children, err := n.Children()
if err != nil {
// check single value
if n.Data() == string(f.AssertionValue()) {
log.Printf("= filter %+v matches %+v value %+v", f, node, n)
return true
}
} else {
// check all elements
for _, value := range children {
if value.Data() == string(f.AssertionValue()) {
log.Printf("= filter %+v matches %+v value %+v in %+v", f, node, value, children)
return true
}
}
}
log.Printf("= filter %+v does not match %+v values %+v", f, node, n)
return false
}
func matchesFilterGreaterOrEqual(node *gabs.Container, f message.FilterGreaterOrEqual) bool {
log.Printf(">= filter %+v NEEDS IMPLEMENTING", f) // TODO not yet implemented
return false
}
func matchesFilterLessOrEqual(node *gabs.Container, f message.FilterLessOrEqual) bool {
log.Printf("<= filter %+v NEEDS IMPLEMENTING", f) // TODO not yet implemented
return false
}
func matchesFilterPresent(node *gabs.Container, f message.FilterPresent) bool {
if node.Search(strings.ToLower(string(f))) != nil {
log.Printf("* filter %+v matches %+v", f, node)
return true
}
log.Printf("* filter %+v does not match %+v", f, node)
return false
}
func matchesFilterApproxMatch(node *gabs.Container, f message.FilterApproxMatch) bool {
log.Printf("~ filter %+v NEEDS IMPLEMENTING", f) // TODO not yet implemented
return false
}
func matchesFilterSubstrings(node *gabs.Container, f message.FilterSubstrings) bool {
filters := "S"
search := "^"
for _, fs := range f.Substrings() {
switch fsv := fs.(type) {
case message.SubstringInitial:
filters += "I"
search += string(fsv) + "*"
case message.SubstringAny:
filters += "A"
search += "*" + string(fsv) + "*"
case message.SubstringFinal:
filters += "F"
search += "*" + string(fsv)
}
}
search += "$"
search = strings.Replace(strings.Replace(search, "**", "*", -1), "*", ".*", -1)
log.Printf("%s filter %+v checking %+v with regex=%s", filters, f, node, search)
re := regexp.MustCompile(search)
n := node.Search(strings.ToLower(string(f.Type_())))
// is it an array?
children, err := n.Children()
if err != nil {
// check single value
value := n.Data()
if value != nil && re.MatchString(value.(string)) {
log.Printf("%+v matches", value)
return true
}
} else {
// check all elements
for _, child := range children {
value := child.Data()
if value != nil && re.MatchString(value.(string)) {
log.Printf("child %+v matches", value)
return true
}
}
}
log.Printf("does not match")
return false
}
func matchesFilterFilterExtensibleMatch(node *gabs.Container, f message.FilterExtensibleMatch) bool {
log.Printf("E filter %+v", f)
return false
}
func matches(node *gabs.Container, f message.Filter) bool {
switch f := f.(type) {
case message.FilterAnd:
return matchesFilterAnd(node, f)
case message.FilterOr:
return matchesFilterOr(node, f)
case message.FilterNot:
return matchesFilterNot(node, f)
case message.FilterEqualityMatch:
return matchesFilterEqualityMatch(node, f)
case message.FilterGreaterOrEqual:
return matchesFilterGreaterOrEqual(node, f)
case message.FilterLessOrEqual:
return matchesFilterLessOrEqual(node, f)
case message.FilterPresent:
return matchesFilterPresent(node, f)
case message.FilterApproxMatch:
return matchesFilterApproxMatch(node, f)
case message.FilterSubstrings:
return matchesFilterSubstrings(node, f)
case message.FilterExtensibleMatch:
return matchesFilterFilterExtensibleMatch(node, f)
default:
log.Printf("Unknown filter %+v", f)
}
return false
}