Move code to pkg
This commit is contained in:
parent
bd4c822670
commit
f1b085fa36
465 changed files with 656 additions and 680 deletions
306
pkg/provider/label/internal/element_fill.go
Normal file
306
pkg/provider/label/internal/element_fill.go
Normal file
|
@ -0,0 +1,306 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/containous/flaeg/parse"
|
||||
)
|
||||
|
||||
type initializer interface {
|
||||
SetDefaults()
|
||||
}
|
||||
|
||||
// Fill the fields of the element.
|
||||
// nodes -> element
|
||||
func Fill(element interface{}, node *Node) error {
|
||||
if element == nil || node == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if node.Kind == 0 {
|
||||
return fmt.Errorf("missing node type: %s", node.Name)
|
||||
}
|
||||
|
||||
elem := reflect.ValueOf(element)
|
||||
if elem.Kind() == reflect.Struct {
|
||||
return fmt.Errorf("struct are not supported, use pointer instead")
|
||||
}
|
||||
|
||||
return fill(elem.Elem(), node)
|
||||
}
|
||||
|
||||
func fill(field reflect.Value, node *Node) error {
|
||||
// related to allow-empty tag
|
||||
if node.Disabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch field.Kind() {
|
||||
case reflect.String:
|
||||
field.SetString(node.Value)
|
||||
return nil
|
||||
case reflect.Bool:
|
||||
val, err := strconv.ParseBool(node.Value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
field.SetBool(val)
|
||||
return nil
|
||||
case reflect.Int8:
|
||||
return setInt(field, node.Value, 8)
|
||||
case reflect.Int16:
|
||||
return setInt(field, node.Value, 16)
|
||||
case reflect.Int32:
|
||||
return setInt(field, node.Value, 32)
|
||||
case reflect.Int64, reflect.Int:
|
||||
return setInt(field, node.Value, 64)
|
||||
case reflect.Uint8:
|
||||
return setUint(field, node.Value, 8)
|
||||
case reflect.Uint16:
|
||||
return setUint(field, node.Value, 16)
|
||||
case reflect.Uint32:
|
||||
return setUint(field, node.Value, 32)
|
||||
case reflect.Uint64, reflect.Uint:
|
||||
return setUint(field, node.Value, 64)
|
||||
case reflect.Float32:
|
||||
return setFloat(field, node.Value, 32)
|
||||
case reflect.Float64:
|
||||
return setFloat(field, node.Value, 64)
|
||||
case reflect.Struct:
|
||||
return setStruct(field, node)
|
||||
case reflect.Ptr:
|
||||
return setPtr(field, node)
|
||||
case reflect.Map:
|
||||
return setMap(field, node)
|
||||
case reflect.Slice:
|
||||
return setSlice(field, node)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func setPtr(field reflect.Value, node *Node) error {
|
||||
if field.IsNil() {
|
||||
field.Set(reflect.New(field.Type().Elem()))
|
||||
|
||||
if field.Type().Implements(reflect.TypeOf((*initializer)(nil)).Elem()) {
|
||||
method := field.MethodByName("SetDefaults")
|
||||
if method.IsValid() {
|
||||
method.Call([]reflect.Value{})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fill(field.Elem(), node)
|
||||
}
|
||||
|
||||
func setStruct(field reflect.Value, node *Node) error {
|
||||
for _, child := range node.Children {
|
||||
fd := field.FieldByName(child.FieldName)
|
||||
|
||||
zeroValue := reflect.Value{}
|
||||
if fd == zeroValue {
|
||||
return fmt.Errorf("field not found, node: %s (%s)", child.Name, child.FieldName)
|
||||
}
|
||||
|
||||
err := fill(fd, child)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func setSlice(field reflect.Value, node *Node) error {
|
||||
if field.Type().Elem().Kind() == reflect.Struct {
|
||||
return setSliceAsStruct(field, node)
|
||||
}
|
||||
|
||||
if len(node.Value) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
values := strings.Split(node.Value, ",")
|
||||
|
||||
slice := reflect.MakeSlice(field.Type(), len(values), len(values))
|
||||
field.Set(slice)
|
||||
|
||||
for i := 0; i < len(values); i++ {
|
||||
value := strings.TrimSpace(values[i])
|
||||
|
||||
switch field.Type().Elem().Kind() {
|
||||
case reflect.String:
|
||||
field.Index(i).Set(reflect.ValueOf(value))
|
||||
case reflect.Int:
|
||||
val, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
field.Index(i).SetInt(val)
|
||||
case reflect.Int8:
|
||||
err := setInt(field.Index(i), value, 8)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case reflect.Int16:
|
||||
err := setInt(field.Index(i), value, 16)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case reflect.Int32:
|
||||
err := setInt(field.Index(i), value, 32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case reflect.Int64:
|
||||
err := setInt(field.Index(i), value, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case reflect.Uint:
|
||||
val, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
field.Index(i).SetUint(val)
|
||||
case reflect.Uint8:
|
||||
err := setUint(field.Index(i), value, 8)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case reflect.Uint16:
|
||||
err := setUint(field.Index(i), value, 16)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case reflect.Uint32:
|
||||
err := setUint(field.Index(i), value, 32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case reflect.Uint64:
|
||||
err := setUint(field.Index(i), value, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case reflect.Float32:
|
||||
err := setFloat(field.Index(i), value, 32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case reflect.Float64:
|
||||
err := setFloat(field.Index(i), value, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case reflect.Bool:
|
||||
val, err := strconv.ParseBool(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
field.Index(i).SetBool(val)
|
||||
default:
|
||||
return fmt.Errorf("unsupported type: %s", field.Type().Elem())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setSliceAsStruct(field reflect.Value, node *Node) error {
|
||||
if len(node.Children) == 0 {
|
||||
return fmt.Errorf("invalid slice: node %s", node.Name)
|
||||
}
|
||||
|
||||
// use Ptr to allow "SetDefaults"
|
||||
value := reflect.New(reflect.PtrTo(field.Type().Elem()))
|
||||
err := setPtr(value, node)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
elem := value.Elem().Elem()
|
||||
|
||||
field.Set(reflect.MakeSlice(field.Type(), 1, 1))
|
||||
field.Index(0).Set(elem)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func setMap(field reflect.Value, node *Node) error {
|
||||
if field.IsNil() {
|
||||
field.Set(reflect.MakeMap(field.Type()))
|
||||
}
|
||||
|
||||
for _, child := range node.Children {
|
||||
ptrValue := reflect.New(reflect.PtrTo(field.Type().Elem()))
|
||||
|
||||
err := fill(ptrValue, child)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
value := ptrValue.Elem().Elem()
|
||||
|
||||
key := reflect.ValueOf(child.Name)
|
||||
field.SetMapIndex(key, value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setInt(field reflect.Value, value string, bitSize int) error {
|
||||
switch field.Type() {
|
||||
case reflect.TypeOf(parse.Duration(0)):
|
||||
return setDuration(field, value, bitSize, time.Second)
|
||||
case reflect.TypeOf(time.Duration(0)):
|
||||
return setDuration(field, value, bitSize, time.Nanosecond)
|
||||
default:
|
||||
val, err := strconv.ParseInt(value, 10, bitSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
field.Set(reflect.ValueOf(val).Convert(field.Type()))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func setDuration(field reflect.Value, value string, bitSize int, defaultUnit time.Duration) error {
|
||||
val, err := strconv.ParseInt(value, 10, bitSize)
|
||||
if err == nil {
|
||||
field.Set(reflect.ValueOf(time.Duration(val) * defaultUnit).Convert(field.Type()))
|
||||
return nil
|
||||
}
|
||||
|
||||
duration, err := time.ParseDuration(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
field.Set(reflect.ValueOf(duration).Convert(field.Type()))
|
||||
return nil
|
||||
}
|
||||
|
||||
func setUint(field reflect.Value, value string, bitSize int) error {
|
||||
val, err := strconv.ParseUint(value, 10, bitSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
field.Set(reflect.ValueOf(val).Convert(field.Type()))
|
||||
return nil
|
||||
}
|
||||
|
||||
func setFloat(field reflect.Value, value string, bitSize int) error {
|
||||
val, err := strconv.ParseFloat(value, bitSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
field.Set(reflect.ValueOf(val).Convert(field.Type()))
|
||||
return nil
|
||||
}
|
1208
pkg/provider/label/internal/element_fill_test.go
Normal file
1208
pkg/provider/label/internal/element_fill_test.go
Normal file
File diff suppressed because it is too large
Load diff
163
pkg/provider/label/internal/element_nodes.go
Normal file
163
pkg/provider/label/internal/element_nodes.go
Normal file
|
@ -0,0 +1,163 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// EncodeToNode Converts an element to a node.
|
||||
// element -> nodes
|
||||
func EncodeToNode(element interface{}) (*Node, error) {
|
||||
rValue := reflect.ValueOf(element)
|
||||
node := &Node{Name: "traefik"}
|
||||
|
||||
err := setNodeValue(node, rValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return node, nil
|
||||
}
|
||||
|
||||
func setNodeValue(node *Node, rValue reflect.Value) error {
|
||||
switch rValue.Kind() {
|
||||
case reflect.String:
|
||||
node.Value = rValue.String()
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
node.Value = strconv.FormatInt(rValue.Int(), 10)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
node.Value = strconv.FormatUint(rValue.Uint(), 10)
|
||||
case reflect.Float32, reflect.Float64:
|
||||
node.Value = strconv.FormatFloat(rValue.Float(), 'f', 6, 64)
|
||||
case reflect.Bool:
|
||||
node.Value = strconv.FormatBool(rValue.Bool())
|
||||
case reflect.Struct:
|
||||
return setStructValue(node, rValue)
|
||||
case reflect.Ptr:
|
||||
return setNodeValue(node, rValue.Elem())
|
||||
case reflect.Map:
|
||||
return setMapValue(node, rValue)
|
||||
case reflect.Slice:
|
||||
return setSliceValue(node, rValue)
|
||||
default:
|
||||
// noop
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func setStructValue(node *Node, rValue reflect.Value) error {
|
||||
rType := rValue.Type()
|
||||
|
||||
for i := 0; i < rValue.NumField(); i++ {
|
||||
field := rType.Field(i)
|
||||
fieldValue := rValue.Field(i)
|
||||
|
||||
if !isExported(field) {
|
||||
continue
|
||||
}
|
||||
|
||||
if field.Tag.Get(TagLabel) == "-" {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := isSupportedType(field); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if isSkippedField(field, fieldValue) {
|
||||
continue
|
||||
}
|
||||
|
||||
nodeName := field.Name
|
||||
if field.Type.Kind() == reflect.Slice && len(field.Tag.Get(TagLabelSliceAsStruct)) != 0 {
|
||||
nodeName = field.Tag.Get(TagLabelSliceAsStruct)
|
||||
}
|
||||
|
||||
child := &Node{Name: nodeName, FieldName: field.Name}
|
||||
|
||||
if err := setNodeValue(child, fieldValue); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if field.Type.Kind() == reflect.Ptr && len(child.Children) == 0 {
|
||||
if field.Tag.Get(TagLabel) != "allowEmpty" {
|
||||
continue
|
||||
}
|
||||
|
||||
child.Value = "true"
|
||||
}
|
||||
|
||||
node.Children = append(node.Children, child)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func setMapValue(node *Node, rValue reflect.Value) error {
|
||||
for _, key := range rValue.MapKeys() {
|
||||
child := &Node{Name: key.String(), FieldName: key.String()}
|
||||
node.Children = append(node.Children, child)
|
||||
|
||||
if err := setNodeValue(child, rValue.MapIndex(key)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setSliceValue(node *Node, rValue reflect.Value) error {
|
||||
// label-slice-as-struct
|
||||
if rValue.Type().Elem().Kind() == reflect.Struct && !strings.EqualFold(node.Name, node.FieldName) {
|
||||
if rValue.Len() > 1 {
|
||||
return fmt.Errorf("node %s has too many slice entries: %d", node.Name, rValue.Len())
|
||||
}
|
||||
|
||||
if err := setNodeValue(node, rValue.Index(0)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var values []string
|
||||
|
||||
for i := 0; i < rValue.Len(); i++ {
|
||||
eValue := rValue.Index(i)
|
||||
|
||||
switch eValue.Kind() {
|
||||
case reflect.String:
|
||||
values = append(values, eValue.String())
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
values = append(values, strconv.FormatInt(eValue.Int(), 10))
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
values = append(values, strconv.FormatUint(eValue.Uint(), 10))
|
||||
case reflect.Float32, reflect.Float64:
|
||||
values = append(values, strconv.FormatFloat(eValue.Float(), 'f', 6, 64))
|
||||
case reflect.Bool:
|
||||
values = append(values, strconv.FormatBool(eValue.Bool()))
|
||||
default:
|
||||
// noop
|
||||
}
|
||||
}
|
||||
|
||||
node.Value = strings.Join(values, ", ")
|
||||
return nil
|
||||
}
|
||||
|
||||
func isSkippedField(field reflect.StructField, fieldValue reflect.Value) bool {
|
||||
if field.Type.Kind() == reflect.String && fieldValue.Len() == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
if field.Type.Kind() == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct && fieldValue.IsNil() {
|
||||
return true
|
||||
}
|
||||
|
||||
if (field.Type.Kind() == reflect.Slice || field.Type.Kind() == reflect.Map) &&
|
||||
(fieldValue.IsNil() || fieldValue.Len() == 0) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
593
pkg/provider/label/internal/element_nodes_test.go
Normal file
593
pkg/provider/label/internal/element_nodes_test.go
Normal file
|
@ -0,0 +1,593 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestEncodeToNode(t *testing.T) {
|
||||
type expected struct {
|
||||
node *Node
|
||||
error bool
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
desc string
|
||||
element interface{}
|
||||
expected expected
|
||||
}{
|
||||
{
|
||||
desc: "string",
|
||||
element: struct {
|
||||
Foo string
|
||||
}{Foo: "bar"},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "bar"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "2 string fields",
|
||||
element: struct {
|
||||
Foo string
|
||||
Fii string
|
||||
}{Foo: "bar", Fii: "hii"},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "bar"},
|
||||
{Name: "Fii", FieldName: "Fii", Value: "hii"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "int",
|
||||
element: struct {
|
||||
Foo int
|
||||
}{Foo: 1},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "1"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "int8",
|
||||
element: struct {
|
||||
Foo int8
|
||||
}{Foo: 2},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "2"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "int16",
|
||||
element: struct {
|
||||
Foo int16
|
||||
}{Foo: 2},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "2"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "int32",
|
||||
element: struct {
|
||||
Foo int32
|
||||
}{Foo: 2},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "2"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "int64",
|
||||
element: struct {
|
||||
Foo int64
|
||||
}{Foo: 2},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "2"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "uint",
|
||||
element: struct {
|
||||
Foo uint
|
||||
}{Foo: 1},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "1"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "uint8",
|
||||
element: struct {
|
||||
Foo uint8
|
||||
}{Foo: 2},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "2"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "uint16",
|
||||
element: struct {
|
||||
Foo uint16
|
||||
}{Foo: 2},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "2"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "uint32",
|
||||
element: struct {
|
||||
Foo uint32
|
||||
}{Foo: 2},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "2"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "uint64",
|
||||
element: struct {
|
||||
Foo uint64
|
||||
}{Foo: 2},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "2"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "float32",
|
||||
element: struct {
|
||||
Foo float32
|
||||
}{Foo: 1.12},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "1.120000"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "float64",
|
||||
element: struct {
|
||||
Foo float64
|
||||
}{Foo: 1.12},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "1.120000"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "bool",
|
||||
element: struct {
|
||||
Foo bool
|
||||
}{Foo: true},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "true"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "struct",
|
||||
element: struct {
|
||||
Foo struct {
|
||||
Fii string
|
||||
Fuu string
|
||||
}
|
||||
}{
|
||||
Foo: struct {
|
||||
Fii string
|
||||
Fuu string
|
||||
}{
|
||||
Fii: "hii",
|
||||
Fuu: "huu",
|
||||
},
|
||||
},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Children: []*Node{
|
||||
{Name: "Fii", FieldName: "Fii", Value: "hii"},
|
||||
{Name: "Fuu", FieldName: "Fuu", Value: "huu"},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "struct unexported field",
|
||||
element: struct {
|
||||
Foo struct {
|
||||
Fii string
|
||||
fuu string
|
||||
}
|
||||
}{
|
||||
Foo: struct {
|
||||
Fii string
|
||||
fuu string
|
||||
}{
|
||||
Fii: "hii",
|
||||
fuu: "huu",
|
||||
},
|
||||
},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Children: []*Node{
|
||||
{Name: "Fii", FieldName: "Fii", Value: "hii"},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "struct pointer",
|
||||
element: struct {
|
||||
Foo *struct {
|
||||
Fii string
|
||||
Fuu string
|
||||
}
|
||||
}{
|
||||
Foo: &struct {
|
||||
Fii string
|
||||
Fuu string
|
||||
}{
|
||||
Fii: "hii",
|
||||
Fuu: "huu",
|
||||
},
|
||||
},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Children: []*Node{
|
||||
{Name: "Fii", FieldName: "Fii", Value: "hii"},
|
||||
{Name: "Fuu", FieldName: "Fuu", Value: "huu"},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "string pointer",
|
||||
element: struct {
|
||||
Foo *struct {
|
||||
Fii *string
|
||||
Fuu string
|
||||
}
|
||||
}{
|
||||
Foo: &struct {
|
||||
Fii *string
|
||||
Fuu string
|
||||
}{
|
||||
Fii: func(v string) *string { return &v }("hii"),
|
||||
Fuu: "huu",
|
||||
},
|
||||
},
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "struct nil pointer",
|
||||
element: struct {
|
||||
Foo *struct {
|
||||
Fii *string
|
||||
Fuu string
|
||||
}
|
||||
}{
|
||||
Foo: &struct {
|
||||
Fii *string
|
||||
Fuu string
|
||||
}{
|
||||
Fuu: "huu",
|
||||
},
|
||||
},
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "struct nil struct pointer",
|
||||
element: struct {
|
||||
Foo *struct {
|
||||
Fii *string
|
||||
Fuu string
|
||||
}
|
||||
}{
|
||||
Foo: nil,
|
||||
},
|
||||
expected: expected{node: &Node{Name: "traefik"}},
|
||||
},
|
||||
{
|
||||
desc: "struct pointer, not allowEmpty",
|
||||
element: struct {
|
||||
Foo *struct {
|
||||
Fii string
|
||||
Fuu string
|
||||
}
|
||||
}{
|
||||
Foo: &struct {
|
||||
Fii string
|
||||
Fuu string
|
||||
}{},
|
||||
},
|
||||
expected: expected{node: &Node{Name: "traefik"}},
|
||||
},
|
||||
{
|
||||
desc: "struct pointer, allowEmpty",
|
||||
element: struct {
|
||||
Foo *struct {
|
||||
Fii string
|
||||
Fuu string
|
||||
} `label:"allowEmpty"`
|
||||
}{
|
||||
Foo: &struct {
|
||||
Fii string
|
||||
Fuu string
|
||||
}{},
|
||||
},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "true"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "map",
|
||||
element: struct {
|
||||
Foo struct {
|
||||
Bar map[string]string
|
||||
}
|
||||
}{
|
||||
Foo: struct {
|
||||
Bar map[string]string
|
||||
}{
|
||||
Bar: map[string]string{
|
||||
"name1": "huu",
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Children: []*Node{
|
||||
{Name: "name1", FieldName: "name1", Value: "huu"},
|
||||
}},
|
||||
}},
|
||||
}}},
|
||||
},
|
||||
{
|
||||
desc: "empty map",
|
||||
element: struct {
|
||||
Bar map[string]string
|
||||
}{
|
||||
Bar: map[string]string{},
|
||||
},
|
||||
expected: expected{node: &Node{Name: "traefik"}},
|
||||
},
|
||||
{
|
||||
desc: "map nil",
|
||||
element: struct {
|
||||
Bar map[string]string
|
||||
}{
|
||||
Bar: nil,
|
||||
},
|
||||
expected: expected{node: &Node{Name: "traefik"}},
|
||||
},
|
||||
{
|
||||
desc: "map with non string key",
|
||||
element: struct {
|
||||
Foo struct {
|
||||
Bar map[int]string
|
||||
}
|
||||
}{
|
||||
Foo: struct {
|
||||
Bar map[int]string
|
||||
}{
|
||||
Bar: map[int]string{
|
||||
1: "huu",
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "slice of string",
|
||||
element: struct{ Bar []string }{Bar: []string{"huu", "hii"}},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "huu, hii"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice of int",
|
||||
element: struct{ Bar []int }{Bar: []int{4, 2, 3}},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "4, 2, 3"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice of int8",
|
||||
element: struct{ Bar []int8 }{Bar: []int8{4, 2, 3}},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "4, 2, 3"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice of int16",
|
||||
element: struct{ Bar []int16 }{Bar: []int16{4, 2, 3}},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "4, 2, 3"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice of int32",
|
||||
element: struct{ Bar []int32 }{Bar: []int32{4, 2, 3}},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "4, 2, 3"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice of int64",
|
||||
element: struct{ Bar []int64 }{Bar: []int64{4, 2, 3}},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "4, 2, 3"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice of uint",
|
||||
element: struct{ Bar []uint }{Bar: []uint{4, 2, 3}},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "4, 2, 3"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice of uint8",
|
||||
element: struct{ Bar []uint8 }{Bar: []uint8{4, 2, 3}},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "4, 2, 3"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice of uint16",
|
||||
element: struct{ Bar []uint16 }{Bar: []uint16{4, 2, 3}},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "4, 2, 3"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice of uint32",
|
||||
element: struct{ Bar []uint32 }{Bar: []uint32{4, 2, 3}},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "4, 2, 3"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice of uint64",
|
||||
element: struct{ Bar []uint64 }{Bar: []uint64{4, 2, 3}},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "4, 2, 3"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice of float32",
|
||||
element: struct{ Bar []float32 }{Bar: []float32{4.1, 2, 3.2}},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "4.100000, 2.000000, 3.200000"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice of float64",
|
||||
element: struct{ Bar []float64 }{Bar: []float64{4.1, 2, 3.2}},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "4.100000, 2.000000, 3.200000"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice of bool",
|
||||
element: struct{ Bar []bool }{Bar: []bool{true, false, true}},
|
||||
expected: expected{node: &Node{Name: "traefik", Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "true, false, true"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice label-slice-as-struct",
|
||||
element: &struct {
|
||||
Foo []struct {
|
||||
Bar string
|
||||
Bir string
|
||||
} `label-slice-as-struct:"Fii"`
|
||||
}{
|
||||
Foo: []struct {
|
||||
Bar string
|
||||
Bir string
|
||||
}{
|
||||
{
|
||||
Bar: "haa",
|
||||
Bir: "hii",
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: expected{node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{{
|
||||
Name: "Fii",
|
||||
FieldName: "Foo",
|
||||
Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "haa"},
|
||||
{Name: "Bir", FieldName: "Bir", Value: "hii"},
|
||||
},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
{
|
||||
desc: "slice label-slice-as-struct several slice entries",
|
||||
element: &struct {
|
||||
Foo []struct {
|
||||
Bar string
|
||||
Bir string
|
||||
} `label-slice-as-struct:"Fii"`
|
||||
}{
|
||||
Foo: []struct {
|
||||
Bar string
|
||||
Bir string
|
||||
}{
|
||||
{
|
||||
Bar: "haa",
|
||||
Bir: "hii",
|
||||
},
|
||||
{
|
||||
Bar: "haa",
|
||||
Bir: "hii",
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "empty slice",
|
||||
element: struct {
|
||||
Bar []string
|
||||
}{
|
||||
Bar: []string{},
|
||||
},
|
||||
expected: expected{node: &Node{Name: "traefik"}},
|
||||
},
|
||||
{
|
||||
desc: "nil slice",
|
||||
element: struct {
|
||||
Bar []string
|
||||
}{
|
||||
Bar: nil,
|
||||
},
|
||||
expected: expected{node: &Node{Name: "traefik"}},
|
||||
},
|
||||
{
|
||||
desc: "ignore slice",
|
||||
element: struct {
|
||||
Bar []string `label:"-"`
|
||||
}{
|
||||
Bar: []string{"huu", "hii"},
|
||||
},
|
||||
expected: expected{node: &Node{Name: "traefik"}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
node, err := EncodeToNode(test.element)
|
||||
|
||||
if test.expected.error {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, test.expected.node, node)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
78
pkg/provider/label/internal/labels_decode.go
Normal file
78
pkg/provider/label/internal/labels_decode.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DecodeToNode Converts the labels to a node.
|
||||
// labels -> nodes
|
||||
func DecodeToNode(labels map[string]string, filters ...string) (*Node, error) {
|
||||
var sortedKeys []string
|
||||
for key := range labels {
|
||||
if len(filters) == 0 {
|
||||
sortedKeys = append(sortedKeys, key)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, filter := range filters {
|
||||
if len(key) >= len(filter) && strings.EqualFold(key[:len(filter)], filter) {
|
||||
sortedKeys = append(sortedKeys, key)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(sortedKeys)
|
||||
|
||||
labelRoot := "traefik"
|
||||
|
||||
var node *Node
|
||||
for i, key := range sortedKeys {
|
||||
split := strings.Split(key, ".")
|
||||
|
||||
if split[0] != labelRoot {
|
||||
// TODO (@ldez): error or continue
|
||||
return nil, fmt.Errorf("invalid label root %s", split[0])
|
||||
}
|
||||
|
||||
labelRoot = split[0]
|
||||
|
||||
if i == 0 {
|
||||
node = &Node{}
|
||||
}
|
||||
decodeToNode(node, split, labels[key])
|
||||
}
|
||||
|
||||
return node, nil
|
||||
}
|
||||
|
||||
func decodeToNode(root *Node, path []string, value string) {
|
||||
if len(root.Name) == 0 {
|
||||
root.Name = path[0]
|
||||
}
|
||||
|
||||
// it's a leaf or not -> children
|
||||
if len(path) > 1 {
|
||||
if n := containsNode(root.Children, path[1]); n != nil {
|
||||
// the child already exists
|
||||
decodeToNode(n, path[1:], value)
|
||||
} else {
|
||||
// new child
|
||||
child := &Node{Name: path[1]}
|
||||
decodeToNode(child, path[1:], value)
|
||||
root.Children = append(root.Children, child)
|
||||
}
|
||||
} else {
|
||||
root.Value = value
|
||||
}
|
||||
}
|
||||
|
||||
func containsNode(nodes []*Node, name string) *Node {
|
||||
for _, n := range nodes {
|
||||
if name == n.Name {
|
||||
return n
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
202
pkg/provider/label/internal/labels_decode_test.go
Normal file
202
pkg/provider/label/internal/labels_decode_test.go
Normal file
|
@ -0,0 +1,202 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDecodeToNode(t *testing.T) {
|
||||
type expected struct {
|
||||
error bool
|
||||
node *Node
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
desc string
|
||||
in map[string]string
|
||||
filters []string
|
||||
expected expected
|
||||
}{
|
||||
{
|
||||
desc: "no label",
|
||||
in: map[string]string{},
|
||||
expected: expected{node: nil},
|
||||
},
|
||||
{
|
||||
desc: "level 1",
|
||||
in: map[string]string{
|
||||
"traefik.foo": "bar",
|
||||
},
|
||||
expected: expected{node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "foo", Value: "bar"},
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
desc: "level 1 empty value",
|
||||
in: map[string]string{
|
||||
"traefik.foo": "",
|
||||
},
|
||||
expected: expected{node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "foo", Value: ""},
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
desc: "level 2",
|
||||
in: map[string]string{
|
||||
"traefik.foo.bar": "bar",
|
||||
},
|
||||
expected: expected{node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{{
|
||||
Name: "foo",
|
||||
Children: []*Node{
|
||||
{Name: "bar", Value: "bar"},
|
||||
},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
{
|
||||
desc: "several entries, level 0",
|
||||
in: map[string]string{
|
||||
"traefik": "bar",
|
||||
"traefic": "bur",
|
||||
},
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "several entries, prefix filter",
|
||||
in: map[string]string{
|
||||
"traefik.foo": "bar",
|
||||
"traefik.fii": "bir",
|
||||
},
|
||||
filters: []string{"traefik.Foo"},
|
||||
expected: expected{node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "foo", Value: "bar"},
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
desc: "several entries, level 1",
|
||||
in: map[string]string{
|
||||
"traefik.foo": "bar",
|
||||
"traefik.fii": "bur",
|
||||
},
|
||||
expected: expected{node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "fii", Value: "bur"},
|
||||
{Name: "foo", Value: "bar"},
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
desc: "several entries, level 2",
|
||||
in: map[string]string{
|
||||
"traefik.foo.aaa": "bar",
|
||||
"traefik.foo.bbb": "bur",
|
||||
},
|
||||
expected: expected{node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "foo", Children: []*Node{
|
||||
{Name: "aaa", Value: "bar"},
|
||||
{Name: "bbb", Value: "bur"},
|
||||
}},
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
desc: "several entries, level 2, 3 children",
|
||||
in: map[string]string{
|
||||
"traefik.foo.aaa": "bar",
|
||||
"traefik.foo.bbb": "bur",
|
||||
"traefik.foo.ccc": "bir",
|
||||
},
|
||||
expected: expected{node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "foo", Children: []*Node{
|
||||
{Name: "aaa", Value: "bar"},
|
||||
{Name: "bbb", Value: "bur"},
|
||||
{Name: "ccc", Value: "bir"},
|
||||
}},
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
desc: "several entries, level 3",
|
||||
in: map[string]string{
|
||||
"traefik.foo.bar.aaa": "bar",
|
||||
"traefik.foo.bar.bbb": "bur",
|
||||
},
|
||||
expected: expected{node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "foo", Children: []*Node{
|
||||
{Name: "bar", Children: []*Node{
|
||||
{Name: "aaa", Value: "bar"},
|
||||
{Name: "bbb", Value: "bur"},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
desc: "several entries, level 3, 2 children level 1",
|
||||
in: map[string]string{
|
||||
"traefik.foo.bar.aaa": "bar",
|
||||
"traefik.foo.bar.bbb": "bur",
|
||||
"traefik.bar.foo.bbb": "bir",
|
||||
},
|
||||
expected: expected{node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "bar", Children: []*Node{
|
||||
{Name: "foo", Children: []*Node{
|
||||
{Name: "bbb", Value: "bir"},
|
||||
}},
|
||||
}},
|
||||
{Name: "foo", Children: []*Node{
|
||||
{Name: "bar", Children: []*Node{
|
||||
{Name: "aaa", Value: "bar"},
|
||||
{Name: "bbb", Value: "bur"},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
out, err := DecodeToNode(test.in, test.filters...)
|
||||
|
||||
if test.expected.error {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
|
||||
if !assert.Equal(t, test.expected.node, out) {
|
||||
bytes, err := json.MarshalIndent(out, "", " ")
|
||||
require.NoError(t, err)
|
||||
fmt.Println(string(bytes))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
24
pkg/provider/label/internal/labels_encode.go
Normal file
24
pkg/provider/label/internal/labels_encode.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package internal
|
||||
|
||||
// EncodeNode Converts a node to labels.
|
||||
// nodes -> labels
|
||||
func EncodeNode(node *Node) map[string]string {
|
||||
labels := make(map[string]string)
|
||||
encodeNode(labels, node.Name, node)
|
||||
return labels
|
||||
}
|
||||
|
||||
func encodeNode(labels map[string]string, root string, node *Node) {
|
||||
for _, child := range node.Children {
|
||||
if child.Disabled {
|
||||
continue
|
||||
}
|
||||
|
||||
childName := root + "." + child.Name
|
||||
if len(child.Children) > 0 {
|
||||
encodeNode(labels, childName, child)
|
||||
} else if len(child.Name) > 0 {
|
||||
labels[childName] = child.Value
|
||||
}
|
||||
}
|
||||
}
|
156
pkg/provider/label/internal/labels_encode_test.go
Normal file
156
pkg/provider/label/internal/labels_encode_test.go
Normal file
|
@ -0,0 +1,156 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestEncodeNode(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
node *Node
|
||||
expected map[string]string
|
||||
}{
|
||||
{
|
||||
desc: "1 label",
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "aaa", Value: "bar"},
|
||||
},
|
||||
},
|
||||
expected: map[string]string{
|
||||
"traefik.aaa": "bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "2 labels",
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "aaa", Value: "bar"},
|
||||
{Name: "bbb", Value: "bur"},
|
||||
},
|
||||
},
|
||||
expected: map[string]string{
|
||||
"traefik.aaa": "bar",
|
||||
"traefik.bbb": "bur",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "2 labels, 1 disabled",
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "aaa", Value: "bar"},
|
||||
{Name: "bbb", Value: "bur", Disabled: true},
|
||||
},
|
||||
},
|
||||
expected: map[string]string{
|
||||
"traefik.aaa": "bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "2 levels",
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "foo", Children: []*Node{
|
||||
{Name: "aaa", Value: "bar"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
expected: map[string]string{
|
||||
"traefik.foo.aaa": "bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "3 levels",
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "foo", Children: []*Node{
|
||||
{Name: "bar", Children: []*Node{
|
||||
{Name: "aaa", Value: "bar"},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
expected: map[string]string{
|
||||
"traefik.foo.bar.aaa": "bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "2 levels, same root",
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "foo", Children: []*Node{
|
||||
{Name: "bar", Children: []*Node{
|
||||
{Name: "aaa", Value: "bar"},
|
||||
{Name: "bbb", Value: "bur"},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
expected: map[string]string{
|
||||
"traefik.foo.bar.aaa": "bar",
|
||||
"traefik.foo.bar.bbb": "bur",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "several levels, different root",
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "bar", Children: []*Node{
|
||||
{Name: "ccc", Value: "bir"},
|
||||
}},
|
||||
{Name: "foo", Children: []*Node{
|
||||
{Name: "bar", Children: []*Node{
|
||||
{Name: "aaa", Value: "bar"},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
expected: map[string]string{
|
||||
"traefik.foo.bar.aaa": "bar",
|
||||
"traefik.bar.ccc": "bir",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "multiple labels, multiple levels",
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "bar", Children: []*Node{
|
||||
{Name: "ccc", Value: "bir"},
|
||||
}},
|
||||
{Name: "foo", Children: []*Node{
|
||||
{Name: "bar", Children: []*Node{
|
||||
{Name: "aaa", Value: "bar"},
|
||||
{Name: "bbb", Value: "bur"},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
expected: map[string]string{
|
||||
"traefik.foo.bar.aaa": "bar",
|
||||
"traefik.foo.bar.bbb": "bur",
|
||||
"traefik.bar.ccc": "bir",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
labels := EncodeNode(test.node)
|
||||
|
||||
assert.Equal(t, test.expected, labels)
|
||||
})
|
||||
}
|
||||
}
|
13
pkg/provider/label/internal/node.go
Normal file
13
pkg/provider/label/internal/node.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package internal
|
||||
|
||||
import "reflect"
|
||||
|
||||
// Node a label node.
|
||||
type Node struct {
|
||||
Name string `json:"name"`
|
||||
FieldName string `json:"fieldName"`
|
||||
Value string `json:"value,omitempty"`
|
||||
Disabled bool `json:"disabled,omitempty"`
|
||||
Kind reflect.Kind `json:"kind,omitempty"`
|
||||
Children []*Node `json:"children,omitempty"`
|
||||
}
|
168
pkg/provider/label/internal/nodes_metadata.go
Normal file
168
pkg/provider/label/internal/nodes_metadata.go
Normal file
|
@ -0,0 +1,168 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AddMetadata Adds metadata to a node.
|
||||
// nodes + element -> nodes
|
||||
func AddMetadata(structure interface{}, node *Node) error {
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(node.Children) == 0 {
|
||||
return fmt.Errorf("invalid node %s: no child", node.Name)
|
||||
}
|
||||
|
||||
if structure == nil {
|
||||
return errors.New("nil structure")
|
||||
}
|
||||
|
||||
rootType := reflect.TypeOf(structure)
|
||||
node.Kind = rootType.Kind()
|
||||
|
||||
return browseChildren(rootType, node)
|
||||
}
|
||||
|
||||
func addMetadata(rootType reflect.Type, node *Node) error {
|
||||
rType := rootType
|
||||
if rootType.Kind() == reflect.Ptr {
|
||||
rType = rootType.Elem()
|
||||
}
|
||||
|
||||
field, err := findTypedField(rType, node)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = isSupportedType(field); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fType := field.Type
|
||||
node.Kind = fType.Kind()
|
||||
|
||||
if fType.Kind() == reflect.Struct || fType.Kind() == reflect.Ptr && fType.Elem().Kind() == reflect.Struct ||
|
||||
fType.Kind() == reflect.Map {
|
||||
if len(node.Children) == 0 && field.Tag.Get(TagLabel) != "allowEmpty" {
|
||||
return fmt.Errorf("node %s (type %s) must have children", node.Name, fType)
|
||||
}
|
||||
|
||||
node.Disabled = len(node.Value) > 0 && node.Value != "true" && field.Tag.Get(TagLabel) == "allowEmpty"
|
||||
}
|
||||
|
||||
if len(node.Children) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if fType.Kind() == reflect.Struct || fType.Kind() == reflect.Ptr && fType.Elem().Kind() == reflect.Struct {
|
||||
return browseChildren(fType, node)
|
||||
}
|
||||
|
||||
if fType.Kind() == reflect.Map {
|
||||
for _, child := range node.Children {
|
||||
// elem is a map entry value type
|
||||
elem := fType.Elem()
|
||||
child.Kind = elem.Kind()
|
||||
|
||||
if elem.Kind() == reflect.Map || elem.Kind() == reflect.Struct ||
|
||||
(elem.Kind() == reflect.Ptr && elem.Elem().Kind() == reflect.Struct) {
|
||||
if err = browseChildren(elem, child); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// only for struct/Ptr with label-slice-as-struct tag
|
||||
if fType.Kind() == reflect.Slice {
|
||||
return browseChildren(fType.Elem(), node)
|
||||
}
|
||||
|
||||
return fmt.Errorf("invalid node %s: %v", node.Name, fType.Kind())
|
||||
}
|
||||
|
||||
func findTypedField(rType reflect.Type, node *Node) (reflect.StructField, error) {
|
||||
for i := 0; i < rType.NumField(); i++ {
|
||||
cField := rType.Field(i)
|
||||
|
||||
fieldName := cField.Tag.Get(TagLabelSliceAsStruct)
|
||||
if len(fieldName) == 0 {
|
||||
fieldName = cField.Name
|
||||
}
|
||||
|
||||
if isExported(cField) && strings.EqualFold(fieldName, node.Name) {
|
||||
node.FieldName = cField.Name
|
||||
return cField, nil
|
||||
}
|
||||
}
|
||||
|
||||
return reflect.StructField{}, fmt.Errorf("field not found, node: %s", node.Name)
|
||||
}
|
||||
|
||||
func browseChildren(fType reflect.Type, node *Node) error {
|
||||
for _, child := range node.Children {
|
||||
if err := addMetadata(fType, child); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// isExported return true is a struct field is exported, else false
|
||||
// https://golang.org/pkg/reflect/#StructField
|
||||
func isExported(f reflect.StructField) bool {
|
||||
if f.PkgPath != "" && !f.Anonymous {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isSupportedType(field reflect.StructField) error {
|
||||
fType := field.Type
|
||||
|
||||
if fType.Kind() == reflect.Slice {
|
||||
switch fType.Elem().Kind() {
|
||||
case reflect.String,
|
||||
reflect.Bool,
|
||||
reflect.Int,
|
||||
reflect.Int8,
|
||||
reflect.Int16,
|
||||
reflect.Int32,
|
||||
reflect.Int64,
|
||||
reflect.Uint,
|
||||
reflect.Uint8,
|
||||
reflect.Uint16,
|
||||
reflect.Uint32,
|
||||
reflect.Uint64,
|
||||
reflect.Uintptr,
|
||||
reflect.Float32,
|
||||
reflect.Float64:
|
||||
return nil
|
||||
default:
|
||||
if len(field.Tag.Get(TagLabelSliceAsStruct)) > 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("unsupported slice type: %v", fType)
|
||||
}
|
||||
}
|
||||
|
||||
if fType.Kind() == reflect.Ptr && fType.Elem().Kind() != reflect.Struct {
|
||||
return fmt.Errorf("unsupported pointer type: %v", fType.Elem())
|
||||
}
|
||||
|
||||
if fType.Kind() == reflect.Map && fType.Key().Kind() != reflect.String {
|
||||
return fmt.Errorf("unsupported map key type: %v", fType.Key())
|
||||
}
|
||||
|
||||
if fType.Kind() == reflect.Func {
|
||||
return fmt.Errorf("unsupported type: %v", fType)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
775
pkg/provider/label/internal/nodes_metadata_test.go
Normal file
775
pkg/provider/label/internal/nodes_metadata_test.go
Normal file
|
@ -0,0 +1,775 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAddMetadata(t *testing.T) {
|
||||
type expected struct {
|
||||
node *Node
|
||||
error bool
|
||||
}
|
||||
|
||||
type interf interface{}
|
||||
|
||||
testCases := []struct {
|
||||
desc string
|
||||
tree *Node
|
||||
structure interface{}
|
||||
expected expected
|
||||
}{
|
||||
{
|
||||
desc: "Node Nil",
|
||||
tree: nil,
|
||||
structure: nil,
|
||||
expected: expected{node: nil},
|
||||
},
|
||||
{
|
||||
desc: "Empty Node",
|
||||
tree: &Node{},
|
||||
structure: nil,
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "Nil structure",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Value: "bar"},
|
||||
},
|
||||
},
|
||||
structure: nil,
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "level 0",
|
||||
tree: &Node{Name: "traefik", Value: "bar"},
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "level 1",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "bar"},
|
||||
},
|
||||
},
|
||||
structure: struct{ Foo string }{},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "bar", Kind: reflect.String},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 1, pointer",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Value: "bar"},
|
||||
},
|
||||
},
|
||||
structure: &struct{ Foo string }{},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Ptr,
|
||||
Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "bar", Kind: reflect.String},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 1, slice",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Value: "bar,bur"},
|
||||
},
|
||||
},
|
||||
structure: struct{ Foo []string }{},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "bar,bur", Kind: reflect.Slice},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 1, interface",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Value: "", Children: []*Node{
|
||||
{Name: "Fii", Value: "hii"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
structure: struct{ Foo interf }{},
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "level 1, slice struct",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Value: "1,2"},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo []struct{ Foo string }
|
||||
}{},
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "level 1, map string",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Children: []*Node{
|
||||
{Name: "name1", Value: "bar"},
|
||||
{Name: "name2", Value: "bur"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
structure: struct{ Foo map[string]string }{},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Kind: reflect.Map, Children: []*Node{
|
||||
{Name: "name1", Value: "bar", Kind: reflect.String},
|
||||
{Name: "name2", Value: "bur", Kind: reflect.String},
|
||||
}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 1, map struct",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Children: []*Node{
|
||||
{Name: "name1", Children: []*Node{
|
||||
{Name: "Fii", Value: "bar"},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo map[string]struct{ Fii string }
|
||||
}{},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Kind: reflect.Map, Children: []*Node{
|
||||
{Name: "name1", Kind: reflect.Struct, Children: []*Node{
|
||||
{Name: "Fii", FieldName: "Fii", Value: "bar", Kind: reflect.String},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 1, map int as key",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Children: []*Node{
|
||||
{Name: "name1", Children: []*Node{
|
||||
{Name: "Fii", Value: "bar"},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo map[int]struct{ Fii string }
|
||||
}{},
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "level 1, int pointer",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Value: "0"},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo *int
|
||||
}{},
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "level 1, 2 children with different types",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Value: "bar"},
|
||||
{Name: "Fii", Value: "1"},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo string
|
||||
Fii int
|
||||
}{},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "bar", Kind: reflect.String},
|
||||
{Name: "Fii", FieldName: "Fii", Value: "1", Kind: reflect.Int},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 1, use exported instead of unexported",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "foo", Value: "bar"},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
foo int
|
||||
Foo string
|
||||
}{},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "foo", Value: "bar", FieldName: "Foo", Kind: reflect.String},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 1, unexported",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "foo", Value: "bar"},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
foo string
|
||||
}{},
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "level 1, 3 children with different types",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Value: "bar"},
|
||||
{Name: "Fii", Value: "1"},
|
||||
{Name: "Fuu", Value: "true"},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo string
|
||||
Fii int
|
||||
Fuu bool
|
||||
}{},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "bar", Kind: reflect.String},
|
||||
{Name: "Fii", FieldName: "Fii", Value: "1", Kind: reflect.Int},
|
||||
{Name: "Fuu", FieldName: "Fuu", Value: "true", Kind: reflect.Bool},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 2",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Children: []*Node{
|
||||
{Name: "Bar", Value: "bir"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo struct {
|
||||
Bar string
|
||||
}
|
||||
}{
|
||||
Foo: struct {
|
||||
Bar string
|
||||
}{},
|
||||
},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Kind: reflect.Struct, Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "bir", Kind: reflect.String},
|
||||
}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 2, struct without children",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo"},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo struct {
|
||||
Bar string
|
||||
}
|
||||
}{
|
||||
Foo: struct {
|
||||
Bar string
|
||||
}{},
|
||||
},
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "level 2, slice-as-struct",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Fii", Children: []*Node{
|
||||
{Name: "bar", Value: "haa"},
|
||||
{Name: "bir", Value: "hii"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo []struct {
|
||||
Bar string
|
||||
Bir string
|
||||
} `label-slice-as-struct:"Fii"`
|
||||
}{
|
||||
Foo: []struct {
|
||||
Bar string
|
||||
Bir string
|
||||
}{},
|
||||
},
|
||||
expected: expected{node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{
|
||||
Name: "Fii",
|
||||
FieldName: "Foo",
|
||||
Kind: reflect.Slice,
|
||||
Children: []*Node{
|
||||
{Name: "bar", FieldName: "Bar", Kind: reflect.String, Value: "haa"},
|
||||
{Name: "bir", FieldName: "Bir", Kind: reflect.String, Value: "hii"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
desc: "level 2, slice-as-struct without children",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Fii"},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo []struct {
|
||||
Bar string
|
||||
Bir string
|
||||
} `label-slice-as-struct:"Fii"`
|
||||
}{
|
||||
Foo: []struct {
|
||||
Bar string
|
||||
Bir string
|
||||
}{},
|
||||
},
|
||||
expected: expected{node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{
|
||||
Name: "Fii",
|
||||
FieldName: "Foo",
|
||||
Kind: reflect.Slice,
|
||||
},
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
desc: "level 2, struct with allowEmpty, value true",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Value: "true"},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo struct {
|
||||
Bar string
|
||||
} `label:"allowEmpty"`
|
||||
}{
|
||||
Foo: struct {
|
||||
Bar string
|
||||
}{},
|
||||
},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "true", Kind: reflect.Struct},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 2, struct with allowEmpty, value false",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Value: "false"},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo struct {
|
||||
Bar string
|
||||
} `label:"allowEmpty"`
|
||||
}{
|
||||
Foo: struct {
|
||||
Bar string
|
||||
}{},
|
||||
},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Value: "false", Disabled: true, Kind: reflect.Struct},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 2, struct with allowEmpty with children, value false",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Value: "false", Children: []*Node{
|
||||
{Name: "Bar", Value: "hii"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo struct {
|
||||
Bar string
|
||||
} `label:"allowEmpty"`
|
||||
}{
|
||||
Foo: struct {
|
||||
Bar string
|
||||
}{},
|
||||
},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{
|
||||
Name: "Foo",
|
||||
FieldName: "Foo",
|
||||
Value: "false",
|
||||
Disabled: true,
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "hii", Kind: reflect.String},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 2, struct pointer without children",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo"},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo *struct {
|
||||
Bar string
|
||||
}
|
||||
}{
|
||||
Foo: &struct {
|
||||
Bar string
|
||||
}{},
|
||||
},
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "level 2, map without children",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo"},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo map[string]string
|
||||
}{
|
||||
Foo: map[string]string{},
|
||||
},
|
||||
expected: expected{error: true},
|
||||
},
|
||||
{
|
||||
desc: "level 2, pointer",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Children: []*Node{
|
||||
{Name: "Bar", Value: "bir"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo *struct {
|
||||
Bar string
|
||||
}
|
||||
}{
|
||||
Foo: &struct {
|
||||
Bar string
|
||||
}{},
|
||||
},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Foo", FieldName: "Foo", Kind: reflect.Ptr, Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "bir", Kind: reflect.String},
|
||||
}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 2, 2 children",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Children: []*Node{
|
||||
{Name: "Bar", Value: "bir"},
|
||||
{Name: "Bur", Value: "fuu"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo struct {
|
||||
Bar string
|
||||
Bur string
|
||||
}
|
||||
}{
|
||||
Foo: struct {
|
||||
Bar string
|
||||
Bur string
|
||||
}{},
|
||||
},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{
|
||||
Name: "Foo",
|
||||
FieldName: "Foo",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Bar", FieldName: "Bar", Value: "bir", Kind: reflect.String},
|
||||
{Name: "Bur", FieldName: "Bur", Value: "fuu", Kind: reflect.String},
|
||||
}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 3",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Children: []*Node{
|
||||
{Name: "Bar", Children: []*Node{
|
||||
{Name: "Bur", Value: "fuu"},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo struct {
|
||||
Bar struct {
|
||||
Bur string
|
||||
}
|
||||
}
|
||||
}{
|
||||
Foo: struct {
|
||||
Bar struct {
|
||||
Bur string
|
||||
}
|
||||
}{},
|
||||
},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{
|
||||
Name: "Foo",
|
||||
FieldName: "Foo",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{
|
||||
Name: "Bar",
|
||||
FieldName: "Bar",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Bur", FieldName: "Bur", Value: "fuu", Kind: reflect.String},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "level 3, 2 children level 1, 2 children level 2, 2 children level 3",
|
||||
tree: &Node{
|
||||
Name: "traefik",
|
||||
Children: []*Node{
|
||||
{Name: "Foo", Children: []*Node{
|
||||
{Name: "Bar", Children: []*Node{
|
||||
{Name: "Fii", Value: "fii"},
|
||||
{Name: "Fee", Value: "1"},
|
||||
}},
|
||||
{Name: "Bur", Children: []*Node{
|
||||
{Name: "Faa", Value: "faa"},
|
||||
}},
|
||||
}},
|
||||
{Name: "Fii", Children: []*Node{
|
||||
{Name: "FiiBar", Value: "fiiBar"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
structure: struct {
|
||||
Foo struct {
|
||||
Bar struct {
|
||||
Fii string
|
||||
Fee int
|
||||
}
|
||||
Bur struct {
|
||||
Faa string
|
||||
}
|
||||
}
|
||||
Fii struct {
|
||||
FiiBar string
|
||||
}
|
||||
}{
|
||||
Foo: struct {
|
||||
Bar struct {
|
||||
Fii string
|
||||
Fee int
|
||||
}
|
||||
Bur struct {
|
||||
Faa string
|
||||
}
|
||||
}{},
|
||||
Fii: struct {
|
||||
FiiBar string
|
||||
}{},
|
||||
},
|
||||
expected: expected{
|
||||
node: &Node{
|
||||
Name: "traefik",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{
|
||||
Name: "Foo",
|
||||
FieldName: "Foo",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{
|
||||
Name: "Bar",
|
||||
FieldName: "Bar",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Fii", FieldName: "Fii", Kind: reflect.String, Value: "fii"},
|
||||
{Name: "Fee", FieldName: "Fee", Kind: reflect.Int, Value: "1"},
|
||||
}},
|
||||
{
|
||||
Name: "Bur",
|
||||
FieldName: "Bur",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "Faa", FieldName: "Faa", Kind: reflect.String, Value: "faa"},
|
||||
}},
|
||||
}},
|
||||
{
|
||||
Name: "Fii",
|
||||
FieldName: "Fii",
|
||||
Kind: reflect.Struct,
|
||||
Children: []*Node{
|
||||
{Name: "FiiBar", FieldName: "FiiBar", Kind: reflect.String, Value: "fiiBar"},
|
||||
}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
err := AddMetadata(test.structure, test.tree)
|
||||
|
||||
if test.expected.error {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
|
||||
if !assert.Equal(t, test.expected.node, test.tree) {
|
||||
bytes, errM := json.MarshalIndent(test.tree, "", " ")
|
||||
require.NoError(t, errM)
|
||||
fmt.Println(string(bytes))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
12
pkg/provider/label/internal/tags.go
Normal file
12
pkg/provider/label/internal/tags.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
package internal
|
||||
|
||||
const (
|
||||
// TagLabel allow to apply a custom behavior.
|
||||
// - "allowEmpty": allow to create an empty struct.
|
||||
// - "-": ignore the field.
|
||||
TagLabel = "label"
|
||||
|
||||
// TagLabelSliceAsStruct allow to use a slice of struct by creating one entry into the slice.
|
||||
// The value is the substitution name use in the label to access the slice.
|
||||
TagLabelSliceAsStruct = "label-slice-as-struct"
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue