schema/field: improve error message for GoType (#969)

This commit is contained in:
Ariel Mashraki
2020-11-20 23:48:54 +02:00
committed by GitHub
parent d10b888e1c
commit fe47093c55
8 changed files with 252 additions and 2 deletions

View File

@@ -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

View File

@@ -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 (

View File

@@ -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) {

View File

@@ -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)
}

View File

@@ -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())
}

View File

@@ -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)
}