entc

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

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) (err 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

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://github.com/unng-lab/docs/templates/#annotations.

type DefaultExtension

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

func (DefaultExtension) Annotations() []Annotation

Annotations of the extensions.

func (DefaultExtension) Hooks

func (DefaultExtension) Hooks() []gen.Hook

Hooks of the extensions.

func (DefaultExtension) Options

func (DefaultExtension) Options() []Option

Options of the extensions.

func (DefaultExtension) Templates

func (DefaultExtension) Templates() []*gen.Template

Templates of the extensions.

type Extension

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

func Annotations(annotations ...Annotation) Option

Annotations appends the given annotations to the codegen config.

func Extensions

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
gen
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.
internal
Code generated for package internal by go-bindata DO NOT EDIT.
Code generated for package internal by go-bindata DO NOT EDIT.
integration
ent
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.
internal
Package internal Code generated by go-bindata.
Package internal Code generated by go-bindata.

Jump to

Keyboard shortcuts

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