gen

package
v0.0.0-...-cc80eb7 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 License: BSD-3-Clause Imports: 28 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TemplateFunctions = template.FuncMap{

	"quoteWrap":  func(s string) string { return fmt.Sprintf(`"%s"`, s) },
	"replaceAll": strings.ReplaceAll,

	"singular": strmangle.Singular,
	"plural":   strmangle.Plural,

	"titleCase":     strmangle.TitleCase,
	"titleCasePath": titleCasePath,
	"camelCase":     strmangle.CamelCase,

	"join":               func(sep string, slice []string) string { return strings.Join(slice, sep) },
	"joinSlices":         strmangle.JoinSlices,
	"stringMap":          strmangle.StringMap,
	"prefixStringSlice":  strmangle.PrefixStringSlice,
	"containsAny":        strmangle.ContainsAny,
	"generateIgnoreTags": strmangle.GenerateIgnoreTags,

	"makeStringMap": strmangle.MakeStringMap,

	"import":  templateImport,
	"goType":  templateGoType,
	"typesGo": templateTypesGo,

	"setInclude":    strmangle.SetInclude,
	"setComplement": strmangle.SetComplement,

	"whereClause":     WhereClause,
	"whereInClause":   WhereInClause,
	"joinOnClause":    JoinOnClause,
	"joinWhereClause": JoinWhereClause,

	"sqlNames": func(ps []schema.Path) []string {
		res := make([]string, len(ps))
		for i := range ps {
			res[i] = ps[i].SQLName()
		}
		return res
	},
	"modelColumns":      modelColumns,
	"modelPKColumns":    modelPKColumns,
	"modelNonPKColumns": modelNonPKColumns,

	"quotes": func(s string) string {
		d := Config.Dialect
		lq := strmangle.QuoteCharacter(d.LQ)
		rq := strmangle.QuoteCharacter(d.RQ)

		return fmt.Sprintf("%s%s%s", lq, s, rq)
	},
	"schemaModel": func(model string) string {
		d := Config.Dialect
		lq := strmangle.QuoteCharacter(d.LQ)
		rq := strmangle.QuoteCharacter(d.RQ)
		return strmangle.SchemaModel(lq, rq, model)
	},
	"hook": hook,

	"doCompare": func(a, b string, ca, cb *schema.Field) string {
		if ca.Type.GoType().Name == "[]byte" && cb.Type.GoType().Name == "[]byte" {
			return "0 == bytes.Compare(" + a + ", " + b + ")"
		}

		if ca.Nullable == cb.Nullable {
			return a + " == " + b
		}

		if cb.Nullable {
			a, b = b, a
			ca, cb = cb, ca
		}

		f := ca.Type.(schema.NullableType).GoTypeNullField()
		return a + ".Valid && " + a + "." + f + " == " + b
	},
}

TemplateFunctions is a map of all the functions that get passed into the templates. If you wish to pass a new function into your own template, add a function pointer here.

Functions

func AddCommand

func AddCommand(cmds ...*cobra.Command)

func BaseTemplateData

func BaseTemplateData() map[string]interface{}

func JoinOnClause

func JoinOnClause(lq, rq string, table1 string, cols1 []schema.Path, table2 string, cols2 []schema.Path) string

JoinOnClause returns a join on clause

func JoinWhereClause

func JoinWhereClause(lq, rq string, start int, table string, cols []schema.Path) string

JoinWhereClause returns a where clause explicitly specifying the table name

func OnGen

func OnGen(f func())

func OnHook

func OnHook(name string, f HookFunc)

func Run

func Run(items []ConfigItem)

func WhereClause

func WhereClause(lq, rq string, start int, cols []schema.Path) string

WhereClause returns the where clause using start as the $ flag index For example, if start was 2 output would be: "colthing=$2 AND colstuff=$3"

func WhereClauseRepeated

func WhereClauseRepeated(lq, rq string, start int, cols []schema.Path, count int) string

WhereClauseRepeated returns the where clause repeated with OR clause using start as the $ flag index For example, if start was 2 output would be: "(colthing=$2 AND colstuff=$3) OR (colthing=$4 AND colstuff=$5)"

func WhereInClause

func WhereInClause(lq, rq string, table string, cols []schema.Path) string

WhereClause returns the where clause using start as the $ flag index For example, if start was 2 output would be: "colthing=$2 AND colstuff=$3"

func WriteFile

func WriteFile(outFolder string, fileName string, code []byte)

WriteFile writes to the given folder and filename, formatting the buffer given.

func WriteFileDisclaimer

func WriteFileDisclaimer(out *bytes.Buffer)

WriteFileDisclaimer writes the disclaimer at the top with a trailing newline so the package name doesn't get attached to it.

func WriteImports

func WriteImports(out *bytes.Buffer, imports map[string]string)

WriteImports writes the import list, ignores errors since it's to the concrete buffer type which produces none

func WritePackageName

func WritePackageName(out *bytes.Buffer, pkgName string)

WritePackageName writes the package name correctly, ignores errors since it's to the concrete buffer type which produces none

Types

type ConfigItem

type ConfigItem interface {
	ConfigItem(ctx *Context)
}

type ConfigStruct

type ConfigStruct struct {
	Items  []ConfigItem
	Schema *schema.Schema

	Dialect queries.Dialect

	ModelsPackagePath string
	ModelsPackageName string
}
var Config *ConfigStruct

type Configer

type Configer interface {
	ConfigItem
	BunnyConfig(c *ConfigStruct)
}

type Context

type Context struct {
	Schema *schema.Schema
	// contains filtered or unexported fields
}

func (*Context) AddError

func (ctx *Context) AddError(message string, args ...interface{})

func (*Context) Enqueue

func (ctx *Context) Enqueue(order int, fn func())

func (*Context) Error

func (ctx *Context) Error() error

func (*Context) GetType

func (ctx *Context) GetType(name string, where string) schema.Type

func (*Context) Run

func (ctx *Context) Run()

type HookFunc

type HookFunc func(buf *bytes.Buffer, data map[string]interface{}, args ...interface{})

type Plugin

type Plugin interface {
	ConfigItem
	BunnyPlugin()
}

type TemplateList

type TemplateList struct {
	*template.Template
}

func LoadTemplate

func LoadTemplate(pkg string, path string) (*TemplateList, error)

LoadTemplate loads a single template file

func LoadTemplates

func LoadTemplates(pkg string, path string) (*TemplateList, error)

LoadTemplates loads all of the template files in the specified directory.

func MustLoadTemplate

func MustLoadTemplate(pkg string, path string) *TemplateList

func MustLoadTemplates

func MustLoadTemplates(pkg string, path string) *TemplateList

func (*TemplateList) Execute

func (t *TemplateList) Execute(data map[string]interface{}, filename string)

func (*TemplateList) ExecuteBuf

func (t *TemplateList) ExecuteBuf(data map[string]interface{}, buf *bytes.Buffer)

func (*TemplateList) ExecuteSingleton

func (t *TemplateList) ExecuteSingleton(data map[string]interface{})

func (TemplateList) Templates

func (t TemplateList) Templates() []string

Templates returns the name of all the templates defined in the template list

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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