astTraversal

package
v1.23.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidNodeType = errors.New("invalid node type")
	ErrInvalidIndex    = errors.New("invalid return index")
	ErrBuiltInFunction = errors.New("builtin function")
)

Functions

func FormatDoc

func FormatDoc(doc string) string

func Id added in v1.23.6

func Id(t *types.Struct) string

func LoadPackage

func LoadPackage(pkgPath string, workDir string) (*packages.Package, error)

LoadPackage loads a package from a path. Because of the way the packages.Load function works, we cache the packages to avoid loading the same package multiple times.

func LoadPackageNoCache

func LoadPackageNoCache(pkgPath string, workDir string) (*packages.Package, error)

LoadPackageNoCache loads a package from a path. This function will not use the cache when loading the package.

func ParseStructTag

func ParseStructTag(field string, tag string) (BindingTagMap, ValidationTagMap)

Types

type BaseTraverser

type BaseTraverser struct {
	Log      *zerolog.Logger
	Packages *PackageManager
	// contains filtered or unexported fields
}

func CreateTraverserFromTestFile

func CreateTraverserFromTestFile(testFilePath string) (*BaseTraverser, error)

func New

func New(workDir string) *BaseTraverser

func (*BaseTraverser) ActiveFile

func (t *BaseTraverser) ActiveFile() *FileNode

func (*BaseTraverser) CallExpression

func (t *BaseTraverser) CallExpression(node ast.Node) (*CallExpressionTraverser, error)

func (*BaseTraverser) Declaration

func (t *BaseTraverser) Declaration(node ast.Node, varName string) (*DeclarationTraverser, error)

func (*BaseTraverser) Expression

func (t *BaseTraverser) Expression(node ast.Expr) *ExpressionTraverser

func (*BaseTraverser) ExtractStatusCode

func (t *BaseTraverser) ExtractStatusCode(status ast.Node) (int, error)

ExtractStatusCode extracts the status code from a handler, assuming it's the first argument.

func (*BaseTraverser) ExtractVarName

func (t *BaseTraverser) ExtractVarName(node ast.Node) Result

func (*BaseTraverser) FindDeclarationForNode

func (t *BaseTraverser) FindDeclarationForNode(node ast.Node) (*DeclarationTraverser, error)

func (*BaseTraverser) Function

func (t *BaseTraverser) Function(node ast.Node) (*FunctionTraverser, error)

func (*BaseTraverser) Literal

func (t *BaseTraverser) Literal(node ast.Node, returnNum int) (*LiteralTraverser, error)

func (*BaseTraverser) Reset

func (t *BaseTraverser) Reset()

func (*BaseTraverser) SetActiveFile

func (t *BaseTraverser) SetActiveFile(file *FileNode) *BaseTraverser

func (*BaseTraverser) SetAddComponentFunction

func (t *BaseTraverser) SetAddComponentFunction(addComponent func(result Result) error) *BaseTraverser

func (*BaseTraverser) SetLog

func (t *BaseTraverser) SetLog(log *zerolog.Logger) *BaseTraverser

func (*BaseTraverser) Type

func (t *BaseTraverser) Type(node types.Type, packageNode *PackageNode) *TypeTraverser

type BindingTag

type BindingTag struct {
	Name           string `json:"name,omitempty" yaml:"name,omitempty"`
	NotShown       bool   `json:"not_shown,omitempty" yaml:"not_shown,omitempty"`
	ReturnOptional bool   `json:"return_optional,omitempty" yaml:"return_optional,omitempty"`
}

type BindingTagMap

type BindingTagMap map[BindingTagType]BindingTag

type BindingTagType

type BindingTagType string
const (
	HeaderBindingTag BindingTagType = "header"
	FormBindingTag   BindingTagType = "form"
	URIBindingTag    BindingTagType = "uri"
	JSONBindingTag   BindingTagType = "json"
	XMLBindingTag    BindingTagType = "xml"
	YAMLBindingTag   BindingTagType = "yaml"
	NoBindingTag     BindingTagType = ""
)

type CallExpressionTraverser

type CallExpressionTraverser struct {
	Traverser *BaseTraverser
	Node      *ast.CallExpr
	File      *FileNode
}

