forked from ent/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
524 lines (502 loc) · 18.7 KB
/
user.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// Code generated by ent, DO NOT EDIT.
package ent
import (
"encoding/json"
"fmt"
"strings"
"time"
"entgo.io/contrib/entproto/internal/todo/ent/attachment"
"entgo.io/contrib/entproto/internal/todo/ent/group"
"entgo.io/contrib/entproto/internal/todo/ent/pet"
"entgo.io/contrib/entproto/internal/todo/ent/schema"
"entgo.io/contrib/entproto/internal/todo/ent/skipedgeexample"
"entgo.io/contrib/entproto/internal/todo/ent/user"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"github.com/google/uuid"
)
// User is the model entity for the User schema.
type User struct {
config `json:"-"`
// ID of the ent.
ID uint32 `json:"id,omitempty"`
// UserName holds the value of the "user_name" field.
UserName string `json:"user_name,omitempty"`
// Joined holds the value of the "joined" field.
Joined time.Time `json:"joined,omitempty"`
// Points holds the value of the "points" field.
Points uint `json:"points,omitempty"`
// Exp holds the value of the "exp" field.
Exp uint64 `json:"exp,omitempty"`
// Status holds the value of the "status" field.
Status user.Status `json:"status,omitempty"`
// ExternalID holds the value of the "external_id" field.
ExternalID int `json:"external_id,omitempty"`
// CrmID holds the value of the "crm_id" field.
CrmID uuid.UUID `json:"crm_id,omitempty"`
// Banned holds the value of the "banned" field.
Banned bool `json:"banned,omitempty"`
// CustomPb holds the value of the "custom_pb" field.
CustomPb uint8 `json:"custom_pb,omitempty"`
// OptNum holds the value of the "opt_num" field.
OptNum int `json:"opt_num,omitempty"`
// OptStr holds the value of the "opt_str" field.
OptStr string `json:"opt_str,omitempty"`
// OptBool holds the value of the "opt_bool" field.
OptBool bool `json:"opt_bool,omitempty"`
// BigInt holds the value of the "big_int" field.
BigInt schema.BigInt `json:"big_int,omitempty"`
// BUser1 holds the value of the "b_user_1" field.
BUser1 int `json:"b_user_1,omitempty"`
// HeightInCm holds the value of the "height_in_cm" field.
HeightInCm float32 `json:"height_in_cm,omitempty"`
// AccountBalance holds the value of the "account_balance" field.
AccountBalance float64 `json:"account_balance,omitempty"`
// Unnecessary holds the value of the "unnecessary" field.
Unnecessary string `json:"unnecessary,omitempty"`
// Type holds the value of the "type" field.
Type string `json:"type,omitempty"`
// Labels holds the value of the "labels" field.
Labels []string `json:"labels,omitempty"`
// Int32s holds the value of the "int32s" field.
Int32s []int32 `json:"int32s,omitempty"`
// Int64s holds the value of the "int64s" field.
Int64s []int64 `json:"int64s,omitempty"`
// Uint32s holds the value of the "uint32s" field.
Uint32s []uint32 `json:"uint32s,omitempty"`
// Uint64s holds the value of the "uint64s" field.
Uint64s []uint64 `json:"uint64s,omitempty"`
// DeviceType holds the value of the "device_type" field.
DeviceType user.DeviceType `json:"device_type,omitempty"`
// OmitPrefix holds the value of the "omit_prefix" field.
OmitPrefix user.OmitPrefix `json:"omit_prefix,omitempty"`
// MimeType holds the value of the "mime_type" field.
MimeType user.MimeType `json:"mime_type,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.
// The values are being populated by the UserQuery when eager-loading is set.
Edges UserEdges `json:"edges"`
user_group *int
selectValues sql.SelectValues
}
// UserEdges holds the relations/edges for other nodes in the graph.
type UserEdges struct {
// Group holds the value of the group edge.
Group *Group `json:"group,omitempty"`
// Attachment holds the value of the attachment edge.
Attachment *Attachment `json:"attachment,omitempty"`
// Received1 holds the value of the received_1 edge.
Received1 []*Attachment `json:"received_1,omitempty"`
// Pet holds the value of the pet edge.
Pet *Pet `json:"pet,omitempty"`
// SkipEdge holds the value of the skip_edge edge.
SkipEdge *SkipEdgeExample `json:"skip_edge,omitempty"`
// loadedTypes holds the information for reporting if a
// type was loaded (or requested) in eager-loading or not.
loadedTypes [5]bool
}
// GroupOrErr returns the Group value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e UserEdges) GroupOrErr() (*Group, error) {
if e.loadedTypes[0] {
if e.Group == nil {
// Edge was loaded but was not found.
return nil, &NotFoundError{label: group.Label}
}
return e.Group, nil
}
return nil, &NotLoadedError{edge: "group"}
}
// AttachmentOrErr returns the Attachment value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e UserEdges) AttachmentOrErr() (*Attachment, error) {
if e.loadedTypes[1] {
if e.Attachment == nil {
// Edge was loaded but was not found.
return nil, &NotFoundError{label: attachment.Label}
}
return e.Attachment, nil
}
return nil, &NotLoadedError{edge: "attachment"}
}
// Received1OrErr returns the Received1 value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) Received1OrErr() ([]*Attachment, error) {
if e.loadedTypes[2] {
return e.Received1, nil
}
return nil, &NotLoadedError{edge: "received_1"}
}
// PetOrErr returns the Pet value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e UserEdges) PetOrErr() (*Pet, error) {
if e.loadedTypes[3] {
if e.Pet == nil {
// Edge was loaded but was not found.
return nil, &NotFoundError{label: pet.Label}
}
return e.Pet, nil
}
return nil, &NotLoadedError{edge: "pet"}
}
// SkipEdgeOrErr returns the SkipEdge value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e UserEdges) SkipEdgeOrErr() (*SkipEdgeExample, error) {
if e.loadedTypes[4] {
if e.SkipEdge == nil {
// Edge was loaded but was not found.
return nil, &NotFoundError{label: skipedgeexample.Label}
}
return e.SkipEdge, nil
}
return nil, &NotLoadedError{edge: "skip_edge"}
}
// scanValues returns the types for scanning values from sql.Rows.
func (*User) scanValues(columns []string) ([]any, error) {
values := make([]any, len(columns))
for i := range columns {
switch columns[i] {
case user.FieldLabels, user.FieldInt32s, user.FieldInt64s, user.FieldUint32s, user.FieldUint64s:
values[i] = new([]byte)
case user.FieldBigInt:
values[i] = new(schema.BigInt)
case user.FieldBanned, user.FieldOptBool:
values[i] = new(sql.NullBool)
case user.FieldHeightInCm, user.FieldAccountBalance:
values[i] = new(sql.NullFloat64)
case user.FieldID, user.FieldPoints, user.FieldExp, user.FieldExternalID, user.FieldCustomPb, user.FieldOptNum, user.FieldBUser1:
values[i] = new(sql.NullInt64)
case user.FieldUserName, user.FieldStatus, user.FieldOptStr, user.FieldUnnecessary, user.FieldType, user.FieldDeviceType, user.FieldOmitPrefix, user.FieldMimeType:
values[i] = new(sql.NullString)
case user.FieldJoined:
values[i] = new(sql.NullTime)
case user.FieldCrmID:
values[i] = new(uuid.UUID)
case user.ForeignKeys[0]: // user_group
values[i] = new(sql.NullInt64)
default:
values[i] = new(sql.UnknownType)
}
}
return values, nil
}
// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the User fields.
func (u *User) assignValues(columns []string, values []any) error {
if m, n := len(values), len(columns); m < n {
return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
}
for i := range columns {
switch columns[i] {
case user.FieldID:
value, ok := values[i].(*sql.NullInt64)
if !ok {
return fmt.Errorf("unexpected type %T for field id", value)
}
u.ID = uint32(value.Int64)
case user.FieldUserName:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field user_name", values[i])
} else if value.Valid {
u.UserName = value.String
}
case user.FieldJoined:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field joined", values[i])
} else if value.Valid {
u.Joined = value.Time
}
case user.FieldPoints:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field points", values[i])
} else if value.Valid {
u.Points = uint(value.Int64)
}
case user.FieldExp:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field exp", values[i])
} else if value.Valid {
u.Exp = uint64(value.Int64)
}
case user.FieldStatus:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field status", values[i])
} else if value.Valid {
u.Status = user.Status(value.String)
}
case user.FieldExternalID:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field external_id", values[i])
} else if value.Valid {
u.ExternalID = int(value.Int64)
}
case user.FieldCrmID:
if value, ok := values[i].(*uuid.UUID); !ok {
return fmt.Errorf("unexpected type %T for field crm_id", values[i])
} else if value != nil {
u.CrmID = *value
}
case user.FieldBanned:
if value, ok := values[i].(*sql.NullBool); !ok {
return fmt.Errorf("unexpected type %T for field banned", values[i])
} else if value.Valid {
u.Banned = value.Bool
}
case user.FieldCustomPb:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field custom_pb", values[i])
} else if value.Valid {
u.CustomPb = uint8(value.Int64)
}
case user.FieldOptNum:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field opt_num", values[i])
} else if value.Valid {
u.OptNum = int(value.Int64)
}
case user.FieldOptStr:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field opt_str", values[i])
} else if value.Valid {
u.OptStr = value.String
}
case user.FieldOptBool:
if value, ok := values[i].(*sql.NullBool); !ok {
return fmt.Errorf("unexpected type %T for field opt_bool", values[i])
} else if value.Valid {
u.OptBool = value.Bool
}
case user.FieldBigInt:
if value, ok := values[i].(*schema.BigInt); !ok {
return fmt.Errorf("unexpected type %T for field big_int", values[i])
} else if value != nil {
u.BigInt = *value
}
case user.FieldBUser1:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field b_user_1", values[i])
} else if value.Valid {
u.BUser1 = int(value.Int64)
}
case user.FieldHeightInCm:
if value, ok := values[i].(*sql.NullFloat64); !ok {
return fmt.Errorf("unexpected type %T for field height_in_cm", values[i])
} else if value.Valid {
u.HeightInCm = float32(value.Float64)
}
case user.FieldAccountBalance:
if value, ok := values[i].(*sql.NullFloat64); !ok {
return fmt.Errorf("unexpected type %T for field account_balance", values[i])
} else if value.Valid {
u.AccountBalance = value.Float64
}
case user.FieldUnnecessary:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field unnecessary", values[i])
} else if value.Valid {
u.Unnecessary = value.String
}
case user.FieldType:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field type", values[i])
} else if value.Valid {
u.Type = value.String
}
case user.FieldLabels:
if value, ok := values[i].(*[]byte); !ok {
return fmt.Errorf("unexpected type %T for field labels", values[i])
} else if value != nil && len(*value) > 0 {
if err := json.Unmarshal(*value, &u.Labels); err != nil {
return fmt.Errorf("unmarshal field labels: %w", err)
}
}
case user.FieldInt32s:
if value, ok := values[i].(*[]byte); !ok {
return fmt.Errorf("unexpected type %T for field int32s", values[i])
} else if value != nil && len(*value) > 0 {
if err := json.Unmarshal(*value, &u.Int32s); err != nil {
return fmt.Errorf("unmarshal field int32s: %w", err)
}
}
case user.FieldInt64s:
if value, ok := values[i].(*[]byte); !ok {
return fmt.Errorf("unexpected type %T for field int64s", values[i])
} else if value != nil && len(*value) > 0 {
if err := json.Unmarshal(*value, &u.Int64s); err != nil {
return fmt.Errorf("unmarshal field int64s: %w", err)
}
}
case user.FieldUint32s:
if value, ok := values[i].(*[]byte); !ok {
return fmt.Errorf("unexpected type %T for field uint32s", values[i])
} else if value != nil && len(*value) > 0 {
if err := json.Unmarshal(*value, &u.Uint32s); err != nil {
return fmt.Errorf("unmarshal field uint32s: %w", err)
}
}
case user.FieldUint64s:
if value, ok := values[i].(*[]byte); !ok {
return fmt.Errorf("unexpected type %T for field uint64s", values[i])
} else if value != nil && len(*value) > 0 {
if err := json.Unmarshal(*value, &u.Uint64s); err != nil {
return fmt.Errorf("unmarshal field uint64s: %w", err)
}
}
case user.FieldDeviceType:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field device_type", values[i])
} else if value.Valid {
u.DeviceType = user.DeviceType(value.String)
}
case user.FieldOmitPrefix:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field omit_prefix", values[i])
} else if value.Valid {
u.OmitPrefix = user.OmitPrefix(value.String)
}
case user.FieldMimeType:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field mime_type", values[i])
} else if value.Valid {
u.MimeType = user.MimeType(value.String)
}
case user.ForeignKeys[0]:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for edge-field user_group", value)
} else if value.Valid {
u.user_group = new(int)
*u.user_group = int(value.Int64)
}
default:
u.selectValues.Set(columns[i], values[i])
}
}
return nil
}
// Value returns the ent.Value that was dynamically selected and assigned to the User.
// This includes values selected through modifiers, order, etc.
func (u *User) Value(name string) (ent.Value, error) {
return u.selectValues.Get(name)
}
// QueryGroup queries the "group" edge of the User entity.
func (u *User) QueryGroup() *GroupQuery {
return NewUserClient(u.config).QueryGroup(u)
}
// QueryAttachment queries the "attachment" edge of the User entity.
func (u *User) QueryAttachment() *AttachmentQuery {
return NewUserClient(u.config).QueryAttachment(u)
}
// QueryReceived1 queries the "received_1" edge of the User entity.
func (u *User) QueryReceived1() *AttachmentQuery {
return NewUserClient(u.config).QueryReceived1(u)
}
// QueryPet queries the "pet" edge of the User entity.
func (u *User) QueryPet() *PetQuery {
return NewUserClient(u.config).QueryPet(u)
}
// QuerySkipEdge queries the "skip_edge" edge of the User entity.
func (u *User) QuerySkipEdge() *SkipEdgeExampleQuery {
return NewUserClient(u.config).QuerySkipEdge(u)
}
// Update returns a builder for updating this User.
// Note that you need to call User.Unwrap() before calling this method if this User
// was returned from a transaction, and the transaction was committed or rolled back.
func (u *User) Update() *UserUpdateOne {
return NewUserClient(u.config).UpdateOne(u)
}
// Unwrap unwraps the User entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (u *User) Unwrap() *User {
_tx, ok := u.config.driver.(*txDriver)
if !ok {
panic("ent: User is not a transactional entity")
}
u.config.driver = _tx.drv
return u
}
// String implements the fmt.Stringer.
func (u *User) String() string {
var builder strings.Builder
builder.WriteString("User(")
builder.WriteString(fmt.Sprintf("id=%v, ", u.ID))
builder.WriteString("user_name=")
builder.WriteString(u.UserName)
builder.WriteString(", ")
builder.WriteString("joined=")
builder.WriteString(u.Joined.Format(time.ANSIC))
builder.WriteString(", ")
builder.WriteString("points=")
builder.WriteString(fmt.Sprintf("%v", u.Points))
builder.WriteString(", ")
builder.WriteString("exp=")
builder.WriteString(fmt.Sprintf("%v", u.Exp))
builder.WriteString(", ")
builder.WriteString("status=")
builder.WriteString(fmt.Sprintf("%v", u.Status))
builder.WriteString(", ")
builder.WriteString("external_id=")
builder.WriteString(fmt.Sprintf("%v", u.ExternalID))
builder.WriteString(", ")
builder.WriteString("crm_id=")
builder.WriteString(fmt.Sprintf("%v", u.CrmID))
builder.WriteString(", ")
builder.WriteString("banned=")
builder.WriteString(fmt.Sprintf("%v", u.Banned))
builder.WriteString(", ")
builder.WriteString("custom_pb=")
builder.WriteString(fmt.Sprintf("%v", u.CustomPb))
builder.WriteString(", ")
builder.WriteString("opt_num=")
builder.WriteString(fmt.Sprintf("%v", u.OptNum))
builder.WriteString(", ")
builder.WriteString("opt_str=")
builder.WriteString(u.OptStr)
builder.WriteString(", ")
builder.WriteString("opt_bool=")
builder.WriteString(fmt.Sprintf("%v", u.OptBool))
builder.WriteString(", ")
builder.WriteString("big_int=")
builder.WriteString(fmt.Sprintf("%v", u.BigInt))
builder.WriteString(", ")
builder.WriteString("b_user_1=")
builder.WriteString(fmt.Sprintf("%v", u.BUser1))
builder.WriteString(", ")
builder.WriteString("height_in_cm=")
builder.WriteString(fmt.Sprintf("%v", u.HeightInCm))
builder.WriteString(", ")
builder.WriteString("account_balance=")
builder.WriteString(fmt.Sprintf("%v", u.AccountBalance))
builder.WriteString(", ")
builder.WriteString("unnecessary=")
builder.WriteString(u.Unnecessary)
builder.WriteString(", ")
builder.WriteString("type=")
builder.WriteString(u.Type)
builder.WriteString(", ")
builder.WriteString("labels=")
builder.WriteString(fmt.Sprintf("%v", u.Labels))
builder.WriteString(", ")
builder.WriteString("int32s=")
builder.WriteString(fmt.Sprintf("%v", u.Int32s))
builder.WriteString(", ")
builder.WriteString("int64s=")
builder.WriteString(fmt.Sprintf("%v", u.Int64s))
builder.WriteString(", ")
builder.WriteString("uint32s=")
builder.WriteString(fmt.Sprintf("%v", u.Uint32s))
builder.WriteString(", ")
builder.WriteString("uint64s=")
builder.WriteString(fmt.Sprintf("%v", u.Uint64s))
builder.WriteString(", ")
builder.WriteString("device_type=")
builder.WriteString(fmt.Sprintf("%v", u.DeviceType))
builder.WriteString(", ")
builder.WriteString("omit_prefix=")
builder.WriteString(fmt.Sprintf("%v", u.OmitPrefix))
builder.WriteString(", ")
builder.WriteString("mime_type=")
builder.WriteString(fmt.Sprintf("%v", u.MimeType))
builder.WriteByte(')')
return builder.String()
}
// Users is a parsable slice of User.
type Users []*User