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 (*Importer) AddImport ¶
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 ¶
AliasOf returns the alias of the given package path. If the package is not imported, the function returns an empty string.
func (*Importer) AliasedImports ¶
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!