zclsyntax: evaluation of relative traversal and index nodes

This commit is contained in:
Martin Atkins 2017-06-05 07:41:02 -07:00
parent b604827bb2
commit ace387f5f9
2 changed files with 50 additions and 2 deletions

View File

@ -83,7 +83,10 @@ func (e *RelativeTraversalExpr) walkChildNodes(w internalWalkFunc) {
}
func (e *RelativeTraversalExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) {
panic("RelativeTraversalExpr.Value not yet implemented")
src, diags := e.Source.Value(ctx)
ret, travDiags := e.Traversal.TraverseRel(src)
diags = append(diags, travDiags...)
return ret, diags
}
func (e *RelativeTraversalExpr) Range() zcl.Range {
@ -413,7 +416,13 @@ func (e *IndexExpr) walkChildNodes(w internalWalkFunc) {
}
func (e *IndexExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) {
panic("IndexExpr.Value not yet implemented")
var diags zcl.Diagnostics
coll, collDiags := e.Collection.Value(ctx)
key, keyDiags := e.Key.Value(ctx)
diags = append(diags, collDiags...)
diags = append(diags, keyDiags...)
return zcl.Index(coll, key, &e.SrcRange)
}
func (e *IndexExpr) Range() zcl.Range {

View File

@ -272,6 +272,45 @@ upper(
}),
0,
},
{
`["hello"][0]`,
nil,
cty.StringVal("hello"),
0,
},
{
`[][0]`,
nil,
cty.DynamicVal,
1, // invalid index
},
{
`["hello"][negate(0)]`,
&zcl.EvalContext{
Functions: map[string]function.Function{
"negate": stdlib.NegateFunc,
},
},
cty.StringVal("hello"),
0,
},
{
`[][negate(0)]`,
&zcl.EvalContext{
Functions: map[string]function.Function{
"negate": stdlib.NegateFunc,
},
},
cty.DynamicVal,
1, // invalid index
},
{
`["hello"]["0"]`, // key gets converted to number
nil,
cty.StringVal("hello"),
0,
},
}
for _, test := range tests {