Skip to content

Commit

Permalink
cty: Path.Apply should not panic with marked collections
Browse files Browse the repository at this point in the history
The return value of `HasIndex` can contain marks, so `IndexStep.Apply`
must strip those marks before checking `True`.
  • Loading branch information
jbardin committed Apr 7, 2023
1 parent 2d47ad3 commit bf2d095
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cty/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ func (s IndexStep) Apply(val Value) (Value, error) {
return NilVal, errors.New("key value not number or string")
}

has := val.HasIndex(s.Key)
// This value needs to be stripped of marks to check True(), but Index will
// apply the correct marks for the result.
has, _ := val.HasIndex(s.Key).Unmark()
if !has.IsKnown() {
return UnknownVal(val.Type().ElementType()), nil
}
Expand Down
64 changes: 64 additions & 0 deletions cty/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,70 @@ func TestPathApply(t *testing.T) {
cty.NilVal,
`at step 0: cannot access attributes on a null value`,
},
{
cty.ListVal([]cty.Value{
cty.ListVal([]cty.Value{cty.StringVal("hello")}).Mark(2),
}).Mark(1),
(cty.Path)(nil).Index(cty.NumberIntVal(0)).Index(cty.NumberIntVal(0)),
cty.StringVal("hello").Mark(1).Mark(2),
``,
},
{
cty.TupleVal([]cty.Value{
cty.ListVal([]cty.Value{cty.StringVal("hello")}).Mark(2),
}).Mark(1),
(cty.Path)(nil).Index(cty.NumberIntVal(0)).Index(cty.NumberIntVal(0)),
cty.StringVal("hello").Mark(1).Mark(2),
``,
},
{
cty.MapVal(map[string]cty.Value{
"hello": cty.StringVal("there"),
}).Mark(1),
(cty.Path)(nil).Index(cty.StringVal("hello")),
cty.StringVal("there").Mark(1),
``,
},
{
cty.ObjectVal(map[string]cty.Value{
"hello": cty.StringVal("there"),
}).Mark(1),
cty.GetAttrPath("hello"),
cty.StringVal("there").Mark(1),
``,
},
{
cty.ListVal([]cty.Value{
cty.StringVal("hello").Mark(1),
}),
(cty.Path)(nil).Index(cty.NumberIntVal(0)),
cty.StringVal("hello").Mark(1),
``,
},
{
cty.TupleVal([]cty.Value{
cty.StringVal("hello").Mark(1),
}),
(cty.Path)(nil).Index(cty.NumberIntVal(0)),
cty.StringVal("hello").Mark(1),
``,
},
{
cty.MapVal(map[string]cty.Value{
"hello": cty.StringVal("there").Mark(1),
}),
(cty.Path)(nil).Index(cty.StringVal("hello")),
cty.StringVal("there").Mark(1),
``,
},
{
cty.ObjectVal(map[string]cty.Value{
"hello": cty.StringVal("there").Mark(1),
}),
cty.GetAttrPath("hello"),
cty.StringVal("there").Mark(1),
``,
},
}

for _, test := range tests {
Expand Down

0 comments on commit bf2d095

Please sign in to comment.