Extensions
Defining a New Extension
All extension’s must implement the Extension interface:
To simplify the development of new extensions, developers can embed to create extensions without implementing all methods:
package hello
// GreetExtension implements entc.Extension.
type GreetExtension struct {
entc.DefaultExtension
}
Ent supports adding that will be rendered during code generation. To bundle such external templates on an extension, implement the Templates
method:
templates/greet.tmpl
{{/* 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 }}
{{ template "header" $ }}
{{ range $n := $.Nodes }}
{{ $receiver := $n.Receiver }}
func ({{ $receiver }} *{{ $n.Name }}) Greet() string {
return "Hello, {{ $n.Name }}"
}
{{ end }}
{{ end }}
Adding Global Annotations
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
word GreetingWord
}
Next, implement the Annotations
method:
Now, from within your templates you can access the GreetingWord
annotation:
func ({{ $receiver }} *{{ $n.Name }}) Greet() string {
return "{{ $.Annotations.GreetingWord }}, {{ $n.Name }}"
}
The entc package provides an option to add a list of hooks (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:
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 {
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
ent/entc.go
elk
is an extension that generates RESTful API endpoints from Ent schemas. The extension generates HTTP CRUD handlers from the Ent schema, as well as an OpenAPI JSON file. By using it, you can easily build a RESTful HTTP server for your application. Please note, thatelk
has been discontinued in favor ofentoas
. An implementation generator is in the works.
Read this blog post on how to work withelk
, and on how to generate an OpenAPI Specification.entoas
is an extension that originates fromelk
and was ported into its own extension and is now the official generator for and opinionated OpenAPI Specification document. You can use this to rapidly develop and document a RESTful HTTP server. There will be a new extension released soon providing a generated implementation integrating for the document provided byentoas
usingent
.entgql
This extension helps users build servers from Ent schemas.entgql
integrates with gqlgen, a popular, schema-first Go library for building GraphQL servers. The extension includes the generation of type-safe GraphQL filters, which enable users to effortlessly map GraphQL queries to Ent queries.
Follow to get started.