gen

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

This version is based on the original https://github.com/ent/ent/blob/master/entc/gen/func.go, which is a modified version of code from ent/ent

Index

Constants

This section is empty.

Variables

View Source
var (
	CoreTemplates = []CoreTemplate{
		{
			Name:     "client",
			FilePath: "client.go",
		},
		{
			Name:     "where",
			FilePath: "where.go",
		},
		{
			Name:     "error",
			FilePath: "error.go",
		},
	}
	EntityTemplates = []EntityTemplate{
		{
			Name:     "model",
			FilePath: pkgf("%s.go"),
		},
	}
)
View Source
var (
	// Funcs are the predefined template
	// functions used by the codegen.
	Funcs = template.FuncMap{
		"hasSuffix": strings.HasSuffix,
		"lower":     strings.ToLower,
		"pascal":    pascal,
		"plural":    plural,
		"replace":   strings.ReplaceAll,
		"singular":  rules.Singularize,
		"snake":     snake,
		"split":     strings.Split,
		"trim":      strings.Trim,
		"upper":     strings.ToUpper,
	}
)

Functions

func Gen

func Gen(db *sql.DB, outputDir string) error

func MustParse

func MustParse(t *template.Template, err error) *template.Template

Types

type Config

type Config struct {
	// Header allows users to provide an optional header signature for
	// the generated files. It defaults to the standard 'go generate'
	// format: '// Code generated by not, DO NOT EDIT.'.
	Header string

	// Target defines the filepath for the target directory that
	// holds the generated code. For example, "./project/not".
	//
	// By default, 'not postgres generate ...' uses './not' as a
	// target directory.
	Target string

	// Package defines the Go package path of the target directory
	// mentioned above. For example, "github.com/org/project/not".
	//
	// By default, 'not generate' uses "<project>/not" as a default package.
	Package string
}

type CoreTemplate

type CoreTemplate struct {
	// Name of the template.
	Name string

	// FilePath is the path of the file to be generated.
	FilePath string
}

type DataModel

type DataModel struct {
	*Config

	Entities []*Entity
}

func (*DataModel) AddEntity

func (m *DataModel) AddEntity(entity *Entity)

func (*DataModel) IntrospectDatabase

func (dm *DataModel) IntrospectDatabase(db *sql.DB) error

type Entity

type Entity struct {
	*Config

	// Name is the name of the entity in the database schema.
	Name string

	// Fields holds all the primitive fields of this type.
	Fields []*Field
}

Entity is a struct that represents a database table.

func (Entity) ClientName

func (t Entity) ClientName() string

ClientName returns the struct name denoting the client of this type.

func (Entity) OptionsName

func (t Entity) OptionsName() string

OptionsName returns the name of the options struct.

func (Entity) PackageDir

func (t Entity) PackageDir() string

PackageDir returns the name of the package directory.

func (Entity) Receiver

func (t Entity) Receiver() string

Receiver returns the receiver name of this node. It makes sure the receiver names doesn't conflict with import names.

func (Entity) TableName

func (t Entity) TableName() string

Table returns SQL table name of the entity.

func (Entity) WhereInputName

func (t Entity) WhereInputName() string

WhereInputName returns the name of the where input struct.

type EntityTemplate

type EntityTemplate struct {
	// Name of the template.
	Name string

	// FilePath is a function that returns the path of the file to be generated.
	FilePath func(*Entity) string
}

type Field

type Field struct {
	// Name is the name of the field in the database schema.
	Name string

	// ColumnName is the name of the column in the database.
	ColumnName string

	// Type holds the type information of the field.
	Type string
}

Field is a struct that represents a database column.

Jump to

Keyboard shortcuts

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