entc

package
v0.11.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2022 License: Apache-2.0 Imports: 13 Imported by: 148

Documentation

Overview

Package entc provides an interface for interacting with entc (ent codegen) as a package rather than an executable.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(schemaPath string, cfg *gen.Config, options ...Option) error

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},
})

func LoadGraph

func LoadGraph(schemaPath string, cfg *gen.Config) (*gen.Graph, error)

LoadGraph loads the schema package from the given schema path, and constructs a *gen.Graph.

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 interface{}) 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

type Option func(*gen.Config) error

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 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

func Extensions(extensions ...Extension) Option

Extensions evaluates the list of Extensions on the gen.Config.

func FeatureNames

func FeatureNames(names ...string) Option

FeatureNames enables sets of features by their names.

func Storage

func Storage(typ string) Option

Storage sets the storage-driver type to support by the codegen.

func TemplateDir

func TemplateDir(path string) Option

TemplateDir parses the template definitions from the files in the directory and associates the resulting templates with codegen templates.

func TemplateFiles

func TemplateFiles(filenames ...string) Option

TemplateFiles parses the named files and associates the resulting templates with codegen templates.

func TemplateGlob

func TemplateGlob(pattern string) Option

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
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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL