-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencoder.go
271 lines (242 loc) · 5.48 KB
/
encoder.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
package rison
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"sort"
"strings"
)
// Marshal returns the Rison encoding of v.
//
// The object keys corresponding the struct fields can be
// specified in struct tag (not "rison" but) "json".
func Marshal(v interface{}, m Mode) ([]byte, error) {
j, err := json.Marshal(v)
if err != nil {
return nil, err
}
return FromJSON(j, m)
}
// FromJSON parses the JSON-encoded data and returns the
// Rison-encoded data that expresses the equal value.
func FromJSON(data []byte, m Mode) ([]byte, error) {
return (&encoder{Mode: m}).encode(data)
}
// Encode is an alias of Marshal.
func Encode(v interface{}, m Mode) ([]byte, error) {
return Marshal(v, m)
}
type encoder struct {
Mode Mode
buffer *bytes.Buffer
}
func checkKindMatchesMode(kind reflect.Kind, mode Mode) error {
switch mode {
case ORison:
if kind != reflect.Map {
return fmt.Errorf("only a struct or a map[string] can be encoded to the O-Rison")
}
case ARison:
if !(kind == reflect.Slice || kind == reflect.Array) {
return fmt.Errorf("only a slice or an array can be encoded to the A-Rison")
}
}
return nil
}
func convertRisonToMode(r []byte, mode Mode) ([]byte, error) {
n := len(r)
switch mode {
case ORison:
if !(3 <= n && r[0] == '(' && r[n-1] == ')') {
return nil, fmt.Errorf("failed to encode the value to the O-Rison")
}
r = r[1 : n-1]
case ARison:
if !(4 <= n && r[0] == '!' && r[1] == '(' && r[n-1] == ')') {
return nil, fmt.Errorf("failed to encode the value to the A-Rison")
}
r = r[2 : n-1]
}
return r, nil
}
func (e *encoder) encode(data []byte) ([]byte, error) {
e.buffer = bytes.NewBuffer([]byte{})
var v interface{}
err := json.Unmarshal(data, &v)
if err != nil {
return nil, err
}
vv := reflect.ValueOf(v)
err = checkKindMatchesMode(vv.Kind(), e.Mode)
if err != nil {
return nil, err
}
if bytes.Equal(data, []byte("null")) {
return []byte("!n"), nil
}
if !vv.IsValid() {
return nil, fmt.Errorf("invalid JSON: %s", string(data))
}
err = e.encodeValue("", vv)
if err != nil {
return nil, err
}
r := e.buffer.Bytes()
e.buffer = nil
return convertRisonToMode(r, e.Mode)
}
func idOk(s string) bool {
n := len(s)
if n == 0 {
return false
}
if 0 <= strings.IndexByte(notIDStart, s[0]) {
return false
}
for i := 1; i < n; i++ {
if 0 <= strings.IndexByte(notIDChar, s[i]) {
return false
}
}
return true
}
func (e *encoder) writeString(v reflect.Value) bool {
if !v.CanInterface() {
return false
}
s, ok := v.Interface().(string)
if !ok {
return false
}
if idOk(s) {
e.buffer.WriteString(s)
return true
}
n := len(s)
e.buffer.WriteByte('\'')
for i := 0; i < n; i++ {
c := s[i]
if c == '\'' || c == '!' {
e.buffer.WriteByte('!')
}
e.buffer.WriteByte(c)
}
e.buffer.WriteByte('\'')
return true
}
func (e *encoder) encodeBool(path string, v reflect.Value) error {
if !v.CanInterface() {
return fmt.Errorf("internal error")
}
b, ok := v.Interface().(bool)
if !ok {
return fmt.Errorf("internal error")
}
if b {
e.buffer.WriteString("!t")
} else {
e.buffer.WriteString("!f")
}
return nil
}
func (e *encoder) encodeNumber(path string, v reflect.Value) error {
if !v.CanInterface() {
return fmt.Errorf("internal error")
}
j, err := json.Marshal(v.Interface())
if err != nil {
return err
}
j = bytes.Replace(j, []byte{'+'}, []byte{}, -1)
e.buffer.Write(j)
return nil
}
func (e *encoder) encodeMap(path string, v reflect.Value) error {
e.buffer.WriteByte('(')
keys := v.MapKeys()
sort.Slice(keys, func(i, j int) bool {
if !keys[i].CanInterface() {
return false
}
ki, ok := keys[i].Interface().(string)
if !ok {
return false
}
if !keys[j].CanInterface() {
return true
}
kj, ok := keys[j].Interface().(string)
if !ok {
return true
}
return ki < kj
})
for i, k := range keys {
if 0 < i {
e.buffer.WriteByte(',')
}
if !e.writeString(k) {
return fmt.Errorf(`invalid key %+v`, k)
}
e.buffer.WriteByte(':')
err := e.encodeValue(path+"."+k.Interface().(string), v.MapIndex(k))
if err != nil {
return err
}
}
e.buffer.WriteByte(')')
return nil
}
func (e *encoder) encodeArray(path string, v reflect.Value) error {
e.buffer.WriteString("!(")
for i := 0; i < v.Len(); i++ {
if 0 < i {
e.buffer.WriteByte(',')
}
err := e.encodeValue(fmt.Sprintf("%s[%d]", path, i), v.Index(i))
if err != nil {
return err
}
}
e.buffer.WriteByte(')')
return nil
}
func (e *encoder) encodeValue(path string, v reflect.Value) error {
var errDetail error
switch v.Kind() {
case reflect.Bool:
errDetail = e.encodeBool(path, v)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
errDetail = e.encodeNumber(path, v)
case reflect.String:
if !e.writeString(v) {
errDetail = fmt.Errorf("internal error")
}
case reflect.Map:
errDetail = e.encodeMap(path, v)
case reflect.Slice, reflect.Array:
errDetail = e.encodeArray(path, v)
case reflect.Ptr, reflect.Interface:
if v.IsNil() {
e.buffer.WriteString("!n")
return nil
}
return e.encodeValue(path, v.Elem())
default:
errDetail = fmt.Errorf("%s is non-supported kind", v.Kind())
}
if errDetail == nil {
return nil
}
if path == "" {
path = "."
}
var vi interface{} = v
if v.IsValid() && v.CanInterface() {
vi = v.Interface()
}
return fmt.Errorf("non-encodable %s value at %s in %+v: %s", v.Kind(), path, vi, errDetail.Error())
}