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 ¶
- func Append(targetArray *yaml.Node, values ...*yaml.Node) error
- func AppendSlice(targetArray *yaml.Node, values []*yaml.Node) error
- func CheckType(node *yaml.Node, expected NodeKind) error
- func CheckTypes(node *yaml.Node, expected []NodeKind) error
- func CopyNode(node *yaml.Node) *yaml.Node
- func FindFieldKeyIndex(targetObject *yaml.Node, key string) int
- func FindFieldValueIndex(targetObject *yaml.Node, key string) int
- func FromObject(data map[string]interface{}) (*yaml.Node, error)
- func GetFieldValue(targetObject *yaml.Node, key string) *yaml.Node
- func NewArray() *yaml.Node
- func NewObject() *yaml.Node
- func NewString(value string) *yaml.Node
- func RemoveField(targetObject *yaml.Node, key string)
- func RemoveFieldByIdx(targetObject *yaml.Node, idx int)
- func SetFieldValue(targetObject *yaml.Node, key string, value *yaml.Node)
- func ToArray(data *yaml.Node) ([]interface{}, error)
- func ToObject(data *yaml.Node) (map[string]interface{}, error)
- type NodeKind
- type NodeSet
- type SelectorSet
- type YamlArrayIterator
- type YamlArrayMatcher
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
CheckType returns an error if the given node was not of the expected type.
func CheckTypes ¶ added in v0.1.29
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 ¶
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 ¶
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 ¶
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 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.
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
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
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
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.
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 ¶
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 ¶
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 ¶
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.