entc: add global annotations option

This commit is contained in:
Ariel Mashraki
2021-04-08 21:12:23 +03:00
committed by Ariel Mashraki
parent 8d7bb7fad6
commit 30c947546f
7 changed files with 65 additions and 6 deletions

View File

@@ -14,6 +14,7 @@ import (
"entgo.io/ent/entc"
"entgo.io/ent/entc/gen"
"entgo.io/ent/schema"
)
func main() {
@@ -22,6 +23,7 @@ func main() {
// rest are provided with the `Templates` option.
opts := []entc.Option{
entc.TemplateFiles("template/stringer.tmpl"),
entc.Annotations(Annotation{StructTag: "rql"}),
}
err := entc.Generate("./schema", &gen.Config{
Header: `
@@ -69,3 +71,17 @@ func TagFields(name string) gen.Hook {
})
}
}
const AnnotationName = "RQL"
// Annotation defines a custom annotation
// to be inject globally to all templates.
type Annotation struct {
StructTag string
}
func (Annotation) Name() string {
return AnnotationName
}
var _ schema.Annotation = (*Annotation)(nil)

View File

@@ -7,6 +7,6 @@
{{- if eq $.Name "User" }}
{{- /* A usage of an external function. */ -}}
// StaticField defined by templates (titled {{ title "staticField" }}).
StaticField string `json:"static_field,omitempty"`
StaticField string `json:"static_field,omitempty"{{ with $.Config.Annotations.RQL }} {{ .StructTag }}:"static_field"{{ end }}`
{{- end }}
{{ end }}

View File

@@ -26,7 +26,7 @@ type User struct {
Age int `json:"age"`
// StaticField defined by templates (titled STATICFIELD).
StaticField string `json:"static_field,omitempty"`
StaticField string `json:"static_field,omitempty" rql:"static_field"`
}
// scanValues returns the types for scanning values from sql.Rows.