Documentation
¶
Overview ¶
Package goref provides tools to analyze a set of Go packages, starting from one or more `main` packages, and produce a graph of relations between identifiers in these packages. In other words, it indexes your Go code and tells you where an exported identifier is used. It can answer questions such as:
- Where is this type instantiated?
- Where is this function called?
- Where are all the references to this identifier?
- What types implement this interface?
- What interfaces are implemented by this type?
Index ¶
- Constants
- func CandidatePaths(loadpath, parent string) []string
- func CleanImportSpec(spec *ast.ImportSpec) string
- func ConstantVersion(v int64) func(loader.Program, loader.PackageInfo) (int64, error)
- func FileMTimeVersion(prog loader.Program, pi loader.PackageInfo) (int64, error)
- func FilterPass(loadpath string, version int64) bool
- type Corpus
- type Package
- type PackageGraph
- type Position
- type Ref
- type RefType
Examples ¶
Constants ¶
const ( // Instantiation of a type in another package. Instantiation = iota // Call of a function in another package. Call // Implementation of an interface by a type. Implementation // Extension of an interface by another interface. Extension // Import is the import of a package by another. `fromIdent` // may differ from the name of the target package in the case // of named imports. For dot-imports, `fromIdent` is ".". Import // Reference is the default, used when we can't determine the // type of reference. Reference )
These are the possible types of edges in a graph.
const ( // NoPos represents a mising position. It has the same // semantics as token.NoPos. It may be used in place of an // "end" Pos if the end of an identifier isn't known. NoPos = token.NoPos )
Variables ¶
This section is empty.
Functions ¶
func CandidatePaths ¶
CandidatePaths returns a slice enumerating all the possible import paths for a package. This means inserting the possible "vendor" directory location from the load path of the importing package.
If package a/b imports c/d, the following paths are candidates: a/b/vendor/c/d a/vendor/c/d vendor/c/d c/d
Order matters, as the most-specific vendored package is selected. Note that multi-level vendoring works, as PackageGraph will consider the full import path, including path/to/vendor/, as the package path when building the graph. In this sense we follow the go tool's convention to not try to detect when two packages loaded through different paths are the same package.
Example ¶
package main import ( "fmt" "github.com/korfuri/goref" ) func main() { for _, p := range goref.CandidatePaths("lib/util", "program/bin") { fmt.Println(p) } }
Output: program/bin/vendor/lib/util program/vendor/lib/util vendor/lib/util lib/util
func CleanImportSpec ¶
func CleanImportSpec(spec *ast.ImportSpec) string
CleanImportSpec takes an ast.ImportSpec and cleans the Path component by trimming the quotes (") that surround it.
Example ¶
package main import ( "fmt" "go/ast" "github.com/korfuri/goref" ) func main() { fmt.Println(goref.CleanImportSpec(&ast.ImportSpec{Path: &ast.BasicLit{Value: "\"foo/bar/baz\""}})) }
Output: foo/bar/baz
func ConstantVersion ¶
ConstantVersion returns a versionF function that always replies with a constant version. Useful for experimenting, or for graphs who load from an immutable snapshot of the Go universe.
func FileMTimeVersion ¶
FileMTimeVersion is a versionF function that processes all files in the provided PackageInfo and returns the newest mtime's second as a time.Time-compatible int64.
func FilterPass ¶
FilterPass is a filterF function that always says yes.
Types ¶
type Corpus ¶
type Corpus string
A Corpus represents a prefix from which Go packages may be loaded. Default corpora are $GOROOT/src and each of $GOPATH/src
func DefaultCorpora ¶
func DefaultCorpora() []Corpus
DefaultCorpora returns the set of default corpora based on GOROOT and GOPATH.
func (Corpus) ContainsRel ¶
ContainsRel returns whether the provided relpath exists under this Corpus.
type Package ¶
type Package struct { // Name of the package Name string `json:"-"` // Files in this package Files []string `json:"-"` // OutRefs and InRefs are slices of references. For OutRefs // the Ref is to an identifier in another package. For InRefs // the Ref is to an identifier within this package. Most // RefTypes are not indexed if the ToPackage and the // FromPackage are the same, but some do such as // Implementation. This means that a ref can exist in both // OutRefs and InRefs of the same package. OutRefs []*Ref `json:"-"` InRefs []*Ref `json:"-"` // Interfaces is the list of interface types in this package. // // This is used to compute the interface-implementation matrix. // // Only named interfaces matter, because an unnamed interface // can't be exported. // // Interfaces equivalent to interface{} are excluded. Interfaces []*types.Named `json:"-"` // Impls is the list of non-interface types in this package. // // This is used to compute the interface-implementation matrix. // // Only named types matter, because an unnamed type can't have // methods. Impls []*types.Named `json:"-"` // Fset is a reference to the token.FileSet that loaded this // package. Fset *token.FileSet `json:"-"` // Version is the version of the package that was loaded. Version int64 `json:"version"` // Path is the package's load path Path string `json:"loadpath"` // Corpus is the corpus that contains this package Corpus `json:"-"` }
Package represents a Go Package, including its dependencies.
func (Package) DocumentID ¶
DocumentID returns a consistent id for this package at this version. This can be used to index the package e.g. in ElasticSearch. The ID contains the document version and path.
func (Package) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler interface
type PackageGraph ¶
type PackageGraph struct { // Map of package load-path to Package objects. Packages map[string]*Package // Slice of corpora that files may be loaded from Corpora []Corpus // contains filtered or unexported fields }
PackageGraph represents a collection of Go packages and their mutual dependencies. All dependencies of a Package in the PackageGraph are also part of the PackageGraph.
func NewPackageGraph ¶
func NewPackageGraph(versionF func(loader.Program, loader.PackageInfo) (int64, error)) *PackageGraph
NewPackageGraph returns a new, empty PackageGraph.
func (*PackageGraph) ComputeInterfaceImplementationMatrix ¶
func (pg *PackageGraph) ComputeInterfaceImplementationMatrix()
ComputeInterfaceImplementationMatrix processes all loaded types and adds cross-package and intra-package Refs for Implementation and Extension edges of the graph.
func (*PackageGraph) LoadPackages ¶
func (pg *PackageGraph) LoadPackages(packages []string, includeTests bool) error
LoadPackages loads the specified packages and their transitive dependencies, as well as XTests (as defined by go/loader) if includeTests is true. It may be called multiple times to load multiple package sets in the PackageGraph.
func (*PackageGraph) SetFilterF ¶
func (pg *PackageGraph) SetFilterF(f func(string, int64) bool)
SetFilterF sets the filterF for this PackageGraph
type Position ¶
type Position struct { File string `json:"filename"` PosL int `json:"start_line"` PosC int `json:"start_col"` EndL int `json:"end_line"` EndC int `json:"end_col"` }
A Position is similar to token.Position in that it gives an absolute position within a file, but it may also denote the Pos + End concept of token.Pos.
The End is optional. If NoPos is used as the End, Position only contains file:line:column.
The Pos is not optional and must resolve to file:line:column.
func NewPosition ¶
NewPosition creates a Position from a token.FileSet and a pair of Pos in that FileSet. It will panic if both Pos are not from the same Filename.
func (Position) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler interface
type Ref ¶
type Ref struct { // Type of reference RefType // Where this reference points from, i.e. where the identifier // was used in another package. FromPosition Position // Where this reference points to, i.e. where the definition // is ToPosition Position // What identifier points to this Ref FromIdent string // What identifier this Ref points to ToIdent string // What package contains what the identifier refers to ToPackage *Package // What package the ref is from, i.e. what foreign package was // this identifier used in. FromPackage *Package }
A Ref is a reference to an identifier whose definition lives in another package.
func (Ref) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler interface
type RefType ¶
type RefType int
RefType is an enum of the various ways a package can reference an identifier in another package (e.g. as a call, an instantiation, etc.)
func (RefType) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler interface
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
cmd
|
|
serve/proto
Package serve is a generated protocol buffer package.
|
Package serve is a generated protocol buffer package. |
Package elasticsearch is a package of utilities to store a PackageGraph's state in an ElasticSearch index.
|
Package elasticsearch is a package of utilities to store a PackageGraph's state in an ElasticSearch index. |
mocks
Code generated by mockery v1.0.0
|
Code generated by mockery v1.0.0 |
Package goref is a generated protocol buffer package.
|
Package goref is a generated protocol buffer package. |
testprograms
|
|
interfaces
Package main is a test program that demonstrates interface-implementation relations in goref.
|
Package main is a test program that demonstrates interface-implementation relations in goref. |
interfaces/lib
Package lib contains interfaces and types that implement them.
|
Package lib contains interfaces and types that implement them. |