diff --git a/doc/md/schema-fields.md b/doc/md/schema-fields.md index 69ea14a4a..d052dc162 100755 --- a/doc/md/schema-fields.md +++ b/doc/md/schema-fields.md @@ -399,6 +399,9 @@ The framework provides a few built-in validators for each type: - `Match(regexp.Regexp)` - `NotEmpty` +- `[]byte` + - `MaxLen(i)` + ## Optional Optional fields are fields that are not required in the entity creation, and diff --git a/entc/integration/ent/entql.go b/entc/integration/ent/entql.go index 0f5e8ad79..ac8e56a25 100644 --- a/entc/integration/ent/entql.go +++ b/entc/integration/ent/entql.go @@ -119,6 +119,7 @@ var schemaGraph = func() *sqlgraph.Schema { fieldtype.FieldNullActive: {Type: field.TypeBool, Column: fieldtype.FieldNullActive}, fieldtype.FieldDeleted: {Type: field.TypeBool, Column: fieldtype.FieldDeleted}, fieldtype.FieldDeletedAt: {Type: field.TypeTime, Column: fieldtype.FieldDeletedAt}, + fieldtype.FieldRawData: {Type: field.TypeBytes, Column: fieldtype.FieldRawData}, fieldtype.FieldIP: {Type: field.TypeBytes, Column: fieldtype.FieldIP}, fieldtype.FieldNullInt64: {Type: field.TypeInt, Column: fieldtype.FieldNullInt64}, fieldtype.FieldSchemaInt: {Type: field.TypeInt, Column: fieldtype.FieldSchemaInt}, @@ -1040,6 +1041,11 @@ func (f *FieldTypeFilter) WhereDeletedAt(p entql.TimeP) { f.Where(p.Field(fieldtype.FieldDeletedAt)) } +// WhereRawData applies the entql []byte predicate on the raw_data field. +func (f *FieldTypeFilter) WhereRawData(p entql.BytesP) { + f.Where(p.Field(fieldtype.FieldRawData)) +} + // WhereIP applies the entql []byte predicate on the ip field. func (f *FieldTypeFilter) WhereIP(p entql.BytesP) { f.Where(p.Field(fieldtype.FieldIP)) diff --git a/entc/integration/ent/fieldtype.go b/entc/integration/ent/fieldtype.go index 1eaf2aff7..3a52abe0f 100644 --- a/entc/integration/ent/fieldtype.go +++ b/entc/integration/ent/fieldtype.go @@ -110,6 +110,8 @@ type FieldType struct { Deleted *sql.NullBool `json:"deleted,omitempty"` // DeletedAt holds the value of the "deleted_at" field. DeletedAt *sql.NullTime `json:"deleted_at,omitempty"` + // RawData holds the value of the "raw_data" field. + RawData []byte `json:"raw_data,omitempty"` // IP holds the value of the "ip" field. IP net.IP `json:"ip,omitempty"` // NullInt64 holds the value of the "null_int64" field. @@ -164,7 +166,7 @@ func (*FieldType) scanValues(columns []string) ([]interface{}, error) { values[i] = &sql.NullScanner{S: new(schema.StringScanner)} case fieldtype.FieldNillableUUID: values[i] = &sql.NullScanner{S: new(uuid.UUID)} - case fieldtype.FieldIP, fieldtype.FieldStrings: + case fieldtype.FieldRawData, fieldtype.FieldIP, fieldtype.FieldStrings: values[i] = new([]byte) case fieldtype.FieldPriority: values[i] = new(role.Priority) @@ -479,6 +481,12 @@ func (ft *FieldType) assignValues(columns []string, values []interface{}) error } else if value.Valid { ft.DeletedAt = value } + case fieldtype.FieldRawData: + if value, ok := values[i].(*[]byte); !ok { + return fmt.Errorf("unexpected type %T for field raw_data", values[i]) + } else if value != nil { + ft.RawData = *value + } case fieldtype.FieldIP: if value, ok := values[i].(*[]byte); !ok { return fmt.Errorf("unexpected type %T for field ip", values[i]) @@ -736,6 +744,8 @@ func (ft *FieldType) String() string { } builder.WriteString(", deleted_at=") builder.WriteString(fmt.Sprintf("%v", ft.DeletedAt)) + builder.WriteString(", raw_data=") + builder.WriteString(fmt.Sprintf("%v", ft.RawData)) builder.WriteString(", ip=") builder.WriteString(fmt.Sprintf("%v", ft.IP)) builder.WriteString(", null_int64=") diff --git a/entc/integration/ent/fieldtype/fieldtype.go b/entc/integration/ent/fieldtype/fieldtype.go index b1f845721..bb6fab769 100644 --- a/entc/integration/ent/fieldtype/fieldtype.go +++ b/entc/integration/ent/fieldtype/fieldtype.go @@ -106,6 +106,8 @@ const ( FieldDeleted = "deleted" // FieldDeletedAt holds the string denoting the deleted_at field in the database. FieldDeletedAt = "deleted_at" + // FieldRawData holds the string denoting the raw_data field in the database. + FieldRawData = "raw_data" // FieldIP holds the string denoting the ip field in the database. FieldIP = "ip" // FieldNullInt64 holds the string denoting the null_int64 field in the database. @@ -193,6 +195,7 @@ var Columns = []string{ FieldNullActive, FieldDeleted, FieldDeletedAt, + FieldRawData, FieldIP, FieldNullInt64, FieldSchemaInt, @@ -254,6 +257,8 @@ var ( DefaultNullStr func() *sql.NullString // LinkValidator is a validator for the "link" field. It is called by the builders before save. LinkValidator func(string) error + // RawDataValidator is a validator for the "raw_data" field. It is called by the builders before save. + RawDataValidator func([]byte) error // DefaultIP holds the default value on creation for the "ip" field. DefaultIP func() net.IP // IPValidator is a validator for the "ip" field. It is called by the builders before save. diff --git a/entc/integration/ent/fieldtype/where.go b/entc/integration/ent/fieldtype/where.go index 3030cd79f..6b235e441 100644 --- a/entc/integration/ent/fieldtype/where.go +++ b/entc/integration/ent/fieldtype/where.go @@ -393,6 +393,13 @@ func DeletedAt(v *sql.NullTime) predicate.FieldType { }) } +// RawData applies equality check predicate on the "raw_data" field. It's identical to RawDataEQ. +func RawData(v []byte) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldRawData), v)) + }) +} + // IP applies equality check predicate on the "ip" field. It's identical to IPEQ. func IP(v net.IP) predicate.FieldType { vc := []byte(v) @@ -4370,6 +4377,96 @@ func DeletedAtNotNil() predicate.FieldType { }) } +// RawDataEQ applies the EQ predicate on the "raw_data" field. +func RawDataEQ(v []byte) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldRawData), v)) + }) +} + +// RawDataNEQ applies the NEQ predicate on the "raw_data" field. +func RawDataNEQ(v []byte) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.NEQ(s.C(FieldRawData), v)) + }) +} + +// RawDataIn applies the In predicate on the "raw_data" field. +func RawDataIn(vs ...[]byte) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = 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(FieldRawData), v...)) + }) +} + +// RawDataNotIn applies the NotIn predicate on the "raw_data" field. +func RawDataNotIn(vs ...[]byte) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = 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(FieldRawData), v...)) + }) +} + +// RawDataGT applies the GT predicate on the "raw_data" field. +func RawDataGT(v []byte) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.GT(s.C(FieldRawData), v)) + }) +} + +// RawDataGTE applies the GTE predicate on the "raw_data" field. +func RawDataGTE(v []byte) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.GTE(s.C(FieldRawData), v)) + }) +} + +// RawDataLT applies the LT predicate on the "raw_data" field. +func RawDataLT(v []byte) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.LT(s.C(FieldRawData), v)) + }) +} + +// RawDataLTE applies the LTE predicate on the "raw_data" field. +func RawDataLTE(v []byte) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.LTE(s.C(FieldRawData), v)) + }) +} + +// RawDataIsNil applies the IsNil predicate on the "raw_data" field. +func RawDataIsNil() predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.IsNull(s.C(FieldRawData))) + }) +} + +// RawDataNotNil applies the NotNil predicate on the "raw_data" field. +func RawDataNotNil() predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.NotNull(s.C(FieldRawData))) + }) +} + // IPEQ applies the EQ predicate on the "ip" field. func IPEQ(v net.IP) predicate.FieldType { vc := []byte(v) diff --git a/entc/integration/ent/fieldtype_create.go b/entc/integration/ent/fieldtype_create.go index 1edb46bd2..a3746c62f 100644 --- a/entc/integration/ent/fieldtype_create.go +++ b/entc/integration/ent/fieldtype_create.go @@ -531,6 +531,12 @@ func (ftc *FieldTypeCreate) SetDeletedAt(st *sql.NullTime) *FieldTypeCreate { return ftc } +// SetRawData sets the "raw_data" field. +func (ftc *FieldTypeCreate) SetRawData(b []byte) *FieldTypeCreate { + ftc.mutation.SetRawData(b) + return ftc +} + // SetIP sets the "ip" field. func (ftc *FieldTypeCreate) SetIP(n net.IP) *FieldTypeCreate { ftc.mutation.SetIP(n) @@ -891,6 +897,11 @@ func (ftc *FieldTypeCreate) check() error { return &ValidationError{Name: "link", err: fmt.Errorf(`ent: validator failed for field "link": %w`, err)} } } + if v, ok := ftc.mutation.RawData(); ok { + if err := fieldtype.RawDataValidator(v); err != nil { + return &ValidationError{Name: "raw_data", err: fmt.Errorf(`ent: validator failed for field "raw_data": %w`, err)} + } + } if v, ok := ftc.mutation.IP(); ok { if err := fieldtype.IPValidator([]byte(v)); err != nil { return &ValidationError{Name: "ip", err: fmt.Errorf(`ent: validator failed for field "ip": %w`, err)} @@ -1282,6 +1293,14 @@ func (ftc *FieldTypeCreate) createSpec() (*FieldType, *sqlgraph.CreateSpec) { }) _node.DeletedAt = value } + if value, ok := ftc.mutation.RawData(); ok { + _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ + Type: field.TypeBytes, + Value: value, + Column: fieldtype.FieldRawData, + }) + _node.RawData = value + } if value, ok := ftc.mutation.IP(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeBytes, @@ -2208,6 +2227,24 @@ func (u *FieldTypeUpsert) ClearDeletedAt() *FieldTypeUpsert { return u } +// SetRawData sets the "raw_data" field. +func (u *FieldTypeUpsert) SetRawData(v []byte) *FieldTypeUpsert { + u.Set(fieldtype.FieldRawData, v) + return u +} + +// UpdateRawData sets the "raw_data" field to the value that was provided on create. +func (u *FieldTypeUpsert) UpdateRawData() *FieldTypeUpsert { + u.SetExcluded(fieldtype.FieldRawData) + return u +} + +// ClearRawData clears the value of the "raw_data" field. +func (u *FieldTypeUpsert) ClearRawData() *FieldTypeUpsert { + u.SetNull(fieldtype.FieldRawData) + return u +} + // SetIP sets the "ip" field. func (u *FieldTypeUpsert) SetIP(v net.IP) *FieldTypeUpsert { u.Set(fieldtype.FieldIP, v) @@ -3408,6 +3445,27 @@ func (u *FieldTypeUpsertOne) ClearDeletedAt() *FieldTypeUpsertOne { }) } +// SetRawData sets the "raw_data" field. +func (u *FieldTypeUpsertOne) SetRawData(v []byte) *FieldTypeUpsertOne { + return u.Update(func(s *FieldTypeUpsert) { + s.SetRawData(v) + }) +} + +// UpdateRawData sets the "raw_data" field to the value that was provided on create. +func (u *FieldTypeUpsertOne) UpdateRawData() *FieldTypeUpsertOne { + return u.Update(func(s *FieldTypeUpsert) { + s.UpdateRawData() + }) +} + +// ClearRawData clears the value of the "raw_data" field. +func (u *FieldTypeUpsertOne) ClearRawData() *FieldTypeUpsertOne { + return u.Update(func(s *FieldTypeUpsert) { + s.ClearRawData() + }) +} + // SetIP sets the "ip" field. func (u *FieldTypeUpsertOne) SetIP(v net.IP) *FieldTypeUpsertOne { return u.Update(func(s *FieldTypeUpsert) { @@ -4823,6 +4881,27 @@ func (u *FieldTypeUpsertBulk) ClearDeletedAt() *FieldTypeUpsertBulk { }) } +// SetRawData sets the "raw_data" field. +func (u *FieldTypeUpsertBulk) SetRawData(v []byte) *FieldTypeUpsertBulk { + return u.Update(func(s *FieldTypeUpsert) { + s.SetRawData(v) + }) +} + +// UpdateRawData sets the "raw_data" field to the value that was provided on create. +func (u *FieldTypeUpsertBulk) UpdateRawData() *FieldTypeUpsertBulk { + return u.Update(func(s *FieldTypeUpsert) { + s.UpdateRawData() + }) +} + +// ClearRawData clears the value of the "raw_data" field. +func (u *FieldTypeUpsertBulk) ClearRawData() *FieldTypeUpsertBulk { + return u.Update(func(s *FieldTypeUpsert) { + s.ClearRawData() + }) +} + // SetIP sets the "ip" field. func (u *FieldTypeUpsertBulk) SetIP(v net.IP) *FieldTypeUpsertBulk { return u.Update(func(s *FieldTypeUpsert) { diff --git a/entc/integration/ent/fieldtype_update.go b/entc/integration/ent/fieldtype_update.go index b06f524de..d04fcf227 100644 --- a/entc/integration/ent/fieldtype_update.go +++ b/entc/integration/ent/fieldtype_update.go @@ -919,6 +919,18 @@ func (ftu *FieldTypeUpdate) ClearDeletedAt() *FieldTypeUpdate { return ftu } +// SetRawData sets the "raw_data" field. +func (ftu *FieldTypeUpdate) SetRawData(b []byte) *FieldTypeUpdate { + ftu.mutation.SetRawData(b) + return ftu +} + +// ClearRawData clears the value of the "raw_data" field. +func (ftu *FieldTypeUpdate) ClearRawData() *FieldTypeUpdate { + ftu.mutation.ClearRawData() + return ftu +} + // SetIP sets the "ip" field. func (ftu *FieldTypeUpdate) SetIP(n net.IP) *FieldTypeUpdate { ftu.mutation.SetIP(n) @@ -1366,6 +1378,11 @@ func (ftu *FieldTypeUpdate) check() error { return &ValidationError{Name: "link", err: fmt.Errorf("ent: validator failed for field \"link\": %w", err)} } } + if v, ok := ftu.mutation.RawData(); ok { + if err := fieldtype.RawDataValidator(v); err != nil { + return &ValidationError{Name: "raw_data", err: fmt.Errorf("ent: validator failed for field \"raw_data\": %w", err)} + } + } if v, ok := ftu.mutation.IP(); ok { if err := fieldtype.IPValidator([]byte(v)); err != nil { return &ValidationError{Name: "ip", err: fmt.Errorf("ent: validator failed for field \"ip\": %w", err)} @@ -2087,6 +2104,19 @@ func (ftu *FieldTypeUpdate) sqlSave(ctx context.Context) (n int, err error) { Column: fieldtype.FieldDeletedAt, }) } + if value, ok := ftu.mutation.RawData(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeBytes, + Value: value, + Column: fieldtype.FieldRawData, + }) + } + if ftu.mutation.RawDataCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeBytes, + Column: fieldtype.FieldRawData, + }) + } if value, ok := ftu.mutation.IP(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeBytes, @@ -3254,6 +3284,18 @@ func (ftuo *FieldTypeUpdateOne) ClearDeletedAt() *FieldTypeUpdateOne { return ftuo } +// SetRawData sets the "raw_data" field. +func (ftuo *FieldTypeUpdateOne) SetRawData(b []byte) *FieldTypeUpdateOne { + ftuo.mutation.SetRawData(b) + return ftuo +} + +// ClearRawData clears the value of the "raw_data" field. +func (ftuo *FieldTypeUpdateOne) ClearRawData() *FieldTypeUpdateOne { + ftuo.mutation.ClearRawData() + return ftuo +} + // SetIP sets the "ip" field. func (ftuo *FieldTypeUpdateOne) SetIP(n net.IP) *FieldTypeUpdateOne { ftuo.mutation.SetIP(n) @@ -3708,6 +3750,11 @@ func (ftuo *FieldTypeUpdateOne) check() error { return &ValidationError{Name: "link", err: fmt.Errorf("ent: validator failed for field \"link\": %w", err)} } } + if v, ok := ftuo.mutation.RawData(); ok { + if err := fieldtype.RawDataValidator(v); err != nil { + return &ValidationError{Name: "raw_data", err: fmt.Errorf("ent: validator failed for field \"raw_data\": %w", err)} + } + } if v, ok := ftuo.mutation.IP(); ok { if err := fieldtype.IPValidator([]byte(v)); err != nil { return &ValidationError{Name: "ip", err: fmt.Errorf("ent: validator failed for field \"ip\": %w", err)} @@ -4446,6 +4493,19 @@ func (ftuo *FieldTypeUpdateOne) sqlSave(ctx context.Context) (_node *FieldType, Column: fieldtype.FieldDeletedAt, }) } + if value, ok := ftuo.mutation.RawData(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeBytes, + Value: value, + Column: fieldtype.FieldRawData, + }) + } + if ftuo.mutation.RawDataCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeBytes, + Column: fieldtype.FieldRawData, + }) + } if value, ok := ftuo.mutation.IP(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeBytes, diff --git a/entc/integration/ent/migrate/schema.go b/entc/integration/ent/migrate/schema.go index a9361065e..841ef2751 100644 --- a/entc/integration/ent/migrate/schema.go +++ b/entc/integration/ent/migrate/schema.go @@ -112,6 +112,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: "raw_data", Type: field.TypeBytes, Nullable: true, Size: 20}, {Name: "ip", Type: field.TypeBytes, Nullable: true}, {Name: "null_int64", Type: field.TypeInt, Nullable: true}, {Name: "schema_int", Type: field.TypeInt, Nullable: true}, @@ -141,7 +142,7 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "field_types_files_field", - Columns: []*schema.Column{FieldTypesColumns[62]}, + Columns: []*schema.Column{FieldTypesColumns[63]}, RefColumns: []*schema.Column{FilesColumns[0]}, OnDelete: schema.SetNull, }, diff --git a/entc/integration/ent/mutation.go b/entc/integration/ent/mutation.go index 2d2a7a22b..c2d399c28 100644 --- a/entc/integration/ent/mutation.go +++ b/entc/integration/ent/mutation.go @@ -1377,6 +1377,7 @@ type FieldTypeMutation struct { null_active *schema.Status deleted **sql.NullBool deleted_at **sql.NullTime + raw_data *[]byte ip *net.IP null_int64 **sql.NullInt64 schema_int *schema.Int @@ -3987,6 +3988,55 @@ func (m *FieldTypeMutation) ResetDeletedAt() { delete(m.clearedFields, fieldtype.FieldDeletedAt) } +// SetRawData sets the "raw_data" field. +func (m *FieldTypeMutation) SetRawData(b []byte) { + m.raw_data = &b +} + +// RawData returns the value of the "raw_data" field in the mutation. +func (m *FieldTypeMutation) RawData() (r []byte, exists bool) { + v := m.raw_data + if v == nil { + return + } + return *v, true +} + +// OldRawData returns the old "raw_data" field's value of the FieldType entity. +// 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 the database query fails. +func (m *FieldTypeMutation) OldRawData(ctx context.Context) (v []byte, err error) { + if !m.op.Is(OpUpdateOne) { + return v, fmt.Errorf("OldRawData is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, fmt.Errorf("OldRawData requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldRawData: %w", err) + } + return oldValue.RawData, nil +} + +// ClearRawData clears the value of the "raw_data" field. +func (m *FieldTypeMutation) ClearRawData() { + m.raw_data = nil + m.clearedFields[fieldtype.FieldRawData] = struct{}{} +} + +// RawDataCleared returns if the "raw_data" field was cleared in this mutation. +func (m *FieldTypeMutation) RawDataCleared() bool { + _, ok := m.clearedFields[fieldtype.FieldRawData] + return ok +} + +// ResetRawData resets all changes to the "raw_data" field. +func (m *FieldTypeMutation) ResetRawData() { + m.raw_data = nil + delete(m.clearedFields, fieldtype.FieldRawData) +} + // SetIP sets the "ip" field. func (m *FieldTypeMutation) SetIP(n net.IP) { m.ip = &n @@ -5011,7 +5061,7 @@ func (m *FieldTypeMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *FieldTypeMutation) Fields() []string { - fields := make([]string, 0, 61) + fields := make([]string, 0, 62) if m.int != nil { fields = append(fields, fieldtype.FieldInt) } @@ -5138,6 +5188,9 @@ func (m *FieldTypeMutation) Fields() []string { if m.deleted_at != nil { fields = append(fields, fieldtype.FieldDeletedAt) } + if m.raw_data != nil { + fields = append(fields, fieldtype.FieldRawData) + } if m.ip != nil { fields = append(fields, fieldtype.FieldIP) } @@ -5287,6 +5340,8 @@ func (m *FieldTypeMutation) Field(name string) (ent.Value, bool) { return m.Deleted() case fieldtype.FieldDeletedAt: return m.DeletedAt() + case fieldtype.FieldRawData: + return m.RawData() case fieldtype.FieldIP: return m.IP() case fieldtype.FieldNullInt64: @@ -5418,6 +5473,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.FieldRawData: + return m.OldRawData(ctx) case fieldtype.FieldIP: return m.OldIP(ctx) case fieldtype.FieldNullInt64: @@ -5759,6 +5816,13 @@ func (m *FieldTypeMutation) SetField(name string, value ent.Value) error { } m.SetDeletedAt(v) return nil + case fieldtype.FieldRawData: + v, ok := value.([]byte) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetRawData(v) + return nil case fieldtype.FieldIP: v, ok := value.(net.IP) if !ok { @@ -6405,6 +6469,9 @@ func (m *FieldTypeMutation) ClearedFields() []string { if m.FieldCleared(fieldtype.FieldDeletedAt) { fields = append(fields, fieldtype.FieldDeletedAt) } + if m.FieldCleared(fieldtype.FieldRawData) { + fields = append(fields, fieldtype.FieldRawData) + } if m.FieldCleared(fieldtype.FieldIP) { fields = append(fields, fieldtype.FieldIP) } @@ -6572,6 +6639,9 @@ func (m *FieldTypeMutation) ClearField(name string) error { case fieldtype.FieldDeletedAt: m.ClearDeletedAt() return nil + case fieldtype.FieldRawData: + m.ClearRawData() + return nil case fieldtype.FieldIP: m.ClearIP() return nil @@ -6751,6 +6821,9 @@ func (m *FieldTypeMutation) ResetField(name string) error { case fieldtype.FieldDeletedAt: m.ResetDeletedAt() return nil + case fieldtype.FieldRawData: + m.ResetRawData() + return nil case fieldtype.FieldIP: m.ResetIP() return nil diff --git a/entc/integration/ent/runtime.go b/entc/integration/ent/runtime.go index 0f6775713..545835a32 100644 --- a/entc/integration/ent/runtime.go +++ b/entc/integration/ent/runtime.go @@ -93,22 +93,26 @@ func init() { fieldtypeDescLink := fieldtypeFields[36].Descriptor() // fieldtype.LinkValidator is a validator for the "link" field. It is called by the builders before save. fieldtype.LinkValidator = fieldtypeDescLink.Validators[0].(func(string) error) + // fieldtypeDescRawData is the schema descriptor for raw_data field. + fieldtypeDescRawData := fieldtypeFields[42].Descriptor() + // fieldtype.RawDataValidator is a validator for the "raw_data" field. It is called by the builders before save. + fieldtype.RawDataValidator = fieldtypeDescRawData.Validators[0].(func([]byte) error) // fieldtypeDescIP is the schema descriptor for ip field. - fieldtypeDescIP := fieldtypeFields[42].Descriptor() + fieldtypeDescIP := fieldtypeFields[43].Descriptor() // fieldtype.DefaultIP holds the default value on creation for the ip field. fieldtype.DefaultIP = fieldtypeDescIP.Default.(func() net.IP) // fieldtype.IPValidator is a validator for the "ip" field. It is called by the builders before save. fieldtype.IPValidator = fieldtypeDescIP.Validators[0].(func([]byte) error) // fieldtypeDescPair is the schema descriptor for pair field. - fieldtypeDescPair := fieldtypeFields[55].Descriptor() + fieldtypeDescPair := fieldtypeFields[56].Descriptor() // fieldtype.DefaultPair holds the default value on creation for the pair field. fieldtype.DefaultPair = fieldtypeDescPair.Default.(func() schema.Pair) // fieldtypeDescVstring is the schema descriptor for vstring field. - fieldtypeDescVstring := fieldtypeFields[57].Descriptor() + fieldtypeDescVstring := fieldtypeFields[58].Descriptor() // fieldtype.DefaultVstring holds the default value on creation for the vstring field. fieldtype.DefaultVstring = fieldtypeDescVstring.Default.(func() schema.VString) // fieldtypeDescTriple is the schema descriptor for triple field. - fieldtypeDescTriple := fieldtypeFields[58].Descriptor() + fieldtypeDescTriple := fieldtypeFields[59].Descriptor() // fieldtype.DefaultTriple holds the default value on creation for the triple field. fieldtype.DefaultTriple = fieldtypeDescTriple.Default.(func() schema.Triple) fileFields := schema.File{}.Fields() diff --git a/entc/integration/ent/schema/fieldtype.go b/entc/integration/ent/schema/fieldtype.go index 09a630ce3..611dec3f8 100644 --- a/entc/integration/ent/schema/fieldtype.go +++ b/entc/integration/ent/schema/fieldtype.go @@ -195,6 +195,9 @@ func (FieldType) Fields() []ent.Field { //nolint:funlen field.Time("deleted_at"). Optional(). GoType(&sql.NullTime{}), + field.Bytes("raw_data"). + Optional(). + MaxLen(20), field.Bytes("ip"). Optional(). GoType(net.IP("127.0.0.1")). diff --git a/entc/integration/gremlin/ent/fieldtype.go b/entc/integration/gremlin/ent/fieldtype.go index b6038f198..b940f2726 100644 --- a/entc/integration/gremlin/ent/fieldtype.go +++ b/entc/integration/gremlin/ent/fieldtype.go @@ -110,6 +110,8 @@ type FieldType struct { Deleted *sql.NullBool `json:"deleted,omitempty"` // DeletedAt holds the value of the "deleted_at" field. DeletedAt *sql.NullTime `json:"deleted_at,omitempty"` + // RawData holds the value of the "raw_data" field. + RawData []byte `json:"raw_data,omitempty"` // IP holds the value of the "ip" field. IP net.IP `json:"ip,omitempty"` // NullInt64 holds the value of the "null_int64" field. @@ -200,6 +202,7 @@ func (ft *FieldType) FromResponse(res *gremlin.Response) error { NullActive *schema.Status `json:"null_active,omitempty"` Deleted *sql.NullBool `json:"deleted,omitempty"` DeletedAt *sql.NullTime `json:"deleted_at,omitempty"` + RawData []byte `json:"raw_data,omitempty"` IP net.IP `json:"ip,omitempty"` NullInt64 *sql.NullInt64 `json:"null_int64,omitempty"` SchemaInt schema.Int `json:"schema_int,omitempty"` @@ -266,6 +269,7 @@ func (ft *FieldType) FromResponse(res *gremlin.Response) error { ft.NullActive = scanft.NullActive ft.Deleted = scanft.Deleted ft.DeletedAt = scanft.DeletedAt + ft.RawData = scanft.RawData ft.IP = scanft.IP ft.NullInt64 = scanft.NullInt64 ft.SchemaInt = scanft.SchemaInt @@ -416,6 +420,8 @@ func (ft *FieldType) String() string { } builder.WriteString(", deleted_at=") builder.WriteString(fmt.Sprintf("%v", ft.DeletedAt)) + builder.WriteString(", raw_data=") + builder.WriteString(fmt.Sprintf("%v", ft.RawData)) builder.WriteString(", ip=") builder.WriteString(fmt.Sprintf("%v", ft.IP)) builder.WriteString(", null_int64=") @@ -514,6 +520,7 @@ func (ft *FieldTypes) FromResponse(res *gremlin.Response) error { NullActive *schema.Status `json:"null_active,omitempty"` Deleted *sql.NullBool `json:"deleted,omitempty"` DeletedAt *sql.NullTime `json:"deleted_at,omitempty"` + RawData []byte `json:"raw_data,omitempty"` IP net.IP `json:"ip,omitempty"` NullInt64 *sql.NullInt64 `json:"null_int64,omitempty"` SchemaInt schema.Int `json:"schema_int,omitempty"` @@ -582,6 +589,7 @@ func (ft *FieldTypes) FromResponse(res *gremlin.Response) error { NullActive: v.NullActive, Deleted: v.Deleted, DeletedAt: v.DeletedAt, + RawData: v.RawData, IP: v.IP, NullInt64: v.NullInt64, SchemaInt: v.SchemaInt, diff --git a/entc/integration/gremlin/ent/fieldtype/fieldtype.go b/entc/integration/gremlin/ent/fieldtype/fieldtype.go index 40fb41bdc..e7d07f00c 100644 --- a/entc/integration/gremlin/ent/fieldtype/fieldtype.go +++ b/entc/integration/gremlin/ent/fieldtype/fieldtype.go @@ -106,6 +106,8 @@ const ( FieldDeleted = "deleted" // FieldDeletedAt holds the string denoting the deleted_at field in the database. FieldDeletedAt = "deleted_at" + // FieldRawData holds the string denoting the raw_data field in the database. + FieldRawData = "raw_data" // FieldIP holds the string denoting the ip field in the database. FieldIP = "ip" // FieldNullInt64 holds the string denoting the null_int64 field in the database. @@ -165,6 +167,8 @@ var ( DefaultNullStr func() *sql.NullString // LinkValidator is a validator for the "link" field. It is called by the builders before save. LinkValidator func(string) error + // RawDataValidator is a validator for the "raw_data" field. It is called by the builders before save. + RawDataValidator func([]byte) error // DefaultIP holds the default value on creation for the "ip" field. DefaultIP func() net.IP // IPValidator is a validator for the "ip" field. It is called by the builders before save. diff --git a/entc/integration/gremlin/ent/fieldtype/where.go b/entc/integration/gremlin/ent/fieldtype/where.go index b8813408e..26ffa5444 100644 --- a/entc/integration/gremlin/ent/fieldtype/where.go +++ b/entc/integration/gremlin/ent/fieldtype/where.go @@ -384,6 +384,13 @@ func DeletedAt(v *sql.NullTime) predicate.FieldType { }) } +// RawData applies equality check predicate on the "raw_data" field. It's identical to RawDataEQ. +func RawData(v []byte) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldRawData, p.EQ(v)) + }) +} + // IP applies equality check predicate on the "ip" field. It's identical to IPEQ. func IP(v net.IP) predicate.FieldType { vc := []byte(v) @@ -3751,6 +3758,84 @@ func DeletedAtNotNil() predicate.FieldType { }) } +// RawDataEQ applies the EQ predicate on the "raw_data" field. +func RawDataEQ(v []byte) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldRawData, p.EQ(v)) + }) +} + +// RawDataNEQ applies the NEQ predicate on the "raw_data" field. +func RawDataNEQ(v []byte) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldRawData, p.NEQ(v)) + }) +} + +// RawDataIn applies the In predicate on the "raw_data" field. +func RawDataIn(vs ...[]byte) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldRawData, p.Within(v...)) + }) +} + +// RawDataNotIn applies the NotIn predicate on the "raw_data" field. +func RawDataNotIn(vs ...[]byte) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldRawData, p.Without(v...)) + }) +} + +// RawDataGT applies the GT predicate on the "raw_data" field. +func RawDataGT(v []byte) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldRawData, p.GT(v)) + }) +} + +// RawDataGTE applies the GTE predicate on the "raw_data" field. +func RawDataGTE(v []byte) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldRawData, p.GTE(v)) + }) +} + +// RawDataLT applies the LT predicate on the "raw_data" field. +func RawDataLT(v []byte) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldRawData, p.LT(v)) + }) +} + +// RawDataLTE applies the LTE predicate on the "raw_data" field. +func RawDataLTE(v []byte) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldRawData, p.LTE(v)) + }) +} + +// RawDataIsNil applies the IsNil predicate on the "raw_data" field. +func RawDataIsNil() predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.HasLabel(Label).HasNot(FieldRawData) + }) +} + +// RawDataNotNil applies the NotNil predicate on the "raw_data" field. +func RawDataNotNil() predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.HasLabel(Label).Has(FieldRawData) + }) +} + // IPEQ applies the EQ predicate on the "ip" field. func IPEQ(v net.IP) predicate.FieldType { vc := []byte(v) diff --git a/entc/integration/gremlin/ent/fieldtype_create.go b/entc/integration/gremlin/ent/fieldtype_create.go index d5d63c1e1..a92c4d9d6 100644 --- a/entc/integration/gremlin/ent/fieldtype_create.go +++ b/entc/integration/gremlin/ent/fieldtype_create.go @@ -531,6 +531,12 @@ func (ftc *FieldTypeCreate) SetDeletedAt(st *sql.NullTime) *FieldTypeCreate { return ftc } +// SetRawData sets the "raw_data" field. +func (ftc *FieldTypeCreate) SetRawData(b []byte) *FieldTypeCreate { + ftc.mutation.SetRawData(b) + return ftc +} + // SetIP sets the "ip" field. func (ftc *FieldTypeCreate) SetIP(n net.IP) *FieldTypeCreate { ftc.mutation.SetIP(n) @@ -891,6 +897,11 @@ func (ftc *FieldTypeCreate) check() error { return &ValidationError{Name: "link", err: fmt.Errorf(`ent: validator failed for field "link": %w`, err)} } } + if v, ok := ftc.mutation.RawData(); ok { + if err := fieldtype.RawDataValidator(v); err != nil { + return &ValidationError{Name: "raw_data", err: fmt.Errorf(`ent: validator failed for field "raw_data": %w`, err)} + } + } if v, ok := ftc.mutation.IP(); ok { if err := fieldtype.IPValidator([]byte(v)); err != nil { return &ValidationError{Name: "ip", err: fmt.Errorf(`ent: validator failed for field "ip": %w`, err)} @@ -1065,6 +1076,9 @@ func (ftc *FieldTypeCreate) gremlin() *dsl.Traversal { if value, ok := ftc.mutation.DeletedAt(); ok { v.Property(dsl.Single, fieldtype.FieldDeletedAt, value) } + if value, ok := ftc.mutation.RawData(); ok { + v.Property(dsl.Single, fieldtype.FieldRawData, value) + } if value, ok := ftc.mutation.IP(); ok { v.Property(dsl.Single, fieldtype.FieldIP, value) } diff --git a/entc/integration/gremlin/ent/fieldtype_update.go b/entc/integration/gremlin/ent/fieldtype_update.go index c00498704..882f907bf 100644 --- a/entc/integration/gremlin/ent/fieldtype_update.go +++ b/entc/integration/gremlin/ent/fieldtype_update.go @@ -921,6 +921,18 @@ func (ftu *FieldTypeUpdate) ClearDeletedAt() *FieldTypeUpdate { return ftu } +// SetRawData sets the "raw_data" field. +func (ftu *FieldTypeUpdate) SetRawData(b []byte) *FieldTypeUpdate { + ftu.mutation.SetRawData(b) + return ftu +} + +// ClearRawData clears the value of the "raw_data" field. +func (ftu *FieldTypeUpdate) ClearRawData() *FieldTypeUpdate { + ftu.mutation.ClearRawData() + return ftu +} + // SetIP sets the "ip" field. func (ftu *FieldTypeUpdate) SetIP(n net.IP) *FieldTypeUpdate { ftu.mutation.SetIP(n) @@ -1368,6 +1380,11 @@ func (ftu *FieldTypeUpdate) check() error { return &ValidationError{Name: "link", err: fmt.Errorf("ent: validator failed for field \"link\": %w", err)} } } + if v, ok := ftu.mutation.RawData(); ok { + if err := fieldtype.RawDataValidator(v); err != nil { + return &ValidationError{Name: "raw_data", err: fmt.Errorf("ent: validator failed for field \"raw_data\": %w", err)} + } + } if v, ok := ftu.mutation.IP(); ok { if err := fieldtype.IPValidator([]byte(v)); err != nil { return &ValidationError{Name: "ip", err: fmt.Errorf("ent: validator failed for field \"ip\": %w", err)} @@ -1607,6 +1624,9 @@ func (ftu *FieldTypeUpdate) gremlin() *dsl.Traversal { if value, ok := ftu.mutation.DeletedAt(); ok { v.Property(dsl.Single, fieldtype.FieldDeletedAt, value) } + if value, ok := ftu.mutation.RawData(); ok { + v.Property(dsl.Single, fieldtype.FieldRawData, value) + } if value, ok := ftu.mutation.IP(); ok { v.Property(dsl.Single, fieldtype.FieldIP, value) } @@ -1791,6 +1811,9 @@ func (ftu *FieldTypeUpdate) gremlin() *dsl.Traversal { if ftu.mutation.DeletedAtCleared() { properties = append(properties, fieldtype.FieldDeletedAt) } + if ftu.mutation.RawDataCleared() { + properties = append(properties, fieldtype.FieldRawData) + } if ftu.mutation.IPCleared() { properties = append(properties, fieldtype.FieldIP) } @@ -2735,6 +2758,18 @@ func (ftuo *FieldTypeUpdateOne) ClearDeletedAt() *FieldTypeUpdateOne { return ftuo } +// SetRawData sets the "raw_data" field. +func (ftuo *FieldTypeUpdateOne) SetRawData(b []byte) *FieldTypeUpdateOne { + ftuo.mutation.SetRawData(b) + return ftuo +} + +// ClearRawData clears the value of the "raw_data" field. +func (ftuo *FieldTypeUpdateOne) ClearRawData() *FieldTypeUpdateOne { + ftuo.mutation.ClearRawData() + return ftuo +} + // SetIP sets the "ip" field. func (ftuo *FieldTypeUpdateOne) SetIP(n net.IP) *FieldTypeUpdateOne { ftuo.mutation.SetIP(n) @@ -3189,6 +3224,11 @@ func (ftuo *FieldTypeUpdateOne) check() error { return &ValidationError{Name: "link", err: fmt.Errorf("ent: validator failed for field \"link\": %w", err)} } } + if v, ok := ftuo.mutation.RawData(); ok { + if err := fieldtype.RawDataValidator(v); err != nil { + return &ValidationError{Name: "raw_data", err: fmt.Errorf("ent: validator failed for field \"raw_data\": %w", err)} + } + } if v, ok := ftuo.mutation.IP(); ok { if err := fieldtype.IPValidator([]byte(v)); err != nil { return &ValidationError{Name: "ip", err: fmt.Errorf("ent: validator failed for field \"ip\": %w", err)} @@ -3433,6 +3473,9 @@ func (ftuo *FieldTypeUpdateOne) gremlin(id string) *dsl.Traversal { if value, ok := ftuo.mutation.DeletedAt(); ok { v.Property(dsl.Single, fieldtype.FieldDeletedAt, value) } + if value, ok := ftuo.mutation.RawData(); ok { + v.Property(dsl.Single, fieldtype.FieldRawData, value) + } if value, ok := ftuo.mutation.IP(); ok { v.Property(dsl.Single, fieldtype.FieldIP, value) } @@ -3617,6 +3660,9 @@ func (ftuo *FieldTypeUpdateOne) gremlin(id string) *dsl.Traversal { if ftuo.mutation.DeletedAtCleared() { properties = append(properties, fieldtype.FieldDeletedAt) } + if ftuo.mutation.RawDataCleared() { + properties = append(properties, fieldtype.FieldRawData) + } if ftuo.mutation.IPCleared() { properties = append(properties, fieldtype.FieldIP) } diff --git a/entc/integration/gremlin/ent/mutation.go b/entc/integration/gremlin/ent/mutation.go index cdc1e5ede..c603dda19 100644 --- a/entc/integration/gremlin/ent/mutation.go +++ b/entc/integration/gremlin/ent/mutation.go @@ -1377,6 +1377,7 @@ type FieldTypeMutation struct { null_active *schema.Status deleted **sql.NullBool deleted_at **sql.NullTime + raw_data *[]byte ip *net.IP null_int64 **sql.NullInt64 schema_int *schema.Int @@ -3987,6 +3988,55 @@ func (m *FieldTypeMutation) ResetDeletedAt() { delete(m.clearedFields, fieldtype.FieldDeletedAt) } +// SetRawData sets the "raw_data" field. +func (m *FieldTypeMutation) SetRawData(b []byte) { + m.raw_data = &b +} + +// RawData returns the value of the "raw_data" field in the mutation. +func (m *FieldTypeMutation) RawData() (r []byte, exists bool) { + v := m.raw_data + if v == nil { + return + } + return *v, true +} + +// OldRawData returns the old "raw_data" field's value of the FieldType entity. +// 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 the database query fails. +func (m *FieldTypeMutation) OldRawData(ctx context.Context) (v []byte, err error) { + if !m.op.Is(OpUpdateOne) { + return v, fmt.Errorf("OldRawData is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, fmt.Errorf("OldRawData requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldRawData: %w", err) + } + return oldValue.RawData, nil +} + +// ClearRawData clears the value of the "raw_data" field. +func (m *FieldTypeMutation) ClearRawData() { + m.raw_data = nil + m.clearedFields[fieldtype.FieldRawData] = struct{}{} +} + +// RawDataCleared returns if the "raw_data" field was cleared in this mutation. +func (m *FieldTypeMutation) RawDataCleared() bool { + _, ok := m.clearedFields[fieldtype.FieldRawData] + return ok +} + +// ResetRawData resets all changes to the "raw_data" field. +func (m *FieldTypeMutation) ResetRawData() { + m.raw_data = nil + delete(m.clearedFields, fieldtype.FieldRawData) +} + // SetIP sets the "ip" field. func (m *FieldTypeMutation) SetIP(n net.IP) { m.ip = &n @@ -5011,7 +5061,7 @@ func (m *FieldTypeMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *FieldTypeMutation) Fields() []string { - fields := make([]string, 0, 61) + fields := make([]string, 0, 62) if m.int != nil { fields = append(fields, fieldtype.FieldInt) } @@ -5138,6 +5188,9 @@ func (m *FieldTypeMutation) Fields() []string { if m.deleted_at != nil { fields = append(fields, fieldtype.FieldDeletedAt) } + if m.raw_data != nil { + fields = append(fields, fieldtype.FieldRawData) + } if m.ip != nil { fields = append(fields, fieldtype.FieldIP) } @@ -5287,6 +5340,8 @@ func (m *FieldTypeMutation) Field(name string) (ent.Value, bool) { return m.Deleted() case fieldtype.FieldDeletedAt: return m.DeletedAt() + case fieldtype.FieldRawData: + return m.RawData() case fieldtype.FieldIP: return m.IP() case fieldtype.FieldNullInt64: @@ -5418,6 +5473,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.FieldRawData: + return m.OldRawData(ctx) case fieldtype.FieldIP: return m.OldIP(ctx) case fieldtype.FieldNullInt64: @@ -5759,6 +5816,13 @@ func (m *FieldTypeMutation) SetField(name string, value ent.Value) error { } m.SetDeletedAt(v) return nil + case fieldtype.FieldRawData: + v, ok := value.([]byte) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetRawData(v) + return nil case fieldtype.FieldIP: v, ok := value.(net.IP) if !ok { @@ -6405,6 +6469,9 @@ func (m *FieldTypeMutation) ClearedFields() []string { if m.FieldCleared(fieldtype.FieldDeletedAt) { fields = append(fields, fieldtype.FieldDeletedAt) } + if m.FieldCleared(fieldtype.FieldRawData) { + fields = append(fields, fieldtype.FieldRawData) + } if m.FieldCleared(fieldtype.FieldIP) { fields = append(fields, fieldtype.FieldIP) } @@ -6572,6 +6639,9 @@ func (m *FieldTypeMutation) ClearField(name string) error { case fieldtype.FieldDeletedAt: m.ClearDeletedAt() return nil + case fieldtype.FieldRawData: + m.ClearRawData() + return nil case fieldtype.FieldIP: m.ClearIP() return nil @@ -6751,6 +6821,9 @@ func (m *FieldTypeMutation) ResetField(name string) error { case fieldtype.FieldDeletedAt: m.ResetDeletedAt() return nil + case fieldtype.FieldRawData: + m.ResetRawData() + return nil case fieldtype.FieldIP: m.ResetIP() return nil diff --git a/entc/integration/gremlin/ent/runtime.go b/entc/integration/gremlin/ent/runtime.go index e04ca7de9..3420a33b3 100644 --- a/entc/integration/gremlin/ent/runtime.go +++ b/entc/integration/gremlin/ent/runtime.go @@ -93,22 +93,26 @@ func init() { fieldtypeDescLink := fieldtypeFields[36].Descriptor() // fieldtype.LinkValidator is a validator for the "link" field. It is called by the builders before save. fieldtype.LinkValidator = fieldtypeDescLink.Validators[0].(func(string) error) + // fieldtypeDescRawData is the schema descriptor for raw_data field. + fieldtypeDescRawData := fieldtypeFields[42].Descriptor() + // fieldtype.RawDataValidator is a validator for the "raw_data" field. It is called by the builders before save. + fieldtype.RawDataValidator = fieldtypeDescRawData.Validators[0].(func([]byte) error) // fieldtypeDescIP is the schema descriptor for ip field. - fieldtypeDescIP := fieldtypeFields[42].Descriptor() + fieldtypeDescIP := fieldtypeFields[43].Descriptor() // fieldtype.DefaultIP holds the default value on creation for the ip field. fieldtype.DefaultIP = fieldtypeDescIP.Default.(func() net.IP) // fieldtype.IPValidator is a validator for the "ip" field. It is called by the builders before save. fieldtype.IPValidator = fieldtypeDescIP.Validators[0].(func([]byte) error) // fieldtypeDescPair is the schema descriptor for pair field. - fieldtypeDescPair := fieldtypeFields[55].Descriptor() + fieldtypeDescPair := fieldtypeFields[56].Descriptor() // fieldtype.DefaultPair holds the default value on creation for the pair field. fieldtype.DefaultPair = fieldtypeDescPair.Default.(func() schema.Pair) // fieldtypeDescVstring is the schema descriptor for vstring field. - fieldtypeDescVstring := fieldtypeFields[57].Descriptor() + fieldtypeDescVstring := fieldtypeFields[58].Descriptor() // fieldtype.DefaultVstring holds the default value on creation for the vstring field. fieldtype.DefaultVstring = fieldtypeDescVstring.Default.(func() schema.VString) // fieldtypeDescTriple is the schema descriptor for triple field. - fieldtypeDescTriple := fieldtypeFields[58].Descriptor() + fieldtypeDescTriple := fieldtypeFields[59].Descriptor() // fieldtype.DefaultTriple holds the default value on creation for the triple field. fieldtype.DefaultTriple = fieldtypeDescTriple.Default.(func() schema.Triple) fileFields := schema.File{}.Fields() diff --git a/entc/integration/migrate/entv1/runtime.go b/entc/integration/migrate/entv1/runtime.go index aae39bdf4..d30b226b8 100644 --- a/entc/integration/migrate/entv1/runtime.go +++ b/entc/integration/migrate/entv1/runtime.go @@ -21,6 +21,10 @@ func init() { userDescName := userFields[2].Descriptor() // user.NameValidator is a validator for the "name" field. It is called by the builders before save. user.NameValidator = userDescName.Validators[0].(func(string) error) + // userDescBlob is the schema descriptor for blob field. + userDescBlob := userFields[7].Descriptor() + // user.BlobValidator is a validator for the "blob" field. It is called by the builders before save. + user.BlobValidator = userDescBlob.Validators[0].(func([]byte) error) // userDescWorkplace is the schema descriptor for workplace field. userDescWorkplace := userFields[10].Descriptor() // user.WorkplaceValidator is a validator for the "workplace" field. It is called by the builders before save. diff --git a/entc/integration/migrate/entv1/user/user.go b/entc/integration/migrate/entv1/user/user.go index c9e331e27..a614853f3 100644 --- a/entc/integration/migrate/entv1/user/user.go +++ b/entc/integration/migrate/entv1/user/user.go @@ -108,6 +108,8 @@ func ValidColumn(column string) bool { var ( // NameValidator is a validator for the "name" field. It is called by the builders before save. NameValidator func(string) error + // BlobValidator is a validator for the "blob" field. It is called by the builders before save. + BlobValidator func([]byte) error // WorkplaceValidator is a validator for the "workplace" field. It is called by the builders before save. WorkplaceValidator func(string) error ) diff --git a/entc/integration/migrate/entv1/user_create.go b/entc/integration/migrate/entv1/user_create.go index 9a2effb16..c10cc0a6d 100644 --- a/entc/integration/migrate/entv1/user_create.go +++ b/entc/integration/migrate/entv1/user_create.go @@ -294,6 +294,11 @@ func (uc *UserCreate) check() error { if _, ok := uc.mutation.Nickname(); !ok { return &ValidationError{Name: "nickname", err: errors.New(`entv1: missing required field "nickname"`)} } + if v, ok := uc.mutation.Blob(); ok { + if err := user.BlobValidator(v); err != nil { + return &ValidationError{Name: "blob", err: fmt.Errorf(`entv1: validator failed for field "blob": %w`, err)} + } + } if v, ok := uc.mutation.State(); ok { if err := user.StateValidator(v); err != nil { return &ValidationError{Name: "state", err: fmt.Errorf(`entv1: validator failed for field "state": %w`, err)} diff --git a/entc/integration/migrate/entv1/user_update.go b/entc/integration/migrate/entv1/user_update.go index 3dd1dac8a..d855ed464 100644 --- a/entc/integration/migrate/entv1/user_update.go +++ b/entc/integration/migrate/entv1/user_update.go @@ -371,6 +371,11 @@ func (uu *UserUpdate) check() error { return &ValidationError{Name: "name", err: fmt.Errorf("entv1: validator failed for field \"name\": %w", err)} } } + if v, ok := uu.mutation.Blob(); ok { + if err := user.BlobValidator(v); err != nil { + return &ValidationError{Name: "blob", err: fmt.Errorf("entv1: validator failed for field \"blob\": %w", err)} + } + } if v, ok := uu.mutation.State(); ok { if err := user.StateValidator(v); err != nil { return &ValidationError{Name: "state", err: fmt.Errorf("entv1: validator failed for field \"state\": %w", err)} @@ -1046,6 +1051,11 @@ func (uuo *UserUpdateOne) check() error { return &ValidationError{Name: "name", err: fmt.Errorf("entv1: validator failed for field \"name\": %w", err)} } } + if v, ok := uuo.mutation.Blob(); ok { + if err := user.BlobValidator(v); err != nil { + return &ValidationError{Name: "blob", err: fmt.Errorf("entv1: validator failed for field \"blob\": %w", err)} + } + } if v, ok := uuo.mutation.State(); ok { if err := user.StateValidator(v); err != nil { return &ValidationError{Name: "state", err: fmt.Errorf("entv1: validator failed for field \"state\": %w", err)} diff --git a/entc/integration/migrate/entv2/runtime.go b/entc/integration/migrate/entv2/runtime.go index ad1131779..6144f3723 100644 --- a/entc/integration/migrate/entv2/runtime.go +++ b/entc/integration/migrate/entv2/runtime.go @@ -42,6 +42,10 @@ func init() { userDescTitle := userFields[7].Descriptor() // user.DefaultTitle holds the default value on creation for the title field. user.DefaultTitle = userDescTitle.Default.(string) + // userDescBlob is the schema descriptor for blob field. + userDescBlob := userFields[9].Descriptor() + // user.BlobValidator is a validator for the "blob" field. It is called by the builders before save. + user.BlobValidator = userDescBlob.Validators[0].(func([]byte) error) // userDescCreatedAt is the schema descriptor for created_at field. userDescCreatedAt := userFields[13].Descriptor() // user.DefaultCreatedAt holds the default value on creation for the created_at field. diff --git a/entc/integration/migrate/entv2/user/user.go b/entc/integration/migrate/entv2/user/user.go index b83c04b41..dc383dabf 100644 --- a/entc/integration/migrate/entv2/user/user.go +++ b/entc/integration/migrate/entv2/user/user.go @@ -123,6 +123,8 @@ var ( DefaultBuffer func() []byte // DefaultTitle holds the default value on creation for the "title" field. DefaultTitle string + // BlobValidator is a validator for the "blob" field. It is called by the builders before save. + BlobValidator func([]byte) error // DefaultCreatedAt holds the default value on creation for the "created_at" field. DefaultCreatedAt func() time.Time ) diff --git a/entc/integration/migrate/entv2/user_create.go b/entc/integration/migrate/entv2/user_create.go index 8f1dbb7e4..fd94d13ca 100644 --- a/entc/integration/migrate/entv2/user_create.go +++ b/entc/integration/migrate/entv2/user_create.go @@ -381,6 +381,11 @@ func (uc *UserCreate) check() error { if _, ok := uc.mutation.Title(); !ok { return &ValidationError{Name: "title", err: errors.New(`entv2: missing required field "title"`)} } + if v, ok := uc.mutation.Blob(); ok { + if err := user.BlobValidator(v); err != nil { + return &ValidationError{Name: "blob", err: fmt.Errorf(`entv2: validator failed for field "blob": %w`, err)} + } + } if v, ok := uc.mutation.State(); ok { if err := user.StateValidator(v); err != nil { return &ValidationError{Name: "state", err: fmt.Errorf(`entv2: validator failed for field "state": %w`, err)} diff --git a/entc/integration/migrate/entv2/user_update.go b/entc/integration/migrate/entv2/user_update.go index 771ceaa8a..42718d114 100644 --- a/entc/integration/migrate/entv2/user_update.go +++ b/entc/integration/migrate/entv2/user_update.go @@ -426,6 +426,11 @@ func (uu *UserUpdate) check() error { return &ValidationError{Name: "nickname", err: fmt.Errorf("entv2: validator failed for field \"nickname\": %w", err)} } } + if v, ok := uu.mutation.Blob(); ok { + if err := user.BlobValidator(v); err != nil { + return &ValidationError{Name: "blob", err: fmt.Errorf("entv2: validator failed for field \"blob\": %w", err)} + } + } if v, ok := uu.mutation.State(); ok { if err := user.StateValidator(v); err != nil { return &ValidationError{Name: "state", err: fmt.Errorf("entv2: validator failed for field \"state\": %w", err)} @@ -1173,6 +1178,11 @@ func (uuo *UserUpdateOne) check() error { return &ValidationError{Name: "nickname", err: fmt.Errorf("entv2: validator failed for field \"nickname\": %w", err)} } } + if v, ok := uuo.mutation.Blob(); ok { + if err := user.BlobValidator(v); err != nil { + return &ValidationError{Name: "blob", err: fmt.Errorf("entv2: validator failed for field \"blob\": %w", err)} + } + } if v, ok := uuo.mutation.State(); ok { if err := user.StateValidator(v); err != nil { return &ValidationError{Name: "state", err: fmt.Errorf("entv2: validator failed for field \"state\": %w", err)} diff --git a/entc/integration/type_test.go b/entc/integration/type_test.go index 3b426f45a..8a744a09f 100644 --- a/entc/integration/type_test.go +++ b/entc/integration/type_test.go @@ -79,6 +79,7 @@ func Types(t *testing.T, client *ent.Client) { SetNilPair(&schema.Pair{K: []byte("K"), V: []byte("V")}). SetStringArray([]string{"foo", "bar", "baz"}). SetBigInt(bigint). + SetRawData([]byte{1, 2, 3}). SaveX(ctx) require.Equal(int8(math.MinInt8), ft.OptionalInt8) @@ -89,6 +90,7 @@ func Types(t *testing.T, client *ent.Client) { require.Equal(int16(math.MinInt16), *ft.NillableInt16) require.Equal(int32(math.MinInt32), *ft.NillableInt32) require.Equal(int64(math.MinInt64), *ft.NillableInt64) + require.Equal([]byte{1, 2, 3}, ft.RawData) require.Equal(http.Dir("dir"), ft.Dir) require.NotNil(*ft.Ndir) require.Equal(http.Dir("ndir"), *ft.Ndir) @@ -115,6 +117,16 @@ func Types(t *testing.T, client *ent.Client) { require.NoError(err) require.False(exists) + _, err = client.FieldType.Create(). + SetInt(1). + SetInt8(8). + SetInt16(16). + SetInt32(32). + SetInt64(64). + SetRawData(make([]byte, 40)). + Save(ctx) + require.Error(err) + ft = ft.Update(). SetInt(1). SetInt8(math.MaxInt8). diff --git a/schema/field/field.go b/schema/field/field.go index fef3d7b8e..b11453885 100644 --- a/schema/field/field.go +++ b/schema/field/field.go @@ -579,6 +579,12 @@ func (b *bytesBuilder) StructTag(s string) *bytesBuilder { // In SQLite, it does not have any effect on the type size, which is default to 1B bytes. func (b *bytesBuilder) MaxLen(i int) *bytesBuilder { b.desc.Size = i + b.desc.Validators = append(b.desc.Validators, func(buf []byte) error { + if len(buf) > i { + return errors.New("value is greater than the required length") + } + return nil + }) return b } diff --git a/schema/field/field_test.go b/schema/field/field_test.go index 7381ecccd..39ba00d89 100644 --- a/schema/field/field_test.go +++ b/schema/field/field_test.go @@ -199,6 +199,7 @@ func TestBytes(t *testing.T) { Validate(func(bytes []byte) error { return nil }). + MaxLen(50). Descriptor() assert.Equal(t, "active", fd.Name) assert.True(t, fd.Unique) @@ -206,7 +207,7 @@ func TestBytes(t *testing.T) { assert.NotNil(t, fd.Default) assert.Equal(t, []byte("{}"), fd.Default) assert.Equal(t, "comment", fd.Comment) - assert.Len(t, fd.Validators, 1) + assert.Len(t, fd.Validators, 2) fd = field.Bytes("ip").GoType(net.IP("127.0.0.1")).Descriptor() assert.NoError(t, fd.Err)