golang

package
v0.0.0-...-78c55be Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2022 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateCompositeType

func CreateCompositeType(
	pkgPath string,
	pgt pg.CompositeType,
	resolver TypeResolver,
	caser casing.Caser,
) (gotype.CompositeType, error)

CreateCompositeType creates a struct to represent a Postgres composite type. The type is rooted under pkgPath.

func Generate

func Generate(opts GenerateOptions, queryFiles []codegen.QueryFile) error

Generate emits generated Go files for each of the queryFiles.

func NameArrayInitFunc

func NameArrayInitFunc(typ gotype.ArrayType) string

NameArrayInitFunc returns the name for the function that creates an initialized pgtype.ValueTranscoder for the array type that's used to encode query parameters. This function is only necessary for top-level types. Descendant types use the raw functions, named by NameArrayRawFunc.

func NameArrayRawFunc

func NameArrayRawFunc(typ gotype.ArrayType) string

NameArrayRawFunc returns the function name that create the []interface{} array for the array type so that we can use it with a parent encoder function, like NameCompositeInitFunc, in the pgtype.Value Set call.

func NameArrayTranscoderFunc

func NameArrayTranscoderFunc(typ gotype.ArrayType) string

NameArrayTranscoderFunc returns the function name that creates a pgtype.ValueTranscoder for the array type that's used to decode rows returned by Postgres.

func NameCompositeInitFunc

func NameCompositeInitFunc(typ gotype.CompositeType) string

NameCompositeInitFunc returns the name of the function that creates an initialized pgtype.ValueTranscoder for the composite type used as a query parameters. This function is only necessary for top-level types. Descendant types use the raw functions, named by NameCompositeRawFunc.

func NameCompositeRawFunc

func NameCompositeRawFunc(typ gotype.CompositeType) string

NameCompositeRawFunc returns the function name that create the []interface{} array for the composite type so that we can use it with a parent encoder function, like NameCompositeInitFunc, in the pgtype.Value Set call.

func NameCompositeTranscoderFunc

func NameCompositeTranscoderFunc(typ gotype.CompositeType) string

NameCompositeTranscoderFunc returns the function name that creates a pgtype.ValueTranscoder for the composite type that's used to decode rows returned by Postgres.

func NameEnumTranscoderFunc

func NameEnumTranscoderFunc(typ gotype.EnumType) string

Types

type ArrayInitDeclarer

type ArrayInitDeclarer struct {
	// contains filtered or unexported fields
}

ArrayInitDeclarer declares a new Go function that creates an *initialized* pgtype.ValueTranscoder for the Postgres type represented by the gotype.ArrayType.

We need a separate declarer from ArrayTranscoderDeclarer because setting a pgtype.ValueTranscoder is much less flexible on the values allowed compared to AssignTo. We can assign a pgtype.ArrayType to any struct but we can only set it with an [][]interface{} if the array elements are composite types.

Additionally, we need to use the Postgres text format exclusively because the Postgres binary format requires the type OID but pggen doesn't necessarily know the OIDs of the types. The text format, however, doesn't require OIDs.

func NewArrayInitDeclarer

func NewArrayInitDeclarer(typ gotype.ArrayType) ArrayInitDeclarer

func (ArrayInitDeclarer) Declare

func (a ArrayInitDeclarer) Declare(string) (string, error)

func (ArrayInitDeclarer) DedupeKey

func (a ArrayInitDeclarer) DedupeKey() string

type ArrayRawDeclarer

type ArrayRawDeclarer struct {
	// contains filtered or unexported fields
}

ArrayRawDeclarer declares a new Go function that returns all fields as a generic array: []interface{}. Necessary because we can only set pgtype.ArrayType from a []interface{}.

func NewArrayRawDeclarer

func NewArrayRawDeclarer(typ gotype.ArrayType) ArrayRawDeclarer

func (ArrayRawDeclarer) Declare

func (a ArrayRawDeclarer) Declare(string) (string, error)

func (ArrayRawDeclarer) DedupeKey

func (a ArrayRawDeclarer) DedupeKey() string

type ArrayTranscoderDeclarer

type ArrayTranscoderDeclarer struct {
	// contains filtered or unexported fields
}

ArrayTranscoderDeclarer declares a new Go function that creates a pgtype.ValueTranscoder decoder for an array Postgres type.

func NewArrayDecoderDeclarer

func NewArrayDecoderDeclarer(typ gotype.ArrayType) ArrayTranscoderDeclarer

func (ArrayTranscoderDeclarer) Declare

func (ArrayTranscoderDeclarer) DedupeKey

func (a ArrayTranscoderDeclarer) DedupeKey() string

type CompositeInitDeclarer

