Documentation
¶
Overview ¶
Package codegen contains Go code generation facilities.
Index ¶
- func GoToolsAssumedPackageName(importPath string) string
- type Import
- type Imports
- func (i *Imports) AssumedPackageName(importPath string) string
- func (i *Imports) FindByImportPath(importPath string) *Import
- func (i *Imports) FindByPackageName(packageName string) *Import
- func (i *Imports) Register(importPath string) *Import
- func (i *Imports) RegisterAliased(importPath, alias string) (*Import, error)
- type ImportsOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GoToolsAssumedPackageName ¶
GoToolsAssumedPackageName returns the assumed package name of an import path. It does this using only string parsing of the import path.
It picks the last element of the path that does not look like a major version, and then picks the valid identifier off the start of that element. It is used to determine if a local rename should be added to an import for clarity.
This function is copied from https://pkg.go.dev/golang.org/x/tools/internal/imports#ImportPathToAssumedName.
Types ¶
type Import ¶
type Import struct {
// contains filtered or unexported fields
}
Import corresponds to a single import in a Go file.
func (*Import) Alias ¶
Alias returns the explicit package name of the import or ""
For an import like `import xyz "x/y/z"`, Alias() returns "xyz".
For an import like `import "x/y/z"`, Alias() returns "".
func (*Import) GoFragment ¶
GoFragment returns the Go fragment for the import statement.
The return value will be something like `a "x/y/z"` for aliased imports and `"xyz"` for non-aliased imports.
func (*Import) PackageName ¶
PackageName returns the prefix that can be used by identifiers to refer to symbols from the package.
For an import like `import xyz "x/y/z"`, PackageName() returns "xyz".
For an import like `import "x/y/z"`, PackageName() would probably return "z". The package name is set by the Imports object that created the Import. See the UseAssumedPackageNameFunc function for more of an explanation.
If Alias() returns non-empty, PackageName() will be equal to Alias().
type Imports ¶
type Imports struct {
// contains filtered or unexported fields
}
Imports maintains a set of imports. The imports are safe for concurrent access.
func NewImports ¶
func NewImports(options ...ImportsOption) *Imports
NewImports returns an initialized Imports value.
func (*Imports) AssumedPackageName ¶
AssumedPackageName returns the assumed package name of an import path. It does this using only string parsing of the import path.
By default, this will call GoToolsAssumedPackageName(importPath). If
func (*Imports) FindByImportPath ¶
FindByPackageName returns the definition by package name, the name used in the source code identifiers.
func (*Imports) FindByPackageName ¶
FindByPackageName returns the definition by package name, the name used in the source code identifiers.
type ImportsOption ¶
type ImportsOption struct {
// contains filtered or unexported fields
}
ImportsOption configures the behavior of Imports.
func UseAssumedPackageNameFunc ¶
func UseAssumedPackageNameFunc(f func(importPath string) string) ImportsOption
UseAssumedPackageNameFunc returns an Option to configure Imports to use a different function to infer a package name from an import path.