tpath

package
v0.0.0-...-dea2efb Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

struct.go contains functions for traversing and modifying trees of Go structs.

tree.go contains functions for traversing and updating a tree constructed from yaml or json.Unmarshal. Nodes in such trees have the form map[interface{}]interface{} or map[interface{}][]interface{}. For some tree updates, like delete or append, it's necessary to have access to the parent node. PathContext is a tree constructed during tree traversal that gives access to ancestor nodes all the way up to the root, which can be used for this purpose.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddSpecRoot

func AddSpecRoot(tree string) (string, error)

AddSpecRoot adds a root node called "spec" to the given tree and returns the resulting tree.

func Delete

func Delete(root map[string]any, path util.Path) (bool, error)

Delete sets value at path of input untyped tree to nil

func Find

func Find(inputTree map[string]any, path util.Path) (any, bool, error)

Find returns the value at path from the given tree, or false if the path does not exist. It behaves differently from GetPathContext in that it never creates map entries at the leaf and does not provide a way to mutate the parent of the found node.

func GetConfigSubtree

func GetConfigSubtree(manifest, path string) (string, error)

GetConfigSubtree returns the subtree at the given path.

func GetFromStructPath

func GetFromStructPath(node any, path string) (any, bool, error)

GetFromStructPath returns the value at path from the given node, or false if the path does not exist.

func GetSpecSubtree

func GetSpecSubtree(yml string) (string, error)

GetSpecSubtree returns the subtree under "spec".

func MergeNode

func MergeNode(root any, path util.Path, value any) error

MergeNode merges value to the tree in root at the given path, creating any required missing internal nodes in path.

func Set

func Set(val, out any) error

Set sets out with the value at path from node. out is not set if the path doesn't exist or the value is nil.

func SetFromPath

func SetFromPath(node any, path string, out any) (bool, error)

SetFromPath sets out with the value at path from node. out is not set if the path doesn't exist or the value is nil. All intermediate along path must be type struct ptr. Out must be either a struct ptr or map ptr. TODO: move these out to a separate package (istio/istio#15494).

func WriteNode

func WriteNode(root any, path util.Path, value any) error

WriteNode writes value to the tree in root at the given path, creating any required missing internal nodes in path.

func WritePathContext

func WritePathContext(nc *PathContext, value any, merge bool) error

WritePathContext writes the given value to the Node in the given PathContext.

Types

type PathContext

type PathContext struct {
	// Parent in the Parent of this PathContext.
	Parent *PathContext
	// KeyToChild is the key required to reach the child.
	KeyToChild any
	// Node is the actual Node in the data tree.
	Node any
}

PathContext provides a means for traversing a tree towards the root.

func GetPathContext

func GetPathContext(root any, path util.Path, createMissing bool) (*PathContext, bool, error)

GetPathContext returns the PathContext for the Node which has the given path from root. It returns false and no error if the given path is not found, or an error code in other error situations, like a malformed path. It also creates a tree of PathContexts during the traversal so that Parent nodes can be updated if required. This is required when (say) appending to a list, where the parent list itself must be updated.

func (*PathContext) String

func (nc *PathContext) String() string

String implements the Stringer interface.

Jump to

Keyboard shortcuts

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