/
level_create.go
206 lines (191 loc) · 5.04 KB
/
level_create.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
// Code generated by entc, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"github.com/arpb2/C-3PO/third_party/ent/level"
"github.com/arpb2/C-3PO/third_party/ent/schema"
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
"github.com/facebookincubator/ent/schema/field"
)
// LevelCreate is the builder for creating a Level entity.
type LevelCreate struct {
config
mutation *LevelMutation
hooks []Hook
}
// SetCreatedAt sets the created_at field.
func (lc *LevelCreate) SetCreatedAt(t time.Time) *LevelCreate {
lc.mutation.SetCreatedAt(t)
return lc
}
// SetNillableCreatedAt sets the created_at field if the given value is not nil.
func (lc *LevelCreate) SetNillableCreatedAt(t *time.Time) *LevelCreate {
if t != nil {
lc.SetCreatedAt(*t)
}
return lc
}
// SetUpdatedAt sets the updated_at field.
func (lc *LevelCreate) SetUpdatedAt(t time.Time) *LevelCreate {
lc.mutation.SetUpdatedAt(t)
return lc
}
// SetNillableUpdatedAt sets the updated_at field if the given value is not nil.
func (lc *LevelCreate) SetNillableUpdatedAt(t *time.Time) *LevelCreate {
if t != nil {
lc.SetUpdatedAt(*t)
}
return lc
}
// SetName sets the name field.
func (lc *LevelCreate) SetName(s string) *LevelCreate {
lc.mutation.SetName(s)
return lc
}
// SetDescription sets the description field.
func (lc *LevelCreate) SetDescription(s string) *LevelCreate {
lc.mutation.SetDescription(s)
return lc
}
// SetDefinition sets the definition field.
func (lc *LevelCreate) SetDefinition(sd *schema.LevelDefinition) *LevelCreate {
lc.mutation.SetDefinition(sd)
return lc
}
// SetID sets the id field.
func (lc *LevelCreate) SetID(u uint) *LevelCreate {
lc.mutation.SetID(u)
return lc
}
// Save creates the Level in the database.
func (lc *LevelCreate) Save(ctx context.Context) (*Level, error) {
if _, ok := lc.mutation.CreatedAt(); !ok {
v := level.DefaultCreatedAt()
lc.mutation.SetCreatedAt(v)
}
if _, ok := lc.mutation.UpdatedAt(); !ok {
v := level.DefaultUpdatedAt()
lc.mutation.SetUpdatedAt(v)
}
if _, ok := lc.mutation.Name(); !ok {
return nil, errors.New("ent: missing required field \"name\"")
}
if v, ok := lc.mutation.Name(); ok {
if err := level.NameValidator(v); err != nil {
return nil, fmt.Errorf("ent: validator failed for field \"name\": %v", err)
}
}
if _, ok := lc.mutation.Description(); !ok {
return nil, errors.New("ent: missing required field \"description\"")
}
if v, ok := lc.mutation.Description(); ok {
if err := level.DescriptionValidator(v); err != nil {
return nil, fmt.Errorf("ent: validator failed for field \"description\": %v", err)
}
}
if _, ok := lc.mutation.Definition(); !ok {
return nil, errors.New("ent: missing required field \"definition\"")
}
var (
err error
node *Level
)
if len(lc.hooks) == 0 {
node, err = lc.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*LevelMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
lc.mutation = mutation
node, err = lc.sqlSave(ctx)
return node, err
})
for i := len(lc.hooks) - 1; i >= 0; i-- {
mut = lc.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, lc.mutation); err != nil {
return nil, err
}
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (lc *LevelCreate) SaveX(ctx context.Context) *Level {
v, err := lc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
func (lc *LevelCreate) sqlSave(ctx context.Context) (*Level, error) {
var (
l = &Level{config: lc.config}
_spec = &sqlgraph.CreateSpec{
Table: level.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeUint,
Column: level.FieldID,
},
}
)
if id, ok := lc.mutation.ID(); ok {
l.ID = id
_spec.ID.Value = id
}
if value, ok := lc.mutation.CreatedAt(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Value: value,
Column: level.FieldCreatedAt,
})
l.CreatedAt = value
}
if value, ok := lc.mutation.UpdatedAt(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Value: value,
Column: level.FieldUpdatedAt,
})
l.UpdatedAt = value
}
if value, ok := lc.mutation.Name(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: level.FieldName,
})
l.Name = value
}
if value, ok := lc.mutation.Description(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: level.FieldDescription,
})
l.Description = value
}
if value, ok := lc.mutation.Definition(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeJSON,
Value: value,
Column: level.FieldDefinition,
})
l.Definition = value
}
if err := sqlgraph.CreateNode(ctx, lc.driver, _spec); err != nil {
if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return nil, err
}
if l.ID == 0 {
id := _spec.ID.Value.(int64)
l.ID = uint(id)
}
return l, nil
}