func (*CallExpressionTraverser) ArgIndex

func (c *CallExpressionTraverser) ArgIndex(argName string) (int, bool)

func (*CallExpressionTraverser) ArgType

func (c *CallExpressionTraverser) ArgType(argNum int) (types.Object, error)

func (*CallExpressionTraverser) Args

func (c *CallExpressionTraverser) Args() []ast.Expr

func (*CallExpressionTraverser) Function

func (*CallExpressionTraverser) ReturnType

func (c *CallExpressionTraverser) ReturnType(returnNum int) (types.Type, error)

func (*CallExpressionTraverser) Type

func (c *CallExpressionTraverser) Type() (*types.Func, error)

type DeclarationTraverser

type DeclarationTraverser struct {
	Traverser *BaseTraverser
	Decl      ast.Node
	File      *FileNode
	VarName   string
}

func (*DeclarationTraverser) Doc

func (d *DeclarationTraverser) Doc() (string, error)

func (*DeclarationTraverser) Value

func (d *DeclarationTraverser) Value() (ast.Node, error)

type ExpressionTraverser

type ExpressionTraverser struct {
	Traverser *BaseTraverser
	Node      ast.Expr
	File      *FileNode
	ReturnNum int
}

func (*ExpressionTraverser) BuiltInFunctions

func (e *ExpressionTraverser) BuiltInFunctions(callExpr *ast.CallExpr) (types.Type, error)

func (*ExpressionTraverser) SetReturnNum

func (e *ExpressionTraverser) SetReturnNum(returnNum int) *ExpressionTraverser

func (*ExpressionTraverser) Type

func (e *ExpressionTraverser) Type() (types.Type, error)

func (*ExpressionTraverser) Value

func (e *ExpressionTraverser) Value() (string, error)

type FileImport

type FileImport struct {
	Package *PackageNode
	Name    string
}

type FileNode

type FileNode struct {
	Package  *PackageNode
	FileName string
	Imports  []FileImport
	AST      *ast.File
}

func (*FileNode) FindImport

func (f *FileNode) FindImport(ident string) (FileImport, bool)

func (*FileNode) IsImportedPackage

func (f *FileNode) IsImportedPackage(ident string) bool

type FunctionTraverser

type FunctionTraverser struct {
	Traverser *BaseTraverser
	Node      *ast.FuncLit
	File      *FileNode
	DeclNode  *ast.FuncDecl
}

func (*FunctionTraverser) Arguments

func (f *FunctionTraverser) Arguments() []*ast.Field

func (*FunctionTraverser) Doc

func (f *FunctionTraverser) Doc() (string, error)

func (*FunctionTraverser) FindArgumentNameByType

func (f *FunctionTraverser) FindArgumentNameByType(typeName string, packagePath string, isPointer bool) string

func (*FunctionTraverser) Name

func (f *FunctionTraverser) Name() string

func (*FunctionTraverser) Results

func (f *FunctionTraverser) Results() []*ast.Field

type LiteralTraverser

type LiteralTraverser struct {
	Traverser *BaseTraverser
	Node      ast.Node
	ReturnNum int
}

func (*LiteralTraverser) Type

func (lt *LiteralTraverser) Type() (types.Type, error)

type PackageManager

type PackageManager struct {
	// contains filtered or unexported fields
}

func NewPackageManager

func NewPackageManager(workDir string) *PackageManager

func (*PackageManager) AddPackage

func (pm *PackageManager) AddPackage(pkgPath string) *PackageNode

func (*PackageManager) AddPathLoader

func (pm *PackageManager) AddPathLoader(loader PackagePathLoader)

func (*PackageManager) Find

func (pm *PackageManager) Find(path string) *PackageNode

func (*PackageManager) FindOrAdd

func (pm *PackageManager) FindOrAdd(path string) *PackageNode

func (*PackageManager) Get

func (*PackageManager) MapImportSpecs

func (pm *PackageManager) MapImportSpecs(imports []*ast.ImportSpec) []FileImport

type PackageNode

