Documentation
¶
Index ¶
- Variables
- func AppendNode(listNode, valNode *yaml.Node) error
- func AssignMapNode(mapNode, keyNode, valNode *yaml.Node) error
- func AssignNode(destNode, srcNode *yaml.Node)
- func CopyNode(src *yaml.Node) *yaml.Node
- func Equal(a *yaml.Node, b *yaml.Node) bool
- func ErrDecode(err error) error
- func ErrFilename(err error, filename string) error
- func GetIndex(parent *yaml.Node, target *yaml.Node) int
- func GetKey(mapNode *yaml.Node, key interface{}) (node *yaml.Node)
- func GetKeyValue(mapNode *yaml.Node, key *yaml.Node) (keyNode *yaml.Node, valueNode *yaml.Node)
- func HasKey(mapNode *yaml.Node, key interface{}) bool
- func Indirect(node *yaml.Node) *yaml.Node
- func IsNull(node *yaml.Node) bool
- func KindString(k yaml.Kind) string
- func NewBoolNode(value bool) *yaml.Node
- func NewDocumentNode() *yaml.Node
- func NewFloatNode(value float64) *yaml.Node
- func NewIntNode(value int64) *yaml.Node
- func NewMappingNode() *yaml.Node
- func NewSequenceNode() *yaml.Node
- func NewStringNode(value string) *yaml.Node
- func NewYAMLError(err error, node *yaml.Node) error
- func RangeMap(node *yaml.Node, f RangerFunc, opts ...RangeOption) error
- func ReadFile(filepath string) (*yaml.Node, error)
- func Remove(parent *yaml.Node, target *yaml.Node) bool
- func ShallowCopyNode(src *yaml.Node) *yaml.Node
- func SortableNodeMap(mapNode *yaml.Node) sort.Interface
- func ToNode(val interface{}) (*yaml.Node, error)
- func UnwrapDocument(node *yaml.Node) *yaml.Node
- func Walk(node *yaml.Node, f WalkFunc, walkOpts ...WalkOpt) error
- func WalkPath(root *yaml.Node, fn NodeFunc, path ...interface{}) error
- func WalkPathMatchers(root *yaml.Node, fn NodeFunc, matchers ...PathMatcher) error
- type NodeFunc
- type PathMatcher
- type RangeOption
- type RangerFunc
- type WalkFunc
- type WalkOpt
- type WalkOptions
- type WalkStatus
- type YAMLError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrStopRange = errors.New("stop ranging")
ErrStopRange can be returned from the RangerFunc to immediately stop iterating over the map. If this is returned from the RangerFunc then RangeMap will return nil.
Functions ¶
func AppendNode ¶
func AssignMapNode ¶
func AssignNode ¶
AssignNode copies over the structure data from `srcNode` leaving the document data alone (comments, line numbers etc are preserved in `destNode`).
func ErrFilename ¶
func GetIndex ¶
GetIndex returns the index of the target node found in the parent node. If the parent is a MappingNode the index corresponds to the key node (the value will be the key node index + 1). If the parent node is not a SequenceNode or a MappingNode, then -1 will be returned. Also if the target node is not found then -1 will be returned.
func GetKeyValue ¶
GetKeyValue is used to to simplify getting both the key and value nodes from the provided MappingNode. If the key node is not found then the returned nodes will both be `nil`
func Indirect ¶
Indirect will return the aliased node if this node is an alias, otherwise it will return the original node.
func KindString ¶
KindString will return a human-readable string that represents the yaml.Kind arguments.
func NewBoolNode ¶
NewBoolNode creates a new Node with the value of the provided bool.
func NewDocumentNode ¶
func NewFloatNode ¶
NewFloatNode creates a new Node with the value of the provided float64.
func NewIntNode ¶
NewIntNode creates a new Node with the value of the provided int64.
func NewMappingNode ¶
func NewSequenceNode ¶
func NewStringNode ¶
NewStringNode creates a new Node with the value of the provided string.
func RangeMap ¶
func RangeMap(node *yaml.Node, f RangerFunc, opts ...RangeOption) error
RangeMap will iterate over `node`, calling the RangerFunc for each key/value pair. An error will be returned if the node is not a mapping node (or an alias referencing a mapping node). An error will be returned unless the node.Content is an even length. If the RangerFunc returns an error it will be returned immediately.
func Remove ¶
Remove will delete target node from parent node. If parent is a MappingNode then target should correspond to the mapping Key. If parent is a SequenceNode then the target node will be deleted. Returns true if and only if the target was found in the parent.
func ShallowCopyNode ¶
ShallowCopyNode will do a shallow copy of the src Node and return a copy. Any Contents and Alias will not be copied.
func UnwrapDocument ¶
UnwrapDocument removes the root node if and only if the root node has a Kind value of `yaml.DocumentNode`. It returns the first child node of the root, which is typically the document data.
func WalkPath ¶
Example ¶
package main
import (
"fmt"
"sort"
"github.com/coryb/walky"
"gopkg.in/yaml.v3"
)
func main() {
doc := `# foo is a bar
foo: bar
someMap:
- someKey: value # line comment
# bin is a baz
bin: baz
`
var root yaml.Node
_ = yaml.Unmarshal([]byte(doc), &root)
// we can choose to sort the map if we want
sort.Sort(walky.SortableNodeMap(&root))
// create new *yaml.Node from interface{} value
newNode, _ := walky.ToNode("new\nvalue")
// walk the document tree, looking for keys `someMap`, then
// index 0, then key `someKey`. That node resulting from
// the walk is passed to the `func(node *yaml.Node) error`
// callback, where we reassign the node.
_ = walky.WalkPath(&root, func(node *yaml.Node) error {
walky.AssignNode(node, newNode)
return nil
}, "someMap", 0, "someKey")
// we can also insert new keys to maps and slices
keyNode, _ := walky.ToNode("newkey")
newNode, _ = walky.ToNode("new value")
// Add comments via code
newNode.LineComment = "<-- Look Here"
_ = walky.AssignMapNode(&root, keyNode, newNode)
// now we will fetch the sequence under someMap and
// append a new value to it
newNode, _ = walky.ToNode(42)
_ = walky.WalkPath(&root, func(node *yaml.Node) error {
walky.AppendNode(node, newNode)
return nil
}, "someMap")
newDoc, _ := yaml.Marshal(&root)
fmt.Println(string(newDoc))
}
Output: # bin is a baz bin: baz # foo is a bar foo: bar newkey: new value # <-- Look Here someMap: - someKey: |- # line comment new value - 42
func WalkPathMatchers ¶
func WalkPathMatchers(root *yaml.Node, fn NodeFunc, matchers ...PathMatcher) error
Types ¶
type PathMatcher ¶
func AnyMatcher ¶
func AnyMatcher(walkOpts ...WalkOpt) PathMatcher
func IndexMatcher ¶
func IndexMatcher(i int) PathMatcher
func NodeMatcher ¶
func NodeMatcher(n *yaml.Node) PathMatcher
func StringMatcher ¶
func StringMatcher(key string) PathMatcher
type RangeOption ¶
type RangeOption func(*rangeOption)
func WithMergesLast ¶
func WithMergesLast() RangeOption
WithMergesLast changes the `!!merge` node processing from being inline to be appended to the current iteration. For example:
defs:
- &mymap {key: 1}
mystuff:
<<: *mymap
key: 2
by default RangeMap will process the `!!merge` since it is the first key in `mystuff`, so the RangerFunc will see `key: 1` first, then `key: 2`. If `WithMergesLast` is used, then the RangerFunc will see `key: 2` first, then `key: 1`
type RangerFunc ¶
RangerFunc is the callback used to iterate over a map via RangeMap. The function is called for each key/value pair found in the map. If an error is returned then RangeMap will immediately return the error. To stop iteration without RangeMap returning an error, you can return ErrStopRange.
type WalkFunc ¶
type WalkFunc func(current, parent *yaml.Node, position int, opts *WalkOptions) (WalkStatus, error)
func IndexWalker ¶
func ScalarValuesWalker ¶
func StringWalker ¶
StringWalker is used with Walk to apply `f` to map values that match the provided key string. If the match is against a map key then the `NodeFunc` will be called with the map value. If the match is not a map key, then the `NodeFunc` will be called on the matched node.
type WalkOpt ¶
type WalkOpt func(*WalkOptions)
func WithBreadthFirst ¶
func WithBreadthFirst() WalkOpt
func WithFirstOnly ¶
func WithFirstOnly() WalkOpt
func WithMaxDepth ¶
type WalkOptions ¶
type WalkOptions struct {
// contains filtered or unexported fields
}
func (*WalkOptions) MatchStatus ¶
func (opts *WalkOptions) MatchStatus() WalkStatus
func (*WalkOptions) MissStatus ¶
func (opts *WalkOptions) MissStatus() WalkStatus
type WalkStatus ¶
type WalkStatus int
const ( WalkExit WalkStatus = iota WalkDepthFirst WalkBreadthFirst WalkPrune )
func (WalkStatus) String ¶
func (ws WalkStatus) String() string