Documentation
¶
Index ¶
- type BuildContext
- type Config
- type FieldInfo
- type GoModLocator
- type Loader
- type Package
- func (p *Package) Files() (map[string]*ast.File, error)
- func (p *Package) FindInterface(interfaceName string) (*ast.InterfaceType, *ast.TypeSpec, error)
- func (p *Package) FindTypeSpec(typeName string) (*ast.TypeSpec, *Package, error)
- func (p *Package) GetImportPathBySelector(ctx context.Context, selectorName string, astFile *ast.File) (string, *Package, error)
- func (p *Package) GetMethodsForType(typeName string) ([]*ast.FuncDecl, error)
- func (p *Package) GetStruct(name string) (*StructInfo, error)
- func (p *Package) ResolveImport(ctx context.Context, importPath string) (*Package, error)
- type PackageLocator
- type PackageMetaInfo
- type PackageNotFoundError
- type ParseError
- type StructInfo
- type SymbolInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BuildContext ¶
type BuildContext struct {
GOOS string
GOARCH string
BuildTags []string
ToolDir string // Optional: directory for build tools like go command
UseGoModule bool // Whether to operate in Go modules mode
}
BuildContext defines the build parameters for locating and loading packages.
type Config ¶
type Config struct {
// Context specifies the build context.
Context BuildContext
// Fset is the file set used for parsing files.
Fset *token.FileSet
// Locator is a function that finds packages based on a pattern.
// If nil, a default locator (e.g., using `go list`) will be used.
Locator PackageLocator
}
Config defines the configuration for a Loader.
type FieldInfo ¶
type FieldInfo struct {
Name string
Tag string // Raw tag string (e.g., `json:"name,omitempty"`)
TypeExpr ast.Expr // AST expression for the field's type
Embedded bool
// ParentStruct allows FieldInfo to access its containing struct's context, like its package.
ParentStruct *StructInfo
}
FieldInfo represents information about a single field in a struct.
func (*FieldInfo) GetTag ¶
GetTag parses the struct tag and returns the value associated with the given key.
func (*FieldInfo) ImplementsInterface ¶
func (fi *FieldInfo) ImplementsInterface(ctx context.Context, targetInterfacePackagePath string, targetInterfaceName string) (bool, error)
ImplementsInterface checks if the field's type implements the target interface. targetInterfacePackagePath is the full import path of the package defining the interface. targetInterfaceName is the name of the interface. This is a simplified AST-based check and has limitations (e.g., complex type matching in signatures).
type GoModLocator ¶
type GoModLocator struct {
WorkingDir string // The working directory, typically the root of the main module. (Exported)
}
GoModLocator is a PackageLocator that resolves import paths without relying on the `go list` command.
func (*GoModLocator) Locate ¶
func (gml *GoModLocator) Locate(ctx context.Context, pattern string, buildCtx BuildContext) ([]PackageMetaInfo, error)
Locate implements the PackageLocator interface for GoModLocator. It resolves package paths without using `go list`.
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
Loader is responsible for loading packages.
func (*Loader) GetAST ¶
GetAST retrieves a parsed AST from the cache by its canonical file path. It returns the AST and true if found, otherwise nil and false.
func (*Loader) Load ¶
Load loads the packages matching the given patterns. It only loads the metadata for the top-level packages. Dependent packages are loaded lazily when accessed.
func (*Loader) LookupSymbol ¶
func (l *Loader) LookupSymbol(fullSymbolName string) (SymbolInfo, bool)
LookupSymbol retrieves symbol information from the cache by its full name (e.g., "<package_path>:<symbol_name>"). It returns the SymbolInfo and true if found, otherwise an empty SymbolInfo and false.
type Package ¶
type Package struct {
Name string // Package name
ImportPath string // Import path
Dir string // Directory containing package sources
GoFiles []string // Go source files (non-test) relative to Dir
RawMeta PackageMetaInfo // Raw metadata from locator
// contains filtered or unexported fields
}
Package represents a single Go package. Its AST and resolved imports are loaded lazily.
func NewPackage ¶
func NewPackage(meta PackageMetaInfo, loader *Loader) *Package
NewPackage creates a new Package instance from metadata. The loader is used to resolve dependencies later.
func (*Package) Files ¶
Files returns the parsed ASTs for all Go files in the package. It triggers parsing if not already done.
func (*Package) FindInterface ¶
FindInterface searches for an interface type by name within the package. It returns the *ast.InterfaceType, its parent *ast.TypeSpec, and an error if not found.
func (*Package) FindTypeSpec ¶
FindTypeSpec searches for an *ast.TypeSpec by name within the package. It returns the found TypeSpec, the package it belongs to (p), and an error if not found.
func (*Package) GetImportPathBySelector ¶
func (p *Package) GetImportPathBySelector(ctx context.Context, selectorName string, astFile *ast.File) (string, *Package, error)
GetImportPathBySelector resolves a package selector (e.g., "json" from json.Marshal) used in a given astFile to its full import path and the resolved *Package. It uses the package's own resolved imports.
func (*Package) GetMethodsForType ¶
GetMethodsForType collects all *ast.FuncDecl that have typeName or *typeName as their receiver.
type PackageLocator ¶
type PackageLocator func(ctx context.Context, pattern string, buildCtx BuildContext) ([]PackageMetaInfo, error)
PackageLocator is a function type that locates packages based on a pattern and returns their metadata. The build context provides parameters like GOOS, GOARCH, and build tags.
type PackageMetaInfo ¶
type PackageMetaInfo struct {
ImportPath string // Canonical import path
Name string // Package name (can be empty if not determined by locator)
Dir string // Directory containing package sources
GoFiles []string // Go source files (non-test, relative to Dir)
TestGoFiles []string // _test.go files in package (relative to Dir)
XTestGoFiles []string // _test.go files for external tests (relative to Dir)
DirectImports []string // List of canonical import paths directly imported by this package
ModulePath string // Module path if part of a module
ModuleDir string // Module root directory if part of a module
Error string // Error message if package loading failed (from go list)
}
PackageMetaInfo holds basic information about a Go package, sufficient for initiating a lazy load.
func GoListLocator ¶
func GoListLocator(ctx context.Context, pattern string, buildCtx BuildContext) ([]PackageMetaInfo, error)
GoListLocator is a PackageLocator that uses `go list` command.
type PackageNotFoundError ¶
type PackageNotFoundError struct {
Path string
}
PackageNotFoundError indicates that a package could not be found.
func (*PackageNotFoundError) Error ¶
func (e *PackageNotFoundError) Error() string
type ParseError ¶
ParseError indicates an error during parsing of a Go source file.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
func (*ParseError) Unwrap ¶
func (e *ParseError) Unwrap() error
type StructInfo ¶
type StructInfo struct {
PackagePath string // Import path of the package containing this struct
Name string
Node *ast.TypeSpec // The AST node for the type spec (contains comments, etc.)
Fields []FieldInfo
// contains filtered or unexported fields
}
StructInfo represents information about a struct type.
type SymbolInfo ¶
type SymbolInfo struct {
// PackagePath is the import path of the package where the symbol is defined.
PackagePath string
// SymbolName is the name of the symbol (e.g., function name, type name).
SymbolName string
// FilePath is the canonical path to the .go file where the symbol is defined.
FilePath string
// Node is the AST node representing the symbol's declaration
// (e.g., *ast.FuncDecl, *ast.TypeSpec, *ast.ValueSpec).
Node ast.Node
}
SymbolInfo stores information about a resolved symbol, providing easy access to its definition and location.