-
Notifications
You must be signed in to change notification settings - Fork 0
/
opPathIdent.go
206 lines (171 loc) · 4.84 KB
/
opPathIdent.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
package mpath
import (
"fmt"
"reflect"
"strings"
"cuelang.org/go/cue"
)
type opPathIdent struct {
IdentName string
opCommon
}
func (x *opPathIdent) Validate(rootValue cue.Value, cuePath CuePath, blockedRootFields []string) (part *PathIdent, returnedType InputOrOutput) {
errFunc := func(e error) (*PathIdent, InputOrOutput) {
if part == nil {
part = &PathIdent{}
}
part.String = x.UserString()
part.Error = strPtr(e.Error())
return part, returnedType
}
part = &PathIdent{
pathIdentFields: pathIdentFields{
Available: &Available{},
String: x.UserString(),
},
}
cuePathValue, err := findValueAtPath(rootValue, cuePath)
if err != nil {
return errFunc(err)
}
k := cuePathValue.IncompleteKind()
if k == cue.BottomKind {
cuePathValue = cuePathValue.LookupPath(cue.MakePath(cue.AnyIndex))
k = cuePathValue.IncompleteKind()
}
wasList := false
seenBottomKind := false
loop:
switch k {
// Primative Kinds:
case cue.BoolKind:
if wasList {
returnedType = inputOrOutput(PT_Boolean, IOOT_Array)
} else {
returnedType = inputOrOutput(PT_Boolean, IOOT_Single)
}
case cue.StringKind, cue.BytesKind:
if wasList {
returnedType = inputOrOutput(PT_String, IOOT_Array)
} else {
returnedType = inputOrOutput(PT_String, IOOT_Single)
}
case cue.NumberKind, cue.IntKind, cue.FloatKind:
if wasList {
returnedType = inputOrOutput(PT_Number, IOOT_Array)
} else {
returnedType = inputOrOutput(PT_Number, IOOT_Single)
}
case cue.TopKind:
if wasList {
returnedType = inputOrOutput(PT_Any, IOOT_Array)
} else {
returnedType = inputOrOutput(PT_Any, IOOT_Single)
}
case cue.StructKind:
if wasList {
returnedType = inputOrOutput(PT_Object, IOOT_Array)
} else {
returnedType = inputOrOutput(PT_Object, IOOT_Single)
}
// Get the fields for the next value:
availableFields, err := getAvailableFieldsForValue(cuePathValue, blockedRootFields)
if err != nil {
return errFunc(fmt.Errorf("couldn't get fields for struct type to build filters: %w", err))
}
if !wasList {
part.Available.Fields = availableFields
}
if wasList {
for _, af := range availableFields {
part.Available.Filters = append(part.Available.Filters, "@."+af)
}
}
case cue.ListKind:
if wasList {
returnedType = inputOrOutput(PT_Any, IOOT_Single)
returnedType.CueExpr = getExpr(cuePathValue)
return
}
wasList = true
// Check what kind of array
k, err = getUnderlyingKind(cuePathValue)
if err != nil {
return errFunc(fmt.Errorf("couldn't ascertain underlying kind of list for field '%s': %w", part.String, err))
}
goto loop
case cue.BottomKind:
if !seenBottomKind {
seenBottomKind = true
cuePathValue = cuePathValue.LookupPath(cue.MakePath(cue.AnyIndex))
k = cuePathValue.IncompleteKind()
goto loop
}
errMessage := "unable to find field"
thisValue := cuePathValue.LookupPath(cue.MakePath(cue.AnyIndex))
errMessage += fmt.Sprint(thisValue.Err())
errMessage += thisValue.IncompleteKind().String()
part.Error = &errMessage
return
default:
// sels := cuePathValue.IncompleteKind()
// fmt.Println(sels)
return errFunc(fmt.Errorf("encountered unknown cue kind %v", k))
}
part.Available.Functions = getAvailableFunctionsForKind(returnedType)
part.Type = returnedType
returnedType.CueExpr = getExpr(cuePathValue)
return
}
func (x *opPathIdent) Type() OT_OpType { return OT_PathIdent }
func (x *opPathIdent) Sprint(depth int) (out string) {
return x.IdentName
}
var ErrKeyNotFound = fmt.Errorf("key not found")
func (x *opPathIdent) Do(currentData, _ any) (dataToUse any, err error) {
// Ident paths require that the data is a struct or map[string]any
// Deal with maps
// if m, ok := currentData.(map[string]any); ok {
v := reflect.ValueOf(currentData)
switch v.Kind() {
case reflect.Pointer, reflect.Interface:
v = v.Elem()
}
if v.Kind() == reflect.Map {
for _, e := range v.MapKeys() {
mks, ok := e.Interface().(string)
if !ok {
if reflect.TypeOf(e.Interface()).ConvertibleTo(reflect.TypeOf("")) {
mksTemp := reflect.ValueOf(e.Interface()).Convert(reflect.TypeOf("")).Interface()
mks, ok = mksTemp.(string)
if !ok || mks == "" {
continue
}
} else {
continue
}
}
if !strings.EqualFold(mks, x.IdentName) {
continue
}
dataToUse = v.MapIndex(e).Interface()
if _, ok := dataToUse.(string); !ok {
dataToUse = convertToDecimalIfNumber(dataToUse)
}
return
}
return nil, ErrKeyNotFound
}
// If we get here, the data must be a struct
// and we will look for the field by name
return getValuesByName(x.IdentName, currentData)
}
func (x *opPathIdent) Parse(s *scanner, r rune) (nextR rune, err error) {
x.IdentName = s.TokenText()
x.userString = x.IdentName
if strings.HasSuffix(x.IdentName, "?") {
x.IdentName = x.IdentName[:len(x.IdentName)-1]
x.propagateNull = true
}
return s.Scan(), nil
}