-
Notifications
You must be signed in to change notification settings - Fork 19
/
circle_model.go
101 lines (80 loc) · 2.65 KB
/
circle_model.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
package tbf
import (
"context"
"errors"
"fmt"
"time"
"go.mercari.io/datastore"
)
var _ datastore.PropertyTranslator = CircleID(0)
var _ datastore.KeyLoader = &Circle{}
const kindCircle = "Circle"
// CircleID means ID of Circle kind.
type CircleID int64
// Circle represents information on participating organizations.
type Circle struct {
ID CircleID `json:"id" datastore:"-"`
Name string `json:"name"`
ImageIDs []imageID `json:"-"`
Images []*Image `json:"images" datastore:"-"`
CreatedAt unixTime `json:"createdAt"`
UpdatedAt unixTime `json:"updatedAt"`
}
func keyToCircleID(key datastore.Key) (CircleID, error) {
if key.Kind() != kindCircle {
return 0, fmt.Errorf("unexpected kind: %s", key.Kind())
}
return CircleID(key.ID()), nil
}
// ToPropertyValue convert the value to the valid value as the property of datastore.
func (id CircleID) ToPropertyValue(ctx context.Context) (interface{}, error) {
client := ctx.Value(contextClient{}).(datastore.Client)
key := client.IDKey(kindCircle, int64(id), nil)
return key, nil
}
// FromPropertyValue convert property value to the valid value as the application.
func (id CircleID) FromPropertyValue(ctx context.Context, p datastore.Property) (dst interface{}, err error) {
key, ok := p.Value.(datastore.Key)
if !ok {
return nil, datastore.ErrInvalidEntityType
}
return keyToCircleID(key)
}
// ToKey convert the value to datastore.Key.
func (id CircleID) ToKey(client datastore.Client) datastore.Key {
return client.IDKey(kindCircle, int64(id), nil)
}
// Load loads all of the provided properties into struct.
func (circle *Circle) Load(ctx context.Context, ps []datastore.Property) error {
err := datastore.LoadStruct(ctx, circle, ps)
if err != nil {
return err
}
// 子画像をBatchGetしていく
batch, ok := ctx.Value(contextBatch{}).(*datastore.Batch)
if !ok || batch == nil {
return errors.New("can't pickup batch client")
}
client := ctx.Value(contextClient{}).(datastore.Client)
circle.Images = make([]*Image, len(circle.ImageIDs))
for idx, imageID := range circle.ImageIDs {
imageKey := imageID.ToKey(client)
image := &Image{}
circle.Images[idx] = image
batch.Get(imageKey, image, nil)
}
return nil
}
// Save saves all of struct fields to properties.
func (circle *Circle) Save(ctx context.Context) ([]datastore.Property, error) {
if time.Time(circle.CreatedAt).IsZero() {
circle.CreatedAt = unixTime(timeNow())
}
circle.UpdatedAt = unixTime(timeNow())
return datastore.SaveStruct(ctx, circle)
}
// LoadKey loads key data into struct.
func (circle *Circle) LoadKey(ctx context.Context, k datastore.Key) error {
circle.ID = CircleID(k.ID())
return nil
}