mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc: blob storage support
This commit is contained in:
@@ -63,6 +63,10 @@ type Field struct {
|
||||
Comment string `json:"comment,omitempty"`
|
||||
Deprecated bool `json:"deprecated,omitempty"`
|
||||
DeprecatedReason string `json:"deprecated_reason,omitempty"`
|
||||
BlobKey bool `json:"blob_key,omitempty"`
|
||||
BlobDualWrite bool `json:"blob_dual_write,omitempty"`
|
||||
BlobDWSchemaType map[string]string `json:"blob_dw_schema_type,omitempty"`
|
||||
BlobLazy bool `json:"blob_lazy,omitempty"`
|
||||
}
|
||||
|
||||
// Edge represents an ent.Edge that was loaded from a complied user package.
|
||||
@@ -144,6 +148,10 @@ func NewField(fd *field.Descriptor) (*Field, error) {
|
||||
Comment: fd.Comment,
|
||||
Deprecated: fd.Deprecated,
|
||||
DeprecatedReason: fd.DeprecatedReason,
|
||||
BlobKey: fd.BlobKey != nil,
|
||||
BlobDualWrite: fd.BlobDualWrite,
|
||||
BlobLazy: fd.BlobLazy,
|
||||
BlobDWSchemaType: fd.BlobDWSchemaType,
|
||||
}
|
||||
for _, at := range fd.Annotations {
|
||||
sf.addAnnotation(at)
|
||||
|
||||
@@ -344,6 +344,52 @@ func TestMarshalDefaults(t *testing.T) {
|
||||
require.Equal(t, schema.Fields[8].DefaultKind, reflect.Func)
|
||||
}
|
||||
|
||||
// BlobDoc is a test schema with blob-stored fields.
|
||||
type BlobDoc struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
func (BlobDoc) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Blob("content").
|
||||
Comment("blob content"),
|
||||
field.Blob("thumbnail"),
|
||||
}
|
||||
}
|
||||
|
||||
func (BlobDoc) Edges() []ent.Edge { return nil }
|
||||
|
||||
func TestMarshalBlobSchema(t *testing.T) {
|
||||
d := BlobDoc{}
|
||||
buf, err := MarshalSchema(d)
|
||||
require.NoError(t, err)
|
||||
|
||||
s := &Schema{}
|
||||
err = json.Unmarshal(buf, s)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "BlobDoc", s.Name)
|
||||
require.Len(t, s.Fields, 2)
|
||||
|
||||
// First blob field: content.
|
||||
f0 := s.Fields[0]
|
||||
require.Equal(t, "content", f0.Name)
|
||||
require.Equal(t, field.TypeBlob, f0.Info.Type)
|
||||
require.Equal(t, "blob content", f0.Comment)
|
||||
|
||||
// Second blob field: thumbnail.
|
||||
f1 := s.Fields[1]
|
||||
require.Equal(t, "thumbnail", f1.Name)
|
||||
require.Equal(t, field.TypeBlob, f1.Info.Type)
|
||||
|
||||
// Verify JSON roundtrip preserves type.
|
||||
buf2, err := json.Marshal(s)
|
||||
require.NoError(t, err)
|
||||
s2 := &Schema{}
|
||||
require.NoError(t, json.Unmarshal(buf2, s2))
|
||||
require.Equal(t, field.TypeBlob, s2.Fields[0].Info.Type)
|
||||
}
|
||||
|
||||
type TimeMixin struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user