schema/field: add annotations for overriding field struct-tags

This commit is contained in:
Ariel Mashraki
2020-10-11 22:15:21 +03:00
committed by Ariel Mashraki
parent f1a841d235
commit ab9aa1fa45
7 changed files with 64 additions and 9 deletions

View File

@@ -20,13 +20,13 @@ import (
type Card struct {
config `json:"-"`
// ID of the ent.
ID int `json:"id,omitempty"`
ID int `json:"-"`
// CreateTime holds the value of the "create_time" field.
CreateTime time.Time `json:"create_time,omitempty"`
// UpdateTime holds the value of the "update_time" field.
UpdateTime time.Time `json:"update_time,omitempty"`
// Number holds the value of the "number" field.
Number string `json:"number,omitempty"`
Number string `json:"-"`
// Name holds the value of the "name" field.
Name string `json:"name,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.

View File

@@ -20,6 +20,10 @@ type Card struct {
func (Card) Annotations() []schema.Annotation {
return []schema.Annotation{
field.StructTag(
"id", `json:"-"`,
"number", `json:"-"`,
),
edge.StructTag(`json:"card_edges" mashraki:"edges"`),
}
}

View File

@@ -19,13 +19,13 @@ import (
type Card struct {
config `json:"-"`
// ID of the ent.
ID string `json:"id,omitempty"`
ID string `json:"-"`
// CreateTime holds the value of the "create_time" field.
CreateTime time.Time `json:"create_time,omitempty"`
// UpdateTime holds the value of the "update_time" field.
UpdateTime time.Time `json:"update_time,omitempty"`
// Number holds the value of the "number" field.
Number string `json:"number,omitempty"`
Number string `json:"-"`
// Name holds the value of the "name" field.
Name string `json:"name,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.

View File

@@ -221,6 +221,12 @@ func Sanity(t *testing.T, client *ent.Client) {
fi, ok := reflect.TypeOf(ent.Card{}).FieldByName("Edges")
require.True(ok)
require.NotEmpty(fi.Tag.Get("mashraki"))
fi, ok = reflect.TypeOf(ent.Card{}).FieldByName("ID")
require.True(ok)
require.Equal("-", fi.Tag.Get("json"))
fi, ok = reflect.TypeOf(ent.Card{}).FieldByName("Number")
require.True(ok)
require.Equal("-", fi.Tag.Get("json"))
}
func Clone(t *testing.T, client *ent.Client) {