forked from mercari/datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_json.go
189 lines (164 loc) · 4.13 KB
/
model_json.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
// generated by jwg -output model_json.go -transcripttag swagger .; DO NOT EDIT
package favcliptools
import (
"encoding/json"
)
// UserJSON is jsonized struct for User.
type UserJSON struct {
ID UserID `json:"id,omitempty"`
Name string `json:"name,omitempty"`
MentorID UserID `json:"mentorID,omitempty"`
}
// UserJSONList is synonym about []*UserJSON.
type UserJSONList []*UserJSON
// UserPropertyEncoder is property encoder for [1]sJSON.
type UserPropertyEncoder func(src *User, dest *UserJSON) error
// UserPropertyDecoder is property decoder for [1]sJSON.
type UserPropertyDecoder func(src *UserJSON, dest *User) error
// UserPropertyInfo stores property information.
type UserPropertyInfo struct {
name string
Encoder UserPropertyEncoder
Decoder UserPropertyDecoder
}
// UserJSONBuilder convert between User to UserJSON mutually.
type UserJSONBuilder struct {
_properties map[string]*UserPropertyInfo
ID *UserPropertyInfo
Name *UserPropertyInfo
MentorID *UserPropertyInfo
}
// NewUserJSONBuilder make new UserJSONBuilder.
func NewUserJSONBuilder() *UserJSONBuilder {
return &UserJSONBuilder{
_properties: map[string]*UserPropertyInfo{},
ID: &UserPropertyInfo{
name: "ID",
Encoder: func(src *User, dest *UserJSON) error {
if src == nil {
return nil
}
dest.ID = src.ID
return nil
},
Decoder: func(src *UserJSON, dest *User) error {
if src == nil {
return nil
}
dest.ID = src.ID
return nil
},
},
Name: &UserPropertyInfo{
name: "Name",
Encoder: func(src *User, dest *UserJSON) error {
if src == nil {
return nil
}
dest.Name = src.Name
return nil
},
Decoder: func(src *UserJSON, dest *User) error {
if src == nil {
return nil
}
dest.Name = src.Name
return nil
},
},
MentorID: &UserPropertyInfo{
name: "MentorID",
Encoder: func(src *User, dest *UserJSON) error {
if src == nil {
return nil
}
dest.MentorID = src.MentorID
return nil
},
Decoder: func(src *UserJSON, dest *User) error {
if src == nil {
return nil
}
dest.MentorID = src.MentorID
return nil
},
},
}
}
// AddAll adds all property to UserJSONBuilder.
func (b *UserJSONBuilder) AddAll() *UserJSONBuilder {
b._properties["ID"] = b.ID
b._properties["Name"] = b.Name
b._properties["MentorID"] = b.MentorID
return b
}
// Add specified property to UserJSONBuilder.
func (b *UserJSONBuilder) Add(info *UserPropertyInfo) *UserJSONBuilder {
b._properties[info.name] = info
return b
}
// Remove specified property to UserJSONBuilder.
func (b *UserJSONBuilder) Remove(info *UserPropertyInfo) *UserJSONBuilder {
delete(b._properties, info.name)
return b
}
// Convert specified non-JSON object to JSON object.
func (b *UserJSONBuilder) Convert(orig *User) (*UserJSON, error) {
if orig == nil {
return nil, nil
}
ret := &UserJSON{}
for _, info := range b._properties {
if err := info.Encoder(orig, ret); err != nil {
return nil, err
}
}
return ret, nil
}
// ConvertList specified non-JSON slice to JSONList.
func (b *UserJSONBuilder) ConvertList(orig []*User) (UserJSONList, error) {
if orig == nil {
return nil, nil
}
list := make(UserJSONList, len(orig))
for idx, or := range orig {
json, err := b.Convert(or)
if err != nil {
return nil, err
}
list[idx] = json
}
return list, nil
}
// Convert specified JSON object to non-JSON object.
func (orig *UserJSON) Convert() (*User, error) {
ret := &User{}
b := NewUserJSONBuilder().AddAll()
for _, info := range b._properties {
if err := info.Decoder(orig, ret); err != nil {
return nil, err
}
}
return ret, nil
}
// Convert specified JSONList to non-JSON slice.
func (jsonList UserJSONList) Convert() ([]*User, error) {
orig := ([]*UserJSON)(jsonList)
list := make([]*User, len(orig))
for idx, or := range orig {
obj, err := or.Convert()
if err != nil {
return nil, err
}
list[idx] = obj
}
return list, nil
}
// Marshal non-JSON object to JSON string.
func (b *UserJSONBuilder) Marshal(orig *User) ([]byte, error) {
ret, err := b.Convert(orig)
if err != nil {
return nil, err
}
return json.Marshal(ret)
}