/
main.go
162 lines (134 loc) · 3.15 KB
/
main.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
//go:generate jwg -output model_json.go -transcripttag swagger .
//go:generate qbg -output model_query.go -usedatastorewrapper .
package favcliptools
import (
"context"
"encoding/json"
"fmt"
"testing"
"go.mercari.io/datastore"
"go.mercari.io/datastore/boom"
"go.mercari.io/datastore/testsuite"
)
var _ datastore.PropertyTranslator = UserID(0)
var _ json.Marshaler = UserID(1)
var _ json.Unmarshaler = (*UserID)(nil)
type contextClient struct{}
const kindUser = "User"
type UserID int64
// User kind
// +jwg
// +qbg
type User struct {
ID UserID `datastore:"-" boom:"id" json:"id"`
Name string `json:"name"`
MentorID UserID `json:"mentorID"`
}
func (id UserID) ToPropertyValue(ctx context.Context) (interface{}, error) {
client := ctx.Value(contextClient{}).(datastore.Client)
key := client.IDKey(kindUser, int64(id), nil)
return key, nil
}
func (id UserID) FromPropertyValue(ctx context.Context, p datastore.Property) (dst interface{}, err error) {
key, ok := p.Value.(datastore.Key)
if !ok {
return nil, datastore.ErrInvalidEntityType
}
return UserID(key.ID()), nil
}
func (id UserID) MarshalJSON() ([]byte, error) {
jsonNumber := json.Number(fmt.Sprintf("%d", int64(id)))
return json.Marshal(jsonNumber)
}
func (id *UserID) UnmarshalJSON(b []byte) error {
var jsonNumber json.Number
err := json.Unmarshal(b, &jsonNumber)
if err != nil {
return err
}
v, err := jsonNumber.Int64()
if err != nil {
return err
}
*id = UserID(v)
return nil
}
var TestSuite = map[string]testsuite.Test{
"FavclipTools": FavclipTools,
}
func init() {
testsuite.MergeTestSuite(TestSuite)
}
func FavclipTools(t *testing.T, ctx context.Context, client datastore.Client) {
defer func() {
err := client.Close()
if err != nil {
t.Fatal(err)
}
}()
// qbg using client.Context internally
ctx = context.WithValue(ctx, contextClient{}, client)
client.SwapContext(ctx)
bm := boom.FromClient(ctx, client)
user := &User{
ID: UserID(100),
Name: "foobar",
MentorID: UserID(200),
}
_, err := bm.Put(user)
if err != nil {
t.Fatal(err)
}
b, err := json.Marshal(user)
if err != nil {
t.Fatal(err)
}
if v := string(b); v != `{"id":100,"name":"foobar","mentorID":200}` {
t.Errorf("unexpected: %v", v)
}
user = &User{}
err = json.Unmarshal(b, user)
if err != nil {
t.Fatal(err)
}
if v := int64(user.ID); v != 100 {
t.Errorf("unexpected: %v", v)
}
if v := user.Name; v != "foobar" {
t.Errorf("unexpected: %v", v)
}
if v := int64(user.MentorID); v != 200 {
t.Errorf("unexpected: %v", v)
}
{ // for jwg
b := NewUserJSONBuilder()
b.Add(b.ID)
b.Add(b.MentorID)
userJSON, err := b.Convert(user)
if err != nil {
t.Fatal(err)
}
if v := int64(userJSON.ID); v != 100 {
t.Errorf("unexpected: %v", v)
}
// removed
if v := userJSON.Name; v != "" {
t.Errorf("unexpected: %v", v)
}
if v := int64(userJSON.MentorID); v != 200 {
t.Errorf("unexpected: %v", v)
}
}
{ // for qbg
b := NewUserQueryBuilder(client)
b.MentorID.Equal(UserID(200))
var list []*User
_, err = bm.GetAll(b.Query(), &list)
if err != nil {
t.Fatal(err)
}
if v := len(list); v != 1 {
t.Errorf("unexpected: %v", v)
}
}
}