Files
ent/schema/field/type.go
Ariel Mashraki c3955a08f1 schema/field: json type support (#38)
Summary:
Pull Request resolved: https://github.com/facebookincubator/ent/pull/38

Only `IsNil` and `NotNil` predicates are supported this moment

Reviewed By: alexsn

Differential Revision: D17444976

fbshipit-source-id: 37336fa0bc7749af995933baee2e23bb7366dd78
2019-09-19 05:00:11 -07:00

106 lines
1.9 KiB
Go

package field
import "strings"
type Type uint8
const (
TypeInvalid Type = iota
TypeBool
TypeTime
TypeJSON
TypeBytes
TypeString
TypeInt8
TypeInt16
TypeInt32
TypeInt
TypeInt64
TypeUint8
TypeUint16
TypeUint32
TypeUint
TypeUint64
TypeFloat32
TypeFloat64
endTypes
)
// String returns the string representation of a type.
func (t Type) String() string {
if t < endTypes {
return typeNames[t]
}
return typeNames[TypeInvalid]
}
// Numeric reports if the given type is a numeric type.
func (t Type) Numeric() bool {
return t >= TypeInt8 && t < endTypes
}
// ConstName returns the constant name of a info type.
// It's used by entc for printing the constant name in templates.
func (t Type) ConstName() string {
switch t {
case TypeJSON:
return "TypeJSON"
case TypeTime:
return "TypeTime"
case TypeBytes:
return "TypeBytes"
default:
return "Type" + strings.Title(typeNames[t])
}
}
type TypeInfo struct {
Type Type
Ident string
PkgPath string
Nillable bool // slices or pointers.
}
// String returns the string representation of a type.
func (t TypeInfo) String() string {
switch {
case t.Ident != "":
return t.Ident
case t.Type < endTypes:
return typeNames[t.Type]
default:
return typeNames[TypeInvalid]
}
}
// Valid reports if the given type if known type.
func (t TypeInfo) Valid() bool {
return t.Type > TypeInvalid && t.Type < endTypes
}
// Numeric reports if the given type is a numeric type.
func (t TypeInfo) Numeric() bool {
return t.Type.Numeric()
}
var typeNames = [...]string{
TypeInvalid: "invalid",
TypeBool: "bool",
TypeTime: "time.Time",
TypeJSON: "json.RawMessage",
TypeBytes: "[]byte",
TypeString: "string",
TypeInt: "int",
TypeInt8: "int8",
TypeInt16: "int16",
TypeInt32: "int32",
TypeInt64: "int64",
TypeUint: "uint",
TypeUint8: "uint8",
TypeUint16: "uint16",
TypeUint32: "uint32",
TypeUint64: "uint64",
TypeFloat32: "float32",
TypeFloat64: "float64",
}