Skip to content

Commit 0c2de24

Browse files
authored
Merge pull request #78 from wata727/fix_empty_list_panic
Fix panic when parsing empty list
2 parents be45ff4 + e465076 commit 0c2de24

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

evaluator/variable.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -134,30 +134,37 @@ func parseVariable(val interface{}, varType string) hilast.Variable {
134134
case HCL_LIST_VARTYPE:
135135
s := reflect.ValueOf(val)
136136

137-
switch reflect.TypeOf(s.Index(0).Interface()).Kind() {
138-
case reflect.Map:
139-
var variables map[string]hilast.Variable
140-
variables = map[string]hilast.Variable{}
141-
for i := 0; i < s.Len(); i++ {
142-
ms := reflect.ValueOf(s.Index(i).Interface())
143-
for _, k := range ms.MapKeys() {
144-
key := k.Interface().(string)
145-
value := ms.MapIndex(reflect.ValueOf(key)).Interface()
146-
variables[key] = parseVariable(value, "")
147-
}
148-
}
149-
hilVar = hilast.Variable{
150-
Type: hilast.TypeMap,
151-
Value: variables,
152-
}
153-
default:
154-
var variables []hilast.Variable
155-
for i := 0; i < s.Len(); i++ {
156-
variables = append(variables, parseVariable(s.Index(i).Interface(), ""))
157-
}
137+
if s.Len() == 0 {
158138
hilVar = hilast.Variable{
159139
Type: hilast.TypeList,
160-
Value: variables,
140+
Value: []hilast.Variable{},
141+
}
142+
} else {
143+
switch reflect.TypeOf(s.Index(0).Interface()).Kind() {
144+
case reflect.Map:
145+
var variables map[string]hilast.Variable
146+
variables = map[string]hilast.Variable{}
147+
for i := 0; i < s.Len(); i++ {
148+
ms := reflect.ValueOf(s.Index(i).Interface())
149+
for _, k := range ms.MapKeys() {
150+
key := k.Interface().(string)
151+
value := ms.MapIndex(reflect.ValueOf(key)).Interface()
152+
variables[key] = parseVariable(value, "")
153+
}
154+
}
155+
hilVar = hilast.Variable{
156+
Type: hilast.TypeMap,
157+
Value: variables,
158+
}
159+
default:
160+
var variables []hilast.Variable
161+
for i := 0; i < s.Len(); i++ {
162+
variables = append(variables, parseVariable(s.Index(i).Interface(), ""))
163+
}
164+
hilVar = hilast.Variable{
165+
Type: hilast.TypeList,
166+
Value: variables,
167+
}
161168
}
162169
}
163170
}

evaluator/variable_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,17 @@ func TestParseVariable(t *testing.T) {
550550
},
551551
},
552552
},
553+
{
554+
Name: "parse empty list",
555+
Input: Input{
556+
Val: []string{},
557+
Type: "list",
558+
},
559+
Result: hilast.Variable{
560+
Type: hilast.TypeList,
561+
Value: []hilast.Variable{},
562+
},
563+
},
553564
}
554565

555566
for _, tc := range cases {

0 commit comments

Comments
 (0)