Documentation
¶
Index ¶
- Variables
- type File
- func (f *File) AppendCallArg(varName, arg string) (bool, error)
- func (f *File) AppendCallArgIn(funcName, callee, arg string) (bool, error)
- func (f *File) AppendVarSpec(name, value string) (bool, error)
- func (f *File) EnsureImport(path string, groups ImportGroups) (bool, error)
- func (f *File) EnsureNamedImport(alias, path string, groups ImportGroups) (bool, error)
- func (f *File) Format() error
- func (f *File) Source() []byte
- type Import
- type ImportGroups
Constants ¶
This section is empty.
Variables ¶
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") )
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 ¶
New wraps in-memory source for patching, for tests and for a file the caller has already rendered.
func (*File) AppendCallArg ¶
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 ¶
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 ¶
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.
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.