Skip to content

Commit

Permalink
Improved variable finding for nested vars
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Lhussiez committed Oct 25, 2018
1 parent 4235a79 commit 2148d5a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
17 changes: 11 additions & 6 deletions conf/variables.go
Expand Up @@ -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
}
}
Expand Down
3 changes: 2 additions & 1 deletion conf/variables_test.go
Expand Up @@ -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) {
Expand Down

0 comments on commit 2148d5a

Please sign in to comment.