Documentation
¶
Overview ¶
Package load is the interface for loading an ent/schema package into a Go program.
Index ¶
- func MarshalSchema(schema ent.Interface, resolver TypeResolver) (b []byte, err error)
- type BasicKind
- type ChannelDirection
- type Config
- type Edge
- type Field
- type FieldType
- type Index
- type InterfaceMethod
- type LogicalType
- type Package
- type Position
- type RuntimeType
- type Schema
- type SchemaSpec
- type StorageType
- type StructField
- type TypeCapabilities
- type TypeCompatibility
- type TypeExpression
- type TypeID
- type TypeKind
- type TypeName
- type TypeParameter
- type TypeResolver
- type UnionTerm
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarshalSchema ¶
func MarshalSchema(schema ent.Interface, resolver TypeResolver) (b []byte, err error)
MarshalSchema encodes the ent.Schema interface into a JSON that can be decoded into the Schema objects declared above.
Types ¶
type BasicKind ¶ added in v0.2.0
type BasicKind string
BasicKind identifies a predeclared basic Go type.
const ( BasicKindInvalid BasicKind = "invalid" BasicKindBool BasicKind = "bool" BasicKindInt BasicKind = "int" BasicKindInt8 BasicKind = "int8" BasicKindInt16 BasicKind = "int16" BasicKindInt32 BasicKind = "int32" BasicKindInt64 BasicKind = "int64" BasicKindUint BasicKind = "uint" BasicKindUint8 BasicKind = "uint8" BasicKindUint16 BasicKind = "uint16" BasicKindUint32 BasicKind = "uint32" BasicKindUint64 BasicKind = "uint64" BasicKindUintptr BasicKind = "uintptr" BasicKindFloat32 BasicKind = "float32" BasicKindFloat64 BasicKind = "float64" BasicKindComplex64 BasicKind = "complex64" BasicKindComplex128 BasicKind = "complex128" BasicKindString BasicKind = "string" BasicKindByte BasicKind = "byte" BasicKindRune BasicKind = "rune" BasicKindUnsafePointer BasicKind = "unsafe.Pointer" BasicKindUntypedBool BasicKind = "untyped bool" BasicKindUntypedInt BasicKind = "untyped int" BasicKindUntypedRune BasicKind = "untyped rune" BasicKindUntypedFloat BasicKind = "untyped float" BasicKindUntypedComplex BasicKind = "untyped complex" BasicKindUntypedString BasicKind = "untyped string" BasicKindUntypedNil BasicKind = "untyped nil" )
type ChannelDirection ¶ added in v0.2.0
type ChannelDirection string
ChannelDirection identifies the direction of a channel type.
const ( ChannelBoth ChannelDirection = "both" ChannelSend ChannelDirection = "send" ChannelReceive ChannelDirection = "receive" )
type Config ¶
type Config struct {
// Path is the path for the schema package.
Path string
// Names are the schema names to load. Empty means all schemas in the directory.
Names []string
// BuildFlags are forwarded to package loading and schema execution.
BuildFlags []string
}
Config holds the configuration for loading an ent/schema package.
func (*Config) Load ¶
func (c *Config) Load() (*SchemaSpec, error)
Load loads the schemas package and build the Go plugin with this info.
type Edge ¶
type Edge struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Tag string `json:"tag,omitempty"`
Field string `json:"field,omitempty"`
RefName string `json:"ref_name,omitempty"`
Ref *Edge `json:"ref,omitempty"`
Through *struct{ N, T string } `json:"through,omitempty"`
Unique bool `json:"unique,omitempty"`
Inverse bool `json:"inverse,omitempty"`
Required bool `json:"required,omitempty"`
Immutable bool `json:"immutable,omitempty"`
StorageKey *edge.StorageKey `json:"storage_key,omitempty"`
Annotations map[string]any `json:"annotations,omitempty"`
Comment string `json:"comment,omitempty"`
}
Edge represents an ent.Edge that was loaded from a complied user package.
func NewEdge ¶
func NewEdge(ed *edge.Descriptor) *Edge
NewEdge creates an loaded edge from edge descriptor.
type Field ¶
type Field struct {
Name string `json:"name,omitempty"`
Type field.Type `json:"type"`
Semantic *FieldType `json:"semantic_type"`
ValueScanner bool `json:"value_scanner,omitempty"`
Tag string `json:"tag,omitempty"`
Size *int64 `json:"size,omitempty"`
Enums []struct{ N, V string } `json:"enums,omitempty"`
Unique bool `json:"unique,omitempty"`
Nillable bool `json:"nillable,omitempty"`
Optional bool `json:"optional,omitempty"`
Default bool `json:"default,omitempty"`
DefaultValue any `json:"default_value,omitempty"`
DefaultKind reflect.Kind `json:"default_kind,omitempty"`
UpdateDefault bool `json:"update_default,omitempty"`
Immutable bool `json:"immutable,omitempty"`
Validators int `json:"validators,omitempty"`
ValidatorKinds []field.ValidatorKind `json:"validator_kinds,omitempty"`
StorageKey string `json:"storage_key,omitempty"`
Position *Position `json:"position,omitempty"`
Sensitive bool `json:"sensitive,omitempty"`
SchemaType map[string]string `json:"schema_type,omitempty"`
Annotations map[string]any `json:"annotations,omitempty"`
Comment string `json:"comment,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
DeprecatedReason string `json:"deprecated_reason,omitempty"`
}
Field represents an ent.Field that was loaded from a complied user package.
func NewField ¶
func NewField(fd *field.Descriptor, resolver TypeResolver) (*Field, error)
NewField creates a loaded field from field descriptor.
type FieldType ¶ added in v0.2.0
type FieldType struct {
ID TypeID `json:"id"`
Logical LogicalType `json:"logical"`
Base *TypeExpression `json:"base"`
Representation *TypeExpression `json:"representation"`
Storage StorageType `json:"storage,omitempty"`
Capabilities TypeCapabilities `json:"capabilities,omitempty"`
}
FieldType connects a logical field family to its compiler-authored Go type.
func FieldTypeOf ¶ added in v0.2.0
func FieldTypeOf(logical field.Type, representation *TypeExpression) (*FieldType, error)
FieldTypeOf creates compiler-authored field metadata for a representation.
type Index ¶
type Index struct {
Unique bool `json:"unique,omitempty"`
Edges []string `json:"edges,omitempty"`
Fields []string `json:"fields,omitempty"`
StorageKey string `json:"storage_key,omitempty"`
Annotations map[string]any `json:"annotations,omitempty"`
}
Index represents an ent.Index that was loaded from a complied user package.
func NewIndex ¶
func NewIndex(idx *index.Descriptor) *Index
NewIndex creates an loaded index from index descriptor.
type InterfaceMethod ¶ added in v0.2.0
type InterfaceMethod struct {
Name string `json:"name"`
Package *Package `json:"package,omitempty"`
Signature *TypeExpression `json:"signature"`
}
InterfaceMethod describes one method in an interface method set.
type LogicalType ¶ added in v0.2.0
type LogicalType string
LogicalType identifies the field family independently from its Go representation.
const ( LogicalTypeBool LogicalType = "bool" LogicalTypeTime LogicalType = "time" LogicalTypeJSON LogicalType = "json" LogicalTypeUUID LogicalType = "uuid" LogicalTypeBytes LogicalType = "bytes" LogicalTypeEnum LogicalType = "enum" LogicalTypeString LogicalType = "string" LogicalTypeInt LogicalType = "int" LogicalTypeUint LogicalType = "uint" LogicalTypeFloat LogicalType = "float" LogicalTypeOther LogicalType = "other" )
type Package ¶ added in v0.2.0
Package identifies a Go package. Path defines package identity and Name is retained for source rendering.
type Position ¶
type Position struct {
Index int // Index in the field/hook list.
MixedIn bool // Indicates if the schema object was mixed-in.
MixinIndex int // Mixin index in the mixin list.
}
Position describes a position in the schema.
type RuntimeType ¶ added in v0.2.0
type RuntimeType struct {
ID TypeID
GoType types.Type
Expression *TypeExpression
Capabilities TypeCapabilities
Compatibility map[string]TypeCompatibility
Source string
}
RuntimeType describes one concrete Go type collected from typed field calls.
func CollectRuntimeTypes ¶ added in v0.2.0
func CollectRuntimeTypes(packagesToCollect []*packages.Package) ([]RuntimeType, error)
CollectRuntimeTypes uses SSA generic instantiation to collect the concrete representation passed to field.newTypedBuilder. Instantiated helper and mixin bodies are included, preserving nested and phantom arguments.
type Schema ¶
type Schema struct {
Name string `json:"name,omitempty"`
Pos string `json:"-"`
View bool `json:"view,omitempty"`
Config ent.Config `json:"config,omitempty"`
Edges []*Edge `json:"edges,omitempty"`
Fields []*Field `json:"fields,omitempty"`
Indexes []*Index `json:"indexes,omitempty"`
Hooks []*Position `json:"hooks,omitempty"`
Interceptors []*Position `json:"interceptors,omitempty"`
Policy []*Position `json:"policy,omitempty"`
Annotations map[string]any `json:"annotations,omitempty"`
}
Schema represents an ent.Schema that was loaded from a complied user package.
func UnmarshalSchema ¶
UnmarshalSchema decodes the given buffer to a loaded schema.
type SchemaSpec ¶
type SchemaSpec struct {
// Schemas defines the loaded schema descriptors.
Schemas []*Schema
// PkgPath is the package path of the loaded
// ent.Schema package.
PkgPath string
// Module defines the module information for
// the user schema package if exists.
Module *packages.Module
}
A SchemaSpec holds a serializable version of an ent.Schema and its Go package and module information.
type StorageType ¶ added in v0.2.0
type StorageType struct {
Family string `json:"family,omitempty"`
Dialects map[string]string `json:"dialects,omitempty"`
}
StorageType describes the default SQL storage family and dialect overrides.
type StructField ¶ added in v0.2.0
type StructField struct {
Name string `json:"name"`
Package *Package `json:"package,omitempty"`
Type *TypeExpression `json:"type"`
Tag string `json:"tag,omitempty"`
Embedded bool `json:"embedded,omitempty"`
}
StructField describes one field in an anonymous struct type.
type TypeCapabilities ¶ added in v0.2.0
type TypeCapabilities struct {
Comparable bool `json:"comparable,omitempty"`
Nillable bool `json:"nillable,omitempty"`
Scanner bool `json:"scanner,omitempty"`
Valuer bool `json:"valuer,omitempty"`
NullableScanner bool `json:"nullable_scanner,omitempty"`
Validator bool `json:"validator,omitempty"`
Stringer bool `json:"stringer,omitempty"`
Adder bool `json:"adder,omitempty"`
Underlying *TypeExpression `json:"underlying,omitempty"`
AssignableToLogical bool `json:"assignable_to_logical,omitempty"`
ConvertibleToLogical bool `json:"convertible_to_logical,omitempty"`
LogicalReverseConvertible bool `json:"logical_reverse_convertible,omitempty"`
LogicalProjection string `json:"logical_projection,omitempty"`
}
TypeCapabilities records compiler-derived properties used by generation.
type TypeCompatibility ¶ added in v0.2.0
type TypeCompatibility struct {
Assignable bool `json:"assignable,omitempty"`
Convertible bool `json:"convertible,omitempty"`
ReverseConvertible bool `json:"reverse_convertible,omitempty"`
Projection string `json:"projection,omitempty"`
}
TypeCompatibility records compiler-derived conversion properties between a representation and one logical field base type.
type TypeExpression ¶ added in v0.2.0
type TypeExpression struct {
Kind TypeKind `json:"kind"`
Basic BasicKind `json:"basic,omitempty"`
Named *TypeName `json:"named,omitempty"`
Alias *TypeName `json:"alias,omitempty"`
Target *TypeExpression `json:"target,omitempty"`
Arguments []*TypeExpression `json:"arguments,omitempty"`
Element *TypeExpression `json:"element,omitempty"`
Key *TypeExpression `json:"key,omitempty"`
Length int64 `json:"length,omitempty"`
Fields []StructField `json:"fields,omitempty"`
Methods []InterfaceMethod `json:"methods,omitempty"`
Embedded []*TypeExpression `json:"embedded,omitempty"`
TypeParameters []TypeParameter `json:"type_parameters,omitempty"`
Parameters []*TypeExpression `json:"parameters,omitempty"`
Results []*TypeExpression `json:"results,omitempty"`
Variadic bool `json:"variadic,omitempty"`
ChannelDirection ChannelDirection `json:"channel_direction,omitempty"`
TypeParameter *TypeParameter `json:"type_parameter,omitempty"`
Variables []*TypeExpression `json:"variables,omitempty"`
Terms []UnionTerm `json:"terms,omitempty"`
}
TypeExpression is a recursive, JSON-safe semantic representation of a Go type. Fields that do not apply to Kind are left empty.
func TypeExpressionFor ¶ added in v0.2.0
func TypeExpressionFor[T any]() (*TypeExpression, error)
TypeExpressionFor returns a semantic expression for T when reflection exposes enough metadata to represent it accurately.
func TypeExpressionOf ¶ added in v0.2.0
func TypeExpressionOf(goType types.Type) (*TypeExpression, error)
TypeExpressionOf converts an arbitrary go/types type to its semantic representation.
func (*TypeExpression) Canonical ¶ added in v0.2.0
func (t *TypeExpression) Canonical() ([]byte, error)
Canonical returns the deterministic, alias-independent identity encoding of t.
func (*TypeExpression) ID ¶ added in v0.2.0
func (t *TypeExpression) ID() (TypeID, error)
ID returns the stable identity hash of t.
type TypeID ¶ added in v0.2.0
type TypeID string
TypeID is a stable SHA-256 identity for a semantic Go type.
type TypeKind ¶ added in v0.2.0
type TypeKind string
TypeKind identifies a semantic Go type form.
const ( TypeKindBasic TypeKind = "basic" TypeKindNamed TypeKind = "named" TypeKindAlias TypeKind = "alias" TypeKindPointer TypeKind = "pointer" TypeKindSlice TypeKind = "slice" TypeKindArray TypeKind = "array" TypeKindMap TypeKind = "map" TypeKindStruct TypeKind = "struct" TypeKindInterface TypeKind = "interface" TypeKindSignature TypeKind = "signature" TypeKindChannel TypeKind = "channel" TypeKindTypeParameter TypeKind = "type_parameter" TypeKindTuple TypeKind = "tuple" TypeKindUnion TypeKind = "union" )
type TypeParameter ¶ added in v0.2.0
type TypeParameter struct {
Package Package `json:"package"`
Name string `json:"name"`
Index int `json:"index"`
Scope int `json:"scope,omitempty"`
Owner string `json:"owner,omitempty"`
Constraint *TypeExpression `json:"constraint,omitempty"`
}
TypeParameter identifies a type parameter reference or declaration.
type TypeResolver ¶ added in v0.2.0
TypeResolver resolves an opaque runtime key to compiler-authored field metadata.
type UnionTerm ¶ added in v0.2.0
type UnionTerm struct {
Type *TypeExpression `json:"type"`
Tilde bool `json:"tilde,omitempty"`
}
UnionTerm describes one term in an interface type-set union.