schema: shared annotation interface for schema objects (#813)

This commit is contained in:
Ariel Mashraki
2020-10-03 16:51:00 +03:00
committed by GitHub
parent a56b3b24d0
commit 1b5f51b827
9 changed files with 74 additions and 67 deletions

View File

@@ -6,28 +6,22 @@ package edge
import (
"reflect"
)
// Annotation is used to attach arbitrary metadata to the edge object in codegen.
// The object must be serializable to JSON raw value (e.g. struct, map or slice).
// Template extensions can retrieve this metadata and use it inside their templates.
type Annotation interface {
// Name defines the name of the annotation to be retrieved by the codegen.
Name() string
}
"github.com/facebook/ent/schema"
)
// A Descriptor for edge configuration.
type Descriptor struct {
Tag string // struct tag.
Type string // edge type.
Name string // edge name.
RefName string // ref name; inverse only.
Ref *Descriptor // edge reference; to/from of the same type.
Unique bool // unique edge.
Inverse bool // inverse edge.
Required bool // required on creation.
StorageKey *StorageKey // optional storage-key configuration.
Annotations []Annotation // edge annotations.
Tag string // struct tag.
Type string // edge type.
Name string // edge name.
RefName string // ref name; inverse only.
Ref *Descriptor // edge reference; to/from of the same type.
Unique bool // unique edge.
Inverse bool // inverse edge.
Required bool // required on creation.
StorageKey *StorageKey // optional storage-key configuration.
Annotations []schema.Annotation // edge annotations.
}
// To defines an association edge between two vertices.
@@ -105,7 +99,7 @@ func (b *assocBuilder) StorageKey(opts ...StorageOption) *assocBuilder {
// FieldName: "Pets",
// })
//
func (b *assocBuilder) Annotations(annotations ...Annotation) *assocBuilder {
func (b *assocBuilder) Annotations(annotations ...schema.Annotation) *assocBuilder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
@@ -161,7 +155,7 @@ func (b *inverseBuilder) Comment(string) *inverseBuilder {
// FieldName: "Owner",
// })
//
func (b *inverseBuilder) Annotations(annotations ...Annotation) *inverseBuilder {
func (b *inverseBuilder) Annotations(annotations ...schema.Annotation) *inverseBuilder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}

View File

@@ -7,12 +7,12 @@ package edge_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/facebook/ent"
"github.com/facebook/ent/schema"
"github.com/facebook/ent/schema/edge"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEdge(t *testing.T) {
@@ -100,16 +100,16 @@ func TestAnnotations(t *testing.T) {
to := edge.To("user", User.Type).
Annotations(GQL{Field: "to"}).
Descriptor()
require.Equal(t, []edge.Annotation{GQL{Field: "to"}}, to.Annotations)
require.Equal(t, []schema.Annotation{GQL{Field: "to"}}, to.Annotations)
from := edge.From("user", User.Type).
Annotations(GQL{Field: "from"}).
Descriptor()
require.Equal(t, []edge.Annotation{GQL{Field: "from"}}, from.Annotations)
require.Equal(t, []schema.Annotation{GQL{Field: "from"}}, from.Annotations)
bidi := edge.To("following", User.Type).
Annotations(GQL{Field: "to"}).
From("followers").
Annotations(GQL{Field: "from"}).
Descriptor()
require.Equal(t, []edge.Annotation{GQL{Field: "from"}}, bidi.Annotations)
require.Equal(t, []edge.Annotation{GQL{Field: "to"}}, bidi.Ref.Annotations)
require.Equal(t, []schema.Annotation{GQL{Field: "from"}}, bidi.Annotations)
require.Equal(t, []schema.Annotation{GQL{Field: "to"}}, bidi.Ref.Annotations)
}