codegen

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2021 License: Apache-2.0, BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package codegen contains Go code generation facilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoToolsAssumedPackageName

func GoToolsAssumedPackageName(importPath string) string

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

func (s *Import) Alias() string

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

func (s *Import) GoFragment() string

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

func (s *Import) PackageName() string

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().

func (*Import) Path

func (s *Import) Path() string

Path returns the quoted part of the import statement.

For an import like `import xyz "x/y/z"`, Path() returns "x/y/z".

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

func (i *Imports) AssumedPackageName(importPath string) string

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

func (i *Imports) FindByImportPath(importPath string) *Import

FindByPackageName returns the definition by package name, the name used in the source code identifiers.

func (*Imports) FindByPackageName

func (i *Imports) FindByPackageName(packageName string) *Import

FindByPackageName returns the definition by package name, the name used in the source code identifiers.

func (*Imports) Register

func (i *Imports) Register(importPath string) *Import

Register registers an import path so that it will be included in the output.

func (*Imports) RegisterAliased

func (i *Imports) RegisterAliased(importPath, alias string) (*Import, error)

Register registers an import path so that it will be included in the output.

If there is already an import for the given alias, an error will be returned if it has a different import path.

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.

Jump to

Keyboard shortcuts

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