type CompositeInitDeclarer struct {
	// contains filtered or unexported fields
}

CompositeInitDeclarer declares a new Go function that creates an initialized pgtype.ValueTranscoder for the Postgres type represented by the gotype.CompositeType.

We need a separate encoder because setting a pgtype.ValueTranscoder is much less flexible on the values allowed compared to AssignTo. We can assign a pgtype.CompositeType to any struct but we can only set it with an []interface{}.

Additionally, we need to use the Postgres text format exclusively because the Postgres binary format requires the type OID but pggen doesn't necessarily know the OIDs of the types. The text format, however, doesn't require OIDs.

func NewCompositeInitDeclarer

func NewCompositeInitDeclarer(typ gotype.CompositeType) CompositeInitDeclarer

func (CompositeInitDeclarer) Declare

func (c CompositeInitDeclarer) Declare(string) (string, error)

func (CompositeInitDeclarer) DedupeKey

func (c CompositeInitDeclarer) DedupeKey() string

type CompositeRawDeclarer

type CompositeRawDeclarer struct {
	// contains filtered or unexported fields
}

CompositeRawDeclarer declares a new Go function that returns all fields of a composite type as a generic array: []interface{}. Necessary because we can only set pgtype.CompositeType from a []interface{}.

Revisit after https://github.com/jackc/pgtype/pull/100 to see if we can simplify

func NewCompositeRawDeclarer

func NewCompositeRawDeclarer(typ gotype.CompositeType) CompositeRawDeclarer

func (CompositeRawDeclarer) Declare

func (c CompositeRawDeclarer) Declare(string) (string, error)

func (CompositeRawDeclarer) DedupeKey

func (c CompositeRawDeclarer) DedupeKey() string

type CompositeTranscoderDeclarer

type CompositeTranscoderDeclarer struct {
	// contains filtered or unexported fields
}

CompositeTranscoderDeclarer declares a new Go function that creates a pgx decoder for the Postgres type represented by the gotype.CompositeType.

func NewCompositeTranscoderDeclarer

func NewCompositeTranscoderDeclarer(typ gotype.CompositeType) CompositeTranscoderDeclarer

func (CompositeTranscoderDeclarer) Declare

func (c CompositeTranscoderDeclarer) Declare(pkgPath string) (string, error)

func (CompositeTranscoderDeclarer) DedupeKey

func (c CompositeTranscoderDeclarer) DedupeKey() string

type CompositeTypeDeclarer

type CompositeTypeDeclarer struct {
	// contains filtered or unexported fields
}

CompositeTypeDeclarer declares a new Go struct to represent a Postgres composite type.

func NewCompositeTypeDeclarer

func NewCompositeTypeDeclarer(comp gotype.CompositeType) CompositeTypeDeclarer

func (CompositeTypeDeclarer) Declare

func (c CompositeTypeDeclarer) Declare(pkgPath string) (string, error)

func (CompositeTypeDeclarer) DedupeKey

func (c CompositeTypeDeclarer) DedupeKey() string

type ConstantDeclarer

type ConstantDeclarer struct {
	// contains filtered or unexported fields
}

ConstantDeclarer declares a new string literal.

func NewConstantDeclarer

func NewConstantDeclarer(key, str string) ConstantDeclarer

func NewTypeResolverDeclarer

func NewTypeResolverDeclarer() ConstantDeclarer

NewTypeResolverDeclarer declares type resolver body code sometimes needed.

func NewTypeResolverInitDeclarer

func NewTypeResolverInitDeclarer() ConstantDeclarer

NewTypeResolverInitDeclarer declare type resolver init code always needed.

func (ConstantDeclarer) Declare

func (c ConstantDeclarer) Declare(string) (string, error)

func (ConstantDeclarer) DedupeKey

func (c ConstantDeclarer) DedupeKey() string

type Declarer

type Declarer interface {
	// DedupeKey uniquely identifies the declaration so that we only emit
	// declarations once. Should be namespaced like enum::some_enum.
	DedupeKey() string
	// Declare returns the string of the Go code for the declaration.
	Declare(pkgPath string) (string, error)
}

Declarer is implemented by any value that needs to declare types, data, or functions before use. For example, Postgres enums map to a Go enum with a type declaration and const values. If we use the enum in any Querier function, we need to declare the enum.

type DeclarerSet

type DeclarerSet map[string]Declarer

DeclarerSet is a set of declarers, identified by the dedupe key.

func FindInputDeclarers

func FindInputDeclarers(typ gotype.Type) DeclarerSet

FindInputDeclarers finds all necessary Declarers for types that appear in the input parameters. Returns nil if no declarers are needed.

func FindOutputDeclarers

