context

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare added in v0.9.4

func Compare(left GoLangVersion, right GoLangVersion) int

Types

type AdviceContext

type AdviceContext interface {
	AspectContext

	// Child creates a child of this context using the supplied node, property
	// name and index.
	Child(dst.Node, string, int) AdviceContext

	// ReplaceNode replaces the current AST node with the supplied one.
	ReplaceNode(dst.Node)

	// ParseSource parses Go source code from the provided bytes and returns a
	// *dst.File value.
	ParseSource([]byte) (*dst.File, error)

	// AddImport records a new synthetic import on this context.
	AddImport(path string, alias string) bool

	// AddLink records a new link-time requirement on this context.
	AddLink(string) bool

	// EnsureMinGoLang ensures that the current compile unit uses at least the
	// specified language level when passed to the compiler.
	EnsureMinGoLang(GoLangVersion)
}

type AspectContext

type AspectContext interface {
	// Chain returns the node chain at this context.
	Chain() *NodeChain

	// Node returns the node represented by this context. Upon entering a join
	// point, this is the node being inspected. Upon entering advice, this is the
	// node being advised.
	Node() dst.Node

	// Parent returns an AspectContext representing the current node's parent.
	// Returns nil if the current node is the root of the AST (usually true of
	// the *dst.File node).
	Parent() AspectContext

	// Config loops up the node chain to find a value for the provided
	// configuration key.
	Config(string) (string, bool)

	// File provides direct access to the AST file containing the current node.
	File() *dst.File

	// ImportPath returns the import path for the package containing this node.
	ImportPath() string

	// Package returns the name of the package containing this node.
	Package() string

	// TestMain returns true if the current node is in a synthetic main package.
	TestMain() bool

	// Release returns this context to the memory pool so that it can be reused
	// later.
	Release()
}

type ContextArgs added in v0.9.4

type ContextArgs struct {
	// Cursor denotes the current node and its context in the AST.
	Cursor *dstutil.Cursor
	// ImportPath is the fully qualified import path of the package containing the
	// current AST.
	ImportPath string
	// File is the AST of the file which the current node belongs in.
	File *dst.File
	// RefMap is the output reference map that will collect all synthetic
	// references added to the AST.
	RefMap *typed.ReferenceMap
	// SourceParser is used to parse generated source files.
	SourceParser SourceParser
	// MinGoLang is a pointer to the result value containing the minimum Go
	// language level required by the compile unit after it has been modified.
	MinGoLang *GoLangVersion
	// TestMain is true when injecting into a synthetic main package.
	TestMain bool
}

type GoLangVersion added in v0.9.4

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

GoLangVersion represents a go language level. It's a string of the form "go1.18".

func MustParseGoLangVersion added in v0.9.4

func MustParseGoLangVersion(lang string) GoLangVersion

func ParseGoLangVersion added in v0.9.4

func ParseGoLangVersion(lang string) (GoLangVersion, error)

func (GoLangVersion) Hash added in v1.0.0

func (GoLangVersion) IsAny added in v0.9.4

func (g GoLangVersion) IsAny() bool

IsAny returns true if the GoLang version selection is blank, meaning no particular constraint is imposed on language level.

func (*GoLangVersion) SetAtLeast added in v0.9.4

func (g *GoLangVersion) SetAtLeast(other GoLangVersion)

func (GoLangVersion) String added in v0.9.4

func (g GoLangVersion) String() string

func (*GoLangVersion) UnmarshalYAML added in v0.9.4

func (g *GoLangVersion) UnmarshalYAML(node *yaml.Node) error

type NodeChain

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

func (*NodeChain) Child

func (n *NodeChain) Child(cursor *dstutil.Cursor) *NodeChain

Child creates a new NodeChain that represents the node pointed to by the provided dstutil.Cursor. The cursor's parent must match the receiver. Values returned by NodeChain.Child should be returned to the pool by calling NodeChain.Release on them, as this allows for a significant reduction of allocations.

func (*NodeChain) Config

func (n *NodeChain) Config(name string) (string, bool)

func (*NodeChain) Context

func (n *NodeChain) Context(args ContextArgs) *context

Context returns a new *context instance that represents the ndoe at the provided cursor. The context.Release function should be called on values returned by this function to allow for memory re-use, which can significantly reduce allocations performed during AST traversal.

func (*NodeChain) Index

func (n *NodeChain) Index() int

func (*NodeChain) Node

func (n *NodeChain) Node() dst.Node

func (*NodeChain) Parent

func (n *NodeChain) Parent() *NodeChain

func (*NodeChain) PropertyName

func (n *NodeChain) PropertyName() string

func (*NodeChain) Release added in v0.9.1

func (n *NodeChain) Release()

Release returns the NodeChain to the backing pool so that it can be re-used. This signficicantly reduces allocations while walking ASTs, as one NodeChain is used for each AST node, but they are very short-lived.

func (*NodeChain) SetConfig

func (n *NodeChain) SetConfig(val map[string]string)

type SourceParser

type SourceParser interface {
	Parse(any) (*dst.File, error)
}

Jump to

Keyboard shortcuts

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