diff --git a/entc/gen/type.go b/entc/gen/type.go index ad856a447..074be967c 100644 --- a/entc/gen/type.go +++ b/entc/gen/type.go @@ -822,6 +822,10 @@ func (f Field) BasicType(ident string) (expr string) { case rt.TypeEqual(nullBoolType): expr = fmt.Sprintf("%s.Bool", ident) } + case field.TypeBytes: + if rt.Kind == reflect.Slice { + expr = fmt.Sprintf("[]byte(%s)", ident) + } case field.TypeTime: switch { case rt.Kind == reflect.Bool: diff --git a/entc/integration/ent/fieldtype.go b/entc/integration/ent/fieldtype.go index bf4f66996..92e48c2aa 100644 --- a/entc/integration/ent/fieldtype.go +++ b/entc/integration/ent/fieldtype.go @@ -8,6 +8,7 @@ package ent import ( "fmt" + "net" "net/http" "strings" "time" @@ -93,7 +94,9 @@ type FieldType struct { // Deleted holds the value of the "deleted" field. Deleted sql.NullBool `json:"deleted,omitempty"` // DeletedAt holds the value of the "deleted_at" field. - DeletedAt sql.NullTime `json:"deleted_at,omitempty"` + DeletedAt sql.NullTime `json:"deleted_at,omitempty"` + // IP holds the value of the "ip" field. + IP net.IP `json:"ip,omitempty"` file_field *int } @@ -137,6 +140,7 @@ func (*FieldType) scanValues() []interface{} { &sql.NullBool{}, // null_active &sql.NullBool{}, // deleted &sql.NullTime{}, // deleted_at + &[]byte{}, // ip } } @@ -346,7 +350,12 @@ func (ft *FieldType) assignValues(values ...interface{}) error { } else if value != nil { ft.DeletedAt = *value } - values = values[36:] + if value, ok := values[36].(*[]byte); !ok { + return fmt.Errorf("unexpected type %T for field ip", values[36]) + } else if value != nil { + ft.IP = *value + } + values = values[37:] if len(values) == len(fieldtype.ForeignKeys) { if value, ok := values[0].(*sql.NullInt64); !ok { return fmt.Errorf("unexpected type %T for edge-field file_field", value) @@ -471,6 +480,8 @@ func (ft *FieldType) String() string { builder.WriteString(fmt.Sprintf("%v", ft.Deleted)) builder.WriteString(", deleted_at=") builder.WriteString(fmt.Sprintf("%v", ft.DeletedAt)) + builder.WriteString(", ip=") + builder.WriteString(fmt.Sprintf("%v", ft.IP)) builder.WriteByte(')') return builder.String() } diff --git a/entc/integration/ent/fieldtype/fieldtype.go b/entc/integration/ent/fieldtype/fieldtype.go index c56543bea..bbc9e1e82 100644 --- a/entc/integration/ent/fieldtype/fieldtype.go +++ b/entc/integration/ent/fieldtype/fieldtype.go @@ -87,6 +87,8 @@ const ( FieldDeleted = "deleted" // FieldDeletedAt holds the string denoting the deleted_at field in the database. FieldDeletedAt = "deleted_at" + // FieldIP holds the string denoting the ip field in the database. + FieldIP = "ip" // Table holds the table name of the fieldtype in the database. Table = "field_types" @@ -131,6 +133,7 @@ var Columns = []string{ FieldNullActive, FieldDeleted, FieldDeletedAt, + FieldIP, } // ForeignKeys holds the SQL foreign-keys that are owned by the FieldType type. diff --git a/entc/integration/ent/fieldtype/where.go b/entc/integration/ent/fieldtype/where.go index e55923725..cece902a1 100644 --- a/entc/integration/ent/fieldtype/where.go +++ b/entc/integration/ent/fieldtype/where.go @@ -7,6 +7,7 @@ package fieldtype import ( + "net" "net/http" "time" @@ -353,6 +354,14 @@ func DeletedAt(v sql.NullTime) predicate.FieldType { }) } +// IP applies equality check predicate on the "ip" field. It's identical to IPEQ. +func IP(v net.IP) predicate.FieldType { + vc := []byte(v) + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldIP), vc)) + }) +} + // IntEQ applies the EQ predicate on the "int" field. func IntEQ(v int) predicate.FieldType { return predicate.FieldType(func(s *sql.Selector) { @@ -3597,6 +3606,102 @@ func DeletedAtNotNil() predicate.FieldType { }) } +// IPEQ applies the EQ predicate on the "ip" field. +func IPEQ(v net.IP) predicate.FieldType { + vc := []byte(v) + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldIP), vc)) + }) +} + +// IPNEQ applies the NEQ predicate on the "ip" field. +func IPNEQ(v net.IP) predicate.FieldType { + vc := []byte(v) + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.NEQ(s.C(FieldIP), vc)) + }) +} + +// IPIn applies the In predicate on the "ip" field. +func IPIn(vs ...net.IP) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = []byte(vs[i]) + } + return predicate.FieldType(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(FieldIP), v...)) + }) +} + +// IPNotIn applies the NotIn predicate on the "ip" field. +func IPNotIn(vs ...net.IP) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = []byte(vs[i]) + } + return predicate.FieldType(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(FieldIP), v...)) + }) +} + +// IPGT applies the GT predicate on the "ip" field. +func IPGT(v net.IP) predicate.FieldType { + vc := []byte(v) + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.GT(s.C(FieldIP), vc)) + }) +} + +// IPGTE applies the GTE predicate on the "ip" field. +func IPGTE(v net.IP) predicate.FieldType { + vc := []byte(v) + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.GTE(s.C(FieldIP), vc)) + }) +} + +// IPLT applies the LT predicate on the "ip" field. +func IPLT(v net.IP) predicate.FieldType { + vc := []byte(v) + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.LT(s.C(FieldIP), vc)) + }) +} + +// IPLTE applies the LTE predicate on the "ip" field. +func IPLTE(v net.IP) predicate.FieldType { + vc := []byte(v) + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.LTE(s.C(FieldIP), vc)) + }) +} + +// IPIsNil applies the IsNil predicate on the "ip" field. +func IPIsNil() predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.IsNull(s.C(FieldIP))) + }) +} + +// IPNotNil applies the NotNil predicate on the "ip" field. +func IPNotNil() predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.NotNull(s.C(FieldIP))) + }) +} + // And groups list of predicates with the AND operator between them. func And(predicates ...predicate.FieldType) predicate.FieldType { return predicate.FieldType(func(s *sql.Selector) { diff --git a/entc/integration/ent/fieldtype_create.go b/entc/integration/ent/fieldtype_create.go index 48edb10cf..fae9e68d7 100644 --- a/entc/integration/ent/fieldtype_create.go +++ b/entc/integration/ent/fieldtype_create.go @@ -10,6 +10,7 @@ import ( "context" "errors" "fmt" + "net" "net/http" "time" @@ -443,6 +444,12 @@ func (ftc *FieldTypeCreate) SetDeletedAt(st sql.NullTime) *FieldTypeCreate { return ftc } +// SetIP sets the ip field. +func (ftc *FieldTypeCreate) SetIP(n net.IP) *FieldTypeCreate { + ftc.mutation.SetIP(n) + return ftc +} + // Save creates the FieldType in the database. func (ftc *FieldTypeCreate) Save(ctx context.Context) (*FieldType, error) { if _, ok := ftc.mutation.Int(); !ok { @@ -805,6 +812,14 @@ func (ftc *FieldTypeCreate) sqlSave(ctx context.Context) (*FieldType, error) { }) ft.DeletedAt = value } + if value, ok := ftc.mutation.IP(); ok { + _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ + Type: field.TypeBytes, + Value: value, + Column: fieldtype.FieldIP, + }) + ft.IP = value + } if err := sqlgraph.CreateNode(ctx, ftc.driver, _spec); err != nil { if cerr, ok := isSQLConstraintError(err); ok { err = cerr diff --git a/entc/integration/ent/fieldtype_update.go b/entc/integration/ent/fieldtype_update.go index 8dff26242..169ef370b 100644 --- a/entc/integration/ent/fieldtype_update.go +++ b/entc/integration/ent/fieldtype_update.go @@ -9,6 +9,7 @@ package ent import ( "context" "fmt" + "net" "net/http" "time" @@ -804,6 +805,18 @@ func (ftu *FieldTypeUpdate) ClearDeletedAt() *FieldTypeUpdate { return ftu } +// SetIP sets the ip field. +func (ftu *FieldTypeUpdate) SetIP(n net.IP) *FieldTypeUpdate { + ftu.mutation.SetIP(n) + return ftu +} + +// ClearIP clears the value of ip. +func (ftu *FieldTypeUpdate) ClearIP() *FieldTypeUpdate { + ftu.mutation.ClearIP() + return ftu +} + // Save executes the query and returns the number of rows/vertices matched by this operation. func (ftu *FieldTypeUpdate) Save(ctx context.Context) (int, error) { if v, ok := ftu.mutation.ValidateOptionalInt32(); ok { @@ -1489,6 +1502,19 @@ func (ftu *FieldTypeUpdate) sqlSave(ctx context.Context) (n int, err error) { Column: fieldtype.FieldDeletedAt, }) } + if value, ok := ftu.mutation.IP(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeBytes, + Value: value, + Column: fieldtype.FieldIP, + }) + } + if ftu.mutation.IPCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeBytes, + Column: fieldtype.FieldIP, + }) + } if n, err = sqlgraph.UpdateNodes(ctx, ftu.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{fieldtype.Label} @@ -2277,6 +2303,18 @@ func (ftuo *FieldTypeUpdateOne) ClearDeletedAt() *FieldTypeUpdateOne { return ftuo } +// SetIP sets the ip field. +func (ftuo *FieldTypeUpdateOne) SetIP(n net.IP) *FieldTypeUpdateOne { + ftuo.mutation.SetIP(n) + return ftuo +} + +// ClearIP clears the value of ip. +func (ftuo *FieldTypeUpdateOne) ClearIP() *FieldTypeUpdateOne { + ftuo.mutation.ClearIP() + return ftuo +} + // Save executes the query and returns the updated entity. func (ftuo *FieldTypeUpdateOne) Save(ctx context.Context) (*FieldType, error) { if v, ok := ftuo.mutation.ValidateOptionalInt32(); ok { @@ -2960,6 +2998,19 @@ func (ftuo *FieldTypeUpdateOne) sqlSave(ctx context.Context) (ft *FieldType, err Column: fieldtype.FieldDeletedAt, }) } + if value, ok := ftuo.mutation.IP(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeBytes, + Value: value, + Column: fieldtype.FieldIP, + }) + } + if ftuo.mutation.IPCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeBytes, + Column: fieldtype.FieldIP, + }) + } ft = &FieldType{config: ftuo.config} _spec.Assign = ft.assignValues _spec.ScanValues = ft.scanValues() diff --git a/entc/integration/ent/migrate/schema.go b/entc/integration/ent/migrate/schema.go index b0b9459ea..953a8cf4d 100644 --- a/entc/integration/ent/migrate/schema.go +++ b/entc/integration/ent/migrate/schema.go @@ -89,6 +89,7 @@ var ( {Name: "null_active", Type: field.TypeBool, Nullable: true}, {Name: "deleted", Type: field.TypeBool, Nullable: true}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, + {Name: "ip", Type: field.TypeBytes, Nullable: true}, {Name: "file_field", Type: field.TypeInt, Nullable: true}, } // FieldTypesTable holds the schema information for the "field_types" table. @@ -99,7 +100,7 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "field_types_files_field", - Columns: []*schema.Column{FieldTypesColumns[37]}, + Columns: []*schema.Column{FieldTypesColumns[38]}, RefColumns: []*schema.Column{FilesColumns[0]}, OnDelete: schema.SetNull, diff --git a/entc/integration/ent/mutation.go b/entc/integration/ent/mutation.go index 84c47af49..415004ecb 100644 --- a/entc/integration/ent/mutation.go +++ b/entc/integration/ent/mutation.go @@ -9,6 +9,7 @@ package ent import ( "context" "fmt" + "net" "net/http" "sync" "time" @@ -1262,6 +1263,7 @@ type FieldTypeMutation struct { null_active *schema.Status deleted *sql.NullBool deleted_at *sql.NullTime + ip *net.IP clearedFields map[string]struct{} done bool oldValue func(context.Context) (*FieldType, error) @@ -3580,6 +3582,56 @@ func (m *FieldTypeMutation) ResetDeletedAt() { delete(m.clearedFields, fieldtype.FieldDeletedAt) } +// SetIP sets the ip field. +func (m *FieldTypeMutation) SetIP(n net.IP) { + m.ip = &n +} + +// IP returns the ip value in the mutation. +func (m *FieldTypeMutation) IP() (r net.IP, exists bool) { + v := m.ip + if v == nil { + return + } + return *v, true +} + +// OldIP returns the old ip value of the FieldType. +// If the FieldType 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 database query fails. +func (m *FieldTypeMutation) OldIP(ctx context.Context) (v net.IP, err error) { + if !m.op.Is(OpUpdateOne) { + return v, fmt.Errorf("OldIP is allowed only on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, fmt.Errorf("OldIP requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldIP: %w", err) + } + return oldValue.IP, nil +} + +// ClearIP clears the value of ip. +func (m *FieldTypeMutation) ClearIP() { + m.ip = nil + m.clearedFields[fieldtype.FieldIP] = struct{}{} +} + +// IPCleared returns if the field ip was cleared in this mutation. +func (m *FieldTypeMutation) IPCleared() bool { + _, ok := m.clearedFields[fieldtype.FieldIP] + return ok +} + +// ResetIP reset all changes of the "ip" field. +func (m *FieldTypeMutation) ResetIP() { + m.ip = nil + delete(m.clearedFields, fieldtype.FieldIP) +} + // Op returns the operation name. func (m *FieldTypeMutation) Op() Op { return m.op @@ -3594,7 +3646,7 @@ func (m *FieldTypeMutation) Type() string { // this mutation. Note that, in order to get all numeric // fields that were in/decremented, call AddedFields(). func (m *FieldTypeMutation) Fields() []string { - fields := make([]string, 0, 36) + fields := make([]string, 0, 37) if m.int != nil { fields = append(fields, fieldtype.FieldInt) } @@ -3703,6 +3755,9 @@ func (m *FieldTypeMutation) Fields() []string { if m.deleted_at != nil { fields = append(fields, fieldtype.FieldDeletedAt) } + if m.ip != nil { + fields = append(fields, fieldtype.FieldIP) + } return fields } @@ -3783,6 +3838,8 @@ func (m *FieldTypeMutation) Field(name string) (ent.Value, bool) { return m.Deleted() case fieldtype.FieldDeletedAt: return m.DeletedAt() + case fieldtype.FieldIP: + return m.IP() } return nil, false } @@ -3864,6 +3921,8 @@ func (m *FieldTypeMutation) OldField(ctx context.Context, name string) (ent.Valu return m.OldDeleted(ctx) case fieldtype.FieldDeletedAt: return m.OldDeletedAt(ctx) + case fieldtype.FieldIP: + return m.OldIP(ctx) } return nil, fmt.Errorf("unknown FieldType field %s", name) } @@ -4125,6 +4184,13 @@ func (m *FieldTypeMutation) SetField(name string, value ent.Value) error { } m.SetDeletedAt(v) return nil + case fieldtype.FieldIP: + v, ok := value.(net.IP) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetIP(v) + return nil } return fmt.Errorf("unknown FieldType field %s", name) } @@ -4539,6 +4605,9 @@ func (m *FieldTypeMutation) ClearedFields() []string { if m.FieldCleared(fieldtype.FieldDeletedAt) { fields = append(fields, fieldtype.FieldDeletedAt) } + if m.FieldCleared(fieldtype.FieldIP) { + fields = append(fields, fieldtype.FieldIP) + } return fields } @@ -4646,6 +4715,9 @@ func (m *FieldTypeMutation) ClearField(name string) error { case fieldtype.FieldDeletedAt: m.ClearDeletedAt() return nil + case fieldtype.FieldIP: + m.ClearIP() + return nil } return fmt.Errorf("unknown FieldType nullable field %s", name) } @@ -4763,6 +4835,9 @@ func (m *FieldTypeMutation) ResetField(name string) error { case fieldtype.FieldDeletedAt: m.ResetDeletedAt() return nil + case fieldtype.FieldIP: + m.ResetIP() + return nil } return fmt.Errorf("unknown FieldType field %s", name) } diff --git a/entc/integration/ent/schema/fieldtype.go b/entc/integration/ent/schema/fieldtype.go index 7f35ff6e2..ee43ab337 100644 --- a/entc/integration/ent/schema/fieldtype.go +++ b/entc/integration/ent/schema/fieldtype.go @@ -7,6 +7,7 @@ package schema import ( "database/sql/driver" "fmt" + "net" "net/http" "net/url" @@ -99,6 +100,9 @@ func (FieldType) Fields() []ent.Field { field.Time("deleted_at"). Optional(). GoType(&sql.NullTime{}), + field.Bytes("ip"). + Optional(). + GoType(net.IP("127.0.0.1")), } }