From 55a784470dd793fb2a10e07d4c85b54995a46e4a Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Fri, 29 Jul 2022 09:52:50 +0300 Subject: [PATCH] entc/gen: skip generating predicates that conflicts with FieldID constant --- entc/gen/template/where.tmpl | 2 +- entc/integration/ent/entql.go | 16 ++- entc/integration/ent/file.go | 13 ++- entc/integration/ent/file/file.go | 3 + entc/integration/ent/file/where.go | 78 ++++++++++++++ entc/integration/ent/file_create.go | 102 ++++++++++++++++++ entc/integration/ent/file_update.go | 94 +++++++++++++++++ entc/integration/ent/migrate/schema.go | 13 +-- entc/integration/ent/mutation.go | 109 +++++++++++++++++++- entc/integration/ent/schema/file.go | 4 + entc/integration/gremlin/ent/file.go | 45 ++++---- entc/integration/gremlin/ent/file/file.go | 2 + entc/integration/gremlin/ent/file/where.go | 78 ++++++++++++++ entc/integration/gremlin/ent/file_create.go | 17 +++ entc/integration/gremlin/ent/file_update.go | 72 +++++++++++++ entc/integration/gremlin/ent/mutation.go | 109 +++++++++++++++++++- 16 files changed, 724 insertions(+), 33 deletions(-) diff --git a/entc/gen/template/where.tmpl b/entc/gen/template/where.tmpl index 4e4d13ac06..67b4e8b38c 100644 --- a/entc/gen/template/where.tmpl +++ b/entc/gen/template/where.tmpl @@ -43,7 +43,7 @@ in the LICENSE file in the root directory of this source tree. {{/* JSON cannot be compared using "=" and Enum has a type defined with the field name */}} {{ $hasP := not (or $f.IsJSON $f.IsEnum) }} {{ $comparable := or $f.ConvertedToBasic $f.Type.Valuer }} - {{ $undeclared := (and (ne $func "Label") (ne $func "Hooks") (ne $func "Policy") (ne $func "Table")) }} + {{ $undeclared := (and (ne $func "Label") (ne $func "Hooks") (ne $func "Policy") (ne $func "Table") (ne $func "FieldID")) }} {{- if and $hasP $comparable $undeclared }} {{ $arg := "v" }} // {{ $func }} applies equality check predicate on the {{ quote $f.Name }} field. It's identical to {{ $func }}EQ. diff --git a/entc/integration/ent/entql.go b/entc/integration/ent/entql.go index 4ae5c0323c..2a25580034 100644 --- a/entc/integration/ent/entql.go +++ b/entc/integration/ent/entql.go @@ -158,11 +158,12 @@ var schemaGraph = func() *sqlgraph.Schema { }, Type: "File", Fields: map[string]*sqlgraph.FieldSpec{ - file.FieldSize: {Type: field.TypeInt, Column: file.FieldSize}, - file.FieldName: {Type: field.TypeString, Column: file.FieldName}, - file.FieldUser: {Type: field.TypeString, Column: file.FieldUser}, - file.FieldGroup: {Type: field.TypeString, Column: file.FieldGroup}, - file.FieldOp: {Type: field.TypeBool, Column: file.FieldOp}, + file.FieldSize: {Type: field.TypeInt, Column: file.FieldSize}, + file.FieldName: {Type: field.TypeString, Column: file.FieldName}, + file.FieldUser: {Type: field.TypeString, Column: file.FieldUser}, + file.FieldGroup: {Type: field.TypeString, Column: file.FieldGroup}, + file.FieldOp: {Type: field.TypeBool, Column: file.FieldOp}, + file.FieldFieldID: {Type: field.TypeInt, Column: file.FieldFieldID}, }, } graph.Nodes[4] = &sqlgraph.Node{ @@ -1256,6 +1257,11 @@ func (f *FileFilter) WhereOp(p entql.BoolP) { f.Where(p.Field(file.FieldOp)) } +// WhereFieldID applies the entql int predicate on the field_id field. +func (f *FileFilter) WhereFieldID(p entql.IntP) { + f.Where(p.Field(file.FieldFieldID)) +} + // WhereHasOwner applies a predicate to check if query has an edge owner. func (f *FileFilter) WhereHasOwner() { f.Where(entql.HasEdge("owner")) diff --git a/entc/integration/ent/file.go b/entc/integration/ent/file.go index 25fb4cb5c4..a677bc6a53 100644 --- a/entc/integration/ent/file.go +++ b/entc/integration/ent/file.go @@ -31,6 +31,8 @@ type File struct { Group string `json:"group,omitempty"` // Op holds the value of the "op" field. Op bool `json:"op,omitempty"` + // FieldID holds the value of the "field_id" field. + FieldID int `json:"field_id,omitempty"` // Edges holds the relations/edges for other nodes in the graph. // The values are being populated by the FileQuery when eager-loading is set. Edges FileEdges `json:"file_edges"` @@ -95,7 +97,7 @@ func (*File) scanValues(columns []string) ([]interface{}, error) { switch columns[i] { case file.FieldOp: values[i] = new(sql.NullBool) - case file.FieldID, file.FieldSize: + case file.FieldID, file.FieldSize, file.FieldFieldID: values[i] = new(sql.NullInt64) case file.FieldName, file.FieldUser, file.FieldGroup: values[i] = new(sql.NullString) @@ -157,6 +159,12 @@ func (f *File) assignValues(columns []string, values []interface{}) error { } else if value.Valid { f.Op = value.Bool } + case file.FieldFieldID: + if value, ok := values[i].(*sql.NullInt64); !ok { + return fmt.Errorf("unexpected type %T for field field_id", values[i]) + } else if value.Valid { + f.FieldID = int(value.Int64) + } case file.ForeignKeys[0]: if value, ok := values[i].(*sql.NullInt64); !ok { return fmt.Errorf("unexpected type %T for edge-field file_type_files", value) @@ -237,6 +245,9 @@ func (f *File) String() string { builder.WriteString(", ") builder.WriteString("op=") builder.WriteString(fmt.Sprintf("%v", f.Op)) + builder.WriteString(", ") + builder.WriteString("field_id=") + builder.WriteString(fmt.Sprintf("%v", f.FieldID)) builder.WriteByte(')') return builder.String() } diff --git a/entc/integration/ent/file/file.go b/entc/integration/ent/file/file.go index 11977c1508..c43253f4ae 100644 --- a/entc/integration/ent/file/file.go +++ b/entc/integration/ent/file/file.go @@ -21,6 +21,8 @@ const ( FieldGroup = "group" // FieldOp holds the string denoting the op field in the database. FieldOp = "op" + // FieldFieldID holds the string denoting the field_id field in the database. + FieldFieldID = "field_id" // EdgeOwner holds the string denoting the owner edge name in mutations. EdgeOwner = "owner" // EdgeType holds the string denoting the type edge name in mutations. @@ -60,6 +62,7 @@ var Columns = []string{ FieldUser, FieldGroup, FieldOp, + FieldFieldID, } // ForeignKeys holds the SQL foreign-keys that are owned by the "files" diff --git a/entc/integration/ent/file/where.go b/entc/integration/ent/file/where.go index bca76e9ef4..caa73e3466 100644 --- a/entc/integration/ent/file/where.go +++ b/entc/integration/ent/file/where.go @@ -535,6 +535,84 @@ func OpNotNil() predicate.File { }) } +// FieldIDEQ applies the EQ predicate on the "field_id" field. +func FieldIDEQ(v int) predicate.File { + return predicate.File(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldFieldID), v)) + }) +} + +// FieldIDNEQ applies the NEQ predicate on the "field_id" field. +func FieldIDNEQ(v int) predicate.File { + return predicate.File(func(s *sql.Selector) { + s.Where(sql.NEQ(s.C(FieldFieldID), v)) + }) +} + +// FieldIDIn applies the In predicate on the "field_id" field. +func FieldIDIn(vs ...int) predicate.File { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.File(func(s *sql.Selector) { + s.Where(sql.In(s.C(FieldFieldID), v...)) + }) +} + +// FieldIDNotIn applies the NotIn predicate on the "field_id" field. +func FieldIDNotIn(vs ...int) predicate.File { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.File(func(s *sql.Selector) { + s.Where(sql.NotIn(s.C(FieldFieldID), v...)) + }) +} + +// FieldIDGT applies the GT predicate on the "field_id" field. +func FieldIDGT(v int) predicate.File { + return predicate.File(func(s *sql.Selector) { + s.Where(sql.GT(s.C(FieldFieldID), v)) + }) +} + +// FieldIDGTE applies the GTE predicate on the "field_id" field. +func FieldIDGTE(v int) predicate.File { + return predicate.File(func(s *sql.Selector) { + s.Where(sql.GTE(s.C(FieldFieldID), v)) + }) +} + +// FieldIDLT applies the LT predicate on the "field_id" field. +func FieldIDLT(v int) predicate.File { + return predicate.File(func(s *sql.Selector) { + s.Where(sql.LT(s.C(FieldFieldID), v)) + }) +} + +// FieldIDLTE applies the LTE predicate on the "field_id" field. +func FieldIDLTE(v int) predicate.File { + return predicate.File(func(s *sql.Selector) { + s.Where(sql.LTE(s.C(FieldFieldID), v)) + }) +} + +// FieldIDIsNil applies the IsNil predicate on the "field_id" field. +func FieldIDIsNil() predicate.File { + return predicate.File(func(s *sql.Selector) { + s.Where(sql.IsNull(s.C(FieldFieldID))) + }) +} + +// FieldIDNotNil applies the NotNil predicate on the "field_id" field. +func FieldIDNotNil() predicate.File { + return predicate.File(func(s *sql.Selector) { + s.Where(sql.NotNull(s.C(FieldFieldID))) + }) +} + // HasOwner applies the HasEdge predicate on the "owner" edge. func HasOwner() predicate.File { return predicate.File(func(s *sql.Selector) { diff --git a/entc/integration/ent/file_create.go b/entc/integration/ent/file_create.go index 5e93ae9cdc..25be7092a2 100644 --- a/entc/integration/ent/file_create.go +++ b/entc/integration/ent/file_create.go @@ -90,6 +90,20 @@ func (fc *FileCreate) SetNillableOp(b *bool) *FileCreate { return fc } +// SetFieldID sets the "field_id" field. +func (fc *FileCreate) SetFieldID(i int) *FileCreate { + fc.mutation.SetFieldID(i) + return fc +} + +// SetNillableFieldID sets the "field_id" field if the given value is not nil. +func (fc *FileCreate) SetNillableFieldID(i *int) *FileCreate { + if i != nil { + fc.SetFieldID(*i) + } + return fc +} + // SetOwnerID sets the "owner" edge to the User entity by ID. func (fc *FileCreate) SetOwnerID(id int) *FileCreate { fc.mutation.SetOwnerID(id) @@ -307,6 +321,14 @@ func (fc *FileCreate) createSpec() (*File, *sqlgraph.CreateSpec) { }) _node.Op = value } + if value, ok := fc.mutation.FieldID(); ok { + _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Value: value, + Column: file.FieldFieldID, + }) + _node.FieldID = value + } if nodes := fc.mutation.OwnerIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, @@ -504,6 +526,30 @@ func (u *FileUpsert) ClearOp() *FileUpsert { return u } +// SetFieldID sets the "field_id" field. +func (u *FileUpsert) SetFieldID(v int) *FileUpsert { + u.Set(file.FieldFieldID, v) + return u +} + +// UpdateFieldID sets the "field_id" field to the value that was provided on create. +func (u *FileUpsert) UpdateFieldID() *FileUpsert { + u.SetExcluded(file.FieldFieldID) + return u +} + +// AddFieldID adds v to the "field_id" field. +func (u *FileUpsert) AddFieldID(v int) *FileUpsert { + u.Add(file.FieldFieldID, v) + return u +} + +// ClearFieldID clears the value of the "field_id" field. +func (u *FileUpsert) ClearFieldID() *FileUpsert { + u.SetNull(file.FieldFieldID) + return u +} + // UpdateNewValues updates the mutable fields using the new values that were set on create. // Using this option is equivalent to using: // @@ -644,6 +690,34 @@ func (u *FileUpsertOne) ClearOp() *FileUpsertOne { }) } +// SetFieldID sets the "field_id" field. +func (u *FileUpsertOne) SetFieldID(v int) *FileUpsertOne { + return u.Update(func(s *FileUpsert) { + s.SetFieldID(v) + }) +} + +// AddFieldID adds v to the "field_id" field. +func (u *FileUpsertOne) AddFieldID(v int) *FileUpsertOne { + return u.Update(func(s *FileUpsert) { + s.AddFieldID(v) + }) +} + +// UpdateFieldID sets the "field_id" field to the value that was provided on create. +func (u *FileUpsertOne) UpdateFieldID() *FileUpsertOne { + return u.Update(func(s *FileUpsert) { + s.UpdateFieldID() + }) +} + +// ClearFieldID clears the value of the "field_id" field. +func (u *FileUpsertOne) ClearFieldID() *FileUpsertOne { + return u.Update(func(s *FileUpsert) { + s.ClearFieldID() + }) +} + // Exec executes the query. func (u *FileUpsertOne) Exec(ctx context.Context) error { if len(u.create.conflict) == 0 { @@ -946,6 +1020,34 @@ func (u *FileUpsertBulk) ClearOp() *FileUpsertBulk { }) } +// SetFieldID sets the "field_id" field. +func (u *FileUpsertBulk) SetFieldID(v int) *FileUpsertBulk { + return u.Update(func(s *FileUpsert) { + s.SetFieldID(v) + }) +} + +// AddFieldID adds v to the "field_id" field. +func (u *FileUpsertBulk) AddFieldID(v int) *FileUpsertBulk { + return u.Update(func(s *FileUpsert) { + s.AddFieldID(v) + }) +} + +// UpdateFieldID sets the "field_id" field to the value that was provided on create. +func (u *FileUpsertBulk) UpdateFieldID() *FileUpsertBulk { + return u.Update(func(s *FileUpsert) { + s.UpdateFieldID() + }) +} + +// ClearFieldID clears the value of the "field_id" field. +func (u *FileUpsertBulk) ClearFieldID() *FileUpsertBulk { + return u.Update(func(s *FileUpsert) { + s.ClearFieldID() + }) +} + // Exec executes the query. func (u *FileUpsertBulk) Exec(ctx context.Context) error { for i, b := range u.create.builders { diff --git a/entc/integration/ent/file_update.go b/entc/integration/ent/file_update.go index d556974a28..3455b96df3 100644 --- a/entc/integration/ent/file_update.go +++ b/entc/integration/ent/file_update.go @@ -121,6 +121,33 @@ func (fu *FileUpdate) ClearOp() *FileUpdate { return fu } +// SetFieldID sets the "field_id" field. +func (fu *FileUpdate) SetFieldID(i int) *FileUpdate { + fu.mutation.ResetFieldID() + fu.mutation.SetFieldID(i) + return fu +} + +// SetNillableFieldID sets the "field_id" field if the given value is not nil. +func (fu *FileUpdate) SetNillableFieldID(i *int) *FileUpdate { + if i != nil { + fu.SetFieldID(*i) + } + return fu +} + +// AddFieldID adds i to the "field_id" field. +func (fu *FileUpdate) AddFieldID(i int) *FileUpdate { + fu.mutation.AddFieldID(i) + return fu +} + +// ClearFieldID clears the value of the "field_id" field. +func (fu *FileUpdate) ClearFieldID() *FileUpdate { + fu.mutation.ClearFieldID() + return fu +} + // SetOwnerID sets the "owner" edge to the User entity by ID. func (fu *FileUpdate) SetOwnerID(id int) *FileUpdate { fu.mutation.SetOwnerID(id) @@ -360,6 +387,26 @@ func (fu *FileUpdate) sqlSave(ctx context.Context) (n int, err error) { Column: file.FieldOp, }) } + if value, ok := fu.mutation.FieldID(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Value: value, + Column: file.FieldFieldID, + }) + } + if value, ok := fu.mutation.AddedFieldID(); ok { + _spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Value: value, + Column: file.FieldFieldID, + }) + } + if fu.mutation.FieldIDCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Column: file.FieldFieldID, + }) + } if fu.mutation.OwnerCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, @@ -590,6 +637,33 @@ func (fuo *FileUpdateOne) ClearOp() *FileUpdateOne { return fuo } +// SetFieldID sets the "field_id" field. +func (fuo *FileUpdateOne) SetFieldID(i int) *FileUpdateOne { + fuo.mutation.ResetFieldID() + fuo.mutation.SetFieldID(i) + return fuo +} + +// SetNillableFieldID sets the "field_id" field if the given value is not nil. +func (fuo *FileUpdateOne) SetNillableFieldID(i *int) *FileUpdateOne { + if i != nil { + fuo.SetFieldID(*i) + } + return fuo +} + +// AddFieldID adds i to the "field_id" field. +func (fuo *FileUpdateOne) AddFieldID(i int) *FileUpdateOne { + fuo.mutation.AddFieldID(i) + return fuo +} + +// ClearFieldID clears the value of the "field_id" field. +func (fuo *FileUpdateOne) ClearFieldID() *FileUpdateOne { + fuo.mutation.ClearFieldID() + return fuo +} + // SetOwnerID sets the "owner" edge to the User entity by ID. func (fuo *FileUpdateOne) SetOwnerID(id int) *FileUpdateOne { fuo.mutation.SetOwnerID(id) @@ -859,6 +933,26 @@ func (fuo *FileUpdateOne) sqlSave(ctx context.Context) (_node *File, err error) Column: file.FieldOp, }) } + if value, ok := fuo.mutation.FieldID(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Value: value, + Column: file.FieldFieldID, + }) + } + if value, ok := fuo.mutation.AddedFieldID(); ok { + _spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Value: value, + Column: file.FieldFieldID, + }) + } + if fuo.mutation.FieldIDCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Column: file.FieldFieldID, + }) + } if fuo.mutation.OwnerCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, diff --git a/entc/integration/ent/migrate/schema.go b/entc/integration/ent/migrate/schema.go index 59f62efff1..84e996c000 100644 --- a/entc/integration/ent/migrate/schema.go +++ b/entc/integration/ent/migrate/schema.go @@ -161,6 +161,7 @@ var ( {Name: "user", Type: field.TypeString, Nullable: true}, {Name: "group", Type: field.TypeString, Nullable: true}, {Name: "op", Type: field.TypeBool, Nullable: true}, + {Name: "field_id", Type: field.TypeInt, Nullable: true}, {Name: "file_type_files", Type: field.TypeInt, Nullable: true}, {Name: "group_files", Type: field.TypeInt, Nullable: true}, {Name: "user_files", Type: field.TypeInt, Nullable: true}, @@ -173,19 +174,19 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "files_file_types_files", - Columns: []*schema.Column{FilesColumns[6]}, + Columns: []*schema.Column{FilesColumns[7]}, RefColumns: []*schema.Column{FileTypesColumns[0]}, OnDelete: schema.SetNull, }, { Symbol: "files_groups_files", - Columns: []*schema.Column{FilesColumns[7]}, + Columns: []*schema.Column{FilesColumns[8]}, RefColumns: []*schema.Column{GroupsColumns[0]}, OnDelete: schema.SetNull, }, { Symbol: "files_users_files", - Columns: []*schema.Column{FilesColumns[8]}, + Columns: []*schema.Column{FilesColumns[9]}, RefColumns: []*schema.Column{UsersColumns[0]}, OnDelete: schema.SetNull, }, @@ -204,17 +205,17 @@ var ( { Name: "file_user_files_file_type_files", Unique: false, - Columns: []*schema.Column{FilesColumns[8], FilesColumns[6]}, + Columns: []*schema.Column{FilesColumns[9], FilesColumns[7]}, }, { Name: "file_name_user_files_file_type_files", Unique: true, - Columns: []*schema.Column{FilesColumns[2], FilesColumns[8], FilesColumns[6]}, + Columns: []*schema.Column{FilesColumns[2], FilesColumns[9], FilesColumns[7]}, }, { Name: "file_name_user_files", Unique: false, - Columns: []*schema.Column{FilesColumns[2], FilesColumns[8]}, + Columns: []*schema.Column{FilesColumns[2], FilesColumns[9]}, }, }, } diff --git a/entc/integration/ent/mutation.go b/entc/integration/ent/mutation.go index 9f8d3991f7..1c4b4e5934 100644 --- a/entc/integration/ent/mutation.go +++ b/entc/integration/ent/mutation.go @@ -7371,6 +7371,8 @@ type FileMutation struct { user *string group *string _op *bool + field_id *int + addfield_id *int clearedFields map[string]struct{} owner *int clearedowner bool @@ -7721,6 +7723,76 @@ func (m *FileMutation) ResetOp() { delete(m.clearedFields, file.FieldOp) } +// SetFieldID sets the "field_id" field. +func (m *FileMutation) SetFieldID(i int) { + m.field_id = &i + m.addfield_id = nil +} + +// FieldID returns the value of the "field_id" field in the mutation. +func (m *FileMutation) FieldID() (r int, exists bool) { + v := m.field_id + if v == nil { + return + } + return *v, true +} + +// OldFieldID returns the old "field_id" field's value of the File entity. +// If the File 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 *FileMutation) OldFieldID(ctx context.Context) (v int, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldFieldID is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldFieldID requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldFieldID: %w", err) + } + return oldValue.FieldID, nil +} + +// AddFieldID adds i to the "field_id" field. +func (m *FileMutation) AddFieldID(i int) { + if m.addfield_id != nil { + *m.addfield_id += i + } else { + m.addfield_id = &i + } +} + +// AddedFieldID returns the value that was added to the "field_id" field in this mutation. +func (m *FileMutation) AddedFieldID() (r int, exists bool) { + v := m.addfield_id + if v == nil { + return + } + return *v, true +} + +// ClearFieldID clears the value of the "field_id" field. +func (m *FileMutation) ClearFieldID() { + m.field_id = nil + m.addfield_id = nil + m.clearedFields[file.FieldFieldID] = struct{}{} +} + +// FieldIDCleared returns if the "field_id" field was cleared in this mutation. +func (m *FileMutation) FieldIDCleared() bool { + _, ok := m.clearedFields[file.FieldFieldID] + return ok +} + +// ResetFieldID resets all changes to the "field_id" field. +func (m *FileMutation) ResetFieldID() { + m.field_id = nil + m.addfield_id = nil + delete(m.clearedFields, file.FieldFieldID) +} + // SetOwnerID sets the "owner" edge to the User entity by id. func (m *FileMutation) SetOwnerID(id int) { m.owner = &id @@ -7872,7 +7944,7 @@ func (m *FileMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *FileMutation) Fields() []string { - fields := make([]string, 0, 5) + fields := make([]string, 0, 6) if m.size != nil { fields = append(fields, file.FieldSize) } @@ -7888,6 +7960,9 @@ func (m *FileMutation) Fields() []string { if m._op != nil { fields = append(fields, file.FieldOp) } + if m.field_id != nil { + fields = append(fields, file.FieldFieldID) + } return fields } @@ -7906,6 +7981,8 @@ func (m *FileMutation) Field(name string) (ent.Value, bool) { return m.Group() case file.FieldOp: return m.GetOp() + case file.FieldFieldID: + return m.FieldID() } return nil, false } @@ -7925,6 +8002,8 @@ func (m *FileMutation) OldField(ctx context.Context, name string) (ent.Value, er return m.OldGroup(ctx) case file.FieldOp: return m.OldOp(ctx) + case file.FieldFieldID: + return m.OldFieldID(ctx) } return nil, fmt.Errorf("unknown File field %s", name) } @@ -7969,6 +8048,13 @@ func (m *FileMutation) SetField(name string, value ent.Value) error { } m.SetOp(v) return nil + case file.FieldFieldID: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetFieldID(v) + return nil } return fmt.Errorf("unknown File field %s", name) } @@ -7980,6 +8066,9 @@ func (m *FileMutation) AddedFields() []string { if m.addsize != nil { fields = append(fields, file.FieldSize) } + if m.addfield_id != nil { + fields = append(fields, file.FieldFieldID) + } return fields } @@ -7990,6 +8079,8 @@ func (m *FileMutation) AddedField(name string) (ent.Value, bool) { switch name { case file.FieldSize: return m.AddedSize() + case file.FieldFieldID: + return m.AddedFieldID() } return nil, false } @@ -8006,6 +8097,13 @@ func (m *FileMutation) AddField(name string, value ent.Value) error { } m.AddSize(v) return nil + case file.FieldFieldID: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.AddFieldID(v) + return nil } return fmt.Errorf("unknown File numeric field %s", name) } @@ -8023,6 +8121,9 @@ func (m *FileMutation) ClearedFields() []string { if m.FieldCleared(file.FieldOp) { fields = append(fields, file.FieldOp) } + if m.FieldCleared(file.FieldFieldID) { + fields = append(fields, file.FieldFieldID) + } return fields } @@ -8046,6 +8147,9 @@ func (m *FileMutation) ClearField(name string) error { case file.FieldOp: m.ClearOp() return nil + case file.FieldFieldID: + m.ClearFieldID() + return nil } return fmt.Errorf("unknown File nullable field %s", name) } @@ -8069,6 +8173,9 @@ func (m *FileMutation) ResetField(name string) error { case file.FieldOp: m.ResetOp() return nil + case file.FieldFieldID: + m.ResetFieldID() + return nil } return fmt.Errorf("unknown File field %s", name) } diff --git a/entc/integration/ent/schema/file.go b/entc/integration/ent/schema/file.go index 72d2310fbd..388305acef 100644 --- a/entc/integration/ent/schema/file.go +++ b/entc/integration/ent/schema/file.go @@ -42,6 +42,10 @@ func (File) Fields() []ent.Field { Optional(), field.Bool("op"). Optional(), + // Skip generating the "FieldID" predicate + // as it conflicts with the "FieldID" constant. + field.Int("field_id"). + Optional(), } } diff --git a/entc/integration/gremlin/ent/file.go b/entc/integration/gremlin/ent/file.go index 8cd95d808f..d217ad2a23 100644 --- a/entc/integration/gremlin/ent/file.go +++ b/entc/integration/gremlin/ent/file.go @@ -30,6 +30,8 @@ type File struct { Group string `json:"group,omitempty"` // Op holds the value of the "op" field. Op bool `json:"op,omitempty"` + // FieldID holds the value of the "field_id" field. + FieldID int `json:"field_id,omitempty"` // Edges holds the relations/edges for other nodes in the graph. // The values are being populated by the FileQuery when eager-loading is set. Edges FileEdges `json:"file_edges"` @@ -90,12 +92,13 @@ func (f *File) FromResponse(res *gremlin.Response) error { return err } var scanf struct { - ID string `json:"id,omitempty"` - Size int `json:"fsize,omitempty"` - Name string `json:"name,omitempty"` - User *string `json:"user,omitempty"` - Group string `json:"group,omitempty"` - Op bool `json:"op,omitempty"` + ID string `json:"id,omitempty"` + Size int `json:"fsize,omitempty"` + Name string `json:"name,omitempty"` + User *string `json:"user,omitempty"` + Group string `json:"group,omitempty"` + Op bool `json:"op,omitempty"` + FieldID int `json:"field_id,omitempty"` } if err := vmap.Decode(&scanf); err != nil { return err @@ -106,6 +109,7 @@ func (f *File) FromResponse(res *gremlin.Response) error { f.User = scanf.User f.Group = scanf.Group f.Op = scanf.Op + f.FieldID = scanf.FieldID return nil } @@ -163,6 +167,9 @@ func (f *File) String() string { builder.WriteString(", ") builder.WriteString("op=") builder.WriteString(fmt.Sprintf("%v", f.Op)) + builder.WriteString(", ") + builder.WriteString("field_id=") + builder.WriteString(fmt.Sprintf("%v", f.FieldID)) builder.WriteByte(')') return builder.String() } @@ -177,24 +184,26 @@ func (f *Files) FromResponse(res *gremlin.Response) error { return err } var scanf []struct { - ID string `json:"id,omitempty"` - Size int `json:"fsize,omitempty"` - Name string `json:"name,omitempty"` - User *string `json:"user,omitempty"` - Group string `json:"group,omitempty"` - Op bool `json:"op,omitempty"` + ID string `json:"id,omitempty"` + Size int `json:"fsize,omitempty"` + Name string `json:"name,omitempty"` + User *string `json:"user,omitempty"` + Group string `json:"group,omitempty"` + Op bool `json:"op,omitempty"` + FieldID int `json:"field_id,omitempty"` } if err := vmap.Decode(&scanf); err != nil { return err } for _, v := range scanf { *f = append(*f, &File{ - ID: v.ID, - Size: v.Size, - Name: v.Name, - User: v.User, - Group: v.Group, - Op: v.Op, + ID: v.ID, + Size: v.Size, + Name: v.Name, + User: v.User, + Group: v.Group, + Op: v.Op, + FieldID: v.FieldID, }) } return nil diff --git a/entc/integration/gremlin/ent/file/file.go b/entc/integration/gremlin/ent/file/file.go index 98991c6762..60aee3ec98 100644 --- a/entc/integration/gremlin/ent/file/file.go +++ b/entc/integration/gremlin/ent/file/file.go @@ -21,6 +21,8 @@ const ( FieldGroup = "group" // FieldOp holds the string denoting the op field in the database. FieldOp = "op" + // FieldFieldID holds the string denoting the field_id field in the database. + FieldFieldID = "field_id" // EdgeOwner holds the string denoting the owner edge name in mutations. EdgeOwner = "owner" // EdgeType holds the string denoting the type edge name in mutations. diff --git a/entc/integration/gremlin/ent/file/where.go b/entc/integration/gremlin/ent/file/where.go index fed8740171..e9b51cbcd5 100644 --- a/entc/integration/gremlin/ent/file/where.go +++ b/entc/integration/gremlin/ent/file/where.go @@ -494,6 +494,84 @@ func OpNotNil() predicate.File { }) } +// FieldIDEQ applies the EQ predicate on the "field_id" field. +func FieldIDEQ(v int) predicate.File { + return predicate.File(func(t *dsl.Traversal) { + t.Has(Label, FieldFieldID, p.EQ(v)) + }) +} + +// FieldIDNEQ applies the NEQ predicate on the "field_id" field. +func FieldIDNEQ(v int) predicate.File { + return predicate.File(func(t *dsl.Traversal) { + t.Has(Label, FieldFieldID, p.NEQ(v)) + }) +} + +// FieldIDIn applies the In predicate on the "field_id" field. +func FieldIDIn(vs ...int) predicate.File { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.File(func(t *dsl.Traversal) { + t.Has(Label, FieldFieldID, p.Within(v...)) + }) +} + +// FieldIDNotIn applies the NotIn predicate on the "field_id" field. +func FieldIDNotIn(vs ...int) predicate.File { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.File(func(t *dsl.Traversal) { + t.Has(Label, FieldFieldID, p.Without(v...)) + }) +} + +// FieldIDGT applies the GT predicate on the "field_id" field. +func FieldIDGT(v int) predicate.File { + return predicate.File(func(t *dsl.Traversal) { + t.Has(Label, FieldFieldID, p.GT(v)) + }) +} + +// FieldIDGTE applies the GTE predicate on the "field_id" field. +func FieldIDGTE(v int) predicate.File { + return predicate.File(func(t *dsl.Traversal) { + t.Has(Label, FieldFieldID, p.GTE(v)) + }) +} + +// FieldIDLT applies the LT predicate on the "field_id" field. +func FieldIDLT(v int) predicate.File { + return predicate.File(func(t *dsl.Traversal) { + t.Has(Label, FieldFieldID, p.LT(v)) + }) +} + +// FieldIDLTE applies the LTE predicate on the "field_id" field. +func FieldIDLTE(v int) predicate.File { + return predicate.File(func(t *dsl.Traversal) { + t.Has(Label, FieldFieldID, p.LTE(v)) + }) +} + +// FieldIDIsNil applies the IsNil predicate on the "field_id" field. +func FieldIDIsNil() predicate.File { + return predicate.File(func(t *dsl.Traversal) { + t.HasLabel(Label).HasNot(FieldFieldID) + }) +} + +// FieldIDNotNil applies the NotNil predicate on the "field_id" field. +func FieldIDNotNil() predicate.File { + return predicate.File(func(t *dsl.Traversal) { + t.HasLabel(Label).Has(FieldFieldID) + }) +} + // HasOwner applies the HasEdge predicate on the "owner" edge. func HasOwner() predicate.File { return predicate.File(func(t *dsl.Traversal) { diff --git a/entc/integration/gremlin/ent/file_create.go b/entc/integration/gremlin/ent/file_create.go index 81b404b3d0..49dcb6d995 100644 --- a/entc/integration/gremlin/ent/file_create.go +++ b/entc/integration/gremlin/ent/file_create.go @@ -90,6 +90,20 @@ func (fc *FileCreate) SetNillableOp(b *bool) *FileCreate { return fc } +// SetFieldID sets the "field_id" field. +func (fc *FileCreate) SetFieldID(i int) *FileCreate { + fc.mutation.SetFieldID(i) + return fc +} + +// SetNillableFieldID sets the "field_id" field if the given value is not nil. +func (fc *FileCreate) SetNillableFieldID(i *int) *FileCreate { + if i != nil { + fc.SetFieldID(*i) + } + return fc +} + // SetOwnerID sets the "owner" edge to the User entity by ID. func (fc *FileCreate) SetOwnerID(id string) *FileCreate { fc.mutation.SetOwnerID(id) @@ -280,6 +294,9 @@ func (fc *FileCreate) gremlin() *dsl.Traversal { if value, ok := fc.mutation.GetOp(); ok { v.Property(dsl.Single, file.FieldOp, value) } + if value, ok := fc.mutation.FieldID(); ok { + v.Property(dsl.Single, file.FieldFieldID, value) + } for _, id := range fc.mutation.OwnerIDs() { v.AddE(user.FilesLabel).From(g.V(id)).InV() } diff --git a/entc/integration/gremlin/ent/file_update.go b/entc/integration/gremlin/ent/file_update.go index c9142bddc0..53e55273ff 100644 --- a/entc/integration/gremlin/ent/file_update.go +++ b/entc/integration/gremlin/ent/file_update.go @@ -122,6 +122,33 @@ func (fu *FileUpdate) ClearOp() *FileUpdate { return fu } +// SetFieldID sets the "field_id" field. +func (fu *FileUpdate) SetFieldID(i int) *FileUpdate { + fu.mutation.ResetFieldID() + fu.mutation.SetFieldID(i) + return fu +} + +// SetNillableFieldID sets the "field_id" field if the given value is not nil. +func (fu *FileUpdate) SetNillableFieldID(i *int) *FileUpdate { + if i != nil { + fu.SetFieldID(*i) + } + return fu +} + +// AddFieldID adds i to the "field_id" field. +func (fu *FileUpdate) AddFieldID(i int) *FileUpdate { + fu.mutation.AddFieldID(i) + return fu +} + +// ClearFieldID clears the value of the "field_id" field. +func (fu *FileUpdate) ClearFieldID() *FileUpdate { + fu.mutation.ClearFieldID() + return fu +} + // SetOwnerID sets the "owner" edge to the User entity by ID. func (fu *FileUpdate) SetOwnerID(id string) *FileUpdate { fu.mutation.SetOwnerID(id) @@ -329,6 +356,12 @@ func (fu *FileUpdate) gremlin() *dsl.Traversal { if value, ok := fu.mutation.GetOp(); ok { v.Property(dsl.Single, file.FieldOp, value) } + if value, ok := fu.mutation.FieldID(); ok { + v.Property(dsl.Single, file.FieldFieldID, value) + } + if value, ok := fu.mutation.AddedFieldID(); ok { + v.Property(dsl.Single, file.FieldFieldID, __.Union(__.Values(file.FieldFieldID), __.Constant(value)).Sum()) + } var properties []interface{} if fu.mutation.UserCleared() { properties = append(properties, file.FieldUser) @@ -339,6 +372,9 @@ func (fu *FileUpdate) gremlin() *dsl.Traversal { if fu.mutation.OpCleared() { properties = append(properties, file.FieldOp) } + if fu.mutation.FieldIDCleared() { + properties = append(properties, file.FieldFieldID) + } if len(properties) > 0 { v.SideEffect(__.Properties(properties...).Drop()) } @@ -477,6 +513,33 @@ func (fuo *FileUpdateOne) ClearOp() *FileUpdateOne { return fuo } +// SetFieldID sets the "field_id" field. +func (fuo *FileUpdateOne) SetFieldID(i int) *FileUpdateOne { + fuo.mutation.ResetFieldID() + fuo.mutation.SetFieldID(i) + return fuo +} + +// SetNillableFieldID sets the "field_id" field if the given value is not nil. +func (fuo *FileUpdateOne) SetNillableFieldID(i *int) *FileUpdateOne { + if i != nil { + fuo.SetFieldID(*i) + } + return fuo +} + +// AddFieldID adds i to the "field_id" field. +func (fuo *FileUpdateOne) AddFieldID(i int) *FileUpdateOne { + fuo.mutation.AddFieldID(i) + return fuo +} + +// ClearFieldID clears the value of the "field_id" field. +func (fuo *FileUpdateOne) ClearFieldID() *FileUpdateOne { + fuo.mutation.ClearFieldID() + return fuo +} + // SetOwnerID sets the "owner" edge to the User entity by ID. func (fuo *FileUpdateOne) SetOwnerID(id string) *FileUpdateOne { fuo.mutation.SetOwnerID(id) @@ -702,6 +765,12 @@ func (fuo *FileUpdateOne) gremlin(id string) *dsl.Traversal { if value, ok := fuo.mutation.GetOp(); ok { v.Property(dsl.Single, file.FieldOp, value) } + if value, ok := fuo.mutation.FieldID(); ok { + v.Property(dsl.Single, file.FieldFieldID, value) + } + if value, ok := fuo.mutation.AddedFieldID(); ok { + v.Property(dsl.Single, file.FieldFieldID, __.Union(__.Values(file.FieldFieldID), __.Constant(value)).Sum()) + } var properties []interface{} if fuo.mutation.UserCleared() { properties = append(properties, file.FieldUser) @@ -712,6 +781,9 @@ func (fuo *FileUpdateOne) gremlin(id string) *dsl.Traversal { if fuo.mutation.OpCleared() { properties = append(properties, file.FieldOp) } + if fuo.mutation.FieldIDCleared() { + properties = append(properties, file.FieldFieldID) + } if len(properties) > 0 { v.SideEffect(__.Properties(properties...).Drop()) } diff --git a/entc/integration/gremlin/ent/mutation.go b/entc/integration/gremlin/ent/mutation.go index 7f1dc46496..92e5140d5c 100644 --- a/entc/integration/gremlin/ent/mutation.go +++ b/entc/integration/gremlin/ent/mutation.go @@ -7371,6 +7371,8 @@ type FileMutation struct { user *string group *string _op *bool + field_id *int + addfield_id *int clearedFields map[string]struct{} owner *string clearedowner bool @@ -7721,6 +7723,76 @@ func (m *FileMutation) ResetOp() { delete(m.clearedFields, file.FieldOp) } +// SetFieldID sets the "field_id" field. +func (m *FileMutation) SetFieldID(i int) { + m.field_id = &i + m.addfield_id = nil +} + +// FieldID returns the value of the "field_id" field in the mutation. +func (m *FileMutation) FieldID() (r int, exists bool) { + v := m.field_id + if v == nil { + return + } + return *v, true +} + +// OldFieldID returns the old "field_id" field's value of the File entity. +// If the File 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 *FileMutation) OldFieldID(ctx context.Context) (v int, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldFieldID is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldFieldID requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldFieldID: %w", err) + } + return oldValue.FieldID, nil +} + +// AddFieldID adds i to the "field_id" field. +func (m *FileMutation) AddFieldID(i int) { + if m.addfield_id != nil { + *m.addfield_id += i + } else { + m.addfield_id = &i + } +} + +// AddedFieldID returns the value that was added to the "field_id" field in this mutation. +func (m *FileMutation) AddedFieldID() (r int, exists bool) { + v := m.addfield_id + if v == nil { + return + } + return *v, true +} + +// ClearFieldID clears the value of the "field_id" field. +func (m *FileMutation) ClearFieldID() { + m.field_id = nil + m.addfield_id = nil + m.clearedFields[file.FieldFieldID] = struct{}{} +} + +// FieldIDCleared returns if the "field_id" field was cleared in this mutation. +func (m *FileMutation) FieldIDCleared() bool { + _, ok := m.clearedFields[file.FieldFieldID] + return ok +} + +// ResetFieldID resets all changes to the "field_id" field. +func (m *FileMutation) ResetFieldID() { + m.field_id = nil + m.addfield_id = nil + delete(m.clearedFields, file.FieldFieldID) +} + // SetOwnerID sets the "owner" edge to the User entity by id. func (m *FileMutation) SetOwnerID(id string) { m.owner = &id @@ -7872,7 +7944,7 @@ func (m *FileMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *FileMutation) Fields() []string { - fields := make([]string, 0, 5) + fields := make([]string, 0, 6) if m.size != nil { fields = append(fields, file.FieldSize) } @@ -7888,6 +7960,9 @@ func (m *FileMutation) Fields() []string { if m._op != nil { fields = append(fields, file.FieldOp) } + if m.field_id != nil { + fields = append(fields, file.FieldFieldID) + } return fields } @@ -7906,6 +7981,8 @@ func (m *FileMutation) Field(name string) (ent.Value, bool) { return m.Group() case file.FieldOp: return m.GetOp() + case file.FieldFieldID: + return m.FieldID() } return nil, false } @@ -7925,6 +8002,8 @@ func (m *FileMutation) OldField(ctx context.Context, name string) (ent.Value, er return m.OldGroup(ctx) case file.FieldOp: return m.OldOp(ctx) + case file.FieldFieldID: + return m.OldFieldID(ctx) } return nil, fmt.Errorf("unknown File field %s", name) } @@ -7969,6 +8048,13 @@ func (m *FileMutation) SetField(name string, value ent.Value) error { } m.SetOp(v) return nil + case file.FieldFieldID: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetFieldID(v) + return nil } return fmt.Errorf("unknown File field %s", name) } @@ -7980,6 +8066,9 @@ func (m *FileMutation) AddedFields() []string { if m.addsize != nil { fields = append(fields, file.FieldSize) } + if m.addfield_id != nil { + fields = append(fields, file.FieldFieldID) + } return fields } @@ -7990,6 +8079,8 @@ func (m *FileMutation) AddedField(name string) (ent.Value, bool) { switch name { case file.FieldSize: return m.AddedSize() + case file.FieldFieldID: + return m.AddedFieldID() } return nil, false } @@ -8006,6 +8097,13 @@ func (m *FileMutation) AddField(name string, value ent.Value) error { } m.AddSize(v) return nil + case file.FieldFieldID: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.AddFieldID(v) + return nil } return fmt.Errorf("unknown File numeric field %s", name) } @@ -8023,6 +8121,9 @@ func (m *FileMutation) ClearedFields() []string { if m.FieldCleared(file.FieldOp) { fields = append(fields, file.FieldOp) } + if m.FieldCleared(file.FieldFieldID) { + fields = append(fields, file.FieldFieldID) + } return fields } @@ -8046,6 +8147,9 @@ func (m *FileMutation) ClearField(name string) error { case file.FieldOp: m.ClearOp() return nil + case file.FieldFieldID: + m.ClearFieldID() + return nil } return fmt.Errorf("unknown File nullable field %s", name) } @@ -8069,6 +8173,9 @@ func (m *FileMutation) ResetField(name string) error { case file.FieldOp: m.ResetOp() return nil + case file.FieldFieldID: + m.ResetFieldID() + return nil } return fmt.Errorf("unknown File field %s", name) }