-
Notifications
You must be signed in to change notification settings - Fork 8
/
expand.go
236 lines (195 loc) · 5.44 KB
/
expand.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
package runner
import (
"math"
"github.com/b2wdigital/restQL-golang/v5/internal/domain"
)
const (
valuesParamType string = "values"
bodyParamType = "body"
)
type parameter struct {
paramType string
path []string
value interface{}
}
type listParameters struct {
paramType string
path []string
value interface{}
}
// MultiplexStatements creates a statement for each value in a
// list parameter value.
// In case of multiple list parameter values it makes the cartesian
// product of all the lists and makes a statement for each value in a
// result product.
func MultiplexStatements(resources domain.Resources) domain.Resources {
for resourceID, stmt := range resources {
switch stmt := stmt.(type) {
case domain.Statement:
resources[resourceID] = multiplex(stmt)
default:
resources[resourceID] = stmt
}
}
return resources
}
func multiplex(statement domain.Statement) interface{} {
values := statement.With.Values
body := statement.With.Body
if values == nil && body == nil {
return statement
}
listParams := getListParamsFromValues(values)
bodyParams := getListParamsFromBody(body)
listParams = append(listParams, bodyParams...)
if len(listParams) == 0 {
return statement
}
statementsParameters := zipListParams(listParams)
result := make([]interface{}, len(statementsParameters))
for i, parameters := range statementsParameters {
newStmt := copyStatement(statement)
for _, p := range parameters {
if p.paramType == valuesParamType {
setParameterOnStatement(newStmt.With.Values, p.path, p.value)
}
if p.paramType == bodyParamType {
newStmt.With.Body = p.value
}
}
result[i] = multiplex(newStmt)
}
return result
}
func getListParamsFromBody(body interface{}) []listParameters {
var result []listParameters
if body, ok := body.([]interface{}); ok {
result = append(result, listParameters{paramType: bodyParamType, value: body})
}
return result
}
func setParameterOnStatement(params interface{}, path []string, value interface{}) {
if params, ok := params.(map[string]interface{}); ok {
if len(path) == 1 {
params[path[0]] = value
return
}
field, ok := params[path[0]]
if !ok {
return
}
setParameterOnStatement(field, path[1:], value)
}
}
func getListParamsFromValues(values map[string]interface{}) []listParameters {
var result []listParameters
for key, val := range values {
lp := findListParameters([]string{key}, val)
result = append(result, lp...)
}
return result
}
func findListParameters(path []string, val interface{}) []listParameters {
switch val := val.(type) {
case domain.NoMultiplex:
return []listParameters{}
case domain.AsQuery:
parameters := findListParameters(path, val.Target())
if len(parameters) == 0 {
return []listParameters{}
}
result := make([]listParameters, len(parameters))
for i, lp := range parameters {
fn := val.Map(func(_ interface{}) interface{} { return lp.value })
result[i] = listParameters{path: lp.path, paramType: lp.paramType, value: fn}
}
return result
case map[string]interface{}:
var result []listParameters
for k, v := range val {
lp := findListParameters(append(path, k), v)
result = append(result, lp...)
}
return result
case []interface{}:
return []listParameters{{path: path, paramType: valuesParamType, value: val}}
default:
return []listParameters{}
}
}
func zipListParams(listParams []listParameters) [][]parameter {
statementCount := minimumListParamLength(listParams)
result := make([][]parameter, statementCount)
var i uint64
for i = 0; i < statementCount; i++ {
statementParameters := make([]parameter, len(listParams))
for j, lp := range listParams {
v := lp.value
switch v := v.(type) {
case domain.AsQuery:
fn := v.Map(func(target interface{}) interface{} {
l := target.([]interface{})
return l[i]
})
statementParameters[j] = parameter{path: lp.path, paramType: lp.paramType, value: fn}
case []interface{}:
statementParameters[j] = parameter{path: lp.path, paramType: lp.paramType, value: v[i]}
}
}
result[i] = statementParameters
}
return result
}
func minimumListParamLength(listParams []listParameters) uint64 {
var result uint64 = math.MaxUint64
for _, lp := range listParams {
length := valueLength(lp)
if length < result {
result = length
}
}
return result
}
func valueLength(lp listParameters) uint64 {
switch v := lp.value.(type) {
case []interface{}:
return uint64(len(v))
case domain.AsQuery:
target := v.Target().([]interface{})
return uint64(len(target))
default:
return math.MaxUint64
}
}
func copyStatement(statement domain.Statement) domain.Statement {
newStatement := statement
if statement.Headers != nil {
newStatement.Headers = make(map[string]interface{}, len(statement.Headers))
for k, v := range statement.Headers {
newStatement.Headers[k] = v
}
}
if statement.With.Values != nil {
newStatement.With.Values = make(map[string]interface{}, len(statement.With.Values))
for k, v := range statement.With.Values {
newStatement.With.Values[k] = copyValue(v)
}
}
if statement.Only != nil {
newStatement.Only = make([]interface{}, len(statement.Only))
copy(newStatement.Only, statement.Only)
}
return newStatement
}
func copyValue(value interface{}) interface{} {
switch value := value.(type) {
case map[string]interface{}:
m := make(map[string]interface{})
for k, v := range value {
m[k] = copyValue(v)
}
return m
default:
return value
}
}