ent: add blob tyoe

Reviewed By: idoshveki

Differential Revision: D16600425

fbshipit-source-id: 04c6fe39f9b3b628a1e79eb3063188f582d9e504
This commit is contained in:
Ariel Mashraki
2019-08-01 07:42:14 -07:00
committed by Facebook Github Bot
parent 2d962d5cba
commit c203f043cf
19 changed files with 302 additions and 76 deletions

View File

@@ -16,6 +16,7 @@ const (
TypeInvalid Type = iota
TypeBool
TypeTime
TypeBytes
TypeString
TypeInt8
TypeInt16
@@ -42,21 +43,29 @@ func (t Type) String() string {
// Valid reports if the given type if known type.
func (t Type) Valid() bool { return t > TypeInvalid && t < endTypes }
// Numeric reports of the given type is a numeric type.
// Numeric reports if the given type is a numeric type.
func (t Type) Numeric() bool { return t >= TypeInt && t < endTypes }
// Slice reports if the given type is a slice type.
func (t Type) Slice() bool { return t == TypeBytes }
// ConstName returns the constant name of a type. It's used by entc for printing the constant name in templates.
func (t Type) ConstName() string {
if t == TypeTime {
switch t {
case TypeTime:
return "TypeTime"
case TypeBytes:
return "TypeBytes"
default:
return "Type" + strings.Title(t.String())
}
return "Type" + strings.Title(t.String())
}
var typeNames = [...]string{
TypeInvalid: "invalid",
TypeBool: "bool",
TypeTime: "time.Time",
TypeBytes: "[]byte",
TypeString: "string",
TypeInt: "int",
TypeInt8: "int8",
@@ -113,6 +122,10 @@ func Text(name string) *stringBuilder {
return &stringBuilder{Field{typ: TypeString, name: name, size: math.MaxInt32}}
}
// Bytes returns a new Field with type bytes/buffer.
// In MySQL and SQLite, it is the "BLOB" type, and it does not support for Gremlin.
func Bytes(name string) *bytesBuilder { return &bytesBuilder{Field{typ: TypeBytes, name: name}} }
// Bool returns a new Field with type bool.
func Bool(name string) *boolBuilder { return &boolBuilder{Field{typ: TypeBool, name: name}} }
@@ -480,6 +493,42 @@ func (b *boolBuilder) StructTag(s string) *boolBuilder {
return b
}
// bytesBuilder is the builder for bytes fields.
type bytesBuilder struct {
Field
}
// Default sets the default value of the field.
func (b *bytesBuilder) Default(v []byte) *bytesBuilder {
b.value = v
return b
}
// Nullable indicates that this field is nullable.
// Unlike "Optional", nullable fields are pointers in the generated field.
func (b *bytesBuilder) Nullable() *bytesBuilder {
b.nullable = true
return b
}
// Optional indicates that this field is optional on create.
// Unlike edges, fields are required by default.
func (b *bytesBuilder) Optional() *bytesBuilder {
b.optional = true
return b
}
// Comment sets the comment of the field.
func (b *bytesBuilder) Comment(c string) *bytesBuilder {
return b
}
// StructTag sets the struct tag of the field.
func (b *bytesBuilder) StructTag(s string) *bytesBuilder {
b.tag = s
return b
}
// Charseter is the interface that wraps the Charset method.
type Charseter interface {
Charset() string

View File

@@ -50,6 +50,14 @@ func TestBool(t *testing.T) {
assert.Equal(t, true, f.Value())
}
func TestBytes(t *testing.T) {
f := field.Bytes("active").Default([]byte("{}"))
assert.Equal(t, "active", f.Name())
assert.Equal(t, field.TypeBytes, f.Type())
assert.True(t, f.HasDefault())
assert.Equal(t, []byte("{}"), f.Value())
}
func TestString(t *testing.T) {
re := regexp.MustCompile("[a-zA-Z0-9]")
f := field.String("name").Unique().Match(re).Validate(func(string) error { return nil })