Documentation
¶
Overview ¶
Package goparser parses go modules and extracts module, package, struct, and interface information.
It is used by Blueprint to parse workflow specs.
Index ¶
- func ExprStr(e ast.Expr) string
- type ModuleInfo
- type ParsedField
- type ParsedFile
- func (f *ParsedFile) LoadFuncs() error
- func (f *ParsedFile) LoadImports() error
- func (f *ParsedFile) LoadStructsAndInterfaces() error
- func (f *ParsedFile) LoadVars() error
- func (f *ParsedFile) ResolveIdent(name string, typeParams ...string) gocode.TypeName
- func (f *ParsedFile) ResolveSelector(packageShortName string, name string) gocode.TypeName
- func (f *ParsedFile) ResolveType(expr ast.Expr, typeParams ...string) gocode.TypeName
- func (f *ParsedFile) String() string
- type ParsedFunc
- type ParsedImport
- type ParsedInterface
- type ParsedModule
- type ParsedModuleSet
- func (set *ParsedModuleSet) Add(info *ModuleInfo) (*ParsedModule, error)
- func (set *ParsedModuleSet) AddModule(srcDir string) (*ParsedModule, error)
- func (set *ParsedModuleSet) AddModules(srcDirs ...string) error
- func (set *ParsedModuleSet) AddWorkspace(workspaceDir string) error
- func (set *ParsedModuleSet) FindInterface(pkgName string, name string) (*ParsedInterface, error)
- func (set *ParsedModuleSet) FindStruct(pkgName string, name string) (*ParsedStruct, error)
- func (set *ParsedModuleSet) GetPackage(name string) (*ParsedPackage, error)
- func (set *ParsedModuleSet) String() string
- type ParsedPackage
- type ParsedStruct
- type ParsedVar
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ModuleInfo ¶
type ModuleInfo struct { ShortName string // The last part of the module path Path string // Fully qualified name of the module Version string // Version of the module Dir string // Directory containing the module source IsLocal bool // True if the module is local (ie with a replace directive), false if it's from gocache GoModule *packages.Module // The underlying golang module from [golang.org/x/tools/go/packages] }
Metadata about a module, such as its version and location on the local file system.
func FindModule ¶
func FindModule[T any]() (*ModuleInfo, *gocode.UserType, error)
Finds and returns the module info for a type.
func FindPackageModule ¶
func FindPackageModule(pkgName string) (*ModuleInfo, error)
Get the module info for a package.
func GetModuleInfo ¶
func GetModuleInfo(moduleName string) (*ModuleInfo, error)
Get the info for a module. Better than reading the go.mod. Better than calling FindPackageModule because the root of the module doesn't need to be a golang package.
func (*ModuleInfo) String ¶
func (m *ModuleInfo) String() string
type ParsedField ¶
func (*ParsedField) Parse ¶
func (f *ParsedField) Parse() error
func (*ParsedField) String ¶
func (f *ParsedField) String() string
type ParsedFile ¶
type ParsedFile struct { Package *ParsedPackage Name string // Filename Path string // Fully qualified path to the file PathInModule string // Path within the module to the file AnonymousImports []*ParsedImport // Import declarations that were imported with . NamedImports map[string]*ParsedImport // Import declarations - map from shortname to fully qualified package import name Ast *ast.File // The AST of the file }
func (*ParsedFile) LoadFuncs ¶
func (f *ParsedFile) LoadFuncs() error
Assumes that all structs and interfaces have been loaded for the package containing the file.
Loads the names of all funcs. If the func has a receiver type, then it is saved as a method on the appropriate struct; if it does not have a receiver type, then it is saved as a package func.
This does not parse the arguments or returns of the func
func (*ParsedFile) LoadImports ¶
func (f *ParsedFile) LoadImports() error
func (*ParsedFile) LoadStructsAndInterfaces ¶
func (f *ParsedFile) LoadStructsAndInterfaces() error
Looks for:
- structs defined in the file
- interfaces defined in the file
- other user types defined in the file
Does not:
- look for function declarations
func (*ParsedFile) ResolveIdent ¶
func (f *ParsedFile) ResolveIdent(name string, typeParams ...string) gocode.TypeName
An ident can be:
- a basic type, like int64, float32 etc.
- any
- a type declared locally within the file or package
- a type imported with an `import . "package"` decl
- a generic type from a struct or func's type params
func (*ParsedFile) ResolveSelector ¶
func (f *ParsedFile) ResolveSelector(packageShortName string, name string) gocode.TypeName
func (*ParsedFile) ResolveType ¶
If the expr is in the context of a generic struct or func, typeParams provides the additional named type params
func (*ParsedFile) String ¶
func (f *ParsedFile) String() string
type ParsedFunc ¶
type ParsedFunc struct { gocode.Func File *ParsedFile Ast *ast.FuncType }
func (*ParsedFunc) AsConstructor ¶
func (f *ParsedFunc) AsConstructor() *gocode.Constructor
func (*ParsedFunc) Parse ¶
func (f *ParsedFunc) Parse() error
func (*ParsedFunc) String ¶
func (f *ParsedFunc) String() string
type ParsedImport ¶
type ParsedImport struct { File *ParsedFile Package string }
type ParsedInterface ¶
type ParsedInterface struct { File *ParsedFile Ast *ast.InterfaceType Name string Methods map[string]*ParsedFunc }
func (*ParsedInterface) ServiceInterface ¶
func (iface *ParsedInterface) ServiceInterface(ctx ir.BuildContext) *gocode.ServiceInterface
func (*ParsedInterface) String ¶
func (i *ParsedInterface) String() string
func (*ParsedInterface) Type ¶
func (iface *ParsedInterface) Type() *gocode.UserType
type ParsedModule ¶
type ParsedModule struct { ShortName string // Short name of the module Name string // Fully qualified name of the module Version string // Version of the module SrcDir string // Fully qualified location of the module on the filesystem IsLocal bool // Is this a local module or from the go cache? Modfile *modfile.File // The modfile File struct is sufficiently simple that we just use it directly Packages map[string]*ParsedPackage // Map from fully qualified package name to ParsedPackage }
func (*ParsedModule) Load ¶
func (mod *ParsedModule) Load() error
func (*ParsedModule) String ¶
func (mod *ParsedModule) String() string
type ParsedModuleSet ¶
type ParsedModuleSet struct { Modules map[string]*ParsedModule // Map from FQ module name to module object ModuleDirs map[string]*ParsedModule // Map from module SrcDir to module object Parent *ParsedModuleSet // Another module set to consult for modules if not present in this one }
Represents a set of code modules that have been parsed.
func New ¶
func New(parent *ParsedModuleSet) *ParsedModuleSet
Returns a new *ParsedModuleSet, optionally with a parent module set, which can be nil.
func (*ParsedModuleSet) Add ¶
func (set *ParsedModuleSet) Add(info *ModuleInfo) (*ParsedModule, error)
Adds a module to the parsed module set. This is the preferred method for adding modules, versus [AddModule].
info for a module can be acquired by calling FindPackageModule or FindModule
func (*ParsedModuleSet) AddModule ¶
func (set *ParsedModuleSet) AddModule(srcDir string) (*ParsedModule, error)
Manually parse and add a module to the parsed module set
If the srcDir has already been parsed, then this function will do nothing.
If [set.Parent] is not nil, then the module will be copied from [set.Parent] rather than re-parsed.
If [set] already contains a module with the same FQ module name as the one in srcDir then this function will return an error.
The parsed module will be assumed to be a local module; if it is not, then set [IsLocal] to false
func (*ParsedModuleSet) AddModules ¶
func (set *ParsedModuleSet) AddModules(srcDirs ...string) error
Manually parse and add multiple modules to the set.
Equivalent to calling [AddModule] for each srcDir. If a srcDir has already been parsed then it will not be re-parsed.
Returns an error if any of the modules cannot be parsed.
func (*ParsedModuleSet) AddWorkspace ¶
func (set *ParsedModuleSet) AddWorkspace(workspaceDir string) error
Parses and adds all modules in the specified workspaceDir
func (*ParsedModuleSet) FindInterface ¶
func (set *ParsedModuleSet) FindInterface(pkgName string, name string) (*ParsedInterface, error)
Looks up the specified interface, possibly searching for and parsing the package.
Returns an error if the package cannot be found or parsed.
Returns the *ParsedInterface if found, or nil if no such interface exists in the package.
func (*ParsedModuleSet) FindStruct ¶
func (set *ParsedModuleSet) FindStruct(pkgName string, name string) (*ParsedStruct, error)
Looks up the specified struct, possibly searching for and parsing the package.
Returns an error if the package cannot be found or parsed.
Returns the *ParsedStruct if found, or nil if no such struct exists in the package.
func (*ParsedModuleSet) GetPackage ¶
func (set *ParsedModuleSet) GetPackage(name string) (*ParsedPackage, error)
Gets the *ParsedPackage for the specified name, possibly searching for and parsing the package.
This method will return an error if the package was not found, or if there was a parse error
func (*ParsedModuleSet) String ¶
func (set *ParsedModuleSet) String() string
type ParsedPackage ¶
type ParsedPackage struct { Module *ParsedModule Name string // Fully qualified name of the package including module name ShortName string // Shortname of the package (ie, the name used in an import statement) PackageDir string // Subdirectory within the module containing the package SrcDir string // Fully qualified location of the package on the filesystem Files map[string]*ParsedFile // Map from filename to ParsedFile Ast *ast.Package // The AST of the package DeclaredTypes map[string]gocode.UserType // Types declared within this package Structs map[string]*ParsedStruct // Structs parsed from this package Interfaces map[string]*ParsedInterface // Interfaces parsed from this package Funcs map[string]*ParsedFunc // Functions parsed from this package (does not include funcs with receiver types) Vars map[string]*ParsedVar // Vars declared in this package; we save their AST but don't process them }
func (*ParsedPackage) Load ¶
func (pkg *ParsedPackage) Load() error
func (*ParsedPackage) Parse ¶
func (pkg *ParsedPackage) Parse() error
func (*ParsedPackage) String ¶
func (pkg *ParsedPackage) String() string
type ParsedStruct ¶
type ParsedStruct struct { File *ParsedFile Ast *ast.StructType Name string Methods map[string]*ParsedFunc // Methods declared directly on this struct, does not include promoted methods (not implemented yet) FieldsList []*ParsedField // All fields in the order that they are declared Fields map[string]*ParsedField // Named fields declared in this struct only, does not include promoted fields (not implemented yet) PromotedField *ParsedField // If there is a promoted field, stored here AnonymousFields []*ParsedField // Subsequent anonymous fields TypeParams []string // Names of generic type parameters }
func (*ParsedStruct) String ¶
func (f *ParsedStruct) String() string
func (*ParsedStruct) Type ¶
func (struc *ParsedStruct) Type() *gocode.UserType