importer

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Importer

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

Importer is the type used to manage the package imports. It is used to ensure that the same package is imported only once and to alias the package names avoiding conflicts on same-name packages.

All exported methods are safe for concurrent use if the underlying packages are not modified concurrently by other goroutines.

func New

func New() *Importer

New returns a new Importer.

func (*Importer) AddImport

func (imp *Importer) AddImport(p *types.Package)

AddImport adds the given package to the list of imports ensuring that the same package is imported only once. The comparison is based on the package path. If the given package is nil, the function returns without performing any operation.

func (*Importer) AliasOf

func (imp *Importer) AliasOf(p *types.Package) string

AliasOf returns the alias of the given package path. If the package is not imported, the function returns an empty string.

func (*Importer) AliasedImports

func (imp *Importer) AliasedImports() map[string]string

AliasedImports returns the map of package imports formatted as "path:alias". All the imports are aliased to avoid conflicts on same-name packages. Moreover, the imports are sorted by path before aliasing them, ensuring deterministic results and avoiding, for example, false positives in tests comparing the generated code.

NOTE: The method should be called only once, after all imports have been added, or it may produce inconsistent results. For example, if a package (B) is added after the first call and has the same name of another one (A) but a path that sorts before, A alias will be different from the first result. See the example below for more details.

Example:

a, err := aliaser.New(&aliaser.Config{TargetPackage: "mypkg", Pattern: "github.com/example/package"})
if err != nil {
  // ...
}

a.AddImport(types.NewPackage("github.com/marcozac/go-aliaser/fake1", "fake"))
a.AddImport(types.NewPackage("github.com/marcozac/go-aliaser/fake3", "fake"))
imports := a.AliasedImports()
// "github.com/marcozac/go-aliaser/fake1": "fake"
// "github.com/marcozac/go-aliaser/fake3": "fake_2"

// Bad!
a.AddImport(types.NewPackage("github.com/marcozac/go-aliaser/fake2", "fake"))
imports = a.AliasedImports()
// "github.com/marcozac/go-aliaser/fake1": "fake"
// "github.com/marcozac/go-aliaser/fake2": "fake_2"
// "github.com/marcozac/go-aliaser/fake3": "fake_3" // different alias!

func (*Importer) Imports

func (imp *Importer) Imports() []*types.Package

Imports creates a new slice containing all the imported packages.

Jump to

Keyboard shortcuts

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