/
trait.go
262 lines (236 loc) · 7.35 KB
/
trait.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
// Code generated by ent, DO NOT EDIT.
package ent
import (
"encoding/json"
"fmt"
"strings"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"github.com/ecshreve/dndgen/ent/trait"
)
// Trait is the model entity for the Trait schema.
type Trait struct {
config `json:"-"`
// ID of the ent.
ID int `json:"id,omitempty"`
// Indx holds the value of the "indx" field.
Indx string `json:"index"`
// Name holds the value of the "name" field.
Name string `json:"name,omitempty"`
// Desc holds the value of the "desc" field.
Desc []string `json:"desc,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.
// The values are being populated by the TraitQuery when eager-loading is set.
Edges TraitEdges `json:"-"`
selectValues sql.SelectValues
}
// TraitEdges holds the relations/edges for other nodes in the graph.
type TraitEdges struct {
// Races holds the value of the races edge.
Races []*Race `json:"races,omitempty"`
// Subraces holds the value of the subraces edge.
Subraces []*Subrace `json:"subraces,omitempty"`
// loadedTypes holds the information for reporting if a
// type was loaded (or requested) in eager-loading or not.
loadedTypes [2]bool
// totalCount holds the count of the edges above.
totalCount [2]map[string]int
namedRaces map[string][]*Race
namedSubraces map[string][]*Subrace
}
// RacesOrErr returns the Races value or an error if the edge
// was not loaded in eager-loading.
func (e TraitEdges) RacesOrErr() ([]*Race, error) {
if e.loadedTypes[0] {
return e.Races, nil
}
return nil, &NotLoadedError{edge: "races"}
}
// SubracesOrErr returns the Subraces value or an error if the edge
// was not loaded in eager-loading.
func (e TraitEdges) SubracesOrErr() ([]*Subrace, error) {
if e.loadedTypes[1] {
return e.Subraces, nil
}
return nil, &NotLoadedError{edge: "subraces"}
}
// scanValues returns the types for scanning values from sql.Rows.
func (*Trait) scanValues(columns []string) ([]any, error) {
values := make([]any, len(columns))
for i := range columns {
switch columns[i] {
case trait.FieldDesc:
values[i] = new([]byte)
case trait.FieldID:
values[i] = new(sql.NullInt64)
case trait.FieldIndx, trait.FieldName:
values[i] = new(sql.NullString)
default:
values[i] = new(sql.UnknownType)
}
}
return values, nil
}
// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Trait fields.
func (t *Trait) 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 trait.FieldID:
value, ok := values[i].(*sql.NullInt64)
if !ok {
return fmt.Errorf("unexpected type %T for field id", value)
}
t.ID = int(value.Int64)
case trait.FieldIndx:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field indx", values[i])
} else if value.Valid {
t.Indx = value.String
}
case trait.FieldName:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field name", values[i])
} else if value.Valid {
t.Name = value.String
}
case trait.FieldDesc:
if value, ok := values[i].(*[]byte); !ok {
return fmt.Errorf("unexpected type %T for field desc", values[i])
} else if value != nil && len(*value) > 0 {
if err := json.Unmarshal(*value, &t.Desc); err != nil {
return fmt.Errorf("unmarshal field desc: %w", err)
}
}
default:
t.selectValues.Set(columns[i], values[i])
}
}
return nil
}
// Value returns the ent.Value that was dynamically selected and assigned to the Trait.
// This includes values selected through modifiers, order, etc.
func (t *Trait) Value(name string) (ent.Value, error) {
return t.selectValues.Get(name)
}
// QueryRaces queries the "races" edge of the Trait entity.
func (t *Trait) QueryRaces() *RaceQuery {
return NewTraitClient(t.config).QueryRaces(t)
}
// QuerySubraces queries the "subraces" edge of the Trait entity.
func (t *Trait) QuerySubraces() *SubraceQuery {
return NewTraitClient(t.config).QuerySubraces(t)
}
// Update returns a builder for updating this Trait.
// Note that you need to call Trait.Unwrap() before calling this method if this Trait
// was returned from a transaction, and the transaction was committed or rolled back.
func (t *Trait) Update() *TraitUpdateOne {
return NewTraitClient(t.config).UpdateOne(t)
}
// Unwrap unwraps the Trait 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 (t *Trait) Unwrap() *Trait {
_tx, ok := t.config.driver.(*txDriver)
if !ok {
panic("ent: Trait is not a transactional entity")
}
t.config.driver = _tx.drv
return t
}
// String implements the fmt.Stringer.
func (t *Trait) String() string {
var builder strings.Builder
builder.WriteString("Trait(")
builder.WriteString(fmt.Sprintf("id=%v, ", t.ID))
builder.WriteString("indx=")
builder.WriteString(t.Indx)
builder.WriteString(", ")
builder.WriteString("name=")
builder.WriteString(t.Name)
builder.WriteString(", ")
builder.WriteString("desc=")
builder.WriteString(fmt.Sprintf("%v", t.Desc))
builder.WriteByte(')')
return builder.String()
}
// MarshalJSON implements the json.Marshaler interface.
func (t *Trait) MarshalJSON() ([]byte, error) {
type Alias Trait
return json.Marshal(&struct {
*Alias
TraitEdges
}{
Alias: (*Alias)(t),
TraitEdges: t.Edges,
})
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t *Trait) UnmarshalJSON(data []byte) error {
type Alias Trait
aux := &struct {
*Alias
TraitEdges
}{
Alias: (*Alias)(t),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
t.Edges = aux.TraitEdges
return nil
}
func (tc *TraitCreate) SetTrait(input *Trait) *TraitCreate {
tc.SetIndx(input.Indx)
tc.SetName(input.Name)
tc.SetDesc(input.Desc)
return tc
}
// NamedRaces returns the Races named value or an error if the edge was not
// loaded in eager-loading with this name.
func (t *Trait) NamedRaces(name string) ([]*Race, error) {
if t.Edges.namedRaces == nil {
return nil, &NotLoadedError{edge: name}
}
nodes, ok := t.Edges.namedRaces[name]
if !ok {
return nil, &NotLoadedError{edge: name}
}
return nodes, nil
}
func (t *Trait) appendNamedRaces(name string, edges ...*Race) {
if t.Edges.namedRaces == nil {
t.Edges.namedRaces = make(map[string][]*Race)
}
if len(edges) == 0 {
t.Edges.namedRaces[name] = []*Race{}
} else {
t.Edges.namedRaces[name] = append(t.Edges.namedRaces[name], edges...)
}
}
// NamedSubraces returns the Subraces named value or an error if the edge was not
// loaded in eager-loading with this name.
func (t *Trait) NamedSubraces(name string) ([]*Subrace, error) {
if t.Edges.namedSubraces == nil {
return nil, &NotLoadedError{edge: name}
}
nodes, ok := t.Edges.namedSubraces[name]
if !ok {
return nil, &NotLoadedError{edge: name}
}
return nodes, nil
}
func (t *Trait) appendNamedSubraces(name string, edges ...*Subrace) {
if t.Edges.namedSubraces == nil {
t.Edges.namedSubraces = make(map[string][]*Subrace)
}
if len(edges) == 0 {
t.Edges.namedSubraces[name] = []*Subrace{}
} else {
t.Edges.namedSubraces[name] = append(t.Edges.namedSubraces[name], edges...)
}
}
// Traits is a parsable slice of Trait.
type Traits []*Trait