Filter env vars configuration
This commit is contained in:
parent
adc9a65ae3
commit
a918dcd5a4
19 changed files with 284 additions and 79 deletions
|
@ -9,9 +9,9 @@ import (
|
|||
|
||||
// EncodeToNode converts an element to a node.
|
||||
// element -> nodes
|
||||
func EncodeToNode(element interface{}, omitEmpty bool) (*Node, error) {
|
||||
func EncodeToNode(element interface{}, rootName string, omitEmpty bool) (*Node, error) {
|
||||
rValue := reflect.ValueOf(element)
|
||||
node := &Node{Name: "traefik"}
|
||||
node := &Node{Name: rootName}
|
||||
|
||||
encoder := encoderToNode{omitEmpty: omitEmpty}
|
||||
|
||||
|
|
|
@ -723,7 +723,7 @@ func TestEncodeToNode(t *testing.T) {
|
|||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
node, err := EncodeToNode(test.element, true)
|
||||
node, err := EncodeToNode(test.element, DefaultRootName, true)
|
||||
|
||||
if test.expected.error {
|
||||
require.Error(t, err)
|
||||
|
|
|
@ -6,18 +6,16 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
const labelRoot = "traefik"
|
||||
|
||||
// DecodeToNode converts the labels to a tree of nodes.
|
||||
// If any filters are present, labels which do not match the filters are skipped.
|
||||
func DecodeToNode(labels map[string]string, filters ...string) (*Node, error) {
|
||||
func DecodeToNode(labels map[string]string, rootName string, filters ...string) (*Node, error) {
|
||||
sortedKeys := sortKeys(labels, filters)
|
||||
|
||||
var node *Node
|
||||
for i, key := range sortedKeys {
|
||||
split := strings.Split(key, ".")
|
||||
|
||||
if split[0] != labelRoot {
|
||||
if split[0] != rootName {
|
||||
return nil, fmt.Errorf("invalid label root %s", split[0])
|
||||
}
|
||||
|
||||
|
|
|
@ -218,7 +218,7 @@ func TestDecodeToNode(t *testing.T) {
|
|||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
out, err := DecodeToNode(test.in, test.filters...)
|
||||
out, err := DecodeToNode(test.in, DefaultRootName, test.filters...)
|
||||
|
||||
if test.expected.error {
|
||||
require.Error(t, err)
|
||||
|
|
|
@ -2,6 +2,9 @@ package parser
|
|||
|
||||
import "reflect"
|
||||
|
||||
// DefaultRootName is the default name of the root node and the prefix of element name from the resources.
|
||||
const DefaultRootName = "traefik"
|
||||
|
||||
// MapNamePlaceholder is the placeholder for the map name.
|
||||
const MapNamePlaceholder = "<name>"
|
||||
|
||||
|
|
|
@ -7,8 +7,8 @@ package parser
|
|||
// 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{}, filters ...string) error {
|
||||
node, err := DecodeToNode(labels, filters...)
|
||||
func Decode(labels map[string]string, element interface{}, rootName string, filters ...string) error {
|
||||
node, err := DecodeToNode(labels, rootName, filters...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -28,8 +28,8 @@ func Decode(labels map[string]string, element interface{}, filters ...string) er
|
|||
|
||||
// Encode converts an element to labels.
|
||||
// element -> node (value) -> label (node)
|
||||
func Encode(element interface{}) (map[string]string, error) {
|
||||
node, err := EncodeToNode(element, true)
|
||||
func Encode(element interface{}, rootName string) (map[string]string, error) {
|
||||
node, err := EncodeToNode(element, rootName, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue