entc/gen: fix name conflict in type package (#1274)

Fixed #1268
This commit is contained in:
Ariel Mashraki
2021-02-25 14:26:04 +02:00
committed by GitHub
parent a0c3976ee5
commit 31ec081b0e
11 changed files with 312 additions and 9 deletions

File diff suppressed because one or more lines are too long

View File

@@ -6,7 +6,7 @@ in the LICENSE file in the root directory of this source tree.
{{/* constants needed for gremlin dialects. */}}
{{ define "dialect/gremlin/meta/constants" }}
{{ range $_, $e := $.Edges }}{{ $label := $e.LabelConstant -}}
{{ range $e := $.Edges }}{{ $label := $e.LabelConstant -}}
{{ if $e.IsInverse }}{{- $label = $e.InverseLabelConstant -}}
// {{ $label }} holds the string label denoting the {{ lower $e.Name }} inverse edge type in the database.
{{ else -}}

View File

@@ -35,12 +35,13 @@ func ID(id {{ $.ID.Type }}) predicate.{{ $.Name }} {
{{ end }}
{{ range $f := $.Fields }}
{{ $func := $f.StructField }}
{{/* JSON cannot be compared using "=" and Enum has a type defined with the field name */}}
{{ $hasP := not (or $f.IsJSON $f.IsEnum) }}
{{ $comparable := $f.ConvertedToBasic }}
{{- if and $hasP $comparable }}
{{ $undeclared := (and (ne $func "Label") (ne $func "Hooks") (ne $func "Policy")) }}
{{- if and $hasP $comparable $undeclared }}
{{ $arg := "v" }}
{{ $func := $f.StructField }}
// {{ $func }} applies equality check predicate on the {{ quote $f.Name }} field. It's identical to {{ $func }}EQ.
func {{ $func }}({{ $arg }} {{ $f.Type }}) predicate.{{ $.Name }} {
{{- if $f.HasGoType }}

View File

@@ -17,6 +17,7 @@ var (
UsersColumns = []*schema.Column{
{Name: "user_id", Type: field.TypeInt},
{Name: "name", Type: field.TypeString, Nullable: true, Size: 128},
{Name: "label", Type: field.TypeString, Nullable: true},
}
// UsersTable holds the schema information for the "Users" table.
UsersTable = &schema.Table{

View File

@@ -36,6 +36,7 @@ type UserMutation struct {
typ string
id *int
name *string
label *string
clearedFields map[string]struct{}
done bool
oldValue func(context.Context) (*User, error)
@@ -176,6 +177,55 @@ func (m *UserMutation) ResetName() {
delete(m.clearedFields, user.FieldName)
}
// SetLabel sets the "label" field.
func (m *UserMutation) SetLabel(s string) {
m.label = &s
}
// Label returns the value of the "label" field in the mutation.
func (m *UserMutation) Label() (r string, exists bool) {
v := m.label
if v == nil {
return
}
return *v, true
}
// OldLabel returns the old "label" field's value of the User entity.
// If the User object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldLabel(ctx context.Context) (v string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldLabel is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldLabel requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldLabel: %w", err)
}
return oldValue.Label, nil
}
// ClearLabel clears the value of the "label" field.
func (m *UserMutation) ClearLabel() {
m.label = nil
m.clearedFields[user.FieldLabel] = struct{}{}
}
// LabelCleared returns if the "label" field was cleared in this mutation.
func (m *UserMutation) LabelCleared() bool {
_, ok := m.clearedFields[user.FieldLabel]
return ok
}
// ResetLabel resets all changes to the "label" field.
func (m *UserMutation) ResetLabel() {
m.label = nil
delete(m.clearedFields, user.FieldLabel)
}
// Op returns the operation name.
func (m *UserMutation) Op() Op {
return m.op
@@ -190,10 +240,13 @@ func (m *UserMutation) Type() string {
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *UserMutation) Fields() []string {
fields := make([]string, 0, 1)
fields := make([]string, 0, 2)
if m.name != nil {
fields = append(fields, user.FieldName)
}
if m.label != nil {
fields = append(fields, user.FieldLabel)
}
return fields
}
@@ -204,6 +257,8 @@ func (m *UserMutation) Field(name string) (ent.Value, bool) {
switch name {
case user.FieldName:
return m.Name()
case user.FieldLabel:
return m.Label()
}
return nil, false
}
@@ -215,6 +270,8 @@ func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, er
switch name {
case user.FieldName:
return m.OldName(ctx)
case user.FieldLabel:
return m.OldLabel(ctx)
}
return nil, fmt.Errorf("unknown User field %s", name)
}
@@ -231,6 +288,13 @@ func (m *UserMutation) SetField(name string, value ent.Value) error {
}
m.SetName(v)
return nil
case user.FieldLabel:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetLabel(v)
return nil
}
return fmt.Errorf("unknown User field %s", name)
}
@@ -264,6 +328,9 @@ func (m *UserMutation) ClearedFields() []string {
if m.FieldCleared(user.FieldName) {
fields = append(fields, user.FieldName)
}
if m.FieldCleared(user.FieldLabel) {
fields = append(fields, user.FieldLabel)
}
return fields
}
@@ -281,6 +348,9 @@ func (m *UserMutation) ClearField(name string) error {
case user.FieldName:
m.ClearName()
return nil
case user.FieldLabel:
m.ClearLabel()
return nil
}
return fmt.Errorf("unknown User nullable field %s", name)
}
@@ -292,6 +362,9 @@ func (m *UserMutation) ResetField(name string) error {
case user.FieldName:
m.ResetName()
return nil
case user.FieldLabel:
m.ResetLabel()
return nil
}
return fmt.Errorf("unknown User field %s", name)
}

View File

@@ -46,6 +46,8 @@ func (User) Fields() []ent.Field {
Size: 128,
}).Comment(`Comment line1
Comment line2`),
field.String("label").
Optional(),
}
}

View File

@@ -23,6 +23,8 @@ type User struct {
// Comment line1
// Comment line2
Name string `json:"name,omitempty"`
// Label holds the value of the "label" field.
Label string `json:"label,omitempty"`
}
// scanValues returns the types for scanning values from sql.Rows.
@@ -32,7 +34,7 @@ func (*User) scanValues(columns []string) ([]interface{}, error) {
switch columns[i] {
case user.FieldID:
values[i] = &sql.NullInt64{}
case user.FieldName:
case user.FieldName, user.FieldLabel:
values[i] = &sql.NullString{}
default:
return nil, fmt.Errorf("unexpected column %q for type User", columns[i])
@@ -61,6 +63,12 @@ func (u *User) assignValues(columns []string, values []interface{}) error {
} else if value.Valid {
u.Name = value.String
}
case user.FieldLabel:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field label", values[i])
} else if value.Valid {
u.Label = value.String
}
}
}
return nil
@@ -91,6 +99,8 @@ func (u *User) String() string {
builder.WriteString(fmt.Sprintf("id=%v", u.ID))
builder.WriteString(", name=")
builder.WriteString(u.Name)
builder.WriteString(", label=")
builder.WriteString(u.Label)
builder.WriteByte(')')
return builder.String()
}

View File

