-
Notifications
You must be signed in to change notification settings - Fork 20
/
example_test.go
300 lines (257 loc) · 6.39 KB
/
example_test.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
package datastore_test
import (
"context"
"fmt"
"go.mercari.io/datastore"
"go.mercari.io/datastore/clouddatastore"
"go.mercari.io/datastore/internal/testutils"
)
func Example_clientGet() {
ctx := context.Background()
client, err := clouddatastore.FromContext(ctx)
if err != nil {
panic(err)
}
defer client.Close()
defer testutils.CleanUpAllEntities(ctx, client)
type Data struct {
Name string
}
key := client.IncompleteKey("Data", nil)
entity := &Data{Name: "mercari"}
key, err = client.Put(ctx, key, entity)
if err != nil {
panic(err)
}
entity = &Data{}
err = client.Get(ctx, key, entity)
if err != nil {
panic(err)
}
fmt.Println(entity.Name)
// Output: mercari
}
func Example_batch() {
ctx := context.Background()
client, err := clouddatastore.FromContext(ctx)
if err != nil {
panic(err)
}
defer client.Close()
defer testutils.CleanUpAllEntities(ctx, client)
type Comment struct {
Message string
}
type Post struct {
Content string
CommentIDs []int64 `json:"-"`
Comments []*Comment `datastore:"-"`
}
// preparing entities
for i := 0; i < 4; i++ {
post := &Post{Content: fmt.Sprintf("post #%d", i+1)}
key, err := client.Put(ctx, client.IncompleteKey("Post", nil), post)
if err != nil {
panic(err)
}
for j := 0; j < 5; j++ {
comment := &Comment{Message: fmt.Sprintf("comment #%d", j+1)}
cKey, err := client.Put(ctx, client.IncompleteKey("Comment", nil), comment)
if err != nil {
panic(err)
}
post.CommentIDs = append(post.CommentIDs, cKey.ID())
}
_, err = client.Put(ctx, key, post)
if err != nil {
panic(err)
}
}
// start fetching...
posts := make([]*Post, 0)
_, err = client.GetAll(ctx, client.NewQuery("Post").Order("Content"), &posts)
if err != nil {
panic(err)
}
// Let's batch get!
bt := client.Batch()
for _, post := range posts {
comments := make([]*Comment, 0)
for _, id := range post.CommentIDs {
comment := &Comment{}
bt.Get(client.IDKey("Comment", id, nil), comment, nil)
comments = append(comments, comment)
}
post.Comments = comments
}
err = bt.Exec(ctx)
if err != nil {
panic(err)
}
// check result
for _, post := range posts {
fmt.Println("Post", post.Content)
for _, comment := range post.Comments {
fmt.Println("Comment", comment.Message)
}
}
// Output:
// Post post #1
// Comment comment #1
// Comment comment #2
// Comment comment #3
// Comment comment #4
// Comment comment #5
// Post post #2
// Comment comment #1
// Comment comment #2
// Comment comment #3
// Comment comment #4
// Comment comment #5
// Post post #3
// Comment comment #1
// Comment comment #2
// Comment comment #3
// Comment comment #4
// Comment comment #5
// Post post #4
// Comment comment #1
// Comment comment #2
// Comment comment #3
// Comment comment #4
// Comment comment #5
}
func Example_batchWithBatchErrHandler() {
ctx := context.Background()
client, err := clouddatastore.FromContext(ctx)
if err != nil {
panic(err)
}
defer client.Close()
defer testutils.CleanUpAllEntities(ctx, client)
type Comment struct {
Message string
}
// preparing entities...
// Put ID: 2, 4 into Datastore.
var keys []datastore.Key
for i := 1; i <= 5; i++ {
key := client.IDKey("Comment", int64(i), nil)
keys = append(keys, key)
comment := &Comment{Message: fmt.Sprintf("comment #%d", i)}
if i%2 == 0 {
_, err = client.Put(ctx, key, comment)
if err != nil {
panic(err)
}
}
}
// Let's batch get!
bt := client.Batch()
var comments []*Comment
for _, key := range keys {
comment := &Comment{}
bt.Get(key, comment, func(err error) error {
if err == datastore.ErrNoSuchEntity {
// ignore ErrNoSuchEntity
return nil
} else if err != nil {
return err
}
comments = append(comments, comment)
return nil
})
}
err = bt.Exec(ctx)
if err != nil {
panic(err)
}
// check result
for _, comment := range comments {
fmt.Println("Comment", comment.Message)
}
// Output:
// Comment comment #2
// Comment comment #4
}
var _ datastore.PropertyTranslator = UserID(0)
var _ datastore.PropertyTranslator = UserIDs(nil)
type UserID int64
type UserIDs []UserID
type contextClient struct{}
func (id UserID) ToPropertyValue(ctx context.Context) (interface{}, error) {
// When putting to Datastore, convert to datastore.Key
client := ctx.Value(contextClient{}).(datastore.Client)
key := client.IDKey("User", int64(id), nil)
return key, nil
}
func (id UserID) FromPropertyValue(ctx context.Context, p datastore.Property) (dst interface{}, err error) {
// When getting from Datastore, convert from datastore.Key
key, ok := p.Value.(datastore.Key)
if !ok {
return nil, datastore.ErrInvalidEntityType
}
return UserID(key.ID()), nil
}
func (ids UserIDs) ToPropertyValue(ctx context.Context) (interface{}, error) {
// When putting to Datastore, convert to []datastore.Key
client := ctx.Value(contextClient{}).(datastore.Client)
keys := make([]datastore.Key, 0, len(ids))
for _, id := range ids {
keys = append(keys, client.IDKey("User", int64(id), nil))
}
return keys, nil
}
func (ids UserIDs) FromPropertyValue(ctx context.Context, p datastore.Property) (dst interface{}, err error) {
// When getting from Datastore, convert from datastore.Key
keys, ok := p.Value.([]datastore.Key)
if !ok {
return nil, datastore.ErrInvalidEntityType
}
newIDs := make(UserIDs, 0, len(keys))
for _, key := range keys {
newIDs = append(ids, UserID(key.ID()))
}
return newIDs, nil
}
func ExamplePropertyTranslator() {
ctx := context.Background()
client, err := clouddatastore.FromContext(ctx)
if err != nil {
panic(err)
}
defer client.Close()
defer testutils.CleanUpAllEntities(ctx, client)
ctx = context.WithValue(ctx, contextClient{}, client)
// Each fields are saved as datastore.Key and [] datastore.Key on Datastore.
type Group struct {
OwnerID UserID
MemberIDs UserIDs
}
entity, err := datastore.SaveEntity(
ctx, client.IncompleteKey("Group", nil),
&Group{
OwnerID: 147,
MemberIDs: UserIDs{147, 258, 369},
},
)
if err != nil {
panic(err)
}
if key, ok := entity.Properties[0].Value.(datastore.Key); !ok {
panic("unexpected state")
} else {
fmt.Println("OwnerID", key.ID())
}
if keys, ok := entity.Properties[1].Value.([]datastore.Key); !ok {
panic("unexpected state")
} else {
for _, key := range keys {
fmt.Println("MemberID", key.ID())
}
}
// Output: OwnerID 147
// MemberID 147
// MemberID 258
// MemberID 369
}