-
Notifications
You must be signed in to change notification settings - Fork 3
/
searchutil.go
250 lines (210 loc) · 6.07 KB
/
searchutil.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2017 Keydap. All rights reserved.
// Licensed under the Apache License, Version 2.0, see LICENSE.
package base
import (
"fmt"
"regexp"
"sort"
"sparrow/schema"
"strings"
)
var allAttrsRegex = regexp.MustCompile(`(^\*(,)*)|(\s*,\s*\*\s*(,)*)`) // detect if wildcard is given for ALL attributes, NOT just for a complex attribute like 'name.*'
func SplitAttrCsv(csv string, rTypes ...*schema.ResourceType) (attrMap map[string]int, subAtPresent bool) {
attrMap = make(map[string]int)
csv = strings.TrimSpace(csv)
tokens := strings.Split(csv, ",")
if allAttrsRegex.MatchString(csv) {
for _, rt := range rTypes {
collectAllAtNames(rt.GetMainSchema(), attrMap, false)
for _, se := range rt.SchemaExtensions {
// MUST prepend the schema ID to resolve duplicate attribute names
collectAllAtNames(rt.GetSchema(se.Schema), attrMap, true)
}
}
return attrMap, false
}
outer:
for _, t := range tokens {
t = strings.TrimSpace(t)
if t == "." || strings.HasSuffix(t, ".") { // not a valid attribute name
continue
}
tLen := len(t)
if tLen == 0 {
continue
}
t = strings.ToLower(t)
colonPos := strings.LastIndex(t, ":")
var sc *schema.Schema
isCoreSchema := true
if colonPos > 0 {
urn := strings.ToLower(t[0:colonPos])
// the URN is case insensitive here, lookup the corresponding
// Schema ID from the given ResourceTypes
schemacheck:
for _, rt := range rTypes {
if urn == strings.ToLower(rt.Schema) {
// this is the core schema, we can skip the URN prefix
sc = rt.GetMainSchema()
break
} else {
for _, se := range rt.SchemaExtensions {
if urn == strings.ToLower(se.Schema) {
isCoreSchema = false
sc = rt.GetSchema(se.Schema)
break schemacheck
}
}
}
}
colonPos++
if colonPos >= tLen { // this is an invalid attribute, skip it
continue outer
}
t = t[colonPos:]
}
hasDot := strings.ContainsRune(t, '.')
hasStar := strings.ContainsRune(t, '*')
if hasDot && hasStar { // all the sub attributes of a complex attribute
dotPos := strings.IndexRune(t, '.')
t = t[0:dotPos]
if !isCoreSchema {
t = sc.Id + ":" + t
}
attrMap[t] = 1
} else if hasStar { // all the attributes of the schema with the urn
if sc != nil {
collectAllAtNames(sc, attrMap, !isCoreSchema)
subAtPresent = true
} else {
for _, rt := range rTypes {
// MUST prepend the schema ID to resolve duplicate attribute names
collectAllAtNames(rt.GetMainSchema(), attrMap, true)
for _, se := range rt.SchemaExtensions {
collectAllAtNames(rt.GetSchema(se.Schema), attrMap, true)
}
}
}
} else {
if !isCoreSchema {
t = sc.Id + ":" + t
}
attrMap[t] = 1 // 0 is the default value for non-existing keys, so set the value to 1
if hasDot {
subAtPresent = true
}
}
}
if len(attrMap) == 0 {
return nil, false
}
return attrMap, subAtPresent
}
func collectAllAtNames(sc *schema.Schema, attrMap map[string]int, prependSchemaId bool) {
if sc == nil {
return
}
for _, v := range sc.Attributes {
name := v.NormName
if prependSchemaId {
name = sc.Id + ":" + v.NormName
}
attrMap[name] = 1
}
}
// Converts the given list of attributes to AttributeParam and groups the sub-attributes under
// one parent if applicable.
// For example if "emails.type,emails.value" are requested then an AttributeParam with
// name "emails" will be created with two child attributes "type" and "value"
// This will make filtering the attributes easier
func ConvertToParamAttributes(attrMap map[string]int, subAtPresent bool) map[string]*AttributeParam {
var atpMap map[string]*AttributeParam
if subAtPresent {
tmp := make([]string, len(attrMap))
count := 0
for k, _ := range attrMap {
tmp[count] = k
count++
}
sort.Strings(tmp)
atpMap = make(map[string]*AttributeParam)
var prev *AttributeParam
for _, k := range tmp {
j := &AttributeParam{}
j.Name = k
colonPos := strings.LastIndex(k, ":")
if colonPos > 0 {
j.SchemaId = k[0:colonPos]
}
dotPos := strings.LastIndex(k, ".") // call LastIndex() to avoid the possible '.' char in URN
if dotPos > 0 && (dotPos > colonPos) { // to avoid splitting attribute names that have a '.' in the URN
if prev == nil || !strings.HasPrefix(k, prev.Name+".") {
j.Name = j.SchemaId + k[0:dotPos]
dotPos++
k = k[dotPos:]
j.SubAts = make(map[string]string)
j.SubAts[k] = k
} else if strings.HasPrefix(k, prev.Name+".") {
dotPos++
k = k[dotPos:]
// the below block makes sure to add sub-attributes only when
// the parent attribute itself is not requested, for example, "name.formatted, name.givenname"
// but if "name" is also present in the list then the entire "name" attribute should be listed
if prev.SubAts != nil {
prev.SubAts[k] = k
}
continue
}
}
atpMap[j.Name] = j
prev = j
}
} else {
atpMap = make(map[string]*AttributeParam, len(attrMap))
for k, _ := range attrMap {
j := &AttributeParam{}
j.Name = k
pos := strings.LastIndex(k, ":")
if pos > 0 {
j.SchemaId = k[0:pos]
}
atpMap[j.Name] = j
}
}
return atpMap
}
func FixSchemaUris(node *FilterNode, rTypes []*schema.ResourceType) error {
colonPos := strings.LastIndex(node.Name, ":")
if colonPos > 0 {
t := node.Name
urn := strings.ToLower(t[0:colonPos])
// the URN is case insensitive here, lookup the corresponding
// Schema ID from the given ResourceTypes
schemacheck:
for _, rt := range rTypes {
if urn == strings.ToLower(rt.Schema) {
// this is the core schema, we can skip the URN prefix
urn = ""
colonPos++
if colonPos >= len(t) { // this is an invalid attribute, skip it
return fmt.Errorf("Invalid attribute %s in filter ", node.Name)
}
} else {
for _, se := range rt.SchemaExtensions {
if urn == strings.ToLower(se.Schema) {
urn = se.Schema
break schemacheck
}
}
}
}
t = urn + t[colonPos:]
node.Name = t
}
if node.Children != nil {
for _, ch := range node.Children {
FixSchemaUris(ch, rTypes)
}
}
return nil
}