Skip to content

Commit 7b0372d

Browse files
Support expression slices.
1 parent a50740c commit 7b0372d

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

_tests/expr-slices.hcl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Expressions = [
2+
foo,
3+
bar,
4+
baz,
5+
]

hclencoder_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ func TestEncoder(t *testing.T) {
7070
},
7171
Output: "primitive-lists",
7272
},
73+
{
74+
ID: "expression slice",
75+
Input: struct {
76+
Expressions []string `hcl:",expr"`
77+
}{
78+
[]string{"foo", "bar", "baz"},
79+
},
80+
Output: "expr-slices",
81+
},
7382
{
7483
ID: "repeated blocks",
7584
Input: struct {

nodes.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func encodeField(in reflect.Value, meta fieldMeta) (node ast.Node, key []*ast.Ob
8989
return encodePrimitive(in, meta.expression)
9090

9191
case reflect.Slice:
92-
return encodeList(in, meta.repeatBlock)
92+
return encodeList(in, meta)
9393

9494
case reflect.Map:
9595
return encodeMap(in)
@@ -115,7 +115,7 @@ func encodePrimitive(in reflect.Value, expr bool) (ast.Node, []*ast.ObjectKey, e
115115

116116
// encodeList converts a slice to an appropriate ast.Node type depending on its
117117
// element value type. An ast.ObjectKey is never returned.
118-
func encodeList(in reflect.Value, repeatBlock bool) (ast.Node, []*ast.ObjectKey, error) {
118+
func encodeList(in reflect.Value, meta fieldMeta) (ast.Node, []*ast.ObjectKey, error) {
119119
childType := in.Type().Elem()
120120

121121
childLoop:
@@ -130,20 +130,20 @@ childLoop:
130130

131131
switch childType.Kind() {
132132
case reflect.Map, reflect.Struct, reflect.Interface:
133-
return encodeBlockList(in, repeatBlock)
133+
return encodeBlockList(in, meta)
134134
default:
135-
return encodePrimitiveList(in)
135+
return encodePrimitiveList(in, meta)
136136
}
137137
}
138138

139139
// encodePrimitiveList converts a slice of primitive values to an ast.ListType. An
140140
// ast.ObjectKey is never returned.
141-
func encodePrimitiveList(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
141+
func encodePrimitiveList(in reflect.Value, meta fieldMeta) (ast.Node, []*ast.ObjectKey, error) {
142142
l := in.Len()
143143
n := &ast.ListType{List: make([]ast.Node, 0, l)}
144144

145145
for i := 0; i < l; i++ {
146-
child, _, err := encode(in.Index(i))
146+
child, _, err := encodeField(in.Index(i), meta)
147147
if err != nil {
148148
return nil, nil, err
149149
}
@@ -157,20 +157,20 @@ func encodePrimitiveList(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
157157

158158
// encodeBlockList converts a slice of non-primitive types to an ast.ObjectList. An
159159
// ast.ObjectKey is never returned.
160-
func encodeBlockList(in reflect.Value, repeatBlock bool) (ast.Node, []*ast.ObjectKey, error) {
160+
func encodeBlockList(in reflect.Value, meta fieldMeta) (ast.Node, []*ast.ObjectKey, error) {
161161
l := in.Len()
162162
n := &ast.ObjectList{Items: make([]*ast.ObjectItem, 0, l)}
163163

164164
for i := 0; i < l; i++ {
165-
child, childKey, err := encode(in.Index(i))
165+
child, childKey, err := encodeField(in.Index(i), meta)
166166
if err != nil {
167167
return nil, nil, err
168168
}
169169
if child == nil {
170170
continue
171171
}
172-
if childKey == nil && !repeatBlock {
173-
return encodePrimitiveList(in)
172+
if childKey == nil && !meta.repeatBlock {
173+
return encodePrimitiveList(in, meta)
174174
}
175175

176176
item := &ast.ObjectItem{Val: child}

0 commit comments

Comments
 (0)