-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
args.go
168 lines (142 loc) · 3.2 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package core
import (
"context"
"encoding/json"
"errors"
"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, 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
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
default:
return ar, errors.New("user_id must be an integer or a 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] == '"' {
v1, err := gj.decrypt(string(v[1 : len(v)-1]))
if err != nil {
return ar, err
}
vl[i] = v1
} else {
vl[i] = nil
}
ar.cindx = i
default:
if v, ok := fields[p.Name]; ok {
switch {
case p.IsArray && v[0] != '[':
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] != '{':
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
default:
return string(v)
}
}
func (gj *GraphJin) roleQueryArgList(c context.Context) (args, error) {
ar := args{cindx: -1}
params := gj.roleStmtMD.Params()
vl := make([]interface{}, len(params))
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
default:
return ar, errors.New("user_id must be an integer or a 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)
}
}
}
ar.values = vl
return ar, nil
}
func argErr(p psql.Param) error {
return fmt.Errorf("required variable '%s' of type '%s' must be set", p.Name, p.Type)
}