zclsyntax: catch and test more erroneous use of "for"

This commit is contained in:
Martin Atkins 2017-06-18 08:28:20 -07:00
parent 61ebd9b65b
commit b83b6b27be
2 changed files with 76 additions and 0 deletions

View File

@ -493,6 +493,72 @@ upper(
}),
0,
},
{
`[for k, v in {hello: "world"}: k => v]`,
nil,
cty.ObjectVal(map[string]cty.Value{
"hello": cty.StringVal("world"),
}),
1, // can't have a key expr when producing a tuple
},
{
`{for v in {hello: "world"}: v}`,
nil,
cty.TupleVal([]cty.Value{
cty.StringVal("world"),
}),
1, // must have a key expr when producing a map
},
{
`[for v in {hello: "world"}: v...]`,
nil,
cty.TupleVal([]cty.Value{
cty.StringVal("world"),
}),
1, // can't use grouping when producing a tuple
},
{
`[for v in "hello": v]`,
nil,
cty.DynamicVal,
1, // can't iterate over a string
},
{
`[for v in null: v]`,
nil,
cty.DynamicVal,
1, // can't iterate over a null value
},
{
`[for v in unk: v]`,
&zcl.EvalContext{
Variables: map[string]cty.Value{
"unk": cty.UnknownVal(cty.List(cty.String)),
},
},
cty.DynamicVal,
0,
},
{
`[for v in unk: v]`,
&zcl.EvalContext{
Variables: map[string]cty.Value{
"unk": cty.DynamicVal,
},
},
cty.DynamicVal,
0,
},
{
`[for v in unk: v]`,
&zcl.EvalContext{
Variables: map[string]cty.Value{
"unk": cty.UnknownVal(cty.String),
},
},
cty.DynamicVal,
1, // can't iterate over a string (even if it's unknown)
},
{
`[{name: "Steve"}, {name: "Ermintrude"}].*.name`,

View File

@ -1331,6 +1331,16 @@ func (p *parser) finishParsingForExpr(open Token) (Expression, zcl.Diagnostics)
Context: zcl.RangeBetween(open.Range, close.Range).Ptr(),
})
}
} else {
if keyExpr == nil {
diags = append(diags, &zcl.Diagnostic{
Severity: zcl.DiagError,
Summary: "Invalid 'for' expression",
Detail: "Key expression is required when building an object.",
Subject: valExpr.Range().Ptr(),
Context: zcl.RangeBetween(open.Range, close.Range).Ptr(),
})
}
}
return &ForExpr{