2019-06-17 09:48:05 +00:00
|
|
|
// 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)
|
2020-05-11 10:06:07 +00:00
|
|
|
// "typed" nodes -> typed element.
|
2019-06-21 08:08:04 +00:00
|
|
|
func Decode(labels map[string]string, element interface{}, rootName string, filters ...string) error {
|
|
|
|
node, err := DecodeToNode(labels, rootName, filters...)
|
2019-06-17 09:48:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-28 20:56:04 +00:00
|
|
|
metaOpts := MetadataOpts{TagName: TagLabel, AllowSliceAsStruct: true}
|
|
|
|
err = AddMetadata(element, node, metaOpts)
|
2019-06-17 09:48:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-12 10:56:22 +00:00
|
|
|
return Fill(element, node, FillerOpts{AllowSliceAsStruct: true})
|
2019-06-17 09:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode converts an element to labels.
|
2020-05-11 10:06:07 +00:00
|
|
|
// element -> node (value) -> label (node).
|
2019-06-21 08:08:04 +00:00
|
|
|
func Encode(element interface{}, rootName string) (map[string]string, error) {
|
2019-11-28 20:56:04 +00:00
|
|
|
etnOpts := EncoderToNodeOpts{OmitEmpty: true, TagName: TagLabel, AllowSliceAsStruct: true}
|
|
|
|
node, err := EncodeToNode(element, rootName, etnOpts)
|
2019-06-17 09:48:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return EncodeNode(node), nil
|
|
|
|
}
|