Allows multi-level KV prefixes

This commit is contained in:
Никита Тимофеев 2020-08-11 18:42:05 +03:00 committed by GitHub
parent de458b7357
commit 449afea4fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 38 deletions

View file

@ -20,13 +20,13 @@ func DecodeToNode(pairs []*store.KVPair, rootName string, filters ...string) (*p
var node *parser.Node var node *parser.Node
for i, pair := range sortedPairs { for i, pair := range sortedPairs {
split := strings.FieldsFunc(pair.Key, func(c rune) bool { return c == '/' }) if !strings.HasPrefix(pair.Key, rootName+"/") {
return nil, fmt.Errorf("invalid label root %s", rootName)
if split[0] != rootName {
return nil, fmt.Errorf("invalid label root %s", split[0])
} }
var parts []string split := strings.Split(pair.Key[len(rootName)+1:], "/")
parts := []string{rootName}
for _, fragment := range split { for _, fragment := range split {
if exp.MatchString(fragment) { if exp.MatchString(fragment) {
parts = append(parts, "["+fragment+"]") parts = append(parts, "["+fragment+"]")

View file

@ -8,7 +8,16 @@ import (
) )
func TestDecode(t *testing.T) { func TestDecode(t *testing.T) {
pairs := mapToPairs(map[string]string{ testCases := []struct {
desc string
rootName string
pairs map[string]string
expected *sample
}{
{
desc: "simple case",
rootName: "traefik",
pairs: map[string]string{
"traefik/fielda": "bar", "traefik/fielda": "bar",
"traefik/fieldb": "1", "traefik/fieldb": "1",
"traefik/fieldc": "true", "traefik/fieldc": "true",
@ -19,14 +28,8 @@ func TestDecode(t *testing.T) {
"traefik/fieldf/Test2": "B", "traefik/fieldf/Test2": "B",
"traefik/fieldg/0/name": "A", "traefik/fieldg/0/name": "A",
"traefik/fieldg/1/name": "B", "traefik/fieldg/1/name": "B",
}) },
expected: &sample{
element := &sample{}
err := Decode(pairs, element, "traefik")
require.NoError(t, err)
expected := &sample{
FieldA: "bar", FieldA: "bar",
FieldB: 1, FieldB: 1,
FieldC: true, FieldC: true,
@ -42,8 +45,56 @@ func TestDecode(t *testing.T) {
{Name: "A"}, {Name: "A"},
{Name: "B"}, {Name: "B"},
}, },
},
},
{
desc: "multi-level root name",
rootName: "foo/bar/traefik",
pairs: map[string]string{
"foo/bar/traefik/fielda": "bar",
"foo/bar/traefik/fieldb": "2",
"foo/bar/traefik/fieldc": "true",
"foo/bar/traefik/fieldd/0": "one",
"foo/bar/traefik/fieldd/1": "two",
"foo/bar/traefik/fielde": "",
"foo/bar/traefik/fieldf/Test1": "A",
"foo/bar/traefik/fieldf/Test2": "B",
"foo/bar/traefik/fieldg/0/name": "A",
"foo/bar/traefik/fieldg/1/name": "B",
},
expected: &sample{
FieldA: "bar",
FieldB: 2,
FieldC: true,
FieldD: []string{"one", "two"},
FieldE: &struct {
Name string
}{},
FieldF: map[string]string{
"Test1": "A",
"Test2": "B",
},
FieldG: []sub{
{Name: "A"},
{Name: "B"},
},
},
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
element := &sample{}
err := Decode(mapToPairs(test.pairs), element, test.rootName)
require.NoError(t, err)
assert.Equal(t, test.expected, element)
})
} }
assert.Equal(t, expected, element)
} }
type sample struct { type sample struct {