mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
schema/field: add annotation option to schema field (#622)
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -35,23 +35,24 @@ type Position struct {
|
||||
|
||||
// Field represents an ent.Field that was loaded from a complied user package.
|
||||
type Field struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Info *field.TypeInfo `json:"type,omitempty"`
|
||||
Tag string `json:"tag,omitempty"`
|
||||
Size *int64 `json:"size,omitempty"`
|
||||
Enums []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 interface{} `json:"default_value,omitempty"`
|
||||
UpdateDefault bool `json:"update_default,omitempty"`
|
||||
Immutable bool `json:"immutable,omitempty"`
|
||||
Validators int `json:"validators,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"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Info *field.TypeInfo `json:"type,omitempty"`
|
||||
Tag string `json:"tag,omitempty"`
|
||||
Size *int64 `json:"size,omitempty"`
|
||||
Enums []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 interface{} `json:"default_value,omitempty"`
|
||||
UpdateDefault bool `json:"update_default,omitempty"`
|
||||
Immutable bool `json:"immutable,omitempty"`
|
||||
Validators int `json:"validators,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]interface{} `json:"annotations,omitempty"`
|
||||
}
|
||||
|
||||
// Edge represents an ent.Edge that was loaded from a complied user package.
|
||||
@@ -114,6 +115,10 @@ func NewField(fd *field.Descriptor) (*Field, error) {
|
||||
Validators: len(fd.Validators),
|
||||
Sensitive: fd.Sensitive,
|
||||
SchemaType: fd.SchemaType,
|
||||
Annotations: make(map[string]interface{}),
|
||||
}
|
||||
for _, at := range fd.Annotations {
|
||||
sf.Annotations[at.Name()] = at
|
||||
}
|
||||
if sf.Info == nil {
|
||||
return nil, fmt.Errorf("missing type info for field %q", sf.Name)
|
||||
|
||||
@@ -20,6 +20,18 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type OrderConfig struct {
|
||||
FieldName string
|
||||
}
|
||||
|
||||
func (OrderConfig) Name() string {
|
||||
return "order_config"
|
||||
}
|
||||
|
||||
func (o *OrderConfig) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(*o)
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ent.Schema
|
||||
}
|
||||
@@ -28,7 +40,8 @@ func (User) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("age"),
|
||||
field.String("name").
|
||||
Default("unknown"),
|
||||
Default("unknown").
|
||||
Annotations(&OrderConfig{FieldName: "name"}),
|
||||
field.String("nillable").
|
||||
Nillable(),
|
||||
field.String("optional").
|
||||
@@ -81,8 +94,8 @@ func TestMarshalSchema(t *testing.T) {
|
||||
buf, err := MarshalSchema(u)
|
||||
require.NoError(t, err)
|
||||
|
||||
schema := &Schema{}
|
||||
require.NoError(t, json.Unmarshal(buf, schema))
|
||||
schema, err := UnmarshalSchema(buf)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "User", schema.Name)
|
||||
require.Len(t, schema.Fields, 8)
|
||||
require.Equal(t, "age", schema.Fields[0].Name)
|
||||
@@ -91,6 +104,9 @@ func TestMarshalSchema(t *testing.T) {
|
||||
require.Equal(t, "name", schema.Fields[1].Name)
|
||||
require.Equal(t, field.TypeString, schema.Fields[1].Info.Type)
|
||||
require.Equal(t, "unknown", schema.Fields[1].DefaultValue)
|
||||
require.NotEmpty(t, schema.Fields[1].Annotations)
|
||||
ant := schema.Fields[1].Annotations["order_config"].(map[string]interface{})
|
||||
require.Equal(t, ant["FieldName"], "name")
|
||||
|
||||
require.Equal(t, "nillable", schema.Fields[2].Name)
|
||||
require.Equal(t, field.TypeString, schema.Fields[2].Info.Type)
|
||||
|
||||
Reference in New Issue
Block a user