generate

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package generate provides utilities for building code generators in Go. The standard path for a code generator is: Load -> PrintHeader -> Inspect -> Write.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filepath

func Filepath(pkg *packages.Package, filename string) string

Filepath returns the filepath of a file in the given package with the given filename relative to the package.

func Format

func Format(filename string, src []byte, opt *imports.Options) ([]byte, error)

Format returns the given bytes with goimports applied. It wraps imports.Process by wrapping any error with additional context.

func Inspect

func Inspect(pkg *packages.Package, f func(n ast.Node) (bool, error)) error

Inspect goes through all of the files in the given package and calls the given function on each node in files that are not generated. The bool return value from the given function indicates whether to continue traversing down the AST tree of that node and look at its children. If a non-nil error value is returned by the given function, the traversal of the tree is stopped and the error value is returned.

func Load

func Load(cfg *packages.Config, patterns ...string) ([]*packages.Package, error)

Load loads and returns the Go packages named by the given patterns. Load calls packages.Load and ensures that there is at least one package; this means that, if there is a nil error, the length of the resulting packages is guaranteed to be greater than zero.

func PrintHeader

func PrintHeader(w io.Writer, pkg string, imports ...string)

PrintHeader prints a header to the given writer for a generated file in the given package with the given imports. Imports do not need to be set if you are running Format on the code later, but they should be set for any external packages that many not be found correctly by goimports.

func Write

func Write(filename string, src []byte, opt *imports.Options) error

Write writes the given bytes to the given filename after applying goimports using the given options.

Types

This section is empty.

Jump to

Keyboard shortcuts

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