-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathuser.go
More file actions
88 lines (72 loc) · 2.12 KB
/
user.go
File metadata and controls
88 lines (72 loc) · 2.12 KB
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
package limen
import (
"time"
)
type User struct {
ID any `json:"id"`
Email string `json:"email"`
Password *string `json:"-"`
EmailVerifiedAt *time.Time `json:"email_verified_at"`
raw map[string]any
}
// Raw returns the user raw data as returned from the database
func (u User) Raw() map[string]any {
return u.raw
}
func (c User) TableName() string {
return string(UserSchemaTableName)
}
type UserSchema struct {
BaseSchema
// If true, the schema will include the first name and last name fields
includeNameFields bool
// If true, the schema will include the created at and updated at fields
includeTimestampFields bool
}
type SchemaConfigUserOption func(*SchemaConfig, *UserSchema)
func newDefaultUserSchema(c *SchemaConfig, opts ...SchemaConfigUserOption) *UserSchema {
schema := &UserSchema{
BaseSchema: BaseSchema{},
includeNameFields: true,
includeTimestampFields: true,
}
for _, opt := range opts {
opt(c, schema)
}
return schema
}
func (u *UserSchema) GetEmailField() string {
return u.GetField(UserSchemaEmailField)
}
func (u *UserSchema) GetPasswordField() string {
return u.GetField(UserSchemaPasswordField)
}
func (u *UserSchema) GetEmailVerifiedAtField() string {
return u.GetField(UserSchemaEmailVerifiedAtField)
}
func (u *UserSchema) FromStorage(data map[string]any) Model {
return &User{
ID: data[u.GetIDField()],
Email: data[u.GetEmailField()].(string),
Password: getNullableValue[string](data[u.GetPasswordField()]),
EmailVerifiedAt: getNullableValue[time.Time](data[u.GetEmailVerifiedAtField()]),
raw: data,
}
}
func (u *UserSchema) ToStorage(data Model) map[string]any {
user := data.(*User)
return map[string]any{
u.GetEmailField(): user.Email,
u.GetPasswordField(): user.Password,
u.GetEmailVerifiedAtField(): user.EmailVerifiedAt,
}
}
func (u *UserSchema) Serialize(data Model) map[string]any {
if u.Serializer != nil {
return u.Serializer(data)
}
raw := data.Raw()
delete(raw, u.GetIDField())
delete(raw, u.GetPasswordField())
return raw
}