common

package
v0.0.0-...-bfb23d5 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2018 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDuplicateImport = errors.New("specifier already in use in ImportBag")

ErrDuplicateImport is the error that will be returned when two packages are both requested to be imported using the same specifier.

View Source
var ErrMultipleLocalImport = errors.New("package already imported into the local namespace")

ErrMultipleLocalImport is the error that will be returned when the same package has been imported to the specifer "." more than once.

Functions

This section is empty.

Types

type ImportBag

type ImportBag struct {
	// contains filtered or unexported fields
}

ImportBag captures all of the imports in a Go source file, and attempts to ease the process of working with them.

func NewImportBag

func NewImportBag() *ImportBag

NewImportBag instantiates an empty ImportBag.

func NewImportBagFromFile

func NewImportBagFromFile(filepath string) (*ImportBag, error)

NewImportBagFromFile reads a Go source file, finds all imports, and returns them as an instantiated ImportBag.

func (*ImportBag) AddImport

func (ib *ImportBag) AddImport(pkgPath PackagePath) PackageSpecifier

AddImport includes a package, and returns the name that was selected to be the specified for working with this path. It first attempts to use the package name as the specifier. Should that cause a conflict, it determines a unique name to be used as the specifier.

If the path provided has already been imported, the existing name for it is returned, but err is non-nil.

func (*ImportBag) AddImportWithSpecifier

func (ib *ImportBag) AddImportWithSpecifier(pkgPath PackagePath, specifier PackageSpecifier) error

AddImportWithSpecifier will add an import with a given name. If it would lead to conflicting package specifiers, it returns an error.

func (ImportBag) FindSpecifier

func (ib ImportBag) FindSpecifier(pkgPath PackagePath) (PackageSpecifier, bool)

FindSpecifier finds the specifier assocatied with a particular package.

If the package was not imported, the empty string and false are returned.

If multiple specifiers are assigned to the package, one is returned at random.

If the same package is imported with a named specifier, and the blank identifier, the name is returned.

func (*ImportBag) List

func (ib *ImportBag) List() []string

List returns each import statement as a slice of strings sorted alphabetically by their import paths.

func (*ImportBag) ListAsImportSpec

func (ib *ImportBag) ListAsImportSpec() []*ast.ImportSpec

ListAsImportSpec returns the imports from the ImportBag as a slice of ImportSpecs sorted alphabetically by their import paths.

type PackagePath

type PackagePath string

PackagePath is a string that refers to the location of Go package.

type PackageSpecifier

type PackageSpecifier string

PackageSpecifier is a string that represents the name that will be used to refer to exported functions and variables from a package.

func FindSpecifier

func FindSpecifier(pkgPath PackagePath) (PackageSpecifier, error)

FindSpecifier finds the name of a package by loading it in from GOPATH or a vendor folder.

Jump to

Keyboard shortcuts

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