forked from benthosdev/benthos
/
literals.go
195 lines (178 loc) · 4.6 KB
/
literals.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
package query
import (
"fmt"
)
var _ Function = &mapLiteral{}
type mapLiteral struct {
keyValues [][2]interface{}
}
// NewMapLiteral creates a map literal from a slice of key/value pairs. If all
// keys and values are static then a static map[string]interface{} value is
// returned. However, if any keys or values are dynamic a Function is returned.
func NewMapLiteral(values [][2]interface{}) (interface{}, error) {
isDynamic := false
staticValues := make(map[string]interface{}, len(values))
for i, kv := range values {
var key string
switch t := kv[0].(type) {
case string:
key = t
case *Literal:
var isStr bool
if key, isStr = t.Value.(string); !isStr {
return nil, fmt.Errorf("object keys must be strings, received: %T", t.Value)
}
values[i][0] = key
case Function:
isDynamic = true
default:
return nil, fmt.Errorf("object keys must be strings, received: %T", t)
}
switch t := kv[1].(type) {
case *Literal:
values[i][1] = t.Value
if !isDynamic {
switch t.Value.(type) {
case Delete, Nothing:
default:
staticValues[key] = t.Value
}
}
case Function:
isDynamic = true
default:
if !isDynamic {
switch kv[1].(type) {
case Delete, Nothing:
default:
staticValues[key] = kv[1]
}
}
}
}
if isDynamic {
return &mapLiteral{keyValues: values}, nil
}
return staticValues, nil
}
func (m *mapLiteral) Annotation() string {
return "object literal"
}
func (m *mapLiteral) Exec(ctx FunctionContext) (interface{}, error) {
dynMap := make(map[string]interface{}, len(m.keyValues))
for _, kv := range m.keyValues {
var key string
var value interface{}
var err error
switch t := kv[0].(type) {
case string:
key = t
case Function:
var keyI interface{}
if keyI, err = t.Exec(ctx); err != nil {
return nil, fmt.Errorf("failed to resolve key: %w", err)
}
switch t2 := keyI.(type) {
case string:
key = t2
case []byte:
key = string(t2)
default:
return nil, fmt.Errorf("mapping returned invalid key type: %T", keyI)
}
default:
return nil, fmt.Errorf("invalid key type: %T", kv[0])
}
if fn, isFunction := kv[1].(Function); isFunction {
if value, err = fn.Exec(ctx); err != nil {
return nil, fmt.Errorf("failed to resolve '%v' value: %w", key, err)
}
} else {
value = kv[1]
}
switch value.(type) {
case Delete, Nothing:
default:
dynMap[key] = value
}
}
return dynMap, nil
}
func (m *mapLiteral) QueryTargets(ctx TargetsContext) (TargetsContext, []TargetPath) {
var targetPaths []TargetPath
for _, kv := range m.keyValues {
if fn, ok := kv[0].(Function); ok {
_, paths := fn.QueryTargets(ctx)
targetPaths = append(targetPaths, paths...)
}
if fn, ok := kv[1].(Function); ok {
_, paths := fn.QueryTargets(ctx)
targetPaths = append(targetPaths, paths...)
}
}
// TODO: Mark next context with aliases?
return ctx, targetPaths
}
//------------------------------------------------------------------------------
var _ Function = &arrayLiteral{}
type arrayLiteral struct {
values []interface{}
}
// NewArrayLiteral creates an array literal from a slice of values. If all
// values are static then a static []interface{} value is returned. However, if
// any values are dynamic a Function is returned.
func NewArrayLiteral(values ...interface{}) interface{} {
var expandedValues []interface{}
isDynamic := false
for _, v := range values {
switch t := v.(type) {
case *Literal:
switch t.Value.(type) {
case Delete, Nothing:
default:
expandedValues = append(expandedValues, t.Value)
}
case Delete, Nothing:
case Function:
isDynamic = true
expandedValues = append(expandedValues, v)
default:
expandedValues = append(expandedValues, v)
}
}
if !isDynamic {
return expandedValues
}
return &arrayLiteral{expandedValues}
}
func (a *arrayLiteral) Annotation() string {
return "array literal"
}
func (a *arrayLiteral) Exec(ctx FunctionContext) (interface{}, error) {
dynArray := make([]interface{}, 0, len(a.values))
for _, v := range a.values {
if fn, isFunction := v.(Function); isFunction {
var err error
if v, err = fn.Exec(ctx); err != nil {
return nil, err
}
}
switch v.(type) {
case Delete, Nothing:
default:
dynArray = append(dynArray, v)
}
}
return dynArray, nil
}
func (a *arrayLiteral) QueryTargets(ctx TargetsContext) (TargetsContext, []TargetPath) {
var targetPaths []TargetPath
for _, v := range a.values {
if fn, ok := v.(Function); ok {
_, paths := fn.QueryTargets(ctx)
targetPaths = append(targetPaths, paths...)
}
}
// TODO: Mark next context with aliases?
return ctx, targetPaths
}