forked from vmihailenco/msgpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appengine.go
69 lines (58 loc) · 1.37 KB
/
appengine.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
// +build appengine
package msgpack
import (
"reflect"
ds "google.golang.org/appengine/datastore"
)
var (
keyPtrType = reflect.TypeOf((*ds.Key)(nil))
cursorType = reflect.TypeOf((*ds.Cursor)(nil)).Elem()
)
func init() {
Register(keyPtrType, encodeDatastoreKeyValue, decodeDatastoreKeyValue)
Register(cursorType, encodeDatastoreCursorValue, decodeDatastoreCursorValue)
}
func EncodeDatastoreKey(e *Encoder, key *ds.Key) error {
if key == nil {
return e.EncodeNil()
}
return e.EncodeString(key.Encode())
}
func encodeDatastoreKeyValue(e *Encoder, v reflect.Value) error {
key := v.Interface().(*ds.Key)
return EncodeDatastoreKey(e, key)
}
func DecodeDatastoreKey(d *Decoder) (*ds.Key, error) {
v, err := d.DecodeString()
if err != nil {
return nil, err
}
if v == "" {
return nil, nil
}
return ds.DecodeKey(v)
}
func decodeDatastoreKeyValue(d *Decoder, v reflect.Value) error {
key, err := DecodeDatastoreKey(d)
if err != nil {
return err
}
v.Set(reflect.ValueOf(key))
return nil
}
func encodeDatastoreCursorValue(e *Encoder, v reflect.Value) error {
cursor := v.Interface().(ds.Cursor)
return e.Encode(cursor.String())
}
func decodeDatastoreCursorValue(d *Decoder, v reflect.Value) error {
s, err := d.DecodeString()
if err != nil {
return err
}
cursor, err := ds.DecodeCursor(s)
if err != nil {
return err
}
v.Set(reflect.ValueOf(cursor))
return nil
}