forked from crestonbunch/godata
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathresponse_model.go
104 lines (96 loc) · 2.67 KB
/
response_model.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
package godata
import (
"bytes"
"strconv"
)
// A response is a dictionary of keys to their corresponding fields. This will
// be converted into a JSON dictionary in the response to the web client.
type GoDataResponse struct {
Fields map[string]*GoDataResponseField
}
// Serialize the result as JSON for sending to the client. If an error
// occurs during the serialization, it will be returned.
func (r *GoDataResponse) Json() ([]byte, error) {
result, err := prepareJsonDict(r.Fields)
if err != nil {
return nil, err
}
return result, nil
}
// A response that is a primitive JSON type or a list or a dictionary. When
// writing to JSON, it is automatically mapped from the Go type to a suitable
// JSON data type. Any type can be used, but if the data type is not supported
// for serialization, then an error is thrown.
type GoDataResponseField struct {
Value interface{}
}
// Convert the response field to a JSON serialized form. If the type is not
// string, []byte, int, float64, map[string]*GoDataResponseField, or
// []*GoDataResponseField, then an error will be thrown.
func (f *GoDataResponseField) Json() ([]byte, error) {
switch f.Value.(type) {
case string:
return prepareJsonString([]byte(f.Value.(string)))
case []byte:
return prepareJsonString(f.Value.([]byte))
case int:
return []byte(strconv.Itoa(f.Value.(int))), nil
case float64:
return []byte(strconv.FormatFloat(f.Value.(float64), 'f', -1, 64)), nil
case map[string]*GoDataResponseField:
return prepareJsonDict(f.Value.(map[string]*GoDataResponseField))
case []*GoDataResponseField:
return prepareJsonList(f.Value.([]*GoDataResponseField))
default:
return nil, InternalServerError("Response field type not recognized.")
}
}
func prepareJsonString(s []byte) ([]byte, error) {
// escape double quotes
s = bytes.Replace(s, []byte("\""), []byte("\\\""), -1)
var buf bytes.Buffer
buf.WriteByte('"')
buf.Write(s)
buf.WriteByte('"')
return buf.Bytes(), nil
}
func prepareJsonDict(d map[string]*GoDataResponseField) ([]byte, error) {
var buf bytes.Buffer
buf.WriteByte('{')
count := 0
for k, v := range d {
buf.WriteByte('"')
buf.Write([]byte(k))
buf.WriteByte('"')
buf.WriteByte(':')
field, err := v.Json()
if err != nil {
return nil, err
}
buf.Write(field)
count++
if count < len(d) {
buf.WriteByte(',')
}
}
buf.WriteByte('}')
return buf.Bytes(), nil
}
func prepareJsonList(l []*GoDataResponseField) ([]byte, error) {
var buf bytes.Buffer
buf.WriteByte('[')
count := 0
for _, v := range l {
field, err := v.Json()
if err != nil {
return nil, err
}
buf.Write(field)
count++
if count < len(l) {
buf.WriteByte(',')
}
}
buf.WriteByte(']')
return buf.Bytes(), nil
}