-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
filter.go
101 lines (81 loc) · 2.13 KB
/
filter.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
package goriak
// FilterInclude adds a include filter to Set().
// Call FilterInclude("A") to only include the field A (and children).
// Can be combined with FilterExclude() to form more complicated patterns.
// Use FilterInclude or FilterExclude without parameters to include/exclude the root object.
// If the same field is both included and excluded the include is prioritized.
func (c *MapSetCommand) FilterInclude(path ...string) *MapSetCommand {
c.includeFilter = append(c.includeFilter, path)
return c
}
// FilterExclude does the opposite of FilterInclude.
// See FinterInclude for more info.
func (c *MapSetCommand) FilterExclude(path ...string) *MapSetCommand {
c.excludeFilter = append(c.excludeFilter, path)
return c
}
func (c *MapSetCommand) filterAllowPath(path ...string) bool {
// No filter has been set: Allow all paths
if len(c.includeFilter) == 0 && len(c.excludeFilter) == 0 {
return true
}
allowedByIncludeFilter := false
allowedLevel := 0
disallowedByExcludeFilter := false
disallowedLevel := 0
// Check if in include filter
for _, include := range c.includeFilter {
// Can not be allowed by a filter shorter than us
if len(path) < len(include) {
continue
}
allValid := true
for i, p := range include {
if path[i] != p {
allValid = false
}
}
if allValid {
allowedByIncludeFilter = true
if len(include) > allowedLevel {
allowedLevel = len(include)
}
}
}
// Check if in exclude filter
for _, exclude := range c.excludeFilter {
// Can not be allowed by a filter shorter than us
if len(path) < len(exclude) {
continue
}
allValid := true
for i, p := range exclude {
if path[i] != p {
allValid = false
}
}
if allValid {
disallowedByExcludeFilter = true
if len(exclude) > disallowedLevel {
disallowedLevel = len(exclude)
}
}
}
if allowedByIncludeFilter && disallowedByExcludeFilter {
if allowedLevel >= disallowedLevel {
return true
}
return false
}
if allowedByIncludeFilter {
return true
}
if disallowedByExcludeFilter {
return false
}
// Include by default
if len(c.includeFilter) == 0 {
return true
}
return false
}