This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
/
marshal.go
315 lines (283 loc) · 7.04 KB
/
marshal.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
// Copyright 2014 Ronoaldo JLP <ronoaldo@gmail.com>
// Licensed under the Apache License, Version 2.0
package aetools
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"math"
"regexp"
"strconv"
"strings"
"time"
)
// MarshalJSON implements the json.Marshaller interface by dumping
// the entity key and properties.
func (e *Entity) MarshalJSON() ([]byte, error) {
m, err := e.Map()
if err != nil {
return nil, err
}
b := new(bytes.Buffer)
enc := json.NewEncoder(b)
err = enc.Encode(m)
return b.Bytes(), err
}
// Map converts the entity data into a JSON compatible map.
// Usefull to manually encode the entity using different
// marshallers than the JSON built-in.
func (e *Entity) Map() (map[string]interface{}, error) {
m := make(map[string]interface{})
m["__key__"] = encodeKey(e.Key)
add := func(multi bool, n string, v interface{}) {
if multi {
a := m[n].([]interface{})
m[n] = append(a, v)
} else {
m[n] = v
}
}
for _, p := range e.Properties {
// Check if multi property is consistent so it's safe to append
if p.Multiple {
if _, ok := m[p.Name]; !ok {
m[p.Name] = make([]interface{}, 0)
} else {
if _, ok := m[p.Name].([]interface{}); !ok {
return nil, fmt.Errorf("aetools: %s with invalid Multiple values", p.Name)
}
}
}
switch p.Value.(type) {
case int, int32, int64:
if p.NoIndex {
add(p.Multiple, p.Name, toMap("int", p.NoIndex, p.Value))
} else {
add(p.Multiple, p.Name, p.Value)
}
case float32, float64:
if p.NoIndex {
add(p.Multiple, p.Name, toMap("float", p.NoIndex, float(p.Value.(float64))))
} else {
add(p.Multiple, p.Name, float(p.Value.(float64)))
}
case string:
if p.NoIndex {
add(p.Multiple, p.Name, toMap("string", p.NoIndex, p.Value))
} else {
add(p.Multiple, p.Name, p.Value)
}
case bool:
if p.NoIndex {
add(p.Multiple, p.Name, toMap("bool", p.NoIndex, p.Value))
} else {
add(p.Multiple, p.Name, p.Value)
}
case *datastore.Key:
v := toMap("key", p.NoIndex, encodeKey(p.Value.(*datastore.Key)))
add(p.Multiple, p.Name, v)
case appengine.BlobKey:
v := toMap("blobkey", p.NoIndex, string(p.Value.(appengine.BlobKey)))
add(p.Multiple, p.Name, v)
case time.Time:
s := p.Value.(time.Time).Format(DateTimeFormat)
v := toMap("date", p.NoIndex, s)
add(p.Multiple, p.Name, v)
case []byte:
s := base64.URLEncoding.EncodeToString(p.Value.([]byte))
v := toMap("blob", p.NoIndex, s)
add(p.Multiple, p.Name, v)
default:
if p.Value != nil {
return nil, fmt.Errorf("aetools: invalid property value %s: %#v", p.Name, p.Value)
}
if p.NoIndex {
add(p.Multiple, p.Name, toMap("", p.NoIndex, p.Value))
} else {
add(p.Multiple, p.Name, p.Value)
}
}
}
return m, nil
}
// KeyPath returns a representation of a Key as a string with each value
// in the key path separated by a coma.
// The key representation has all ancestors,
// but has no information about namespaces or ancestors.
func KeyPath(k *datastore.Key) string {
b := new(bytes.Buffer)
path := encodeKey(k)
for i := range path {
fmt.Fprintf(b, "%v", path[i])
if i < len(path)-1 {
fmt.Fprint(b, ",")
}
}
return b.String()
}
type float float64
var hasDecimalPoint = regexp.MustCompile(".*[.eE].*")
func (f float) MarshalJSON() ([]byte, error) {
var b bytes.Buffer
if math.IsInf(float64(f), 0) || math.IsNaN(float64(f)) {
return nil, fmt.Errorf("aetools: unsuported float value: %#v", f)
}
b.Write(strconv.AppendFloat(b.Bytes(), float64(f), 'g', -1, 64))
if !hasDecimalPoint.Match(b.Bytes()) {
b.WriteString(".0")
}
return b.Bytes(), nil
}
func decodeJSONPrimitiveValue(v interface{}, p *datastore.Property) error {
switch v.(type) {
case json.Number:
n := v.(json.Number)
if strings.Contains(n.String(), ".") {
// float64
p.Value, _ = n.Float64()
} else {
// int64
p.Value, _ = n.Int64()
}
case string:
p.Value = v.(string)
case bool:
p.Value = v.(bool)
case nil:
p.Value = nil
default:
return fmt.Errorf("Invalid primitive value: %#v", v)
}
return nil
}
func encodeKey(k *datastore.Key) []interface{} {
path := make([]*datastore.Key, 0)
tmp := k
for tmp != nil {
path = append(path, tmp)
tmp = tmp.Parent()
}
r := make([]interface{}, 0, 2*len(path))
for i := len(path) - 1; i >= 0; i-- {
tmp = path[i]
r = append(r, tmp.Kind())
if !tmp.Incomplete() {
if tmp.StringID() != "" {
r = append(r, tmp.StringID())
} else {
r = append(r, tmp.IntID())
}
} else {
r = append(r, nil)
}
}
return r
}
func decodeKey(c context.Context, v interface{}) (*datastore.Key, error) {
var result, ancestor *datastore.Key
p, ok := v.([]interface{})
if !ok {
return nil, ErrInvalidKeyElement
}
for i := 0; i < len(p); i += 2 {
kind := p[i].(string)
id := p[i+1]
switch id.(type) {
case string:
result = datastore.NewKey(c, kind, id.(string), 0, ancestor)
case json.Number:
n, err := id.(json.Number).Int64()
if err != nil {
return nil, invalidIDError(id)
}
result = datastore.NewKey(c, kind, "", n, ancestor)
default:
return nil, invalidIDError(id)
}
ancestor = result
}
return result, nil
}
func toMap(t string, noIndex bool, v interface{}) map[string]interface{} {
m := make(map[string]interface{}, 3)
m["value"] = v
m["type"] = t
m["indexed"] = !noIndex
if t == "" {
delete(m, "type")
}
return m
}
func decodeProperty(c context.Context, k string, v interface{}, e *Entity) error {
var p datastore.Property
p.Name = k
var err error
switch v.(type) {
// Try to decode property object
case map[string]interface{}:
// Decode custom type
m := v.(map[string]interface{})
t, ok := m["type"]
if !ok {
t = "primitive"
}
if index, ok := m["indexed"]; ok {
if i, ok := index.(bool); ok {
p.NoIndex = !i
}
}
switch t {
case "key":
key, err := decodeKey(c, m["value"])
if err != nil {
return err
}
p.Value = key
case "blobkey":
v, ok := m["value"].(string)
if !ok {
return newDecodePropertyError(k, "blobkey", v)
}
p.Value = appengine.BlobKey(v)
case "blob":
v, ok := m["value"].(string)
if !ok {
return newDecodePropertyError(k, "date", v)
}
p.Value, err = base64.URLEncoding.DecodeString(v)
if err != nil {
return err
}
case "date":
v, ok := m["value"].(string)
if !ok {
return newDecodePropertyError(k, "date", v)
}
var dt time.Time
dt, err = time.Parse(DateTimeFormat, v)
if err != nil {
return newDecodePropertyError(k, "date", err)
}
p.Value = dt.UTC()
default:
if v, ok := m["value"]; ok {
err = decodeJSONPrimitiveValue(v, &p)
} else {
err = fmt.Errorf("aetools: complex property %s without 'value' attribute", k)
}
}
default:
err = decodeJSONPrimitiveValue(v, &p)
}
if err == nil {
e.Properties = append(e.Properties, p)
}
return err
}
func newDecodePropertyError(name, ptype string, raw interface{}) error {
return fmt.Errorf("aetools: can't decode %s, value is not %s: %s", name, ptype, raw)
}