diff --git a/entc/integration/gremlin/ent/fieldtype.go b/entc/integration/gremlin/ent/fieldtype.go index 11bbe1cca..dddfa7aab 100644 --- a/entc/integration/gremlin/ent/fieldtype.go +++ b/entc/integration/gremlin/ent/fieldtype.go @@ -115,6 +115,8 @@ type FieldType struct { NullFloat sql.NullFloat64 `json:"null_float,omitempty"` // Role holds the value of the "role" field. Role role.Role `json:"role,omitempty"` + // MAC holds the value of the "mac" field. + MAC schema.MAC `json:"mac,omitempty"` } // FromResponse scans the gremlin response data into FieldType. @@ -170,6 +172,7 @@ func (ft *FieldType) FromResponse(res *gremlin.Response) error { SchemaFloat32 schema.Float32 `json:"schema_float32,omitempty"` NullFloat sql.NullFloat64 `json:"null_float,omitempty"` Role role.Role `json:"role,omitempty"` + MAC schema.MAC `json:"mac,omitempty"` } if err := vmap.Decode(&scanft); err != nil { return err @@ -220,6 +223,7 @@ func (ft *FieldType) FromResponse(res *gremlin.Response) error { ft.SchemaFloat32 = scanft.SchemaFloat32 ft.NullFloat = scanft.NullFloat ft.Role = scanft.Role + ft.MAC = scanft.MAC return nil } @@ -354,6 +358,8 @@ func (ft *FieldType) String() string { builder.WriteString(fmt.Sprintf("%v", ft.NullFloat)) builder.WriteString(", role=") builder.WriteString(fmt.Sprintf("%v", ft.Role)) + builder.WriteString(", mac=") + builder.WriteString(fmt.Sprintf("%v", ft.MAC)) builder.WriteByte(')') return builder.String() } @@ -414,6 +420,7 @@ func (ft *FieldTypes) FromResponse(res *gremlin.Response) error { SchemaFloat32 schema.Float32 `json:"schema_float32,omitempty"` NullFloat sql.NullFloat64 `json:"null_float,omitempty"` Role role.Role `json:"role,omitempty"` + MAC schema.MAC `json:"mac,omitempty"` } if err := vmap.Decode(&scanft); err != nil { return err @@ -466,6 +473,7 @@ func (ft *FieldTypes) FromResponse(res *gremlin.Response) error { SchemaFloat32: v.SchemaFloat32, NullFloat: v.NullFloat, Role: v.Role, + MAC: v.MAC, }) } return nil diff --git a/entc/integration/gremlin/ent/fieldtype/fieldtype.go b/entc/integration/gremlin/ent/fieldtype/fieldtype.go index 1119a410f..de82f1e22 100644 --- a/entc/integration/gremlin/ent/fieldtype/fieldtype.go +++ b/entc/integration/gremlin/ent/fieldtype/fieldtype.go @@ -107,6 +107,8 @@ const ( FieldNullFloat = "null_float" // FieldRole holds the string denoting the role field in the database. FieldRole = "role" + // FieldMAC holds the string denoting the mac field in the database. + FieldMAC = "mac" ) var ( diff --git a/entc/integration/gremlin/ent/fieldtype/where.go b/entc/integration/gremlin/ent/fieldtype/where.go index 955695b49..7718a5387 100644 --- a/entc/integration/gremlin/ent/fieldtype/where.go +++ b/entc/integration/gremlin/ent/fieldtype/where.go @@ -394,6 +394,14 @@ func SchemaFloat32(v schema.Float32) predicate.FieldType { }) } +// MAC applies equality check predicate on the "mac" field. It's identical to MACEQ. +func MAC(v schema.MAC) predicate.FieldType { + vc := v.String() + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldMAC, p.EQ(vc)) + }) +} + // IntEQ applies the EQ predicate on the "int" field. func IntEQ(v int) predicate.FieldType { return predicate.FieldType(func(t *dsl.Traversal) { @@ -3716,6 +3724,114 @@ func RoleNotIn(vs ...role.Role) predicate.FieldType { }) } +// MACEQ applies the EQ predicate on the "mac" field. +func MACEQ(v schema.MAC) predicate.FieldType { + vc := v.String() + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldMAC, p.EQ(vc)) + }) +} + +// MACNEQ applies the NEQ predicate on the "mac" field. +func MACNEQ(v schema.MAC) predicate.FieldType { + vc := v.String() + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldMAC, p.NEQ(vc)) + }) +} + +// MACIn applies the In predicate on the "mac" field. +func MACIn(vs ...schema.MAC) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i].String() + } + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldMAC, p.Within(v...)) + }) +} + +// MACNotIn applies the NotIn predicate on the "mac" field. +func MACNotIn(vs ...schema.MAC) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i].String() + } + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldMAC, p.Without(v...)) + }) +} + +// MACGT applies the GT predicate on the "mac" field. +func MACGT(v schema.MAC) predicate.FieldType { + vc := v.String() + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldMAC, p.GT(vc)) + }) +} + +// MACGTE applies the GTE predicate on the "mac" field. +func MACGTE(v schema.MAC) predicate.FieldType { + vc := v.String() + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldMAC, p.GTE(vc)) + }) +} + +// MACLT applies the LT predicate on the "mac" field. +func MACLT(v schema.MAC) predicate.FieldType { + vc := v.String() + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldMAC, p.LT(vc)) + }) +} + +// MACLTE applies the LTE predicate on the "mac" field. +func MACLTE(v schema.MAC) predicate.FieldType { + vc := v.String() + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldMAC, p.LTE(vc)) + }) +} + +// MACContains applies the Contains predicate on the "mac" field. +func MACContains(v schema.MAC) predicate.FieldType { + vc := v.String() + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldMAC, p.Containing(vc)) + }) +} + +// MACHasPrefix applies the HasPrefix predicate on the "mac" field. +func MACHasPrefix(v schema.MAC) predicate.FieldType { + vc := v.String() + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldMAC, p.StartingWith(vc)) + }) +} + +// MACHasSuffix applies the HasSuffix predicate on the "mac" field. +func MACHasSuffix(v schema.MAC) predicate.FieldType { + vc := v.String() + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldMAC, p.EndingWith(vc)) + }) +} + +// MACIsNil applies the IsNil predicate on the "mac" field. +func MACIsNil() predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.HasLabel(Label).HasNot(FieldMAC) + }) +} + +// MACNotNil applies the NotNil predicate on the "mac" field. +func MACNotNil() predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.HasLabel(Label).Has(FieldMAC) + }) +} + // And groups list of predicates with the AND operator between them. func And(predicates ...predicate.FieldType) predicate.FieldType { return predicate.FieldType(func(tr *dsl.Traversal) { diff --git a/entc/integration/gremlin/ent/fieldtype_create.go b/entc/integration/gremlin/ent/fieldtype_create.go index 05884cc14..9d1f9e82e 100644 --- a/entc/integration/gremlin/ent/fieldtype_create.go +++ b/entc/integration/gremlin/ent/fieldtype_create.go @@ -548,6 +548,12 @@ func (ftc *FieldTypeCreate) SetNillableRole(r *role.Role) *FieldTypeCreate { return ftc } +// SetMAC sets the mac field. +func (ftc *FieldTypeCreate) SetMAC(s schema.MAC) *FieldTypeCreate { + ftc.mutation.SetMAC(s) + return ftc +} + // Mutation returns the FieldTypeMutation object of the builder. func (ftc *FieldTypeCreate) Mutation() *FieldTypeMutation { return ftc.mutation @@ -807,6 +813,9 @@ func (ftc *FieldTypeCreate) gremlin() *dsl.Traversal { if value, ok := ftc.mutation.Role(); ok { v.Property(dsl.Single, fieldtype.FieldRole, value) } + if value, ok := ftc.mutation.MAC(); ok { + v.Property(dsl.Single, fieldtype.FieldMAC, value) + } return v.ValueMap(true) } diff --git a/entc/integration/gremlin/ent/fieldtype_update.go b/entc/integration/gremlin/ent/fieldtype_update.go index 9066f797a..f8995ee9c 100644 --- a/entc/integration/gremlin/ent/fieldtype_update.go +++ b/entc/integration/gremlin/ent/fieldtype_update.go @@ -994,6 +994,18 @@ func (ftu *FieldTypeUpdate) SetNillableRole(r *role.Role) *FieldTypeUpdate { return ftu } +// SetMAC sets the mac field. +func (ftu *FieldTypeUpdate) SetMAC(s schema.MAC) *FieldTypeUpdate { + ftu.mutation.SetMAC(s) + return ftu +} + +// ClearMAC clears the value of mac. +func (ftu *FieldTypeUpdate) ClearMAC() *FieldTypeUpdate { + ftu.mutation.ClearMAC() + return ftu +} + // Mutation returns the FieldTypeMutation object of the builder. func (ftu *FieldTypeUpdate) Mutation() *FieldTypeMutation { return ftu.mutation @@ -1328,6 +1340,9 @@ func (ftu *FieldTypeUpdate) gremlin() *dsl.Traversal { if value, ok := ftu.mutation.Role(); ok { v.Property(dsl.Single, fieldtype.FieldRole, value) } + if value, ok := ftu.mutation.MAC(); ok { + v.Property(dsl.Single, fieldtype.FieldMAC, value) + } var properties []interface{} if ftu.mutation.OptionalIntCleared() { properties = append(properties, fieldtype.FieldOptionalInt) @@ -1446,6 +1461,9 @@ func (ftu *FieldTypeUpdate) gremlin() *dsl.Traversal { if ftu.mutation.NullFloatCleared() { properties = append(properties, fieldtype.FieldNullFloat) } + if ftu.mutation.MACCleared() { + properties = append(properties, fieldtype.FieldMAC) + } if len(properties) > 0 { v.SideEffect(__.Properties(properties...).Drop()) } @@ -2418,6 +2436,18 @@ func (ftuo *FieldTypeUpdateOne) SetNillableRole(r *role.Role) *FieldTypeUpdateOn return ftuo } +// SetMAC sets the mac field. +func (ftuo *FieldTypeUpdateOne) SetMAC(s schema.MAC) *FieldTypeUpdateOne { + ftuo.mutation.SetMAC(s) + return ftuo +} + +// ClearMAC clears the value of mac. +func (ftuo *FieldTypeUpdateOne) ClearMAC() *FieldTypeUpdateOne { + ftuo.mutation.ClearMAC() + return ftuo +} + // Mutation returns the FieldTypeMutation object of the builder. func (ftuo *FieldTypeUpdateOne) Mutation() *FieldTypeMutation { return ftuo.mutation @@ -2757,6 +2787,9 @@ func (ftuo *FieldTypeUpdateOne) gremlin(id string) *dsl.Traversal { if value, ok := ftuo.mutation.Role(); ok { v.Property(dsl.Single, fieldtype.FieldRole, value) } + if value, ok := ftuo.mutation.MAC(); ok { + v.Property(dsl.Single, fieldtype.FieldMAC, value) + } var properties []interface{} if ftuo.mutation.OptionalIntCleared() { properties = append(properties, fieldtype.FieldOptionalInt) @@ -2875,6 +2908,9 @@ func (ftuo *FieldTypeUpdateOne) gremlin(id string) *dsl.Traversal { if ftuo.mutation.NullFloatCleared() { properties = append(properties, fieldtype.FieldNullFloat) } + if ftuo.mutation.MACCleared() { + properties = append(properties, fieldtype.FieldMAC) + } 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 7c7792ed3..60cb4681a 100644 --- a/entc/integration/gremlin/ent/mutation.go +++ b/entc/integration/gremlin/ent/mutation.go @@ -1301,6 +1301,7 @@ type FieldTypeMutation struct { addschema_float32 *schema.Float32 null_float *sql.NullFloat64 role *role.Role + mac *schema.MAC clearedFields map[string]struct{} done bool oldValue func(context.Context) (*FieldType, error) @@ -4162,6 +4163,56 @@ func (m *FieldTypeMutation) ResetRole() { m.role = nil } +// SetMAC sets the mac field. +func (m *FieldTypeMutation) SetMAC(s schema.MAC) { + m.mac = &s +} + +// MAC returns the mac value in the mutation. +func (m *FieldTypeMutation) MAC() (r schema.MAC, exists bool) { + v := m.mac + if v == nil { + return + } + return *v, true +} + +// OldMAC returns the old mac 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) OldMAC(ctx context.Context) (v schema.MAC, err error) { + if !m.op.Is(OpUpdateOne) { + return v, fmt.Errorf("OldMAC is allowed only on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, fmt.Errorf("OldMAC requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldMAC: %w", err) + } + return oldValue.MAC, nil +} + +// ClearMAC clears the value of mac. +func (m *FieldTypeMutation) ClearMAC() { + m.mac = nil + m.clearedFields[fieldtype.FieldMAC] = struct{}{} +} + +// MACCleared returns if the field mac was cleared in this mutation. +func (m *FieldTypeMutation) MACCleared() bool { + _, ok := m.clearedFields[fieldtype.FieldMAC] + return ok +} + +// ResetMAC reset all changes of the "mac" field. +func (m *FieldTypeMutation) ResetMAC() { + m.mac = nil + delete(m.clearedFields, fieldtype.FieldMAC) +} + // Op returns the operation name. func (m *FieldTypeMutation) Op() Op { return m.op @@ -4176,7 +4227,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, 45) + fields := make([]string, 0, 46) if m.int != nil { fields = append(fields, fieldtype.FieldInt) } @@ -4312,6 +4363,9 @@ func (m *FieldTypeMutation) Fields() []string { if m.role != nil { fields = append(fields, fieldtype.FieldRole) } + if m.mac != nil { + fields = append(fields, fieldtype.FieldMAC) + } return fields } @@ -4410,6 +4464,8 @@ func (m *FieldTypeMutation) Field(name string) (ent.Value, bool) { return m.NullFloat() case fieldtype.FieldRole: return m.Role() + case fieldtype.FieldMAC: + return m.MAC() } return nil, false } @@ -4509,6 +4565,8 @@ func (m *FieldTypeMutation) OldField(ctx context.Context, name string) (ent.Valu return m.OldNullFloat(ctx) case fieldtype.FieldRole: return m.OldRole(ctx) + case fieldtype.FieldMAC: + return m.OldMAC(ctx) } return nil, fmt.Errorf("unknown FieldType field %s", name) } @@ -4833,6 +4891,13 @@ func (m *FieldTypeMutation) SetField(name string, value ent.Value) error { } m.SetRole(v) return nil + case fieldtype.FieldMAC: + v, ok := value.(schema.MAC) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetMAC(v) + return nil } return fmt.Errorf("unknown FieldType field %s", name) } @@ -5331,6 +5396,9 @@ func (m *FieldTypeMutation) ClearedFields() []string { if m.FieldCleared(fieldtype.FieldNullFloat) { fields = append(fields, fieldtype.FieldNullFloat) } + if m.FieldCleared(fieldtype.FieldMAC) { + fields = append(fields, fieldtype.FieldMAC) + } return fields } @@ -5462,6 +5530,9 @@ func (m *FieldTypeMutation) ClearField(name string) error { case fieldtype.FieldNullFloat: m.ClearNullFloat() return nil + case fieldtype.FieldMAC: + m.ClearMAC() + return nil } return fmt.Errorf("unknown FieldType nullable field %s", name) } @@ -5606,6 +5677,9 @@ func (m *FieldTypeMutation) ResetField(name string) error { case fieldtype.FieldRole: m.ResetRole() return nil + case fieldtype.FieldMAC: + m.ResetMAC() + return nil } return fmt.Errorf("unknown FieldType field %s", name) } diff --git a/schema/field/field.go b/schema/field/field.go index 7aed94aee..c774dc127 100644 --- a/schema/field/field.go +++ b/schema/field/field.go @@ -945,6 +945,9 @@ func (d *Descriptor) goType(typ interface{}, expectType reflect.Type) { } default: d.err = fmt.Errorf("GoType must be a %q type or ValueScanner", expectType) + if pt := reflect.PtrTo(t); pt.Implements(valueScannerType) { + d.err = fmt.Errorf("%s. Use %s instead", d.err, pt) + } } d.Info = info } diff --git a/schema/field/field_test.go b/schema/field/field_test.go index 98db82448..ed9aad696 100644 --- a/schema/field/field_test.go +++ b/schema/field/field_test.go @@ -80,8 +80,10 @@ func TestInt(t *testing.T) { assert.True(t, fd.Info.Nillable) assert.True(t, fd.Info.ValueScanner()) + fd = field.Int("count").GoType(sql.NullInt64{}).Descriptor() + assert.EqualError(t, fd.Err(), `GoType must be a "int" type or ValueScanner. Use *sql.NullInt64 instead`) fd = field.Int("count").GoType(false).Descriptor() - assert.Error(t, fd.Err()) + assert.EqualError(t, fd.Err(), `GoType must be a "int" type or ValueScanner`) fd = field.Int("count").GoType(struct{}{}).Descriptor() assert.Error(t, fd.Err()) fd = field.Int("count").GoType(new(Count)).Descriptor()