Documentation
¶
Overview ¶
Package codegen contains shared utilities for generating code.
Index ¶
- func AssertStructUnchanged(t *types.Struct, tname string, params *types.TypeParamList, ctx string, ...) []byte
- func ContainsPointers(typ types.Type) bool
- func FormatTypeParams(params *types.TypeParamList, it *ImportTracker) (constraints, names string)
- func HasNoClone(structTag string) bool
- func IsInvalid(t types.Type) bool
- func IsViewType(typ types.Type) bool
- func LoadTypes(buildTags string, pkgName string) (*packages.Package, map[string]types.Type, error)
- func LookupMethod(t types.Type, name string) *types.Func
- func NamedTypeOf(t types.Type) (named *types.Named, ok bool)
- func WritePackageFile(tool string, pkg *packages.Package, path string, it *ImportTracker, ...) error
- type ImportTracker
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertStructUnchanged ¶
func AssertStructUnchanged(t *types.Struct, tname string, params *types.TypeParamList, ctx string, it *ImportTracker) []byte
AssertStructUnchanged generates code that asserts at compile time that type t is unchanged. thisPkg is the package containing t. tname is the named type corresponding to t. ctx is a single-word context for this assertion, such as "Clone". If non-nil, AssertStructUnchanged will add elements to imports for each package path that the caller must import for the returned code to compile.
func ContainsPointers ¶
ContainsPointers reports whether typ contains any pointers, either explicitly or implicitly. It has special handling for some types that contain pointers that we know are free from memory aliasing/mutation concerns.
func FormatTypeParams ¶
func FormatTypeParams(params *types.TypeParamList, it *ImportTracker) (constraints, names string)
FormatTypeParams formats the specified params and returns two strings:
- constraints are comma-separated type parameters and their constraints in square brackets (e.g. [T any, V constraints.Integer])
- names are comma-separated type parameter names in square brackets (e.g. [T, V])
If params is nil or empty, both return values are empty strings.
func HasNoClone ¶
HasNoClone reports whether the provided tag has `codegen:noclone`.
func IsInvalid ¶
IsInvalid reports whether the provided type is invalid. It is used to allow codegeneration to run even when the target files have build errors or are missing views.
func IsViewType ¶
IsViewType reports whether the provided typ is a View.
func LookupMethod ¶
LookupMethod returns the method with the specified name in t, or nil if the method does not exist.
func NamedTypeOf ¶
NamedTypeOf is like t.(*types.Named), but also works with type aliases.
func WritePackageFile ¶
func WritePackageFile(tool string, pkg *packages.Package, path string, it *ImportTracker, contents *bytes.Buffer) error
WritePackageFile adds a file with the provided imports and contents to package. The tool param is used to identify the tool that generated package file.
Types ¶
type ImportTracker ¶
type ImportTracker struct {
// contains filtered or unexported fields
}
ImportTracker provides a mechanism to track and build import paths.
func NewImportTracker ¶
func NewImportTracker(thisPkg *types.Package) *ImportTracker
func (*ImportTracker) Has ¶
func (it *ImportTracker) Has(pkg string) bool
Has reports whether the specified package has been imported.
func (*ImportTracker) Import ¶
func (it *ImportTracker) Import(pkg string)
func (*ImportTracker) PackagePrefix ¶
func (it *ImportTracker) PackagePrefix(pkg *types.Package) string
PackagePrefix returns the prefix to be used when referencing named objects from pkg.
func (*ImportTracker) QualifiedName ¶
func (it *ImportTracker) QualifiedName(t types.Type) string
QualifiedName returns the string representation of t in the package.
func (*ImportTracker) Write ¶
func (it *ImportTracker) Write(w io.Writer)
Write prints all the tracked imports in a single import block to w.