schema/mixin: add support for mixed-in annotations

This commit is contained in:
Ariel Mashraki
2020-10-11 21:06:10 +03:00
committed by Ariel Mashraki
parent 85c4999472
commit c8a6527612
5 changed files with 38 additions and 8 deletions

View File

@@ -30,14 +30,34 @@ func (OrderConfig) Name() string {
return "order_config"
}
func (o *OrderConfig) MarshalJSON() ([]byte, error) {
return json.Marshal(*o)
type IDConfig struct {
TagName string
}
func (IDConfig) Name() string {
return "id_config"
}
type AnnotationMixin struct {
mixin.Schema
}
func (AnnotationMixin) Annotations() []schema.Annotation {
return []schema.Annotation{
IDConfig{TagName: "id tag"},
}
}
type User struct {
ent.Schema
}
func (User) Mixin() []ent.Mixin {
return []ent.Mixin{
AnnotationMixin{},
}
}
func (User) Annotations() []schema.Annotation {
return []schema.Annotation{
OrderConfig{FieldName: "type annotations"},
@@ -110,7 +130,7 @@ 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)
require.Len(t, schema.Annotations, 2)
ant := schema.Annotations["order_config"].(map[string]interface{})
require.Equal(t, ant["FieldName"], "type annotations")