yamlfile

package
v2.2.9 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DocOutput

func DocOutput(s interface{}) interface{}

DocOutput constructs a documentation interface from the provided struct by using the struct field types as well as the information given in the "doc" struct tag. In the doc tag the following information can be given in a comma-separated list:

  • msg: describing message
  • default: default value
  • req: required field
  • option,o: 1 possible value (multiple values are specified by multiple occurrences)

e.g.: `doc:"msg=this is my message,default=0,o=0, o=1, req"`. If no "doc" tag is present the field will be ignored. The output format is map[string]interface{}, where the interface is either a doc-string or a substructure of the same format.

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 ArrayMergePolicy added in v2.2.4

type ArrayMergePolicy uint8
const (
	Standard ArrayMergePolicy = 1 << iota
	Strict
)

type UpdateSettingsFunc added in v2.2.4

type UpdateSettingsFunc func(*settings)

func SetArrayMergePolicy added in v2.2.4

func SetArrayMergePolicy(policy ArrayMergePolicy) UpdateSettingsFunc

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
	// contains filtered or unexported fields
}

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, opts ...UpdateSettingsFunc) (Yaml, error)

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

func NewFromInterface

func NewFromInterface(i interface{}, opts ...UpdateSettingsFunc) (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 either with standard policy: 1) merges happen element by element 2) if the keys in sub-elements match, elements are deep-merged or with strict policy: 1) the from value overwrites the into value entirely.
  • 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

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