-
Notifications
You must be signed in to change notification settings - Fork 20
/
gob.go
107 lines (95 loc) · 3.13 KB
/
gob.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
package testsuite
import (
"bytes"
"context"
"encoding/base64"
"encoding/gob"
"testing"
"go.mercari.io/datastore"
)
func GobDecode(t *testing.T, ctx context.Context, client datastore.Client) {
defer func() {
err := client.Close()
if err != nil {
t.Fatal(err)
}
}()
type Sub struct {
S string
}
type Data struct {
A string
B int
C datastore.GeoPoint
D *Sub
E datastore.Key
}
var b64 string
if false {
var ps datastore.PropertyList
var err error
ps, err = datastore.SaveStruct(ctx, &Data{
A: "A",
B: 2,
C: datastore.GeoPoint{Lat: 1.1, Lng: 2.2},
D: &Sub{S: "S"},
E: client.IDKey("Test", 111, nil),
})
if err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err = enc.Encode(ps)
if err != nil {
t.Fatal(err)
}
b64 = base64.StdEncoding.EncodeToString(buf.Bytes())
t.Log(b64)
} else {
if IsCloudDatastoreClient(ctx) {
b64 = `G/+DAgEBDFByb3BlcnR5TGlzdAH/hAAB/4IAADX/gQMBAQhQcm9wZXJ0eQH/ggABAwEETmFtZQEMAAEFVmFsdWUBEAABB05vSW5kZXgBAgAAAG//hAAFAQFBAQZzdHJpbmcMAwABQQABAUIBBWludDY0BAIABAABAUMBIGdvLm1lcmNhcmkuaW8vZGF0YXN0b3JlLkdlb1BvaW50/4UDAQEIR2VvUG9pbnQB/4YAAQIBA0xhdAEIAAEDTG5nAQgAAABc/4YVAfiamZmZmZnxPwH4mpmZmZmZAUAAAAEBRAERKmRhdGF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CC2Rldn50ZXN0YXBwAAA=`
} else {
t.Fatal("unexpected state")
}
}
b, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer(b)
dec := gob.NewDecoder(buf)
var ps datastore.PropertyList
err = dec.Decode(&ps)
if err != nil {
t.Fatal(err)
}
obj := &Data{}
err = datastore.LoadStruct(ctx, obj, ps)
if err != nil {
t.Fatal(err)
}
if v := obj.A; v != "A" {
t.Errorf("unexpected: %v", v)
}
if v := obj.B; v != 2 {
t.Errorf("unexpected: %v", v)
}
if v := obj.C.Lat; v != 1.1 {
t.Errorf("unexpected: %v", v)
}
if v := obj.C.Lng; v != 2.2 {
t.Errorf("unexpected: %v", v)
}
if v := obj.D.S; v != "S" {
t.Errorf("unexpected: %v", v)
}
if v := obj.E.Kind(); v != "Test" {
t.Errorf("unexpected: %v", v)
}
if v := obj.E.ID(); v != 111 {
t.Errorf("unexpected: %v", v)
}
}