gopatch

package
v0.53.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrVarNotFound reports that the file declares no package-level variable
	// under the requested name.
	ErrVarNotFound = errors.New("package-level variable not found")
	// ErrNotACall reports that the named variable is not initialized from a
	// function call, so there is no argument list to append to.
	ErrNotACall = errors.New("variable is not initialized from a call expression")
)
View Source
var ErrImportNameConflict = errors.New("import is bound to a different name")

ErrImportNameConflict reports that a file already imports the required path under a name the generated code does not reference.

Functions

This section is empty.

Types

type File

type File struct {
	// Path is where the file was read from, used in error messages and as the
	// filename the parser reports positions against.
	Path string
	// contains filtered or unexported fields
}

File is a Go source file patched in memory.

Every mutation splices text at a position located through the AST rather than re-printing the parsed file. Re-printing would rewrite parts of the file the generator never touched — and these are files the developer owns, where a one-line addition must read as a one-line diff.

func New

func New(path string, src []byte) *File

New wraps in-memory source for patching, for tests and for a file the caller has already rendered.

func Open

func Open(path string) (*File, error)

Open reads a Go source file for patching.

func (*File) AppendCallArg

func (f *File) AppendCallArg(varName, arg string) (bool, error)

AppendCallArg appends arg to the argument list of the call initializing the package-level variable varName — the shape both `var Module = vef.Module(...)` registration blocks take.

It is idempotent: an argument already present (compared with whitespace normalized) leaves the file untouched and reports false, so re-running a generator over an existing entity is a no-op rather than a duplicate registration that fails at boot.

func (*File) AppendCallArgIn

func (f *File) AppendCallArgIn(funcName, callee, arg string) (bool, error)

AppendCallArgIn appends arg to the argument list of a call to callee made inside the named function — the shape an application's entry point takes, where the module list is an argument list rather than a variable.

It reports false when the file declares no such function or the function makes no such call, so a caller can degrade to telling the developer what to add rather than failing over a project that wires itself differently.

func (*File) AppendVarSpec

func (f *File) AppendVarSpec(name, value string) (bool, error)

AppendVarSpec appends `name = value` to the file's first parenthesized package-level var block — the `var ( XxxModel = new(Xxx) ... )` registry shape. A file with no such block gets one appended.

Like AppendCallArg it is idempotent on the declared name.

func (*File) EnsureImport

func (f *File) EnsureImport(path string, groups ImportGroups) (bool, error)

EnsureImport adds path to the file's import block when it is not already imported, inserting it in sorted position inside its own group so the result satisfies gci without a follow-up fix pass.

func (*File) EnsureNamedImport

func (f *File) EnsureNamedImport(alias, path string, groups ImportGroups) (bool, error)

EnsureNamedImport is EnsureImport with an explicit local name, which a generated file needs whenever it reaches into two packages sharing a final segment — the audit user model living in another module's model package beside the entity's own is the ordinary case.

func (*File) Format

func (f *File) Format() error

Format runs gofmt over the buffer, which realigns anything the insertions disturbed (a var block's assignment column, most visibly).

func (*File) Source

func (f *File) Source() []byte

Source returns the current buffer.

type Import

type Import struct {
	// Alias is the local name, empty when the package name is used as-is.
	Alias string
	// Path is the import path.
	Path string
}

Import is one import a generated file needs: a path and an optional local name for the case where two packages share a final segment.

type ImportGroups

type ImportGroups struct {
	// Framework is the framework's module path.
	Framework string
	// Local is the project's own module path.
	Local string
}

ImportGroups names the prefixes that separate a project's import groups. They mirror the gci sections the framework and its consumers lint against: standard, external, framework, then the project's own module.

Jump to

Keyboard shortcuts

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