Documentation ¶
Overview ¶
Decouple analyzes Go packages to find overspecified function parameters. If your function takes a *os.File for example, but only ever calls Read on it, the function can be rewritten to take an io.Reader. This generalizes the function, making it easier to test and decoupling it from whatever the source of the *os.File is.
Index ¶
- Constants
- type Checker
- func (ch Checker) Check() ([]Tuple, error)
- func (ch Checker) CheckFunc(pkg *packages.Package, fndecl *ast.FuncDecl) (map[string]MethodMap, error)
- func (ch Checker) CheckPackage(pkg *packages.Package) ([]Tuple, error)
- func (ch Checker) CheckParam(pkg *packages.Package, fndecl *ast.FuncDecl, name *ast.Ident) (_ MethodMap, err error)
- func (ch Checker) NameForMethods(inp MethodMap) string
- type MethodMap
- type Tuple
Constants ¶
const PkgMode = packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo
PkgMode is the minimal set of bit flags needed for the Config.Mode field of golang.org/x/go/packages for the result to be usable by a Checker.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Checker ¶
type Checker struct { Verbose bool // contains filtered or unexported fields }
Checker is the object that can analyze a directory tree of Go code, or a set of packages loaded with "golang.org/x/go/packages".Load, or a single such package, or a function or function parameter in one.
Set Verbose to true to get (very) verbose debugging output.
func NewCheckerFromDir ¶
NewCheckerFromDir creates a new Checker containing packages loaded (using "golang.org/x/go/packages".Load) from the given directory tree.
func NewCheckerFromPackages ¶
NewCheckerFromPackages creates a new Checker containing the given packages, which should be the result of calling "golang.org/x/go/packages".Load with at least the bits in PkgMode set in the Config.Mode field.
func (Checker) Check ¶
Check checks all the packages in the Checker. It analyzes the functions in them, looking for parameters with concrete types that could be interfaces instead. The result is a list of Tuples, one for each function checked that has parameters eligible for decoupling.
func (Checker) CheckFunc ¶
func (ch Checker) CheckFunc(pkg *packages.Package, fndecl *ast.FuncDecl) (map[string]MethodMap, error)
CheckFunc checks a single function declaration, which should appear in the given package, which should be one of the packages contained in the Checker. The result is a map from parameter names eligible for decoupling to MethodMaps.
func (Checker) CheckPackage ¶
CheckPackage checks a single package. It should be one of the packages contained in the Checker. The result is a list of Tuples, one for each function checked that has parameters eligible for decoupling.
func (Checker) CheckParam ¶
func (ch Checker) CheckParam(pkg *packages.Package, fndecl *ast.FuncDecl, name *ast.Ident) (_ MethodMap, err error)
CheckParam checks a single named parameter in a given function declaration, which must apepar in the given package, which should be one of the packages in the Checker. The result is a MethodMap for the parameter, and may be nil if the parameter is not eligible for decoupling.
func (Checker) NameForMethods ¶
NameForMethods takes a MethodMap and returns the name of an interface defining exactly the methods in it, if it can find one among the packages in the Checker. If there are multiple such interfaces, one is chosen arbitrarily.
type Tuple ¶
type Tuple struct { // F is the function declaration that this result is about. F *ast.FuncDecl // P is the package in which the function declaration appears. P *packages.Package // M is a map from the names of function parameters eligible for decoupling // to MethodMaps for each such parameter. M map[string]MethodMap }
Tuple is the type of a result from Checker.Check and Checker.CheckPackage.