forked from rethinkdb/rethinkdb-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
261 lines (210 loc) · 5.41 KB
/
utils.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
package gorethink
import (
"reflect"
"strconv"
"strings"
"sync/atomic"
"github.com/dancannon/gorethink/encoding"
p "github.com/dancannon/gorethink/ql2"
)
// Helper functions for constructing terms
// constructRootTerm is an alias for creating a new term.
func constructRootTerm(name string, termType p.Term_TermType, args []interface{}, optArgs map[string]interface{}) Term {
return Term{
name: name,
rootTerm: true,
termType: termType,
args: convertTermList(args),
optArgs: convertTermObj(optArgs),
}
}
// constructMethodTerm is an alias for creating a new term. Unlike constructRootTerm
// this function adds the previous expression in the tree to the argument list to
// create a method term.
func constructMethodTerm(prevVal Term, name string, termType p.Term_TermType, args []interface{}, optArgs map[string]interface{}) Term {
args = append([]interface{}{prevVal}, args...)
return Term{
name: name,
rootTerm: false,
termType: termType,
args: convertTermList(args),
optArgs: convertTermObj(optArgs),
}
}
// Helper functions for creating internal RQL types
func newQuery(t Term, qopts map[string]interface{}, copts *ConnectOpts) (q Query, err error) {
queryOpts := map[string]interface{}{}
for k, v := range qopts {
queryOpts[k], err = Expr(v).build()
if err != nil {
return
}
}
if copts.Database != "" {
queryOpts["db"], err = DB(copts.Database).build()
if err != nil {
return
}
}
builtTerm, err := t.build()
if err != nil {
return q, err
}
// Construct query
return Query{
Type: p.Query_START,
Term: &t,
Opts: queryOpts,
builtTerm: builtTerm,
}, nil
}
// makeArray takes a slice of terms and produces a single MAKE_ARRAY term
func makeArray(args termsList) Term {
return Term{
name: "[...]",
termType: p.Term_MAKE_ARRAY,
args: args,
}
}
// makeObject takes a map of terms and produces a single MAKE_OBJECT term
func makeObject(args termsObj) Term {
return Term{
name: "{...}",
termType: p.Term_MAKE_OBJ,
optArgs: args,
}
}
var nextVarID int64
func makeFunc(f interface{}) Term {
value := reflect.ValueOf(f)
valueType := value.Type()
var argNums = make([]interface{}, valueType.NumIn())
var args = make([]reflect.Value, valueType.NumIn())
for i := 0; i < valueType.NumIn(); i++ {
// Get a slice of the VARs to use as the function arguments
args[i] = reflect.ValueOf(constructRootTerm("var", p.Term_VAR, []interface{}{nextVarID}, map[string]interface{}{}))
argNums[i] = nextVarID
atomic.AddInt64(&nextVarID, 1)
// make sure all input arguments are of type Term
if valueType.In(i).String() != "gorethink.Term" {
panic("Function argument is not of type Term")
}
}
if valueType.NumOut() != 1 {
panic("Function does not have a single return value")
}
body := value.Call(args)[0].Interface()
argsArr := makeArray(convertTermList(argNums))
return constructRootTerm("func", p.Term_FUNC, []interface{}{argsArr, body}, map[string]interface{}{})
}
func funcWrap(value interface{}) Term {
val := Expr(value)
if implVarScan(val) && val.termType != p.Term_ARGS {
return makeFunc(func(x Term) Term {
return val
})
}
return val
}
func funcWrapArgs(args []interface{}) []interface{} {
for i, arg := range args {
args[i] = funcWrap(arg)
}
return args
}
// implVarScan recursivly checks a value to see if it contains an
// IMPLICIT_VAR term. If it does it returns true
func implVarScan(value Term) bool {
if value.termType == p.Term_IMPLICIT_VAR {
return true
}
for _, v := range value.args {
if implVarScan(v) {
return true
}
}
for _, v := range value.optArgs {
if implVarScan(v) {
return true
}
}
return false
}
// Convert an opt args struct to a map.
func optArgsToMap(optArgs OptArgs) map[string]interface{} {
data, err := encode(optArgs)
if err == nil && data != nil {
if m, ok := data.(map[string]interface{}); ok {
return m
}
}
return map[string]interface{}{}
}
// Convert a list into a slice of terms
func convertTermList(l []interface{}) termsList {
terms := make(termsList, len(l))
for i, v := range l {
terms[i] = Expr(v)
}
return terms
}
// Convert a map into a map of terms
func convertTermObj(o map[string]interface{}) termsObj {
terms := make(termsObj, len(o))
for k, v := range o {
terms[k] = Expr(v)
}
return terms
}
// Helper functions for debugging
func allArgsToStringSlice(args termsList, optArgs termsObj) []string {
allArgs := make([]string, len(args)+len(optArgs))
i := 0
for _, v := range args {
allArgs[i] = v.String()
i++
}
for k, v := range optArgs {
allArgs[i] = k + "=" + v.String()
i++
}
return allArgs
}
func argsToStringSlice(args termsList) []string {
allArgs := make([]string, len(args))
for i, v := range args {
allArgs[i] = v.String()
}
return allArgs
}
func optArgsToStringSlice(optArgs termsObj) []string {
allArgs := make([]string, len(optArgs))
i := 0
for k, v := range optArgs {
allArgs[i] = k + "=" + v.String()
i++
}
return allArgs
}
func splitAddress(address string) (hostname string, port int) {
hostname = "localhost"
port = 28015
addrParts := strings.Split(address, ":")
if len(addrParts) >= 1 {
hostname = addrParts[0]
}
if len(addrParts) >= 2 {
port, _ = strconv.Atoi(addrParts[1])
}
return
}
func encode(data interface{}) (interface{}, error) {
if _, ok := data.(Term); ok {
return data, nil
}
v, err := encoding.Encode(data)
if err != nil {
return nil, err
}
return v, nil
}