func FindOutputDeclarers(typ gotype.Type) DeclarerSet

FindOutputDeclarers finds all necessary Declarers for types that appear in the output rows. Returns nil if no declarers are needed.

func NewDeclarerSet

func NewDeclarerSet(decls ...Declarer) DeclarerSet

func (DeclarerSet) AddAll

func (d DeclarerSet) AddAll(decls ...Declarer)

func (DeclarerSet) ListAll

func (d DeclarerSet) ListAll() []Declarer

ListAll gets all declarers in the set in a stable sort order.

type Emitter

type Emitter struct {
	// contains filtered or unexported fields
}

Emitter writes a templated query file to a file.

func NewEmitter

func NewEmitter(outDir string, tmpl *template.Template) Emitter

func (Emitter) EmitAllQueryFiles

func (em Emitter) EmitAllQueryFiles(tfs []TemplatedFile) (mErr error)

EmitAllQueryFiles emits a query file for each TemplatedFile. Ensure that emitted files don't clash by prefixing with the parent directory if necessary.

type EnumTranscoderDeclarer

type EnumTranscoderDeclarer struct {
	// contains filtered or unexported fields
}

EnumTranscoderDeclarer declares a new Go function that creates a pgx decoder for the Postgres type represented by the gotype.EnumType.

func NewEnumTranscoderDeclarer

func NewEnumTranscoderDeclarer(enum gotype.EnumType) EnumTranscoderDeclarer

func (EnumTranscoderDeclarer) Declare

func (e EnumTranscoderDeclarer) Declare(string) (string, error)

func (EnumTranscoderDeclarer) DedupeKey

func (e EnumTranscoderDeclarer) DedupeKey() string

type EnumTypeDeclarer

type EnumTypeDeclarer struct {
	// contains filtered or unexported fields
}

EnumTypeDeclarer declares a new string type and the const values to map to a Postgres enum.

func NewEnumTypeDeclarer

func NewEnumTypeDeclarer(enum gotype.EnumType) EnumTypeDeclarer

func (EnumTypeDeclarer) Declare

func (e EnumTypeDeclarer) Declare(string) (string, error)

func (EnumTypeDeclarer) DedupeKey

func (e EnumTypeDeclarer) DedupeKey() string

type GenerateOptions

type GenerateOptions struct {
	GoPkg     string
	OutputDir string
	// A map of lowercase acronyms to the upper case equivalent, like:
	// "api" => "API".
	Acronyms map[string]string
	// A map from a Postgres type name to a fully qualified Go type.
	TypeOverrides map[string]string
}

GenerateOptions are options to control generated Go output.

type ImportSet

type ImportSet struct {
	// contains filtered or unexported fields
}

ImportSet contains a set of imports required by one Go file.

func NewImportSet

func NewImportSet() *ImportSet

func (*ImportSet) AddPackage

func (s *ImportSet) AddPackage(p string)

AddPackage adds a fully qualified package path to the set, like "github.com/djsavvy/pggen/foo".

func (*ImportSet) AddType

func (s *ImportSet) AddType(typ gotype.Type)

AddType adds all fully qualified package paths needed for type and any child types.

func (*ImportSet) SortedPackages

func (s *ImportSet) SortedPackages() []string

SortedPackages returns a new slice containing the sorted packages, suitable for an import statement.

type TemplatedColumn

type TemplatedColumn struct {
	PgName    string // original name of the Postgres column
	UpperName string // name in Go-style (UpperCamelCase) to use for the column
	LowerName string // name in Go-style (lowerCamelCase)
	Type      gotype.Type
	QualType  string // package qualified Go type to use for the column, like "pgtype.Text"
}

type TemplatedFile

type TemplatedFile struct {
	Pkg        TemplatedPackage // the parent package containing this file
	PkgPath    string           // full package path, like "github.com/foo/bar"
	GoPkg      string           // the name of the Go package to use for the generated file
	SourcePath string           // absolute path to source SQL file
	Queries    []TemplatedQuery // the queries with all template information
	Imports    []string         // Go imports
	// True if this file is the leader file. The leader defines common code used
	// by by all queries in the same directory. Only one leader per directory.
	IsLeader bool
	// Any declarations this file should declare. Only set on leader.
	Declarers []Declarer
}

TemplatedFile is the Go version of a SQL query file with all information needed to execute the codegen template.

type TemplatedPackage

type TemplatedPackage struct {
	Files []TemplatedFile // sorted lexicographically by path
}

TemplatedPackage is all templated files in a pggen invocation. The templated files do not necessarily reside in the same directory.

type TemplatedParam

