Skip to content

Commit 77ef278

Browse files
authored
ops: handle unknown objects correctly when looking up by index (#763)
1 parent dfa124f commit 77ef278

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
* Add support for decoding block and attribute source ranges when using `gohcl`. ([#703](https://github.com/hashicorp/hcl/pull/703))
88
* hclsyntax: Detect and reject invalid nested splat result. ([#724](https://github.com/hashicorp/hcl/pull/724))
99

10+
### Bugs Fixed
11+
12+
* Correct handling of unknown objects in Index function. ([#763](https://github.com/hashicorp/hcl/pull/763))
13+
1014
## v2.23.0 (November 15, 2024)
1115

1216
### Bugs Fixed

ops.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,6 @@ func Index(collection, key cty.Value, srcRange *Range) (cty.Value, Diagnostics)
195195
},
196196
}
197197
}
198-
if !collection.IsKnown() {
199-
return cty.DynamicVal.WithSameMarks(collection), nil
200-
}
201198
if !key.IsKnown() {
202199
return cty.DynamicVal.WithSameMarks(collection), nil
203200
}
@@ -225,6 +222,10 @@ func Index(collection, key cty.Value, srcRange *Range) (cty.Value, Diagnostics)
225222
}
226223
}
227224

225+
if !collection.IsKnown() {
226+
return cty.UnknownVal(ty.AttributeType(attrName)).WithSameMarks(collection), nil
227+
}
228+
228229
return collection.GetAttr(attrName), nil
229230

230231
case ty.IsSetType():

ops_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,21 @@ func TestIndex(t *testing.T) {
476476
want: cty.DynamicVal,
477477
err: "Invalid index",
478478
},
479+
"unknown object": {
480+
coll: cty.UnknownVal(cty.Object(map[string]cty.Type{
481+
"foo": cty.String,
482+
})),
483+
key: cty.StringVal("foo"),
484+
want: cty.UnknownVal(cty.String),
485+
},
486+
"unknown object, invalid index": {
487+
coll: cty.UnknownVal(cty.Object(map[string]cty.Type{
488+
"foo": cty.String,
489+
})),
490+
key: cty.NumberIntVal(0),
491+
want: cty.DynamicVal,
492+
err: "Invalid index",
493+
},
479494
}
480495

481496
for name, tc := range tests {

0 commit comments

Comments
 (0)