yamlfile

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Merge

func Merge(x1, x2 interface{}) (interface{}, error)

Merge merges x2 into x1. Merge rules are:

  • maps are merged on matching keys
  • any submap in from is added under the last matching key in into
  • 2 slices are merged by appending the from slice to the into slice
  • scalars from overwrite scalars in into
  • for all other combinations Merge with return an error

Types

type Warning

type Warning struct {
	Keys    []string
	Warning string
}

Warning holds the ordered list of nested keys for which a warning occurred and the associated warning itself

type Yaml

type Yaml struct {
	Node *yaml.Node
}

Yaml objects hold a yaml file in form of a yaml.Node object. This is the central object for all implementations in this package. The yaml.Node representation is recursive and every level of a yaml file is encoded into a dedicated yaml.Node. The different levels are nested via the yaml.Node.Content field. All Implementations in the yamlfile package follow this recursive approach and solve their requirements for each level in the recursive yaml.Node structure.

func New

func New(input []byte) (Yaml, error)

New unmarshalls a yaml input into a yaml.Node representation and returns a Yaml type.

func NewFromInterface

func NewFromInterface(i interface{}) (Yaml, error)

func PartialCopy

func PartialCopy(n Yaml, start, end int) Yaml

PartialCopy creates a copy of the input n but with only the subslice of the content n.Content[start:end]

func (Yaml) Copy

func (y Yaml) Copy() Yaml

Copy creates a deep copy of the Yaml object.

func (*Yaml) Decode

func (y *Yaml) Decode(v interface{}) error

Decode unmarshals the Yaml into a provided interface v.

func (*Yaml) Encode

func (y *Yaml) Encode(w io.Writer, indent int) error

Encode marshalls a Yaml into the provided Writer w. The number of space indentations in the output can be controlled via the indent parameter.

func (*Yaml) FilterBy

func (y *Yaml) FilterBy(filter string) error

FilterBy puts a positive filter on the Node in Yaml. Only elements remain that have the provided filter as a LineComment or a yaml Tag. E.g. for the filter "keepThis", the following yaml

key: "will be removed"
persistentComment: "will stay" # keepThis
persistentTag: !keepThis "will stay"
	root:
		nestedComment: "will stay" # keepThis
		nestedTag: !keepThis "will stay" # keepThis
	key: "will be removed"

turns into

persistentComment: "will stay" # keepThis
persistentTag: !keepThis "will stay"
root:
	nestedComment: "will stay" # keepThis
	nestedTag: !keepThis "will stay" # keepThis

func (*Yaml) FilterByKeys

func (y *Yaml) FilterByKeys(keys []string) error

FilterByKeys traverses the Yaml object via the ordered slice of keys and removes all parts that are not children of the full keys slice.

For the key slice: ["willStay", "subkey"]

the Yaml object:

key1: "will be removed"
key2: "will be removed"
willStay:
	subkey:
		s1: "will stay"
		s2: "will stay"
		s3: ["will stay", "will stay"]

will be reduced to

willStay:
	subkey:
		s1: "will stay"
		s2: "will stay"
		s3: ["will stay", "will stay"]

func (*Yaml) Insert

func (y *Yaml) Insert(parentKeys []string, subelement interface{}) ([]Warning, error)

func (*Yaml) Merge

func (y *Yaml) Merge(from Yaml) ([]Warning, error)

Merge merges the input Yaml (from) into the Yaml object (y). Merge rules are:

  • maps are merged on matching keys
  • any submap in from is added under the last matching key in into
  • slices are merged following the 2 rules: 1) merges happen element by element 2) if the keys in sub-elements match, elements are deep-merged
  • scalars from overwrite scalars in into
  • all other combinations the object in into is overwritten with the object in from

The resulting Yaml object is NOT sorted.

func (*Yaml) MergeBytes added in v1.5.0

func (y *Yaml) MergeBytes(from []byte) ([]Warning, error)

Like Merge just with a raw input

func (*Yaml) MergeSelective

func (y *Yaml) MergeSelective(from Yaml, selectFlag string) ([]Warning, error)

MergeSelective merges the input Yaml (from) into the Yaml object (y). It follows the same merge rules as the Merge function. Before merging, the MergeSelective function filters the from Yaml by the selectFlag and will only keep the elements that have the relevant flag (as line comment or yaml flag).

The resulting Yaml object is NOT sorted.

func (Yaml) SelectSubElement

func (y Yaml) SelectSubElement(keys []string) (Yaml, error)

func (*Yaml) Sort

func (y *Yaml) Sort()

Sort deeply sorts the Yaml object. Sorting rules:

  • maps are sorted alphabetically by key
  • arrays are not sorted

Jump to

Keyboard shortcuts

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