-
Notifications
You must be signed in to change notification settings - Fork 894
/
node_create.go
179 lines (163 loc) · 4.21 KB
/
node_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
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// Code generated by entc, DO NOT EDIT.
package ent
import (
"context"
"errors"
"strconv"
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
"github.com/facebookincubator/ent/entc/integration/ent/node"
"github.com/facebookincubator/ent/schema/field"
)
// NodeCreate is the builder for creating a Node entity.
type NodeCreate struct {
config
value *int
prev map[string]struct{}
next map[string]struct{}
}
// SetValue sets the value field.
func (nc *NodeCreate) SetValue(i int) *NodeCreate {
nc.value = &i
return nc
}
// SetNillableValue sets the value field if the given value is not nil.
func (nc *NodeCreate) SetNillableValue(i *int) *NodeCreate {
if i != nil {
nc.SetValue(*i)
}
return nc
}
// SetPrevID sets the prev edge to Node by id.
func (nc *NodeCreate) SetPrevID(id string) *NodeCreate {
if nc.prev == nil {
nc.prev = make(map[string]struct{})
}
nc.prev[id] = struct{}{}
return nc
}
// SetNillablePrevID sets the prev edge to Node by id if the given value is not nil.
func (nc *NodeCreate) SetNillablePrevID(id *string) *NodeCreate {
if id != nil {
nc = nc.SetPrevID(*id)
}
return nc
}
// SetPrev sets the prev edge to Node.
func (nc *NodeCreate) SetPrev(n *Node) *NodeCreate {
return nc.SetPrevID(n.ID)
}
// SetNextID sets the next edge to Node by id.
func (nc *NodeCreate) SetNextID(id string) *NodeCreate {
if nc.next == nil {
nc.next = make(map[string]struct{})
}
nc.next[id] = struct{}{}
return nc
}
// SetNillableNextID sets the next edge to Node by id if the given value is not nil.
func (nc *NodeCreate) SetNillableNextID(id *string) *NodeCreate {
if id != nil {
nc = nc.SetNextID(*id)
}
return nc
}
// SetNext sets the next edge to Node.
func (nc *NodeCreate) SetNext(n *Node) *NodeCreate {
return nc.SetNextID(n.ID)
}
// Save creates the Node in the database.
func (nc *NodeCreate) Save(ctx context.Context) (*Node, error) {
if len(nc.prev) > 1 {
return nil, errors.New("ent: multiple assignments on a unique edge \"prev\"")
}
if len(nc.next) > 1 {
return nil, errors.New("ent: multiple assignments on a unique edge \"next\"")
}
return nc.sqlSave(ctx)
}
// SaveX calls Save and panics if Save returns an error.
func (nc *NodeCreate) SaveX(ctx context.Context) *Node {
v, err := nc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) {
var (
n = &Node{config: nc.config}
_spec = &sqlgraph.CreateSpec{
Table: node.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: node.FieldID,
},
}
)
if value := nc.value; value != nil {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: *value,
Column: node.FieldValue,
})
n.Value = *value
}
if nodes := nc.prev; len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2O,
Inverse: true,
Table: node.PrevTable,
Columns: []string{node.PrevColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: node.FieldID,
},
},
}
for k, _ := range nodes {
k, err := strconv.Atoi(k)
if err != nil {
return nil, err
}
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := nc.next; len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2O,
Inverse: false,
Table: node.NextTable,
Columns: []string{node.NextColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: node.FieldID,
},
},
}
for k, _ := range nodes {
k, err := strconv.Atoi(k)
if err != nil {
return nil, err
}
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if err := sqlgraph.CreateNode(ctx, nc.driver, _spec); err != nil {
if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return nil, err
}
id := _spec.ID.Value.(int64)
n.ID = strconv.FormatInt(id, 10)
return n, nil
}