Skip to content

Commit

Permalink
Merge pull request #78 from wata727/fix_empty_list_panic
Browse files Browse the repository at this point in the history
Fix panic when parsing empty list
  • Loading branch information
wata727 authored Mar 25, 2017
2 parents be45ff4 + e465076 commit 0c2de24
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
51 changes: 29 additions & 22 deletions evaluator/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,30 +134,37 @@ func parseVariable(val interface{}, varType string) hilast.Variable {
case HCL_LIST_VARTYPE:
s := reflect.ValueOf(val)

switch reflect.TypeOf(s.Index(0).Interface()).Kind() {
case reflect.Map:
var variables map[string]hilast.Variable
variables = map[string]hilast.Variable{}
for i := 0; i < s.Len(); i++ {
ms := reflect.ValueOf(s.Index(i).Interface())
for _, k := range ms.MapKeys() {
key := k.Interface().(string)
value := ms.MapIndex(reflect.ValueOf(key)).Interface()
variables[key] = parseVariable(value, "")
}
}
hilVar = hilast.Variable{
Type: hilast.TypeMap,
Value: variables,
}
default:
var variables []hilast.Variable
for i := 0; i < s.Len(); i++ {
variables = append(variables, parseVariable(s.Index(i).Interface(), ""))
}
if s.Len() == 0 {
hilVar = hilast.Variable{
Type: hilast.TypeList,
Value: variables,
Value: []hilast.Variable{},
}
} else {
switch reflect.TypeOf(s.Index(0).Interface()).Kind() {
case reflect.Map:
var variables map[string]hilast.Variable
variables = map[string]hilast.Variable{}
for i := 0; i < s.Len(); i++ {
ms := reflect.ValueOf(s.Index(i).Interface())
for _, k := range ms.MapKeys() {
key := k.Interface().(string)
value := ms.MapIndex(reflect.ValueOf(key)).Interface()
variables[key] = parseVariable(value, "")
}
}
hilVar = hilast.Variable{
Type: hilast.TypeMap,
Value: variables,
}
default:
var variables []hilast.Variable
for i := 0; i < s.Len(); i++ {
variables = append(variables, parseVariable(s.Index(i).Interface(), ""))
}
hilVar = hilast.Variable{
Type: hilast.TypeList,
Value: variables,
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions evaluator/variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,17 @@ func TestParseVariable(t *testing.T) {
},
},
},
{
Name: "parse empty list",
Input: Input{
Val: []string{},
Type: "list",
},
Result: hilast.Variable{
Type: hilast.TypeList,
Value: []hilast.Variable{},
},
},
}

for _, tc := range cases {
Expand Down

0 comments on commit 0c2de24

Please sign in to comment.