Skip to content

Commit 2e5caa6

Browse files
authored
chore: fix heterogenous lists in plan.json (#146)
Also fixes empty list values
1 parent c9862a1 commit 2e5caa6

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

plan.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ func toCtyValue(a any) (cty.Value, error) {
182182
}
183183
sv = append(sv, v)
184184
}
185-
return cty.ListVal(sv), nil
185+
186+
// Always use a tuple over a list. Tuples are heterogeneous typed lists, which is
187+
// more robust. Functionally equivalent for our use case of looking up values.
188+
return cty.TupleVal(sv), nil
186189
case reflect.Map:
187190
if av.Type().Key().Kind() != reflect.String {
188191
return cty.NilVal, fmt.Errorf("map keys must be string, found %q", av.Type().Key().Kind())

plan_internal_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package preview
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
"github.com/zclconf/go-cty/cty"
8+
)
9+
10+
func Test_toCtyValue(t *testing.T) {
11+
t.Parallel()
12+
13+
t.Run("EmptyList", func(t *testing.T) {
14+
t.Parallel()
15+
val, err := toCtyValue([]any{})
16+
require.NoError(t, err)
17+
require.True(t, val.Type().IsTupleType())
18+
})
19+
20+
t.Run("HeterogeneousList", func(t *testing.T) {
21+
t.Parallel()
22+
val, err := toCtyValue([]any{5, "hello", true})
23+
require.NoError(t, err)
24+
require.True(t, val.Type().IsTupleType())
25+
require.Equal(t, 3, val.LengthInt())
26+
require.True(t, val.Equals(cty.TupleVal([]cty.Value{
27+
cty.NumberIntVal(5),
28+
cty.StringVal("hello"),
29+
cty.BoolVal(true),
30+
})).True())
31+
})
32+
}

0 commit comments

Comments
 (0)