type TemplatedParam struct {
	UpperName string // name of the param in UpperCamelCase, like 'FirstName' from pggen.arg('FirstName')
	LowerName string // name of the param in lowerCamelCase, like 'firstName' from pggen.arg('FirstName')
	QualType  string // package-qualified Go type to use for this param
	Type      gotype.Type
}

type TemplatedQuery

type TemplatedQuery struct {
	Name        string            // name of the query, from the comment preceding the query
	SQLVarName  string            // name of the string variable containing the SQL
	ResultKind  ast.ResultKind    // kind of result: :one, :many, or :exec
	Doc         string            // doc from the source query file, formatted for Go
	PreparedSQL string            // SQL query, ready to run with PREPARE statement
	Inputs      []TemplatedParam  // input parameters to the query
	Outputs     []TemplatedColumn // output columns of the query
}

TemplatedQuery is a query with all information required to execute the codegen template.

func (TemplatedQuery) EmitParamNames

func (tq TemplatedQuery) EmitParamNames() string

EmitParamNames emits the TemplatedQuery.Inputs into comma separated names for use in a method invocation.

func (TemplatedQuery) EmitParamStruct

func (tq TemplatedQuery) EmitParamStruct() string

EmitParamStruct emits the struct definition for query params if needed.

func (TemplatedQuery) EmitParams

func (tq TemplatedQuery) EmitParams() string

EmitParams emits the TemplatedQuery.Inputs into method parameters with both a name and type based on the number of params. For use in a method definition.

func (TemplatedQuery) EmitPreparedSQL

func (tq TemplatedQuery) EmitPreparedSQL() string

EmitPreparedSQL emits the prepared SQL query with appropriate quoting.

func (TemplatedQuery) EmitResultAssigns

func (tq TemplatedQuery) EmitResultAssigns(zeroVal string) (string, error)

EmitResultAssigns writes all the assign statements after scanning the result from pgx.

Copies pgtype.CompositeFields representing a Postgres composite type into the output struct.

Copies pgtype.EnumArray fields into Go enum array types.

func (TemplatedQuery) EmitResultDecoders

func (tq TemplatedQuery) EmitResultDecoders() (string, error)

EmitResultDecoders declares all initialization required for output types.

func (TemplatedQuery) EmitResultElem

func (tq TemplatedQuery) EmitResultElem() (string, error)

EmitResultElem returns the string representing a single item in the overall query result type. For :one and :exec queries, this is the same as EmitResultType. For :many queries, this is the element type of the slice result type.

func (TemplatedQuery) EmitResultExpr

func (tq TemplatedQuery) EmitResultExpr(name string) (string, error)

EmitResultExpr returns the string representation of a single item to return for :one queries or to append for :many queries. Useful for figuring out if we need to use the address operator. Controls the string item and &item in:

items = append(items, item)
items = append(items, &item)

func (TemplatedQuery) EmitResultType

func (tq TemplatedQuery) EmitResultType() (string, error)

EmitResultType returns the string representing the overall query result type, meaning the return result.

func (TemplatedQuery) EmitResultTypeInit

func (tq TemplatedQuery) EmitResultTypeInit(name string) (string, error)

EmitResultTypeInit returns the initialization code for the result type with name, typically "item" or "items". For array types, we take care to not use a var declaration so that JSON serialization returns an empty array instead of null.

func (TemplatedQuery) EmitRowScanArgs

func (tq TemplatedQuery) EmitRowScanArgs() (string, error)

EmitRowScanArgs emits the args to scan a single row from a pgx.Row or pgx.Rows.

func (TemplatedQuery) EmitRowStruct

func (tq TemplatedQuery) EmitRowStruct() string

EmitRowStruct writes the struct definition for query output row if one is needed.

type Templater

type Templater struct {
	// contains filtered or unexported fields
}

Templater creates query file templates.

func NewTemplater

func NewTemplater(opts TemplaterOpts) Templater

func (Templater) TemplateAll

func (tm Templater) TemplateAll(files []codegen.QueryFile) ([]TemplatedFile, error)

TemplateAll creates query template files for each codegen.QueryFile.

type TemplaterOpts

type TemplaterOpts struct {
	Caser    casing.Caser
	Resolver TypeResolver
	Pkg      string // Go package name
}

TemplaterOpts is options to control the template logic.

type TypeResolver

type TypeResolver struct {
	// contains filtered or unexported fields
}

TypeResolver handles the mapping between Postgres and Go types.

func NewTypeResolver

func NewTypeResolver(c casing.Caser, overrides map[string]string) TypeResolver

func (TypeResolver) Resolve

func (tr TypeResolver) Resolve(pgt pg.Type, nullable bool, pkgPath string) (gotype.Type, error)

Resolve maps a Postgres type to a Go type.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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