Skip to content
Permalink
Browse files

Improved variable finding for nested vars

  • Loading branch information...
Depado committed Oct 25, 2018
1 parent 4235a79 commit 2148d5a0e69e0521214f46896dc237343b425b2d
Showing with 13 additions and 7 deletions.
  1. +11 −6 conf/variables.go
  2. +2 −1 conf/variables_test.go
@@ -15,16 +15,21 @@ type Variables []*Variable
// FindNamed will find a variable by name in the global variables. Returns nil
// if not found
func (vv Variables) FindNamed(s string) *Variable {
// First pass, top level
return vv.FindWithParent(nil, s)
}

// FindWithParent tries to find the variable with a prefix
func (vv Variables) FindWithParent(p *Variable, s string) *Variable {
for _, v := range vv {
if v.Name == s {
if p != nil {
if p.Name+"_"+v.Name == s {
return v
}
} else if v.Name == s {
return v
}
}
// Second pass, get inside variables
for _, v := range vv {
if v.Variables != nil {
if out := v.Variables.FindNamed(s); out != nil {
if out := v.Variables.FindWithParent(v, s); out != nil {
return out
}
}
@@ -90,7 +90,8 @@ func TestVariables_FindNamed(t *testing.T) {
{"should find bool", vv, args{hasbool.Name}, hasbool},
{"should find value", vv, args{hasvalue.Name}, hasvalue},
{"shouldn't find", vv, args{"random.jpg"}, nil},
{"should find nested", Variables{parentvar}, args{"sub"}, subvar},
{"should find nested", Variables{parentvar}, args{"parent_sub"}, subvar},
{"shouldn't find nested raw", Variables{parentvar}, args{"sub"}, nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

0 comments on commit 2148d5a

Please sign in to comment.
You can’t perform that action at this time.