yamlbasics

package
v0.1.24 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2023 License: Apache-2.0 Imports: 4 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 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 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