Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path.Apply panics with marked collections #160

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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