File: parser.go

package info (click to toggle)
golang-github-traefik-paerser 0.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 612 kB
  • sloc: makefile: 14
file content (35 lines) | stat: -rw-r--r-- 1,288 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Package parser implements decoding and encoding between a flat map of labels and a typed Configuration.
package parser

// Decode decodes the given map of labels into the given element.
// If any filters are present, labels which do not match the filters are skipped.
// The operation goes through three stages roughly summarized as:
// labels -> tree of untyped nodes
// untyped nodes -> nodes augmented with metadata such as kind (inferred from element)
// "typed" nodes -> typed element.
func Decode(labels map[string]string, element interface{}, rootName string, filters ...string) error {
	node, err := DecodeToNode(labels, rootName, filters...)
	if err != nil {
		return err
	}

	metaOpts := MetadataOpts{TagName: TagLabel, AllowSliceAsStruct: true}
	err = AddMetadata(element, node, metaOpts)
	if err != nil {
		return err
	}

	return Fill(element, node, FillerOpts{AllowSliceAsStruct: true})
}

// Encode converts an element to labels.
// element -> node (value) -> label (node).
func Encode(element interface{}, rootName string) (map[string]string, error) {
	etnOpts := EncoderToNodeOpts{OmitEmpty: true, TagName: TagLabel, AllowSliceAsStruct: true}
	node, err := EncodeToNode(element, rootName, etnOpts)
	if err != nil {
		return nil, err
	}

	return EncodeNode(node), nil
}