Extensions
Defining a New Extension
All extension’s must implement the interface:
To simplify the development of new extensions, developers can embed entc.DefaultExtension to create extensions without implementing all methods:
package hello
// GreetExtension implements entc.Extension.
type GreetExtension {
entc.DefaultExtension
}
Ent supports adding that will be rendered during code generation. To bundle such external templates on an extension, implement the Templates
method:
{{/* Tell Intellij/GoLand to enable the autocompletion based on the *gen.Graph type. */}}
{{/* gotype: entgo.io/ent/entc/gen.Graph */}}
{{ define "greet" }}
{{/* Add the base header for the generated file */}}
{{ $pkg := base $.Config.Package }}
{{/* Loop over all nodes and add the Greet method */}}
{{ range $n := $.Nodes }}
{{ $receiver := $n.Receiver }}
return "Hello, {{ $n.Name }}"
}
{{ end }}
{{ end }}
Adding Global Annotations
Annotations are a convenient way to supply users of our extension with an API to modify the behavior of code generation. To add annotations to our extension, implement the Annotations
method. Let’s say in our GreetExtension
we want to provide users with the ability to configure the greeting word in the generated code:
// GreetingWord implements entc.Annotation.
type GreetingWord string
// Name of the annotation. Used by the codegen templates.
func (GreetingWord) Name() string {
return "GreetingWord"
}
Then add it to the GreetExtension
struct:
type GreetExtension struct {
entc.DefaultExtension
}
Next, implement the Annotations
method:
return "{{ $.Annotations.GreetingWord }}, {{ $n.Name }}"
}
The entc package provides an option to add a list of (middlewares) to the code-generation phase. This option is ideal for adding custom validators for the schema, or for generating additional assets using the graph schema. To bundle code generation hooks with your extension, implement the Hooks
method:
func (s *GreetExtension) Hooks() []gen.Hook {
return []gen.Hook{
DisallowTypeName("Shalom"),
}
}
// DisallowTypeName ensures there is no ent.Schema with the given name in the graph.
func DisallowTypeName(name string) gen.Hook {
return func(next gen.Generator) gen.Generator {
return gen.GenerateFunc(func(g *gen.Graph) error {
for _, node := range g.Nodes {
if node.Name == name {
return fmt.Errorf("entc: validation failed, type named %q not allowed", name)
}
}
return next.Generate(g)
})
}
Using an Extension in Code Generation
To use an extension in our code-generation configuration, use , a helper method that returns an entc.Option
that applies our chosen extensions:
ent/entc.go