go.mercari.io/datastore@v1.8.2/testsuite/favcliptools/model_json.go (about) 1 // Code generated by jwg -output model_json.go -transcripttag swagger .; DO NOT EDIT 2 3 package favcliptools 4 5 import ( 6 "encoding/json" 7 ) 8 9 // UserJSON is jsonized struct for User. 10 type UserJSON struct { 11 ID userID `json:"id,omitempty"` 12 Name string `json:"name,omitempty"` 13 MentorID userID `json:"mentorID,omitempty"` 14 } 15 16 // UserJSONList is synonym about []*UserJSON. 17 type UserJSONList []*UserJSON 18 19 // UserPropertyEncoder is property encoder for [1]sJSON. 20 type UserPropertyEncoder func(src *User, dest *UserJSON) error 21 22 // UserPropertyDecoder is property decoder for [1]sJSON. 23 type UserPropertyDecoder func(src *UserJSON, dest *User) error 24 25 // UserPropertyInfo stores property information. 26 type UserPropertyInfo struct { 27 fieldName string 28 jsonName string 29 Encoder UserPropertyEncoder 30 Decoder UserPropertyDecoder 31 } 32 33 // FieldName returns struct field name of property. 34 func (info *UserPropertyInfo) FieldName() string { 35 return info.fieldName 36 } 37 38 // JSONName returns json field name of property. 39 func (info *UserPropertyInfo) JSONName() string { 40 return info.jsonName 41 } 42 43 // UserJSONBuilder convert between User to UserJSON mutually. 44 type UserJSONBuilder struct { 45 _properties map[string]*UserPropertyInfo 46 _jsonPropertyMap map[string]*UserPropertyInfo 47 _structPropertyMap map[string]*UserPropertyInfo 48 ID *UserPropertyInfo 49 Name *UserPropertyInfo 50 MentorID *UserPropertyInfo 51 } 52 53 // NewUserJSONBuilder make new UserJSONBuilder. 54 func NewUserJSONBuilder() *UserJSONBuilder { 55 jb := &UserJSONBuilder{ 56 _properties: map[string]*UserPropertyInfo{}, 57 _jsonPropertyMap: map[string]*UserPropertyInfo{}, 58 _structPropertyMap: map[string]*UserPropertyInfo{}, 59 ID: &UserPropertyInfo{ 60 fieldName: "ID", 61 jsonName: "id", 62 Encoder: func(src *User, dest *UserJSON) error { 63 if src == nil { 64 return nil 65 } 66 dest.ID = src.ID 67 return nil 68 }, 69 Decoder: func(src *UserJSON, dest *User) error { 70 if src == nil { 71 return nil 72 } 73 dest.ID = src.ID 74 return nil 75 }, 76 }, 77 Name: &UserPropertyInfo{ 78 fieldName: "Name", 79 jsonName: "name", 80 Encoder: func(src *User, dest *UserJSON) error { 81 if src == nil { 82 return nil 83 } 84 dest.Name = src.Name 85 return nil 86 }, 87 Decoder: func(src *UserJSON, dest *User) error { 88 if src == nil { 89 return nil 90 } 91 dest.Name = src.Name 92 return nil 93 }, 94 }, 95 MentorID: &UserPropertyInfo{ 96 fieldName: "MentorID", 97 jsonName: "mentorID", 98 Encoder: func(src *User, dest *UserJSON) error { 99 if src == nil { 100 return nil 101 } 102 dest.MentorID = src.MentorID 103 return nil 104 }, 105 Decoder: func(src *UserJSON, dest *User) error { 106 if src == nil { 107 return nil 108 } 109 dest.MentorID = src.MentorID 110 return nil 111 }, 112 }, 113 } 114 jb._structPropertyMap["ID"] = jb.ID 115 jb._jsonPropertyMap["id"] = jb.ID 116 jb._structPropertyMap["Name"] = jb.Name 117 jb._jsonPropertyMap["name"] = jb.Name 118 jb._structPropertyMap["MentorID"] = jb.MentorID 119 jb._jsonPropertyMap["mentorID"] = jb.MentorID 120 return jb 121 } 122 123 // Properties returns all properties on UserJSONBuilder. 124 func (b *UserJSONBuilder) Properties() []*UserPropertyInfo { 125 return []*UserPropertyInfo{ 126 b.ID, 127 b.Name, 128 b.MentorID, 129 } 130 } 131 132 // AddAll adds all property to UserJSONBuilder. 133 func (b *UserJSONBuilder) AddAll() *UserJSONBuilder { 134 b._properties["ID"] = b.ID 135 b._properties["Name"] = b.Name 136 b._properties["MentorID"] = b.MentorID 137 return b 138 } 139 140 // Add specified property to UserJSONBuilder. 141 func (b *UserJSONBuilder) Add(infos ...*UserPropertyInfo) *UserJSONBuilder { 142 for _, info := range infos { 143 b._properties[info.fieldName] = info 144 } 145 return b 146 } 147 148 // AddByJSONNames add properties to UserJSONBuilder by JSON property name. if name is not in the builder, it will ignore. 149 func (b *UserJSONBuilder) AddByJSONNames(names ...string) *UserJSONBuilder { 150 for _, name := range names { 151 info := b._jsonPropertyMap[name] 152 if info == nil { 153 continue 154 } 155 b._properties[info.fieldName] = info 156 } 157 return b 158 } 159 160 // AddByNames add properties to UserJSONBuilder by struct property name. if name is not in the builder, it will ignore. 161 func (b *UserJSONBuilder) AddByNames(names ...string) *UserJSONBuilder { 162 for _, name := range names { 163 info := b._structPropertyMap[name] 164 if info == nil { 165 continue 166 } 167 b._properties[info.fieldName] = info 168 } 169 return b 170 } 171 172 // Remove specified property to UserJSONBuilder. 173 func (b *UserJSONBuilder) Remove(infos ...*UserPropertyInfo) *UserJSONBuilder { 174 for _, info := range infos { 175 delete(b._properties, info.fieldName) 176 } 177 return b 178 } 179 180 // RemoveByJSONNames remove properties to UserJSONBuilder by JSON property name. if name is not in the builder, it will ignore. 181 func (b *UserJSONBuilder) RemoveByJSONNames(names ...string) *UserJSONBuilder { 182 183 for _, name := range names { 184 info := b._jsonPropertyMap[name] 185 if info == nil { 186 continue 187 } 188 delete(b._properties, info.fieldName) 189 } 190 return b 191 } 192 193 // RemoveByNames remove properties to UserJSONBuilder by struct property name. if name is not in the builder, it will ignore. 194 func (b *UserJSONBuilder) RemoveByNames(names ...string) *UserJSONBuilder { 195 for _, name := range names { 196 info := b._structPropertyMap[name] 197 if info == nil { 198 continue 199 } 200 delete(b._properties, info.fieldName) 201 } 202 return b 203 } 204 205 // Convert specified non-JSON object to JSON object. 206 func (b *UserJSONBuilder) Convert(orig *User) (*UserJSON, error) { 207 if orig == nil { 208 return nil, nil 209 } 210 ret := &UserJSON{} 211 212 for _, info := range b._properties { 213 if err := info.Encoder(orig, ret); err != nil { 214 return nil, err 215 } 216 } 217 218 return ret, nil 219 } 220 221 // ConvertList specified non-JSON slice to JSONList. 222 func (b *UserJSONBuilder) ConvertList(orig []*User) (UserJSONList, error) { 223 if orig == nil { 224 return nil, nil 225 } 226 227 list := make(UserJSONList, len(orig)) 228 for idx, or := range orig { 229 json, err := b.Convert(or) 230 if err != nil { 231 return nil, err 232 } 233 list[idx] = json 234 } 235 236 return list, nil 237 } 238 239 // Convert specified JSON object to non-JSON object. 240 func (orig *UserJSON) Convert() (*User, error) { 241 ret := &User{} 242 243 b := NewUserJSONBuilder().AddAll() 244 for _, info := range b._properties { 245 if err := info.Decoder(orig, ret); err != nil { 246 return nil, err 247 } 248 } 249 250 return ret, nil 251 } 252 253 // Convert specified JSONList to non-JSON slice. 254 func (jsonList UserJSONList) Convert() ([]*User, error) { 255 orig := ([]*UserJSON)(jsonList) 256 257 list := make([]*User, len(orig)) 258 for idx, or := range orig { 259 obj, err := or.Convert() 260 if err != nil { 261 return nil, err 262 } 263 list[idx] = obj 264 } 265 266 return list, nil 267 } 268 269 // Marshal non-JSON object to JSON string. 270 func (b *UserJSONBuilder) Marshal(orig *User) ([]byte, error) { 271 ret, err := b.Convert(orig) 272 if err != nil { 273 return nil, err 274 } 275 return json.Marshal(ret) 276 }