mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc: add global annotations option
This commit is contained in:
committed by
Ariel Mashraki
parent
8d7bb7fad6
commit
30c947546f
@@ -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)
|
||||
|
||||
@@ -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 }}
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user