traefik/pkg/config/parser/parser.go
Ludovic Fernandez 9b9f4be6a4 Add KV store providers (dynamic configuration only)
Co-authored-by: Jean-Baptiste Doumenjou <jb.doumenjou@gmail.com>
2019-11-28 21:56:04 +01:00

40 lines
1.3 KiB
Go

// 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
}
err = Fill(element, node, FillerOpts{AllowSliceAsStruct: true})
if err != nil {
return err
}
return nil
}
// 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
}