Categorygithub.com/marcozac/go-aliaser
modulepackage
0.1.1
Repository: https://github.com/marcozac/go-aliaser.git
Documentation: pkg.go.dev

# README

aliaser

Go Reference CI codecov Go Report Card

aliaser is a Go package designed to streamline the process of generating aliases for Go packages.

In Go projects, naming conflicts between imported packages and your own code can lead to verbose and cumbersome code where you have to frequently alias imported package names.

aliaser solves this problem by automatically generating Go code that provides aliases for all the exported constants, variables, functions, and types in an external package. This allows you to seamlessly integrate and extend external packages without cluttering your code with manual aliases.

Installation

You need a working Go environment.

go get github.com/marcozac/go-aliaser

Usage

To use aliaser in your project, follow these steps:

  1. Create an Aliaser: Start by creating a new aliaser.Aliaser with the target package name and the pattern of the package you want to alias.
import "github.com/marcozac/go-aliaser"

a, err := aliaser.New(&aliaser.Config{TargetPackage: "mypkg", Pattern: "github.com/example/package"})
if err != nil {
  // ...
}
  1. Generate Aliases: With the Aliaser created, you can generate the aliases writing them to a io.Writer or to directly to a file.
if err := a.Generate(io.Discard); err != nil {
  // ...
}

if err := a.GenerateFile("mypkg/alias.go"); err != nil {
  // ...
}

CLI

In addition to the library, aliaser comes with a CLI tool to simplify generating aliases directly from the command line.

After installing aliaser CLI with go install (e.g. go install github.com/marcozac/go-aliaser/cmd/aliaser@latest) or by downloading a release (available soon), you can use the aliaser command to generate aliases for a package.

aliaser generate \
  --from "github.com/example/package" \
  --package "myalias" \
  --file "path/to/output/file.go"

Examples

For simple, but more detailed examples of how to use the aliaser library and CLI, see the examples directory.

Contributing

Contributions to aliaser are welcome! Whether it's reporting bugs, discussing improvements, or contributing code, all contributions are appreciated.

License

This project is licensed under the MIT License. See the LICENSE file for details.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

AssignFunctions sets whether the aliases for the functions should be assigned to a variable instead of being wrapped.
ExcludeConstants excludes the constants from the loaded package.
ExcludeFunctions excludes the functions from the loaded package.
ExcludeNames excludes the given names from the loaded package.
ExcludeTypes excludes the types from the loaded package.
ExcludeVariables excludes the variables from the loaded package.
Generate loads the package defined by the given pattern, generates the aliases for the target package name, and writes the result to the given writer.
GenerateFile behaves like [Generate], but it writes the aliases to the file with the given name.
New returns a new [Aliaser] with the given configuration.
NewAliasedTuple returns a new tuple ensuring none of its variable names is in the given aliases list.
NewConst returns a new [Const] with the given constant.
NewFunc returns a new [Func] with the given function.
NewQualifiedType returns a new [QualifiedType] with the given type and importer.
NewSignature returns a new [Signature] with the given signature.
NewTypeName returns a new [TypeName] with the given type.
NewTypeParam returns a new [TypeParam] with the given type parameter and sets the constraint to a new [QualifiedType] with the given importer.
NewVar returns a new [Var] with the given variable.
OnDuplicate sets the behavior when a duplicate object name is found.
OpenFileWithReset opens a file for reading and writing, creating it if it does not exist.
WithContext sets the context to be used when loading the package.
WithHeader sets an optional header to be written at the top of the file.

# Constants

OnDuplicatePanic panics when a duplicate object name is found.
OnDuplicateReplace replaces the old object (even if it is a different kind of object) with the new one.
OnDuplicateSkip is the default behavior when a duplicate object name is found.

# Variables

ErrEmptyPattern is returned when the given pattern is empty.
ErrEmptyTarget is returned when the given target is empty.
ErrNilConfig is returned when the given config is nil.

# Structs

Aliaser is the primary type of this package.
Config is the configuration used to define the target package.
Const is the type used to represent a constant in the loaded package.
Func is the type used to represent a function in the loaded package.
QualifiedType is the type used to represent a type in the loaded package.
Signature is the type used to represent a function signature in the loaded package.
TypeName is the type used to represent a type in the loaded package.
TypeParam is the type used to represent a type parameter in the loaded package.
Var is the type used to represent a variable in the loaded package.

# Interfaces

File is an interface that extends [fs.File] with the [io.WriteSeeker] and Truncate methods.
Object extends the [types.Object] interface with methods to get the package alias and the resolved type as a string.
Option is the interface implemented by all options.
PackageAliaser is the interface implemented by types that can provide the alias of the package they belong to.
PackageTyper is a subset of the [types.Object] interface that provides methods to get the package and the type of the object.
TypeStringer is the interface implemented by types that can return their type as a string.

# Type aliases

PackagesErrors is a slice of [packages.Error] as returned by [packages.Load] that implements the error interface.