-
Notifications
You must be signed in to change notification settings - Fork 1
/
moduleversion_create.go
153 lines (140 loc) · 3.99 KB
/
moduleversion_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
// Code generated by entc, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"github.com/facebookincubator/ent/dialect/sql"
"github.com/meringu/terraform-private-registry/internal/ent/moduleversion"
)
// ModuleVersionCreate is the builder for creating a ModuleVersion entity.
type ModuleVersionCreate struct {
config
major *int
minor *int
patch *int
tag *string
module map[int]struct{}
}
// SetMajor sets the major field.
func (mvc *ModuleVersionCreate) SetMajor(i int) *ModuleVersionCreate {
mvc.major = &i
return mvc
}
// SetMinor sets the minor field.
func (mvc *ModuleVersionCreate) SetMinor(i int) *ModuleVersionCreate {
mvc.minor = &i
return mvc
}
// SetPatch sets the patch field.
func (mvc *ModuleVersionCreate) SetPatch(i int) *ModuleVersionCreate {
mvc.patch = &i
return mvc
}
// SetTag sets the tag field.
func (mvc *ModuleVersionCreate) SetTag(s string) *ModuleVersionCreate {
mvc.tag = &s
return mvc
}
// SetModuleID sets the module edge to Module by id.
func (mvc *ModuleVersionCreate) SetModuleID(id int) *ModuleVersionCreate {
if mvc.module == nil {
mvc.module = make(map[int]struct{})
}
mvc.module[id] = struct{}{}
return mvc
}
// SetModule sets the module edge to Module.
func (mvc *ModuleVersionCreate) SetModule(m *Module) *ModuleVersionCreate {
return mvc.SetModuleID(m.ID)
}
// Save creates the ModuleVersion in the database.
func (mvc *ModuleVersionCreate) Save(ctx context.Context) (*ModuleVersion, error) {
if mvc.major == nil {
return nil, errors.New("ent: missing required field \"major\"")
}
if err := moduleversion.MajorValidator(*mvc.major); err != nil {
return nil, fmt.Errorf("ent: validator failed for field \"major\": %v", err)
}
if mvc.minor == nil {
return nil, errors.New("ent: missing required field \"minor\"")
}
if err := moduleversion.MinorValidator(*mvc.minor); err != nil {
return nil, fmt.Errorf("ent: validator failed for field \"minor\": %v", err)
}
if mvc.patch == nil {
return nil, errors.New("ent: missing required field \"patch\"")
}
if err := moduleversion.PatchValidator(*mvc.patch); err != nil {
return nil, fmt.Errorf("ent: validator failed for field \"patch\": %v", err)
}
if mvc.tag == nil {
return nil, errors.New("ent: missing required field \"tag\"")
}
if len(mvc.module) > 1 {
return nil, errors.New("ent: multiple assignments on a unique edge \"module\"")
}
if mvc.module == nil {
return nil, errors.New("ent: missing required edge \"module\"")
}
return mvc.sqlSave(ctx)
}
// SaveX calls Save and panics if Save returns an error.
func (mvc *ModuleVersionCreate) SaveX(ctx context.Context) *ModuleVersion {
v, err := mvc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
func (mvc *ModuleVersionCreate) sqlSave(ctx context.Context) (*ModuleVersion, error) {
var (
res sql.Result
mv = &ModuleVersion{config: mvc.config}
)
tx, err := mvc.driver.Tx(ctx)
if err != nil {
return nil, err
}
builder := sql.Insert(moduleversion.Table).Default(mvc.driver.Dialect())
if value := mvc.major; value != nil {
builder.Set(moduleversion.FieldMajor, *value)
mv.Major = *value
}
if value := mvc.minor; value != nil {
builder.Set(moduleversion.FieldMinor, *value)
mv.Minor = *value
}
if value := mvc.patch; value != nil {
builder.Set(moduleversion.FieldPatch, *value)
mv.Patch = *value
}
if value := mvc.tag; value != nil {
builder.Set(moduleversion.FieldTag, *value)
mv.Tag = *value
}
query, args := builder.Query()
if err := tx.Exec(ctx, query, args, &res); err != nil {
return nil, rollback(tx, err)
}
id, err := res.LastInsertId()
if err != nil {
return nil, rollback(tx, err)
}
mv.ID = int(id)
if len(mvc.module) > 0 {
for eid := range mvc.module {
query, args := sql.Update(moduleversion.ModuleTable).
Set(moduleversion.ModuleColumn, eid).
Where(sql.EQ(moduleversion.FieldID, id)).
Query()
if err := tx.Exec(ctx, query, args, &res); err != nil {
return nil, rollback(tx, err)
}
}
}
if err := tx.Commit(); err != nil {
return nil, err
}
return mv, nil
}