paths

package
v0.0.0-...-6cf7a4f Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: Apache-2.0 Imports: 5 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dereference

func Dereference(root ast.Node, path protopath.Path) ast.Node

Dereference walks the given path from the root node and returns the node at the end of the path.

func DereferenceAll

func DereferenceAll(root ast.Node, path protopath.Path) []ast.Node

DereferenceAll walks the given path from the root node and returns all nodes visited. The first node in the returned slice is the root node. Not all path steps reference an actual node in the AST, so the returned slice may contain fewer than len(path) nodes.

func Join

func Join[T protopath.Step, S ~[]T](a protopath.Path, b S) protopath.Path

Join returns a new path constructed by appending the steps of 'b' to 'a'. The first step in 'b' is skipped; it is assumed (but not checked for) that 'b' starts with a Root step matching the message type of the last step in path 'a'.

func NodeAt

func NodeAt[T ast.Node](idx PathIndex) (t T)

NodeAt returns the node at the given index in a path.

func NodeIsConcrete

func NodeIsConcrete(values protopath.Values, index int) bool

NodeIsConcrete returns true if the value at the given index in 'values' is a concrete node value, and false otherwise. A concrete node is a message that is not a wrapper node (or an array access to a wrapper node), and is not a list or map.

func NodeView

func NodeView(in func(v ast.Node)) func(v protopath.Values) error

NodeView returns a visitor suitable for use with ast.WithBefore and ast.WithAfter which calls the given function for each concrete node value encountered, ignoring wrapper nodes and other non-node values.

func Slice

func Slice(from protopath.Values, start, end int) protopath.Values

Slice returns a new path with both its path and values resliced by the given range. It is assumed that both slices are of the same length.

func Suffix2

func Suffix2[T, U ast.Node](values protopath.Values) (
	out struct {
		T      T
		TIndex int
		U      U
		UIndex int
	},
	ok bool,
)

Suffix2 inspects the given values and, if the path logically ends with the sequence of nodes 'T, U' (such that U is a message field of T), returns the two nodes and true. Otherwise, returns false. This method correctly handles non-node path steps such as list accesses. The returned nodes may not necessarily be the final 2 nodes in the path.

func Suffix3

func Suffix3[T, U, V ast.Node](values protopath.Values) (
	out struct {
		T      T
		TIndex int
		U      U
		UIndex int
		V      V
		VIndex int
	},
	ok bool,
)

func Suffix4

func Suffix4[T, U, V, W ast.Node](values protopath.Values) (
	out struct {
		T      T
		TIndex int
		U      U
		UIndex int
		V      V
		VIndex int
		W      W
		WIndex int
	},
	ok bool,
)

func Suffix5

func Suffix5[T, U, V, W, X ast.Node](values protopath.Values) (
	out struct {
		T      T
		TIndex int
		U      U
		UIndex int
		V      V
		VIndex int
		W      W
		WIndex int
		X      X
		XIndex int
	},
	ok bool,
)

func ValuesToNodes

func ValuesToNodes(values protopath.Values) (nodes []ast.Node)

ValuesToNodes returns a slice of nodes from the given values, filtering out wrapper nodes and other non-node values from the path.

Types

type AncestorTracker

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

AncestorTracker is used to track the path of nodes during a walk operation. By passing AsWalkOptions to a call to Walk, a visitor can inspect the path to the node being visited using this tracker.

func (*AncestorTracker) AsWalkOptions

func (t *AncestorTracker) AsWalkOptions() []ast.WalkOption

AsWalkOptions returns WalkOption values that will cause this ancestor tracker to track the path through the AST during the walk operation.

func (*AncestorTracker) Path

func (t *AncestorTracker) Path() protopath.Path

Path returns a slice of nodes that represents the path from the root of the walk operaiton to the currently visited node. The first element in the path is the root supplied to Walk. The last element in the path is the currently visited node.

func (*AncestorTracker) Values

func (t *AncestorTracker) Values() protopath.Values

Values returns a copy of the tracked path and values at the currently visited node.

type PathIndex

type PathIndex = struct {
	Step  protopath.Step
	Value protoreflect.Value
}

Jump to

Keyboard shortcuts

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