codegen

package
v1.24.2 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: BSD-3-Clause Imports: 8 Imported by: 6

Documentation

Overview

Package codegen contains shared utilities for generating code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertStructUnchanged

func AssertStructUnchanged(t *types.Struct, thisPkg *types.Package, tname, ctx string, imports map[string]struct{}) []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

func ContainsPointers(typ types.Type) bool

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 NamedTypes

func NamedTypes(pkg *packages.Package) map[string]*types.Named

NamedTypes returns all named types in pkg, keyed by their type name.

func WriteFormatted

func WriteFormatted(code []byte, path string) error

WriteFormatted writes code to path. It runs gofmt on it before writing; if gofmt fails, it writes code unchanged. Errors can include I/O errors and gofmt errors.

The advantage of always writing code to path, even if gofmt fails, is that it makes debugging easier. The code can be long, but you need it in order to debug. It is nicer to work with it in a file than a terminal. It is also easier to interpret gofmt errors with an editor providing file and line numbers.

Types

This section is empty.

Jump to

Keyboard shortcuts

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