packageschemaimport ("errors""regexp""strings""time""github.com/facebookincubator/ent""github.com/facebookincubator/ent/schema/field")// Group schema.typeGroupstruct {ent.Schema}// Fields of the group.func (Group) Fields() []ent.Field {return []ent.Field{ field.String("name").Match(regexp.MustCompile("[a-zA-Z_]+$")).Validate(func(s string) error {if strings.ToLower(s) == s {return errors.New("group name must begin with uppercase") }returnnil }), }}
// User schema.typeUserstruct {ent.Schema}// Fields of the user.func (User) Fields() []ent.Field {return []ent.Field{ field.Int("age").Optional().Nillable(), field.String("name").StructTag(`gqlgen:"gql_name"`), }}
生成的实体如下:
// User is the model entity for the User schema.typeUserstruct {// Age holds the value of the "age" field. Age *int`json:"age,omitempty"`// Name holds the value of the "name" field. Name string`json:"name,omitempty" gqlgen:"gql_name"`}
{{ define "model/fields/additional" }}
{{- if eq $.Name "User" }}
// StaticField defined by template.
StaticField string `json:"static,omitempty"`
{{- end }}
{{ end }}
生成的实体如下:
// User is the model entity for the User schema.typeUserstruct {// Age holds the value of the "age" field. Age *int`json:"age,omitempty"`// Name holds the value of the "name" field. Name string`json:"name,omitempty" gqlgen:"gql_name"`// StaticField defined by template. StaticField string`json:"static,omitempty"`}
// User schema.typeUserstruct {ent.Schema}// Fields of the user.func (User) Fields() []ent.Field {return []ent.Field{ field.String("password").Sensitive(), }}