@@ -13,6 +13,8 @@ const (
FieldID = "user_id"
// FieldName holds the string denoting the name field in the database.
FieldName = "name"
// FieldLabel holds the string denoting the label field in the database.
FieldLabel = "label"
// Table holds the table name of the user in the database.
Table = "Users"
@@ -22,6 +24,7 @@ const (
var Columns = []string{
FieldID,
FieldName,
FieldLabel,
}
// ValidColumn reports if the column name is valid (part of the table columns).

View File

@@ -226,6 +226,131 @@ func NameContainsFold(v string) predicate.User {
})
}
// LabelEQ applies the EQ predicate on the "label" field.
func LabelEQ(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.EQ(s.C(FieldLabel), v))
})
}
// LabelNEQ applies the NEQ predicate on the "label" field.
func LabelNEQ(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.NEQ(s.C(FieldLabel), v))
})
}
// LabelIn applies the In predicate on the "label" field.
func LabelIn(vs ...string) predicate.User {
v := make([]interface{}, len(vs))
for i := range v {
v[i] = vs[i]
}
return predicate.User(func(s *sql.Selector) {
// if not arguments were provided, append the FALSE constants,
// since we can't apply "IN ()". This will make this predicate falsy.
if len(v) == 0 {
s.Where(sql.False())
return
}
s.Where(sql.In(s.C(FieldLabel), v...))
})
}
// LabelNotIn applies the NotIn predicate on the "label" field.
func LabelNotIn(vs ...string) predicate.User {
v := make([]interface{}, len(vs))
for i := range v {
v[i] = vs[i]
}
return predicate.User(func(s *sql.Selector) {
// if not arguments were provided, append the FALSE constants,
// since we can't apply "IN ()". This will make this predicate falsy.
if len(v) == 0 {
s.Where(sql.False())
return
}
s.Where(sql.NotIn(s.C(FieldLabel), v...))
})
}
// LabelGT applies the GT predicate on the "label" field.
func LabelGT(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.GT(s.C(FieldLabel), v))
})
}
// LabelGTE applies the GTE predicate on the "label" field.
func LabelGTE(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.GTE(s.C(FieldLabel), v))
})
}
// LabelLT applies the LT predicate on the "label" field.
func LabelLT(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.LT(s.C(FieldLabel), v))
})
}
// LabelLTE applies the LTE predicate on the "label" field.
func LabelLTE(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.LTE(s.C(FieldLabel), v))
})
}
// LabelContains applies the Contains predicate on the "label" field.
func LabelContains(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.Contains(s.C(FieldLabel), v))
})
}
// LabelHasPrefix applies the HasPrefix predicate on the "label" field.
func LabelHasPrefix(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.HasPrefix(s.C(FieldLabel), v))
})
}
// LabelHasSuffix applies the HasSuffix predicate on the "label" field.
func LabelHasSuffix(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.HasSuffix(s.C(FieldLabel), v))
})
}
// LabelIsNil applies the IsNil predicate on the "label" field.
func LabelIsNil() predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.IsNull(s.C(FieldLabel)))
})
}
// LabelNotNil applies the NotNil predicate on the "label" field.
func LabelNotNil() predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.NotNull(s.C(FieldLabel)))
})
}
// LabelEqualFold applies the EqualFold predicate on the "label" field.
func LabelEqualFold(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.EqualFold(s.C(FieldLabel), v))
})
}
// LabelContainsFold applies the ContainsFold predicate on the "label" field.
func LabelContainsFold(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.ContainsFold(s.C(FieldLabel), v))
})
}
// And groups predicates with the AND operator between them.
func And(predicates ...predicate.User) predicate.User {
return predicate.User(func(s *sql.Selector) {

View File

@@ -36,6 +36,20 @@ func (uc *UserCreate) SetNillableName(s *string) *UserCreate {
return uc
}
// SetLabel sets the "label" field.
func (uc *UserCreate) SetLabel(s string) *UserCreate {
uc.mutation.SetLabel(s)
return uc
}
// SetNillableLabel sets the "label" field if the given value is not nil.
func (uc *UserCreate) SetNillableLabel(s *string) *UserCreate {
if s != nil {
uc.SetLabel(*s)
}
return uc
}
// SetID sets the "id" field.
func (uc *UserCreate) SetID(i int) *UserCreate {
uc.mutation.SetID(i)
@@ -134,6 +148,14 @@ func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
})
_node.Name = value
}
if value, ok := uc.mutation.Label(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: user.FieldLabel,
})
_node.Label = value
}
return _node, _spec
}

View File

@@ -50,6 +50,26 @@ func (uu *UserUpdate) ClearName() *UserUpdate {
return uu
}
// SetLabel sets the "label" field.
func (uu *UserUpdate) SetLabel(s string) *UserUpdate {
uu.mutation.SetLabel(s)
return uu
}
// SetNillableLabel sets the "label" field if the given value is not nil.
func (uu *UserUpdate) SetNillableLabel(s *string) *UserUpdate {
if s != nil {
uu.SetLabel(*s)
}
return uu
}
// ClearLabel clears the value of the "label" field.
func (uu *UserUpdate) ClearLabel() *UserUpdate {
uu.mutation.ClearLabel()
return uu
}
// Mutation returns the UserMutation object of the builder.
func (uu *UserUpdate) Mutation() *UserMutation {
return uu.mutation
@@ -137,6 +157,19 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
Column: user.FieldName,
})
}
if value, ok := uu.mutation.Label(); ok {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: user.FieldLabel,
})
}
if uu.mutation.LabelCleared() {
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: user.FieldLabel,
})
}
if n, err = sqlgraph.UpdateNodes(ctx, uu.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{user.Label}
@@ -175,6 +208,26 @@ func (uuo *UserUpdateOne) ClearName() *UserUpdateOne {
return uuo
}
// SetLabel sets the "label" field.
func (uuo *UserUpdateOne) SetLabel(s string) *UserUpdateOne {
uuo.mutation.SetLabel(s)
return uuo
}
// SetNillableLabel sets the "label" field if the given value is not nil.
func (uuo *UserUpdateOne) SetNillableLabel(s *string) *UserUpdateOne {
if s != nil {
uuo.SetLabel(*s)
}
return uuo
}
// ClearLabel clears the value of the "label" field.
func (uuo *UserUpdateOne) ClearLabel() *UserUpdateOne {
uuo.mutation.ClearLabel()
return uuo
}
// Mutation returns the UserMutation object of the builder.
func (uuo *UserUpdateOne) Mutation() *UserMutation {
return uuo.mutation
@@ -267,6 +320,19 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error)
Column: user.FieldName,
})
}
if value, ok := uuo.mutation.Label(); ok {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: user.FieldLabel,
})
}
if uuo.mutation.LabelCleared() {
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: user.FieldLabel,
})
}
_node = &User{config: uuo.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues