ent: add annotations to type schema

This commit is contained in:
Ariel Mashraki
2020-10-11 19:48:10 +03:00
committed by Ariel Mashraki
parent 8b458ebf13
commit 85c4999472
6 changed files with 42 additions and 13 deletions

View File

@@ -47,7 +47,8 @@ var (
},
}
T2 = &load.Schema{
Name: "T2",
Name: "T2",
Annotations: dict("GQL", map[string]string{"Name": "T2"}),
Fields: []*load.Field{
{Name: "active", Info: &field.TypeInfo{Type: field.TypeBool}},
},
@@ -112,6 +113,7 @@ func TestNewGraph(t *testing.T) {
}
t2 := graph.Nodes[1]
require.Equal(map[string]string{"Name": "T2"}, t2.Annotations["GQL"])
f1, e1 := t2.Fields[0], t2.Edges[0]
require.Equal("bool", f1.Type.String())
require.Equal("active", f1.Name)

View File

@@ -44,6 +44,9 @@ type (
// ForeignKeys are the foreign-keys that resides in the type table.
ForeignKeys []*ForeignKey
foreignKeys map[string]struct{}
// Annotations that were defined for the field in the schema.
// The mapping is from the Annotation.Name() to a JSON decoded object.
Annotations map[string]interface{}
}
// Field holds the information of a type field used for the templates.
@@ -173,6 +176,7 @@ func NewType(c *Config, schema *load.Schema) (*Type, error) {
},
schema: schema,
Name: schema.Name,
Annotations: schema.Annotations,
Fields: make([]*Field, 0, len(schema.Fields)),
fields: make(map[string]*Field, len(schema.Fields)),
foreignKeys: make(map[string]struct{}),

File diff suppressed because one or more lines are too long

View File

@@ -17,13 +17,14 @@ import (
// Schema represents an ent.Schema that was loaded from a complied user package.
type Schema struct {
Name string `json:"name,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"`
Policy []*Position `json:"policy,omitempty"`
Name string `json:"name,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"`
Policy []*Position `json:"policy,omitempty"`
Annotations map[string]interface{} `json:"annotations,omitempty"`
}
// Position describes a position in the schema.
@@ -153,8 +154,12 @@ func NewIndex(idx *index.Descriptor) *Index {
// that can be decoded into the Schema object object.
func MarshalSchema(schema ent.Interface) (b []byte, err error) {
s := &Schema{
Config: schema.Config(),
Name: indirect(reflect.TypeOf(schema)).Name(),
Config: schema.Config(),
Name: indirect(reflect.TypeOf(schema)).Name(),
Annotations: make(map[string]interface{}),
}
for _, at := range schema.Annotations() {
s.Annotations[at.Name()] = at
}
if err := s.loadMixin(schema); err != nil {
return nil, fmt.Errorf("schema %q: %v", s.Name, err)

View File

@@ -12,6 +12,7 @@ import (
"time"
"github.com/facebook/ent"
"github.com/facebook/ent/schema"
"github.com/facebook/ent/schema/edge"
"github.com/facebook/ent/schema/field"
"github.com/facebook/ent/schema/index"
@@ -37,6 +38,12 @@ type User struct {
ent.Schema
}
func (User) Annotations() []schema.Annotation {
return []schema.Annotation{
OrderConfig{FieldName: "type annotations"},
}
}
func (User) Fields() []ent.Field {
return []ent.Field{
field.Int("age"),
@@ -103,6 +110,10 @@ func TestMarshalSchema(t *testing.T) {
schema, err := UnmarshalSchema(buf)
require.NoError(t, err)
require.Equal(t, "User", schema.Name)
require.Len(t, schema.Annotations, 1)
ant := schema.Annotations["order_config"].(map[string]interface{})
require.Equal(t, ant["FieldName"], "type annotations")
require.Len(t, schema.Fields, 8)
require.Equal(t, "age", schema.Fields[0].Name)
require.Equal(t, field.TypeInt, schema.Fields[0].Info.Type)
@@ -111,7 +122,7 @@ func TestMarshalSchema(t *testing.T) {
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{})
ant = schema.Fields[1].Annotations["order_config"].(map[string]interface{})
require.Equal(t, ant["FieldName"], "name")
require.Equal(t, "nillable", schema.Fields[2].Name)