lang/funcs: short-circuit with unknown index and tuple collection

Since we need to know the index to know the result type for a tuple, we
need a special case here to deal with that situation and return
cty.DynamicVal; we can't predict the result type exactly until we know the
element type.
This commit is contained in:
Martin Atkins 2018-10-15 14:07:46 -07:00
parent d5d8596bb1
commit db58b88c2d
2 changed files with 24 additions and 0 deletions

View File

@ -29,6 +29,12 @@ var ElementFunc = function.New(&function.Spec{
case listTy.IsListType(): case listTy.IsListType():
return listTy.ElementType(), nil return listTy.ElementType(), nil
case listTy.IsTupleType(): case listTy.IsTupleType():
if !args[1].IsKnown() {
// If the index isn't known yet then we can't predict the
// result type since each tuple element can have its own type.
return cty.DynamicPseudoType, nil
}
etys := listTy.TupleElementTypes() etys := listTy.TupleElementTypes()
var index int var index int
err := gocty.FromCtyValue(args[1], &index) err := gocty.FromCtyValue(args[1], &index)

View File

@ -90,6 +90,24 @@ func TestElement(t *testing.T) {
cty.NumberIntVal(2), cty.NumberIntVal(2),
cty.StringVal("hello"), cty.StringVal("hello"),
}, },
{
cty.TupleVal([]cty.Value{
cty.StringVal("hello"),
cty.StringVal("bonjour"),
}),
cty.UnknownVal(cty.Number),
cty.DynamicVal,
},
{
cty.UnknownVal(cty.Tuple([]cty.Type{cty.String, cty.Bool})),
cty.NumberIntVal(1),
cty.UnknownVal(cty.Bool),
},
{
cty.UnknownVal(cty.Tuple([]cty.Type{cty.String, cty.String})),
cty.UnknownVal(cty.Number),
cty.DynamicVal,
},
} }
for _, test := range tests { for _, test := range tests {