/
args.go
150 lines (122 loc) · 2.81 KB
/
args.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
package core
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/dosco/graphjin/core/internal/psql"
"github.com/dosco/graphjin/internal/jsn"
)
// argList function is used to create a list of arguments to pass
// to a prepared statement.
type args struct {
values []interface{}
cindx int // index of cursor arg
}
func (gj *graphjin) argList(c context.Context,
md psql.Metadata,
vars []byte,
pf []byte,
rc *ReqConfig) (args, error) {
ar := args{cindx: -1}
params := md.Params()
vl := make([]interface{}, len(params))
var fields map[string]json.RawMessage
var err error
vars, err = decryptValues(vars, decPrefix, gj.encKey)
if err != nil {
return ar, err
}
if len(vars) != 0 {
fields, _, err = jsn.Tree(vars)
if err != nil {
return ar, err
}
}
for i, p := range params {
switch p.Name {
case "user_id":
if v := c.Value(UserIDKey); v != nil {
switch v1 := v.(type) {
case string:
vl[i] = v1
case int:
vl[i] = v1
case float64:
vl[i] = int(v1)
default:
return ar, fmt.Errorf("user_id must be an integer or a string: %T", v)
}
} else {
return ar, argErr(p)
}
case "user_id_raw":
if v := c.Value(UserIDRawKey); v != nil {
vl[i] = v.(string)
} else {
return ar, argErr(p)
}
case "user_id_provider":
if v := c.Value(UserIDProviderKey); v != nil {
vl[i] = v.(string)
} else {
return ar, argErr(p)
}
case "user_role":
if v := c.Value(UserRoleKey); v != nil {
vl[i] = v.(string)
} else {
return ar, argErr(p)
}
case "cursor":
if v, ok := fields["cursor"]; ok && v[0] == '"' {
vl[i] = string(v[1 : len(v)-1])
} else {
vl[i] = nil
}
ar.cindx = i
default:
if v, ok := fields[p.Name]; ok {
varIsNull := bytes.Equal(v, []byte("null"))
switch {
case p.IsNotNull && varIsNull:
return ar, fmt.Errorf("variable '%s' cannot be null", p.Name)
case p.IsArray && v[0] != '[' && !varIsNull:
return ar, fmt.Errorf("variable '%s' should be an array of type '%s'", p.Name, p.Type)
case p.Type == "json" && v[0] != '[' && v[0] != '{' && !varIsNull:
return ar, fmt.Errorf("variable '%s' should be an array or object", p.Name)
}
vl[i] = parseVarVal(v)
} else if rc != nil {
if v, ok := rc.Vars[p.Name]; ok {
if fn, ok := v.(func() string); ok {
vl[i] = fn()
}
}
} else {
return ar, argErr(p)
}
}
}
ar.values = vl
return ar, nil
}
func parseVarVal(v json.RawMessage) interface{} {
switch v[0] {
case '[', '{':
return v
case '"':
return string(v[1 : len(v)-1])
case 't', 'T':
return true
case 'f', 'F':
return false
case 'n':
return nil
default:
return string(v)
}
}
func argErr(p psql.Param) error {
return fmt.Errorf("required variable '%s' of type '%s' must be set", p.Name, p.Type)
}