Documentation ¶
Overview ¶
Package entc provides an interface for interacting with entc (ent codegen) as a package rather than an executable.
Index ¶
- func Generate(schemaPath string, cfg *gen.Config, options ...Option) error
- func LoadGraph(schemaPath string, cfg *gen.Config) (*gen.Graph, error)
- type Annotation
- type DefaultExtension
- type DependencyOption
- type Extension
- type Option
- func Annotations(annotations ...Annotation) Option
- func BuildFlags(flags ...string) Option
- func BuildTags(tags ...string) Option
- func Dependency(opts ...DependencyOption) Option
- func Extensions(extensions ...Extension) Option
- func FeatureNames(names ...string) Option
- func Storage(typ string) Option
- func TemplateDir(path string) Option
- func TemplateFiles(filenames ...string) Option
- func TemplateGlob(pattern string) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Generate ¶
Generate runs the codegen on the schema path. The default target directory for the assets, is one directory above the schema path. Hence, if the schema package resides in "<project>/ent/schema", the base directory for codegen will be "<project>/ent".
If no storage driver provided by option, SQL driver will be used.
entc.Generate("./ent/path", &gen.Config{ Header: "// Custom header", IDType: &field.TypeInfo{Type: field.TypeInt}, })
Types ¶
type Annotation ¶ added in v0.9.0
type Annotation = schema.Annotation
Annotation is used to attach arbitrary metadata to the schema objects in codegen. Unlike schema annotations, being serializable to JSON raw value is not mandatory.
Template extensions can retrieve this metadata and use it inside their execution. Read more about it in ent website: https://entgo.io/docs/templates/#annotations.
type DefaultExtension ¶ added in v0.9.0
type DefaultExtension struct{}
DefaultExtension is the default implementation for entc.Extension.
Embedding this type allow third-party packages to create extensions without implementing all methods.
type Extension struct { entc.DefaultExtension }
func (DefaultExtension) Annotations ¶ added in v0.9.0
func (DefaultExtension) Annotations() []Annotation
Annotations of the extensions.
func (DefaultExtension) Hooks ¶ added in v0.9.0
func (DefaultExtension) Hooks() []gen.Hook
Hooks of the extensions.
func (DefaultExtension) Options ¶ added in v0.9.0
func (DefaultExtension) Options() []Option
Options of the extensions.
func (DefaultExtension) Templates ¶ added in v0.9.0
func (DefaultExtension) Templates() []*gen.Template
Templates of the extensions.
type DependencyOption ¶ added in v0.10.0
type DependencyOption func(*gen.Dependency) error
DependencyOption allows configuring optional dependencies using functional options.
func DependencyName ¶ added in v0.10.0
func DependencyName(name string) DependencyOption
DependencyName sets the struct field and the option name of the dependency in the generated builders.
func DependencyType ¶ added in v0.10.0
func DependencyType(v any) DependencyOption
DependencyType sets the type of the struct field in the generated builders for the configured dependency.
func DependencyTypeInfo ¶ added in v0.10.0
func DependencyTypeInfo(t *field.TypeInfo) DependencyOption
DependencyTypeInfo is similar to DependencyType, but allows setting the field.TypeInfo explicitly.
type Extension ¶ added in v0.9.0
type Extension interface { // Hooks holds an optional list of Hooks to apply // on the graph before/after the code-generation. Hooks() []gen.Hook // Annotations injects global annotations to the gen.Config object that // can be accessed globally in all templates. Unlike schema annotations, // being serializable to JSON raw value is not mandatory. // // {{- with $.Config.Annotations.GQL }} // {{/* Annotation usage goes here. */}} // {{- end }} // Annotations() []Annotation // Templates specifies a list of alternative templates // to execute or to override the default. Templates() []*gen.Template // Options specifies a list of entc.Options to evaluate on // the gen.Config before executing the code generation. Options() []Option }
Extension describes an Ent code generation extension that allows customizing the code generation and integrate with other tools and libraries (e.g. GraphQL, gRPC, OpenAPI) by registering hooks, templates and global annotations in one simple call.
ex, err := entgql.NewExtension( entgql.WithConfig("../gqlgen.yml"), entgql.WithSchema("../schema.graphql"), ) if err != nil { log.Fatalf("creating graphql extension: %v", err) } err = entc.Generate("./schema", &gen.Config{ Templates: entswag.Templates, }, entc.Extensions(ex)) if err != nil { log.Fatalf("running ent codegen: %v", err) }
type Option ¶
Option allows for managing codegen configuration using functional options.
func Annotations ¶ added in v0.8.0
func Annotations(annotations ...Annotation) Option
Annotations appends the given annotations to the codegen config.
func BuildFlags ¶ added in v0.11.4
BuildFlags appends the given build flags to the codegen config.
func BuildTags ¶ added in v0.11.4
BuildTags appends the given build tags as build flags to the codegen config.
func Dependency ¶ added in v0.10.0
func Dependency(opts ...DependencyOption) Option
Dependency allows configuring optional dependencies as struct fields on the generated builders. For example:
opts := []entc.Option{ entc.Dependency( entc.DependencyType(&http.Client{}), ), entc.Dependency( entc.DependencyName("DB"), entc.DependencyType(&sql.DB{}), ) } if err := entc.Generate("./ent/path", &gen.Config{}, opts...); err != nil { log.Fatalf("running ent codegen: %v", err) }
func Extensions ¶ added in v0.9.0
Extensions evaluates the list of Extensions on the gen.Config.
func FeatureNames ¶
FeatureNames enables sets of features by their names.
func TemplateDir ¶
TemplateDir parses the template definitions from the files in the directory and associates the resulting templates with codegen templates.
func TemplateFiles ¶
TemplateFiles parses the named files and associates the resulting templates with codegen templates.
func TemplateGlob ¶
TemplateGlob parses the template definitions from the files identified by the pattern and associates the resulting templates with codegen templates.
Directories ¶
Path | Synopsis |
---|---|
Package gen is the interface for generating loaded schemas into a Go package.
|
Package gen is the interface for generating loaded schemas into a Go package. |
integration
|
|
ent/schema/dir
Package schemadir is used as an example to test cases where package-name is not equal to the package-path (schemadir <> dir).
|
Package schemadir is used as an example to test cases where package-name is not equal to the package-path (schemadir <> dir). |
Package load is the interface for loading an ent/schema package into a Go program.
|
Package load is the interface for loading an ent/schema package into a Go program. |