schema/field: add Blob to use io.Reader

This commit is contained in:
Giau. Tran Minh
2026-05-05 07:21:16 +00:00
parent 7a21ec2e29
commit 630ab6b914
37 changed files with 2334 additions and 272 deletions

View File

@@ -183,6 +183,22 @@ func Other(name string, typ driver.Valuer) *otherBuilder {
return ob
}
// Blob returns a new Field with type blob. Blob fields store their data in
// external blob storage rather than in the database. The mutation accepts an
// io.Reader for writing, and the model provides methods that return io.ReadCloser
// and io.WriteCloser for streaming access.
//
// field.Blob("content")
//
// field.Blob("avatar").
// Optional()
func Blob(name string) *blobBuilder {
return &blobBuilder{&Descriptor{
Name: name,
Info: &TypeInfo{Type: TypeBlob},
}}
}
// stringBuilder is the builder for string fields.
type stringBuilder struct {
desc *Descriptor
@@ -660,13 +676,6 @@ func (b *bytesBuilder) Optional() *bytesBuilder {
return b
}
// Blob indicates that this field stores its data in external blob storage
// rather than in the database. Blob fields have no SQL column.
func (b *bytesBuilder) Blob() *bytesBuilder {
b.desc.Blob = true
return b
}
// Sensitive fields not printable and not serializable.
func (b *bytesBuilder) Sensitive() *bytesBuilder {
b.desc.Sensitive = true
@@ -1424,6 +1433,63 @@ func (b *otherBuilder) Descriptor() *Descriptor {
return b.desc
}
// blobBuilder is the builder for blob fields.
type blobBuilder struct {
desc *Descriptor
}
// Optional indicates that this field is optional on create.
// Unlike edges, fields are required by default.
func (b *blobBuilder) Optional() *blobBuilder {
b.desc.Optional = true
return b
}
// Immutable indicates that this field cannot be updated.
func (b *blobBuilder) Immutable() *blobBuilder {
b.desc.Immutable = true
return b
}
// Comment sets the comment of the field.
func (b *blobBuilder) Comment(c string) *blobBuilder {
b.desc.Comment = c
return b
}
// StructTag sets the struct tag of the field.
func (b *blobBuilder) StructTag(s string) *blobBuilder {
b.desc.Tag = s
return b
}
// StorageKey sets the storage key of the field.
func (b *blobBuilder) StorageKey(key string) *blobBuilder {
b.desc.StorageKey = key
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
func (b *blobBuilder) Annotations(annotations ...schema.Annotation) *blobBuilder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Deprecated marks the field as deprecated.
func (b *blobBuilder) Deprecated(reason ...string) *blobBuilder {
b.desc.Deprecated = true
if len(reason) > 0 {
b.desc.DeprecatedReason = strings.Join(reason, " ")
}
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *blobBuilder) Descriptor() *Descriptor {
return b.desc
}
// A Descriptor for field configuration.
type Descriptor struct {
Tag string // struct tag.
@@ -1446,7 +1512,6 @@ type Descriptor struct {
Comment string // field comment.
Deprecated bool // mark the field as deprecated.
DeprecatedReason string // deprecation reason.
Blob bool // field is stored in external blob storage.
Err error
}

View File

@@ -943,7 +943,7 @@ func TestTypeString(t *testing.T) {
assert.Equal(t, "bool", typ.String())
typ = field.TypeInvalid
assert.Equal(t, "invalid", typ.String())
typ = 21
typ = 22
assert.Equal(t, "invalid", typ.String())
}
@@ -959,7 +959,7 @@ func TestTypeValid(t *testing.T) {
assert.True(t, typ.Valid())
typ = 0
assert.False(t, typ.Valid())
typ = 21
typ = 22
assert.False(t, typ.Valid())
}
@@ -972,7 +972,7 @@ func TestTypeConstName(t *testing.T) {
assert.Equal(t, "TypeInt64", typ.ConstName())
typ = field.TypeOther
assert.Equal(t, "TypeOther", typ.ConstName())
typ = 21
typ = 22
assert.Equal(t, "invalid", typ.ConstName())
}

View File

@@ -24,6 +24,7 @@ const (
TypeEnum
TypeString
TypeOther
TypeBlob
TypeInt8
TypeInt16
TypeInt32
@@ -166,6 +167,7 @@ var (
TypeEnum: "string",
TypeString: "string",
TypeOther: "other",
TypeBlob: "io.Reader",
TypeInt: "int",
TypeInt8: "int8",
TypeInt16: "int16",
@@ -186,6 +188,7 @@ var (
TypeEnum: "TypeEnum",
TypeBytes: "TypeBytes",
TypeOther: "TypeOther",
TypeBlob: "TypeBlob",
}
)