Documentation ¶
Overview ¶
Package parse provides functionality to parse go files and create representations of code elements suitable for code generation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsSimpleType ¶
Types ¶
type Interface ¶
type Interface struct { // Name of the interface Name string // Package the interface is defined in Package string // Methods of the interface Methods []Method // Comments belonging to this interface, i.e. the comments directly above the type definition in the source code. Comments []string // File (path) this interface is defined in File string }
Interface represents an interface in a more compact way than a tree from the "go/ast" package, which makes it easier to work with for code generation.
type Method ¶
type Method struct { // Name of the method Name string // Parameters/Arguments Params []Param // Return values Returns []Param // Comments belonging to this method, i.e. the comments directly above the method definition in the source code. Comments []string }
Method represents a method of an interface.
type Module ¶
type Module struct { // root path of the module in the filesystem Path string // name of the module, e.g. "github.com/xyz/abc" Name string }
Module provides functions to convert relative package names and file paths to absolute ones, based on a module.
func NewModuleFromDir ¶
Will find the module the given directory belongs to by searching for a go.mod file in the directory and its parents.
func (Module) FullPackagePath ¶
Returns the full package path for the given relative package, i.e. the module name is added as a prefix.
func (Module) PackagePathFromFilePath ¶
Returns the package path from a given file path. E.g. if the root module path is /abc/xyz/somemodule, the module name is somemodule and the file path is "/abc/xyz/somemodule/internal/xyz/file.go" then the resulting package path would be "somemodule/internal/xyz". Note that for this to work the given file path must have the root path of the module as a prefix
func (Module) PackagePathWithoutModule ¶
Returns a package path without the module prefix. E.g. when called with "example.com/xyz/abc" on a module with name "example.com/xyz" will return "abc".
type Param ¶
type Param struct { // Name of the parameter, e.g. "ctx" for the parameter definition "ctx context.Context". // Can be empty e.g. for unnamed return values. Name string // Type of the parameter Type ParamType }
Param represents a method parameter or return value
type ParamType ¶
type ParamType interface { // Returns a list of all the packages required by this type. // E.g. a type map[context.Context]*http.Request requires the pacakges "context" and "http". Packages() []string }
Represents the type of a parameter. Note: this does not support some types like function types, channels and anonymous structs.
type SimpleType ¶
type SimpleType struct { // Name of the type, e.g. "string" or "Request" for "http.Request" Type string // Package the type is defined in, e.g. "http" for "http.Request" // Empty for built-in types. Package string }
Simple types are: bool, string, error, int, float (and all the variations of the numeric types), interface{}, structs like http.Request. They consist of just the type name and possibly a package prefix. On the other hand pointers (*http.Request), slices, maps, function types, channels, anonymous structs, ... are composite types.
func (SimpleType) Packages ¶
func (t SimpleType) Packages() []string