forked from jonbodner/proteus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
316 lines (287 loc) · 9.01 KB
/
builder.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package proteus
import (
"bytes"
"context"
"fmt"
"go/scanner"
"go/token"
"reflect"
"strings"
"text/template"
"database/sql/driver"
"github.com/jonbodner/proteus/logger"
"github.com/jonbodner/proteus/mapper"
"github.com/jonbodner/stackerr"
)
func buildNameOrderMap(paramOrder string, startPos int) map[string]int {
out := map[string]int{}
params := strings.Split(paramOrder, ",")
for k, v := range params {
out[strings.TrimSpace(v)] = k + startPos
}
return out
}
func buildDummyParameters(paramCount int, startPos int) map[string]int {
m := map[string]int{}
for i := startPos; i < paramCount; i++ {
m[fmt.Sprintf("$%d", i-startPos+1)] = i
}
return m
}
// template slice support
type queryHolder interface {
finalize(c context.Context, args []reflect.Value) (string, error)
}
type simpleQueryHolder string
func (sq simpleQueryHolder) finalize(c context.Context, args []reflect.Value) (string, error) {
return string(sq), nil
}
type templateQueryHolder struct {
queryString string
pa ParamAdapter
paramOrder []paramInfo
}
func (tq templateQueryHolder) finalize(c context.Context, args []reflect.Value) (string, error) {
return doFinalize(c, tq.queryString, tq.paramOrder, tq.pa, args)
}
var (
valueType = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
)
type posType interface {
In(i int) reflect.Type
}
func buildFixedQueryAndParamOrder(c context.Context, query string, nameOrderMap map[string]int, funcType posType, pa ParamAdapter) (queryHolder, []paramInfo, error) {
var out bytes.Buffer
var paramOrder []paramInfo
// escapes:
// \ (any character), that character literally (meant for escaping : and \)
// ending on a single \ means the \ is ignored
inEscape := false
inVar := false
curVar := []rune{}
hasSlice := false
for k, v := range query {
if inEscape {
out.WriteRune(v)
inEscape = false
continue
}
switch v {
case '\\':
inEscape = true
case ':':
if inVar {
if len(curVar) == 0 {
//error! must have a something
return nil, nil, stackerr.Errorf("empty variable declaration at position %d", k)
}
curVarS := string(curVar)
id, err := validIdentifier(c, curVarS)
if err != nil {
//error, identifier must be valid go identifier with . for path
return nil, nil, err
}
//it's a valid identifier, but now we need to know if it's a slice or a scalar.
//all we have is the name, not the mapping of the name to the position in the in parameters for the function.
//so we need to do that search now, using the information in the struct tag prop.
//mapper.ExtractType can tell us the kind of what we're expecting
//if it's a scalar, then we use pa to write out the correct symbol for this db type and increment pos.
//if it's a slice, then we put in the slice template syntax instead.
//get just the first part of the name, before any .
path := strings.Split(id, ".")
paramName := path[0]
if paramPos, ok := nameOrderMap[paramName]; ok {
//if the path has more than one part, make sure that the type of the function parameter is map or struct
paramType := funcType.In(paramPos)
if len(path) > 1 {
switch paramType.Kind() {
case reflect.Map, reflect.Struct:
//do nothing
default:
return nil, nil, stackerr.Errorf("query Parameter %s has a path, but the incoming parameter is not a map or a struct", paramName)
}
}
pathType, err := mapper.ExtractType(c, paramType, path)
if err != nil {
return nil, nil, err
}
out.WriteString(addSlice(id))
isSlice := false
//special case -- slice of bytes is never expanded out into a comma-separated list
if pathType != nil && pathType.Kind() == reflect.Slice && !pathType.Implements(valueType) && pathType.Elem().Kind() != reflect.Uint8 {
hasSlice = true
isSlice = true
}
paramOrder = append(paramOrder, paramInfo{id, paramPos, isSlice})
} else {
return nil, nil, stackerr.Errorf("query Parameter %s cannot be found in the incoming parameters", paramName)
}
inVar = false
curVar = []rune{}
} else {
inVar = true
}
default:
if inVar {
curVar = append(curVar, v)
} else {
out.WriteRune(v)
}
}
}
if inVar {
return nil, nil, stackerr.Errorf("missing a closing : somewhere: %s", query)
}
queryString := out.String()
if !hasSlice {
//no slices, so last param is never going to be referenced in doFinalize
queryString, err := doFinalize(c, queryString, paramOrder, pa, nil)
if err != nil {
return nil, nil, err
}
return simpleQueryHolder(queryString), paramOrder, nil
}
return templateQueryHolder{queryString: queryString, pa: pa, paramOrder: paramOrder}, paramOrder, nil
}
func doFinalize(c context.Context, queryString string, paramOrder []paramInfo, pa ParamAdapter, args []reflect.Value) (string, error) {
temp, err := template.New("query").Funcs(template.FuncMap{"join": joinFactory(1, pa)}).Parse(queryString)
if err != nil {
return "", err
}
//can evaluate the template now, with 1 for the length for each item
sliceMap := map[string]interface{}{}
for _, v := range paramOrder {
if v.isSlice {
var val interface{}
val, err = mapper.Extract(c, args[v.posInParams].Interface(), strings.Split(v.name, "."))
if err != nil {
break
}
valV := reflect.ValueOf(val)
sliceMap[fixNameForTemplate(v.name)] = valV.Len()
} else {
sliceMap[fixNameForTemplate(v.name)] = 1
}
}
var b bytes.Buffer
err = temp.Execute(&b, sliceMap)
if err != nil {
return "", err
}
return b.String(), err
}
type paramInfo struct {
name string
posInParams int
isSlice bool
}
const (
sliceTemplate = `{{.%s | join}}`
)
func joinFactory(startPos int, paramAdapter ParamAdapter) func(int) string {
return func(total int) string {
var b bytes.Buffer
for i := 0; i < total; i++ {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(paramAdapter(startPos + i))
}
startPos += total
return b.String()
}
}
func fixNameForTemplate(name string) string {
//need to make sure that foo.bar and fooDOTbar don't collide, however unlikely
name = strings.Replace(name, "DOT", "DOTDOT", -1)
name = strings.Replace(name, ".", "DOT", -1)
name = strings.Replace(name, "DOLLAR", "DOLLARDOLLAR", -1)
name = strings.Replace(name, "$", "DOLLAR", -1)
return name
}
func addSlice(sliceName string) string {
return fmt.Sprintf(sliceTemplate, fixNameForTemplate(sliceName))
}
func validIdentifier(c context.Context, curVar string) (string, error) {
if strings.Contains(curVar, ";") {
return "", stackerr.Errorf("; is not allowed in an identifier: %s", curVar)
}
curVarB := []byte(curVar)
var s scanner.Scanner
fset := token.NewFileSet() // positions are relative to fset
file := fset.AddFile("", fset.Base(), len(curVarB)) // register input "file"
s.Init(file, curVarB, nil, scanner.Mode(0))
lastPeriod := false
first := true
identifier := ""
dollar := false
lastFloat := false
loop:
for {
pos, tok, lit := s.Scan()
logger.Log(c, logger.DEBUG, fmt.Sprintf("%s\t%s\t%q\n", fset.Position(pos), tok, lit))
switch tok {
case token.EOF:
if first || lastPeriod {
return "", stackerr.Errorf("identifiers cannot be empty or end with a .: %s", curVar)
}
break loop
case token.SEMICOLON:
//happens with auto-insert from scanner
//any explicit semicolons are illegal and handled earlier
continue
case token.IDENT:
if !first && !lastPeriod && !lastFloat {
return "", stackerr.Errorf(". missing between parts of an identifier: %s", curVar)
}
first = false
lastPeriod = false
lastFloat = false
identifier += lit
case token.PERIOD:
if first || lastPeriod {
return "", stackerr.Errorf("identifier cannot start with . or have two . in a row: %s", curVar)
}
lastPeriod = true
identifier += "."
case token.ILLEGAL:
//special case to support $N notation, only valid for first part
if lit == "$" && first {
identifier = "$"
dollar = true
first = false
continue
}
return "", stackerr.Errorf("invalid character found in identifier: %s", curVar)
case token.INT:
if !dollar || first {
return "", stackerr.Errorf("invalid character found in identifier: %s", curVar)
}
identifier += lit
if dollar {
dollar = false
}
case token.FLOAT:
//this is weird. If we have $1.NAME, it will think that there's a FLOAT token with value 1.
//due to float support for exponents, if we have an E after the decimal point, the FLOAT token
//will include the E and any subsequent digits.
// also a problem when walking array or slice references (values.0 is the 0th element in array values). This
// returns .0 as the lit value
//Only valid for $ notation and array/slice references.
if first {
return "", stackerr.Errorf("invalid character found in identifier: %s", curVar)
}
identifier += lit
if dollar {
dollar = false
}
lastFloat = true
if lit[len(lit)-1] == '.' {
lastPeriod = true
}
default:
return "", stackerr.Errorf("invalid character found in identifier: %s", curVar)
}
}
return identifier, nil
}