flc

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package flc provides an interface for interacting with flc (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, config *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>/fluent/schema", the base directory for codegen will be "<project>/fluent".

If no storage driver provided by option, SQL driver will be used.

flc.Generate("./fluent/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 fluent website: https://github.com/usalko/fluent/docs/templates/#annotations.

type DefaultExtension

type DefaultExtension struct{}

DefaultExtension is the default implementation for flc.Extension.

Embedding this type allow third-party packages to create extensions without implementing all methods.

type Extension struct {
	flc.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 DependencyOption

type DependencyOption func(*gen.Dependency) error

DependencyOption allows configuring optional dependencies using functional options.

func DependencyName

func DependencyName(name string) DependencyOption

DependencyName sets the struct field and the option name of the dependency in the generated builders.

func DependencyType

func DependencyType(v any) DependencyOption

DependencyType sets the type of the struct field in the generated builders for the configured dependency.

func DependencyTypeInfo

func DependencyTypeInfo(t *field.TypeInfo) DependencyOption

DependencyTypeInfo is similar to DependencyType, but allows setting the field.TypeInfo explicitly.

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 flc.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 := fluent_gql.NewExtension(
	 fluent_gql.WithConfig("../gqlgen.yml"),
	 fluent_gql.WithSchema("../schema.graphql"),
)
if err != nil {
	log.Fatalf("creating graphql extension: %v", err)
}
err = flc.Generate("./schema", &gen.Config{
	Templates: entswag.Templates,
}, flc.Extensions(ex))
if err != nil {
	log.Fatalf("running fluent 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 BuildFlags

func BuildFlags(flags ...string) Option

BuildFlags appends the given build flags to the codegen config.

func BuildTags

func BuildTags(tags ...string) Option

BuildTags appends the given build tags as build flags to the codegen config.

func Dependency

func Dependency(opts ...DependencyOption) Option

Dependency allows configuring optional dependencies as struct fields on the generated builders. For example:

opts := []flc.Option{
	flc.Dependency(
		flc.DependencyType(&http.Client{}),
	),
	flc.Dependency(
		flc.DependencyName("DB"),
		flc.DependencyType(&sql.DB{}),
	)
}
if err := flc.Generate("./fluent/path", &gen.Config{}, opts...); err != nil {
	log.Fatalf("running fluent codegen: %v", err)
}

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
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 module
Package load is the interface for loading an fluent/schema package into a Go program.
Package load is the interface for loading an fluent/schema package into a Go program.

Jump to

Keyboard shortcuts

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