type PackageNode struct {
	Parent  *PackageNode
	Name    string
	Package *packages.Package
	Edges   []*PackageNode
	Files   []*FileNode

	// TypeDocMap is a map of type names to their documentation
	// We cache this to save iterating over types every time we need to find the documentation
	TypeDocMap map[string]string
}

func (*PackageNode) ASTAtPos

func (p *PackageNode) ASTAtPos(pos token.Pos) (ast.Node, error)

func (*PackageNode) AddFile

func (p *PackageNode) AddFile(file *FileNode)

func (*PackageNode) FindASTForType

func (p *PackageNode) FindASTForType(typeName string) (ast.Node, *FileNode, error)

func (*PackageNode) FindDocForType

func (p *PackageNode) FindDocForType(typeName string) (string, bool)

FindDocForType finds the documentation for a type in the package.

func (*PackageNode) FindObjectForIdent

func (p *PackageNode) FindObjectForIdent(ident *ast.Ident) (types.Object, error)

func (*PackageNode) FindObjectForIdentFuzzy

func (p *PackageNode) FindObjectForIdentFuzzy(ident *ast.Ident) (types.Object, error)

func (*PackageNode) FindObjectForName

func (p *PackageNode) FindObjectForName(name string) (types.Object, error)

func (*PackageNode) FindTypeForExpr

func (p *PackageNode) FindTypeForExpr(expr ast.Expr) (types.Type, error)

func (*PackageNode) FindUsesForIdent

func (p *PackageNode) FindUsesForIdent(ident *ast.Ident) (types.Object, error)

func (*PackageNode) Path

func (p *PackageNode) Path() string

type PackagePathLoader

type PackagePathLoader func(path string) (string, error)

type Result

type Result struct {
	// Type is the type of the result
	Type string

	// Package is the package of the result
	// It's used to reference the external package of a types.Named type
	// It's used as the package of a Slice, Array or Map Value
	Package *PackageNode

	// Name is the name of the result if it's a types.Named type
	Name string

	// Names is a list of names that are associated with the result (e.g. for a struct field)
	Names []string

	// IsEmbedded is true if the result is embedded in a struct
	IsEmbedded bool

	// ConstantValue is the constant value of the result (e.g. for a string)
	ConstantValue string

	// EnumValues is a list of enum values (e.g. for an enum { Foo, Bar })
	// This is used for when the result of a types.Named becomes a types.Basic and has constant values defined in the package
	EnumValues []any

	// MapKeyPackage is the package of the map key (e.g. for a map[string]string)
	MapKeyPackage *PackageNode

	// MapKeyType is the type of the map key (e.g. for a map[string]string)
	MapKeyType string

	// MapValueType is the type of the map value (e.g. for a map[string]string)
	MapValueType string

	// SliceType is the type of the slice (e.g. for a []string)
	SliceType string

	// ArrayType is the type of the array (e.g. for a [5]string)
	ArrayType string

	// ArrayLength is the length of the array (e.g. for a [5]string)
	ArrayLength int64

	// StructFields is a map of struct fields (e.g. for a struct { Foo string })
	StructFields map[string]Result

	StructFieldBindingTags BindingTagMap

	StructFieldValidationTags ValidationTagMap

	// Doc is the documentation of the result
	Doc string
}

type TypeTraverser

type TypeTraverser struct {
	Traverser *BaseTraverser
	Node      types.Type
	Package   *PackageNode
	// contains filtered or unexported fields
}

func (*TypeTraverser) Doc

func (t *TypeTraverser) Doc() (string, error)

func (*TypeTraverser) Result

func (t *TypeTraverser) Result() (Result, error)

func (*TypeTraverser) SetName

func (t *TypeTraverser) SetName(name string) *TypeTraverser

type ValidationTag

type ValidationTag struct {
	IsRequired bool `json:"is_required,omitempty" yaml:"is_required,omitempty"`
}

type ValidationTagMap

type ValidationTagMap map[ValidationTagType]ValidationTag

type ValidationTagType

type ValidationTagType string
const (
	GinValidationTag       ValidationTagType = "binding"
	ValidatorValidationTag ValidationTagType = "validate"
	NoValidationTag        ValidationTagType = ""
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL