-
Notifications
You must be signed in to change notification settings - Fork 302
/
fieldmask_utils.go
358 lines (327 loc) · 11.3 KB
/
fieldmask_utils.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ttnpb
import (
"strings"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)
// FieldMask returns a FieldMask with the given paths.
func FieldMask(paths ...string) *fieldmaskpb.FieldMask {
return &fieldmaskpb.FieldMask{Paths: paths}
}
// TopLevelFields returns the unique top level fields of the given paths.
func TopLevelFields(paths []string) []string {
seen := make(map[string]struct{}, len(paths))
out := make([]string, 0, len(paths))
for _, path := range paths {
parts := strings.SplitN(path, ".", 2)
if _, ok := seen[parts[0]]; ok {
continue
}
seen[parts[0]] = struct{}{}
out = append(out, parts[0])
}
return out
}
// BottomLevelFields returns the unique bottom level fields of the given paths.
func BottomLevelFields(paths []string) []string {
seen := make(map[string]struct{}, len(paths))
for _, path := range paths {
prefix := path
if i := strings.LastIndex(prefix, "."); i >= 0 {
prefix = prefix[:i]
}
if _, ok := seen[prefix]; ok {
delete(seen, prefix)
}
seen[path] = struct{}{}
}
out := make([]string, 0, len(seen))
for k := range seen {
out = append(out, k)
}
return out
}
// HasOnlyAllowedFields returns whether the given requested paths only contains paths that are allowed.
// The requested fields (i.e. `a.b`) may be of a lower level than the allowed path (i.e. `a`).
func HasOnlyAllowedFields(requested []string, allowed ...string) bool {
nextRequested:
for _, requested := range requested {
for _, allowed := range allowed {
if requested == allowed || strings.HasPrefix(requested, allowed+".") {
continue nextRequested
}
}
return false
}
return true
}
// HasAnyField returns whether the given requested paths contain any of the given fields.
// The requested fields (i.e. `a.b`) may be of a higher level than the search path (i.e. `a.b.c`).
//
// Note that this function may have unexpected results when non bottom search fields are used,
// as HasAnyField([]string{"a.b"}, "a") is false.
//
// If all possibilities are `[a, a.b, a.c]`, and we have `[a.b]`, then requesting `[a]`
// should be false because if it would be true, then `a.c` can be expected.
func HasAnyField(requested []string, search ...string) bool {
for _, requested := range requested {
for _, search := range search {
if requested == search || strings.HasPrefix(search, requested+".") {
return true
}
}
}
return false
}
// FlattenPaths flattens the paths by the given paths to flatten.
// When paths contains `a.b.c` and flatten contains `a.b`, the result will be `a.b`.
func FlattenPaths(paths, flatten []string) []string {
res := make([]string, 0, len(paths))
flattened := make(map[string]bool)
nextPath:
for _, path := range paths {
var found bool
for _, flatten := range flatten {
if flatten == path || strings.HasPrefix(path, flatten+".") {
if !flattened[flatten] {
res = append(res, flatten)
flattened[flatten] = true
}
continue nextPath
}
}
if !found {
res = append(res, path)
}
}
return res
}
// NonZeroFields returns the fields which are not zero in the provided message.
func NonZeroFields(msg interface{ FieldIsZero(string) bool }, fields ...string) []string {
nonZeroFields := make([]string, 0, len(fields))
for _, field := range fields {
if !msg.FieldIsZero(field) {
nonZeroFields = append(nonZeroFields, field)
}
}
return nonZeroFields
}
var errMissingField = errors.Define("missing_field", "field `{field}` is missing")
// RequireFields returns nil if the given requested paths contain all of the given fields and error otherwise.
// The requested fields (i.e. `a.b`) may be of a higher level than the search path (i.e. `a.b.c`).
func RequireFields(requested []string, search ...string) error {
for _, s := range search {
if !HasAnyField(requested, s) {
return errMissingField.WithAttributes("field", s)
}
}
return nil
}
var errProhibitedField = errors.Define("prohibited_field", "field `{field}` is prohibited")
// ProhibitFields returns nil if the given requested paths contain none of the given fields and error otherwise.
// The requested fields (i.e. `a.b`) may be of a higher level than the search path (i.e. `a.b.c`).
func ProhibitFields(requested []string, search ...string) error {
for _, s := range search {
if HasAnyField(requested, s) {
return errProhibitedField.WithAttributes("field", s)
}
}
return nil
}
// ContainsField returns true if the given paths contains the field path.
func ContainsField(path string, allowedPaths []string) bool {
for _, allowedPath := range allowedPaths {
if path == allowedPath {
return true
}
}
return false
}
// AllowedFields returns the paths from the given paths that are in the allowed paths.
func AllowedFields(paths, allowedPaths []string) []string {
selectedPaths := make([]string, 0, len(paths))
for _, path := range paths {
if ContainsField(path, allowedPaths) {
selectedPaths = append(selectedPaths, path)
continue
}
}
return selectedPaths
}
// AllowedBottomLevelFields returns the bottom level paths from the given paths that are in the allowed paths.
func AllowedBottomLevelFields(paths, allowedPaths []string) []string {
allowedPaths = BottomLevelFields(allowedPaths)
selectedPaths := make([]string, 0, len(allowedPaths))
outer:
for _, allowedPath := range allowedPaths {
for _, path := range paths {
if allowedPath == path || strings.HasPrefix(allowedPath, path+".") {
selectedPaths = append(selectedPaths, allowedPath)
continue outer
}
}
}
return selectedPaths
}
// AllowedReachableBottomLevelFields returns the reachable bottom level paths from the given paths that are in the allowed paths.
// Reachability in this context means that all of the intermediary paths between the given paths and the bottom level paths
// are not zero. Using only reachable paths ensures that no redundant bottom level paths are included.
func AllowedReachableBottomLevelFields(paths, allowedPaths []string, isZero func(string) bool) []string {
nonZeroAllowedPaths := make([]string, 0, len(allowedPaths))
for _, allowedPath := range allowedPaths {
if isZero(allowedPath) {
continue
}
nonZeroAllowedPaths = append(nonZeroAllowedPaths, allowedPath)
}
return AllowedBottomLevelFields(paths, nonZeroAllowedPaths)
}
// ExcludeFields returns the given paths without the given search paths to exclude.
func ExcludeFields(paths []string, excludePaths ...string) []string {
if len(paths) == 0 {
return paths
}
excluded := make([]string, 0, len(paths))
outer:
for _, path := range paths {
for _, excludePath := range excludePaths {
if path == excludePath || strings.HasPrefix(path, excludePath+".") {
continue outer
}
}
excluded = append(excluded, path)
}
return excluded
}
// ExcludeSubFields returns the given paths without sub-fields of excludePaths.
func ExcludeSubFields(paths []string, excludePaths ...string) []string {
if len(paths) == 0 {
return paths
}
excluded := make([]string, 0, len(paths))
outer:
for _, path := range paths {
for _, excludePath := range excludePaths {
if strings.HasPrefix(path, excludePath+".") {
continue outer
}
}
excluded = append(excluded, path)
}
return excluded
}
// AddFields appends all addPaths which are not already present in paths to paths and returns the result.
func AddFields(paths []string, addPaths ...string) []string {
for _, p := range addPaths {
if !HasAnyField(paths, p) {
paths = append(paths, p)
}
}
return ExcludeSubFields(paths, addPaths...)
}
// FieldsWithPrefix returns the paths with each the prefix prepended.
func FieldsWithPrefix(prefix string, paths ...string) []string {
ret := make([]string, 0, len(paths))
for _, p := range paths {
ret = append(ret, prefix+"."+p)
}
return ret
}
// FieldsWithoutPrefix returns the paths under prefix with said prefix removed.
func FieldsWithoutPrefix(prefix string, paths ...string) []string {
ret := make([]string, 0, len(paths))
for _, p := range paths {
if strings.HasPrefix(p, prefix+".") {
ret = append(ret, p[1+len(prefix):])
}
}
return ret
}
// AddImplicitEndDeviceGetFields appends implicit EndDevice get paths to paths if not already present.
func AddImplicitEndDeviceGetFields(paths ...string) []string {
return AddFields(paths,
"created_at",
"ids",
"updated_at",
)
}
// ApplyEndDeviceFieldMask applies fields specified by paths from src to dst and returns the result.
// If dst is nil, a new EndDevice is created.
func ApplyEndDeviceFieldMask(dst, src *EndDevice, paths ...string) (*EndDevice, error) {
if dst == nil {
dst = &EndDevice{}
}
return dst, dst.SetFields(src, paths...)
}
// FilterGetEndDevice returns a new EndDevice with only implicit fields and the ones specified by paths set.
func FilterGetEndDevice(pb *EndDevice, paths ...string) (*EndDevice, error) {
return ApplyEndDeviceFieldMask(nil, pb, AddImplicitEndDeviceGetFields(paths...)...)
}
// AddImplicitSessionKeysGetFields appends implicit SessionKeys get paths to paths if not already present.
func AddImplicitSessionKeysGetFields(paths ...string) []string {
return AddFields(paths,
"session_key_id",
)
}
// ApplySessionKeysFieldMask applies fields specified by paths from src to dst and returns the result.
// If dst is nil, a new SessionKeys is created.
func ApplySessionKeysFieldMask(dst, src *SessionKeys, paths ...string) (*SessionKeys, error) {
if dst == nil {
dst = &SessionKeys{}
}
return dst, dst.SetFields(src, paths...)
}
// FilterGetSessionKeys returns a new SessionKeys with only implicit fields and the ones specified by paths set.
func FilterGetSessionKeys(pb *SessionKeys, paths ...string) (*SessionKeys, error) {
return ApplySessionKeysFieldMask(nil, pb, AddImplicitSessionKeysGetFields(paths...)...)
}
// IncludeFields returns the given paths with the given search paths to include.
func IncludeFields(paths []string, includePaths ...string) []string {
if len(paths) == 0 {
return paths
}
included := make([]string, 0, len(paths))
for _, path := range paths {
if HasAnyField(includePaths, path) {
included = append(included, path)
}
}
return included
}
// FieldsWithoutWrappers returns the paths without the wrapper value paths.
// A wrapper type is a type which contains a singular field called `value`.
func FieldsWithoutWrappers(paths []string) []string {
if len(paths) == 0 {
return paths
}
leaves := make(map[string]int)
for _, path := range paths {
prefix := path
for i := strings.LastIndex(prefix, "."); i != -1; i = strings.LastIndex(prefix, ".") {
prefix = path[:i]
leaves[prefix]++
}
}
result := make([]string, 0, len(paths))
for _, path := range paths {
father := strings.TrimSuffix(path, ".value")
if path != father && leaves[father] == 1 {
continue
}
result = append(result, path)
}
return result
}