yamlbasics

package
v0.1.41 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2025 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Overview

This package provides some basic functions for working with yaml nodes. The assumption is to never directly encode/decode yaml. Instead, we'll convert to/from interface{}.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(targetArray *yaml.Node, values ...*yaml.Node) error

Append adds the given values to the end of the targetArray. If no values are given, then nothing is done. If targetArray is nil or not a sequence node, then an error is returned. If any of the values are nil, then an error is returned (and the array remains unchanged).

func AppendSlice

func AppendSlice(targetArray *yaml.Node, values []*yaml.Node) error

AppendSlice appends all entries in a slice to the end of the targetArray. If targetArray is nil or not a sequence node, then an error is returned. If the slice is nil, then nothing is done. If any of the values in the slice are nil, then an error is returned (and the array remains unchanged).

func CheckType added in v0.1.29

func CheckType(node *yaml.Node, expected NodeKind) error

CheckType returns an error if the given node was not of the expected type.

func CheckTypes added in v0.1.29

func CheckTypes(node *yaml.Node, expected []NodeKind) error

CheckTypes returns an error if the given node was not one of the expected types.

func CopyNode

func CopyNode(node *yaml.Node) *yaml.Node

CopyNode creates a deep copy of the given node.

func FindFieldKeyIndex

func FindFieldKeyIndex(targetObject *yaml.Node, key string) int

FindFieldKeyIndex returns the index of the Node that contains the object-Key in the targets Content array. If the key is not found, it returns -1.

func FindFieldValueIndex

func FindFieldValueIndex(targetObject *yaml.Node, key string) int

FindFieldValueIndex returns the index of the Node that contains the object-Value in the targets Content array. If the value is not found, it returns -1.

func FromObject

func FromObject(data map[string]interface{}) (*yaml.Node, error)

FromObject converts the given map[string]interface{} to an yaml node (map).

func GetFieldValue

func GetFieldValue(targetObject *yaml.Node, key string) *yaml.Node

GetFieldValue returns the value of the given key in the targetObject. If the key is not found, then nil is returned.

func NewArray

func NewArray() *yaml.Node

NewArray creates a new array node.

func NewObject

func NewObject() *yaml.Node

NewObject creates a new object node.

func NewString

func NewString(value string) *yaml.Node

NewString creates a new string node.

func RemoveField

func RemoveField(targetObject *yaml.Node, key string)

RemoveField removes the given key and its value from the targetObject if it exists.

func RemoveFieldByIdx

func RemoveFieldByIdx(targetObject *yaml.Node, idx int)

RemoveFieldByIdx removes the key (by its index) and its value from the targetObject.

func SetFieldValue

func SetFieldValue(targetObject *yaml.Node, key string, value *yaml.Node)

SetFieldValue sets/overwrites the value of the given key in the targetObject to the given value. If value is nil, then the key is removed from the targetObject if it exists.

func ToArray

func ToArray(data *yaml.Node) ([]interface{}, error)

ToArray converts the given yaml node to a []interface{}.

func ToObject

func ToObject(data *yaml.Node) (map[string]interface{}, error)

ToObject converts the given yaml node to a map[string]interface{}.

Types

type NodeKind added in v0.1.29

type NodeKind uint32

yamlType which can also represent the different scalar types. This is different from yaml.Kind!

const (
	TypeDocument NodeKind = NodeKind(yaml.DocumentNode)
	TypeArray    NodeKind = NodeKind(yaml.SequenceNode)
	TypeObject   NodeKind = NodeKind(yaml.MappingNode)
	TypeAlias    NodeKind = NodeKind(yaml.AliasNode)
	TypeScalar   NodeKind = NodeKind(yaml.ScalarNode)
	TypeNull     NodeKind = iota + 100
	TypeNumber
	TypeBool
	TypeString
)

yaml data types including the scalar types

func (NodeKind) String added in v0.1.29

func (t NodeKind) String() string

String returns the string representation of the node type.

type NodeSet added in v0.1.28

type NodeSet []*yaml.Node

represents a set of yaml nodes

func (*NodeSet) Intersection added in v0.1.28

func (mainSet *NodeSet) Intersection(set2 NodeSet) (intersection NodeSet, remainder NodeSet)

Intersection returns the intersection of the two sets of nodes. nil entries will be ignored. The result will a copy and have no duplicates. The second return value is the remainder of set2 after the intersection was removed (also a copy).

func (*NodeSet) IsIntersection added in v0.1.28

func (mainSet *NodeSet) IsIntersection(subset NodeSet) bool

IsIntersection returns true if all nodes in the subset also appear in the main set. nil entries will be ignored. Returns true if subset is empty.

func (*NodeSet) Subtract added in v0.1.28

func (mainSet *NodeSet) Subtract(setToSubtract NodeSet) NodeSet

Subtract returns the set of nodes that are in mainSet but not in setToSubtract. nil entries will be ignored. The result will have no duplicates.

func (*NodeSet) Union added in v0.1.28

func (mainSet *NodeSet) Union(sets ...NodeSet) NodeSet

Union returns the union of the two (or more) sets of nodes. nil entries will be ignored. The result will have no duplicates.

type SelectorSet added in v0.1.28

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

Represents a set of JSONpath selectors. Call NewSelectorSet to create one. The SelectorSet can be empty, in which case it will return only empty results.

func NewSelectorSet added in v0.1.28

func NewSelectorSet(selectors []string) (SelectorSet, error)

NewSelectorSet compiles the given selectors into a list of yaml nodes. If any of the selectors is invalid, an error will be returned. If the selectors are omitted/empty then an empty set is returned.

func (*SelectorSet) Find added in v0.1.28

func (set *SelectorSet) Find(nodeToSearch *yaml.Node) (NodeSet, error)

Find executes the given selectors on the given yaml node. The result will never be nil, will not have duplicates, but can be an empty array. An error is only returned if any of the selectors errors when searching. nodeToSearch cannot be nil, in which case it will panic.

func (*SelectorSet) GetSources added in v0.1.28

func (set *SelectorSet) GetSources() []string

GetSources returns a copy of the selector sources

func (*SelectorSet) IsEmpty added in v0.1.28

func (set *SelectorSet) IsEmpty() bool

IsEmpty returns true if the selector set is empty.

type YamlArrayIterator

type YamlArrayIterator func() (*yaml.Node, int, error)

YamlArrayIterator is a type of function returned by Search. On each call, it returns the next matching node in the targetArray. If no more matches are found, then nil is returned. The second return value is the index of the node in the targetArray.

func Search(targetArray *yaml.Node, match YamlArrayMatcher) YamlArrayIterator

Search returns a YamlArrayIterator function that can be called repeatedly to find the next matching node in the targetArray. If no more matches are found, then nil is returned. The search is resilient against changing the targetArray while searching. The YamlArrayMatcher function is called with each (non-nil) node in the targetArray. If the match function returns true, then the node is returned. If the match function returns false, then the next node is checked. If the match function returns an error, then the search is aborted and the error is returned (calling again returns the same error).

type YamlArrayMatcher

type YamlArrayMatcher func(*yaml.Node) (bool, error)

YamlArrayMatcher is a type of function passed to Search. To match a node against the search criteria, the function should return true if it matches.

Jump to

Keyboard shortcuts

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