-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation.go
188 lines (155 loc) · 4.22 KB
/
operation.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
package mpath
import (
"reflect"
"sort"
"strings"
)
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
type Operation interface {
Do(currentData, originalData any) (dataToUse any, err error)
Parse(s *scanner, r rune) (nextR rune, err error)
Sprint(depth int) (out string)
Type() OT_OpType
UserString() string
PropagateNull() bool
}
type opCommon struct {
userString string
propagateNull bool
}
func (x opCommon) UserString() string {
return x.userString
}
func (x opCommon) PropagateNull() bool {
return x.propagateNull
}
////////////////////////////////////////////////////////////////////////////////////
type OT_OpType int
const (
OT_Path OT_OpType = iota
OT_PathIdent
OT_Filter
OT_LogicalOperation
OT_Function
)
func GetRootFieldsAccessed(op Operation) (rootFieldsAccessed []string) {
accessed := map[string]struct{}{}
switch t := op.(type) {
case *opPath:
thisPath := []string{}
haveSeenIdent := false
for _, pop := range t.Operations {
switch ot := pop.(type) {
case *opPathIdent:
if !haveSeenIdent && !t.IsFilter {
haveSeenIdent = true
thisPath = append(thisPath, ot.IdentName)
}
case *opFilter:
for _, logOp := range ot.LogicalOperation.Operations {
for _, val := range GetRootFieldsAccessed(logOp) {
accessed[val] = struct{}{}
}
}
case *opFunction:
for _, param := range ot.Params.Paths() {
for _, val := range GetRootFieldsAccessed(param.Value) {
accessed[val] = struct{}{}
}
}
}
}
if len(thisPath) > 0 {
accessed[strings.Join(thisPath, ".")] = struct{}{}
}
case *opLogicalOperation:
for _, p := range t.Operations {
for _, val := range GetRootFieldsAccessed(p) {
accessed[val] = struct{}{}
}
}
}
for acc := range accessed {
rootFieldsAccessed = append(rootFieldsAccessed, acc)
}
sort.Strings(rootFieldsAccessed)
return
}
func AddressedPaths(op Operation) (addressedPaths [][]string) {
// check stuff
switch v := op.(type) {
case *opPath:
if len(v.Operations) < 1 {
break
}
idents := []string{}
for _, subOp := range v.Operations {
switch vv := subOp.(type) {
case *opPathIdent:
idents = append(idents, vv.IdentName)
case *opFilter:
for _, logOp := range vv.LogicalOperation.Operations {
for _, val := range AddressedPaths(logOp) {
addressedPaths = append(addressedPaths, append(idents, val...))
}
}
case *opFunction:
for _, p := range vv.Params.Paths() {
for _, val := range AddressedPaths(p.Value) {
addressedPaths = append(addressedPaths, val)
}
}
}
}
addressedPaths = append(addressedPaths, idents)
case *opLogicalOperation:
for _, p := range v.Operations {
for _, val := range AddressedPaths(p) {
addressedPaths = append(addressedPaths, val)
}
}
}
retAddressedPaths := make([][]string, 0, len(addressedPaths))
for _, val := range addressedPaths {
if !sliceContains(retAddressedPaths, val) && !slicesContainsSubsetSlice(retAddressedPaths, val) && len(val) > 0 {
retAddressedPaths = append(retAddressedPaths, val)
}
}
return retAddressedPaths
}
// Checks if a given value is present in a slice.
// If a match is found, it returns true. Otherwise, it returns false.
func sliceContains[T any](sl []T, val T) bool {
for _, v := range sl {
if reflect.DeepEqual(v, val) {
return true
}
}
return false
}
// Takes a slice of any type and returns a 2D slice where each element
// is a prefix of the original slice.
// E.g.
// Input: ["one", "two", "three", "four"]
// Output: [["one"], ["one", "two"], ["one", "two", "three"], ["one", "two", "three", "four"]]
func spreadSlice[T any](sl []T) (ret [][]T) {
ret = make([][]T, len(sl))
for i := range sl {
ret[i] = sl[:i+1]
}
return ret
}
// Checks if any of the slices in the given 2D slice contains the given subset slice.
// It returns true if a match is found, and false otherwise.
func slicesContainsSubsetSlice[T any](slices [][]T, subset []T) bool {
for _, slice := range slices {
sliceSubsets := spreadSlice(slice)
for _, ss := range sliceSubsets {
if reflect.DeepEqual(ss, subset) {
return true
}
}
}
return false
}