-
Notifications
You must be signed in to change notification settings - Fork 0
/
bandicoot.go
351 lines (289 loc) · 7.04 KB
/
bandicoot.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package bandicoot
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"
"strconv"
"strings"
"unicode"
)
const (
Int = "int"
Long = "long"
Real = "real"
String = "string"
)
var url = ""
var headers = make(map[string]string, 0)
func call(method, fn string, body io.Reader) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequest(method, url+fn, body)
if err != nil {
return nil, err
}
for k, v := range headers {
req.Header.Add(k, v)
}
return client.Do(req)
}
// Set the bandicoot URL, e.g.:
// bandicoot.URL("http://localhost:12345")
// bandicoot.Get("Ping", nil)
func URL(httpURL string) {
if httpURL[len(httpURL)-1] != '/' {
httpURL = httpURL + "/"
}
url = httpURL
}
// Set an HTTP header. The headers will be passed to bandicoot on each request.
// bandicoot.SetHeader("X-Auth", "123456789")
func SetHeader(header, value string) {
headers[header] = value
}
// Call a function using HTTP GET, e.g.:
// type Book struct {
// Title string
// Pages int
// Price real
// }
// var books []Book
// if err := bandicoot.Get("ListBooks?maxPrice=10.0", &books); err != nil {
// fmt.Printf("error %v", err)
// }
// for _, b := range books {
// fmt.Printf("%+v\n", b)
// }
func Get(fn string, out interface{}) error {
resp, err := call("GET", fn, nil)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("http call failed: %v", resp.Status)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return unmarshal(string(body), out)
}
// Call a function using HTTP POST, e.g.:
// books := []Book{Book{Title: "Robinson Crusoe", Pages: 312, Price: 11.21}}
// bandicoot.Post("AddBooks", books, nil)
func Post(fn string, in []interface{}, out interface{}) error {
reqBody, err := marshal(in)
if err != nil {
return err
}
resp, err := call("POST", fn, reqBody)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("http call failed: %v", resp.Status)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return unmarshal(string(respBody), out)
}
func marshal(args []interface{}) (io.Reader, error) {
var buf *bytes.Buffer = bytes.NewBufferString("")
if len(args) > 0 {
h := marshalHead(args[0])
if len(h) > 0 {
buf.WriteString(h)
for i := 0; i < len(args); i++ {
t := marshalTuple(args[i])
if len(t) == 0 {
return nil, fmt.Errorf("cannot marshal the value %v", args[i])
}
buf.WriteString(t)
}
} else {
return nil, fmt.Errorf("cannot marshal the header: %v", args[0])
}
}
return buf, nil
}
func isPublic(attr string) bool {
return unicode.IsUpper(([]rune(attr))[0])
}
func attrUpper(s string) string {
if len(s) == 0 {
return s
}
b := []rune(s)
if unicode.IsLower(b[0]) {
b[0] = unicode.ToUpper(b[0])
}
return string(b)
}
func attrLower(s string) string {
if len(s) == 0 {
return s
}
b := []rune(s)
if unicode.IsUpper(b[0]) {
b[0] = unicode.ToLower(b[0])
}
return string(b)
}
func stripComa(s string) string {
if s[len(s)-1] == ',' {
s = s[0 : len(s)-1]
}
return s
}
func marshalHead(v interface{}) string {
t := reflect.TypeOf(v)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return ""
}
res := ""
last := t.NumField() - 1
for i := 0; i <= last; i++ {
a := t.Field(i)
if a.Type.Kind() == reflect.Chan || !isPublic(a.Name) {
continue
}
res += attrLower(a.Name) + ","
}
return stripComa(res) + "\n"
}
func marshalTuple(t interface{}) string {
v := reflect.ValueOf(t)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
res := ""
last := v.NumField() - 1
for i := 0; i <= last; i++ {
if v.Field(i).Kind() == reflect.Chan || !v.Field(i).CanInterface() {
continue
}
value := fmt.Sprintf("%v", v.Field(i).Interface())
value = strings.Replace(value, ",", "\\,", -1)
value = strings.Replace(value, "\n", "\\\n", -1)
res += value + ","
}
return stripComa(res) + "\n"
}
func unmarshalTuple(attrs []string, line string, out reflect.Value) error {
if out.Kind() != reflect.Ptr || out.Elem().Kind() == reflect.Ptr {
return fmt.Errorf("expected a pointer type, got '%v'", out.Type())
}
fields := 0
out = reflect.Indirect(out)
for i, strVal := range split(line, ',') {
if i < len(attrs) {
strVal = strings.Replace(strVal, "\\,", ",", -1)
strVal = strings.Replace(strVal, "\\\n", "\n", -1)
attr := out.FieldByName(attrs[i])
if !attr.IsValid() || !attr.CanSet() {
return fmt.Errorf("cannot set attribute '%s'", attrs[i])
}
switch attr.Type().Kind() {
case reflect.Int, reflect.Uint, reflect.Int32, reflect.Uint32:
v, err := strconv.ParseInt(strVal, 10, 32)
if err != nil {
return fmt.Errorf("invalid integer value %s:'%s'", attrs[i], strVal)
}
attr.SetInt(v)
case reflect.Int64, reflect.Uint64:
v, err := strconv.ParseInt(strVal, 10, 64)
if err != nil {
return fmt.Errorf("invalid long value %s:'%s'", attrs[i], strVal)
}
attr.SetInt(v)
case reflect.Float32, reflect.Float64:
v, err := strconv.ParseFloat(strVal, 64)
if err != nil {
return fmt.Errorf("invalid real value %s:'%s'", attrs[i], strVal)
}
attr.SetFloat(v)
case reflect.String:
attr.SetString(strVal)
default:
return fmt.Errorf("invalid attribute type %s:%v", attrs[i], attr.Type())
}
fields++
} else {
return fmt.Errorf("tuple contains more attributes than expected '%v'", line)
}
}
if fields != len(attrs) {
return fmt.Errorf("tuple contains less attributes than expected '%v'", line)
}
return nil
}
func unmarshal(rel string, v interface{}) error {
out := reflect.ValueOf(v)
if strings.TrimSpace(rel) == "" && !out.IsValid() {
return nil
}
if out.Kind() != reflect.Ptr {
return fmt.Errorf("output should be a pointer (got '%v')", reflect.TypeOf(v))
}
lines := split(rel, '\n')
attrs := make([]string, 0)
for _, a := range split(lines[0], ',') {
attrs = append(attrs, attrUpper(a))
}
out = reflect.Indirect(out)
out.Set(reflect.MakeSlice(out.Type(), 0, len(lines)-1))
for i := 1; i < len(lines); i++ {
if strings.TrimSpace(lines[i]) == "" {
continue
}
t := out.Type().Elem()
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
tuple := reflect.New(t)
if err := unmarshalTuple(attrs, lines[i], tuple); err != nil {
return err
}
pos := out.Len()
out.SetLen(pos + 1)
elem := out.Index(pos)
if elem.Kind() == reflect.Ptr {
elem.Set(tuple)
} else {
elem.Set(reflect.Indirect(tuple))
}
}
return nil
}
func split(str string, sep byte) []string {
const esc = byte('\\')
prev := byte('\000')
res := make([]string, 0)
elem := make([]byte, 0)
for i := 0; i < len(str); i++ {
if str[i] == sep && prev != esc {
res = append(res, string(elem))
elem = make([]byte, 0)
} else {
elem = append(elem, str[i])
}
if prev == esc {
prev = byte('\000')
} else {
prev = str[i]
}
}
if len(str) == 0 || len(elem) != 0 || (str[len(str)-1] == sep && prev != esc) {
res = append(res, string(elem))
}
return res
}