mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
schema/mixin: adding mixin.Annotate helper which adds anootations to mixin fields (#625)
Signed-off-by: Alex Snast <alexsn@fb.com>
This commit is contained in:
@@ -79,3 +79,22 @@ func (Time) Fields() []ent.Field {
|
||||
|
||||
// time mixin must implement `Mixin` interface.
|
||||
var _ ent.Mixin = (*Time)(nil)
|
||||
|
||||
// Annotate adds field annotations to underlying mixin fields.
|
||||
func Annotate(m ent.Mixin, annotations ...field.Annotation) ent.Mixin {
|
||||
return annotator{Mixin: m, annotations: annotations}
|
||||
}
|
||||
|
||||
type annotator struct {
|
||||
ent.Mixin
|
||||
annotations []field.Annotation
|
||||
}
|
||||
|
||||
func (a annotator) Fields() []ent.Field {
|
||||
fields := a.Mixin.Fields()
|
||||
for _, f := range fields {
|
||||
desc := f.Descriptor()
|
||||
desc.Annotations = append(desc.Annotations, a.annotations...)
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package mixin
|
||||
package mixin_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/facebookincubator/ent/schema/field"
|
||||
"github.com/facebookincubator/ent/schema/mixin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -14,7 +16,7 @@ import (
|
||||
func TestTimeMixin(t *testing.T) {
|
||||
t.Run("Create", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
fields := CreateTime{}.Fields()
|
||||
fields := mixin.CreateTime{}.Fields()
|
||||
require.Len(t, fields, 1)
|
||||
desc := fields[0].Descriptor()
|
||||
assert.Equal(t, "create_time", desc.Name)
|
||||
@@ -24,7 +26,7 @@ func TestTimeMixin(t *testing.T) {
|
||||
})
|
||||
t.Run("Update", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
fields := UpdateTime{}.Fields()
|
||||
fields := mixin.UpdateTime{}.Fields()
|
||||
require.Len(t, fields, 1)
|
||||
desc := fields[0].Descriptor()
|
||||
assert.Equal(t, "update_time", desc.Name)
|
||||
@@ -34,9 +36,31 @@ func TestTimeMixin(t *testing.T) {
|
||||
})
|
||||
t.Run("Compose", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
fields := Time{}.Fields()
|
||||
fields := mixin.Time{}.Fields()
|
||||
require.Len(t, fields, 2)
|
||||
assert.Equal(t, "create_time", fields[0].Descriptor().Name)
|
||||
assert.Equal(t, "update_time", fields[1].Descriptor().Name)
|
||||
})
|
||||
}
|
||||
|
||||
type annotation string
|
||||
|
||||
func (annotation) Name() string { return "" }
|
||||
|
||||
func TestAnnotate(t *testing.T) {
|
||||
annotations := []field.Annotation{
|
||||
annotation("foo"),
|
||||
annotation("bar"),
|
||||
annotation("baz"),
|
||||
}
|
||||
fields := mixin.Annotate(
|
||||
mixin.Time{}, annotations...,
|
||||
).Fields()
|
||||
for _, f := range fields {
|
||||
desc := f.Descriptor()
|
||||
require.Len(t, desc.Annotations, len(annotations))
|
||||
for i := range desc.Annotations {
|
||||
assert.Equal(t, annotations[i], desc.Annotations[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user