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

Support IndexStep.Apply for element values #183

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions cty/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,19 @@ func (s IndexStep) Apply(val Value) (Value, error) {
return NilVal, errors.New("cannot index a null value")
}

// For sets, IndexStep.Key is the value itself.
if val.Type().IsSetType() {
findElementResult := val.findElement(s.Key)
has, _ := findElementResult.hasElement.Unmark()
if !has.IsKnown() {
return UnknownVal(val.Type().ElementType()), nil
}
if !has.True() {
return NilVal, errors.New("value does not have given element")
}
return findElementResult.matchingElement, nil
}

switch s.Key.Type() {
case Number:
if !(val.Type().IsListType() || val.Type().IsTupleType()) {
Expand Down
24 changes: 24 additions & 0 deletions cty/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,30 @@ func TestPathApply(t *testing.T) {
cty.StringVal("there").Mark(1),
``,
},
{
cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{"x": cty.StringVal("X1")}),
cty.ObjectVal(map[string]cty.Value{"x": cty.StringVal("X2")}),
}),
cty.IndexPath(cty.ObjectVal(map[string]cty.Value{"x": cty.StringVal("X1")})).GetAttr("x"),
cty.StringVal("X1"),
``,
},
{
cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{"x": cty.StringVal("X1")}),
cty.ObjectVal(map[string]cty.Value{"x": cty.StringVal("X2")}),
}),
cty.IndexPath(cty.ObjectVal(map[string]cty.Value{"x": cty.StringVal("X3")})).GetAttr("x"),
cty.NilVal,
`at step 0: value does not have given element`,
},
{
cty.UnknownVal(cty.Set(cty.Object(map[string]cty.Type{"x": cty.String}))),
cty.IndexPath(cty.ObjectVal(map[string]cty.Value{"x": cty.StringVal("X3")})).GetAttr("x"),
cty.UnknownVal(cty.String),
``,
},
}

for _, test := range tests {
Expand Down
29 changes: 24 additions & 5 deletions cty/value_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,10 +1021,24 @@ func (val Value) HasIndex(key Value) Value {
//
// This method will panic if the receiver is not a set, or if it is a null set.
func (val Value) HasElement(elem Value) Value {
return val.findElement(elem).hasElement
}

type findElementResult struct {
hasElement Value
matchingElement Value
}

// Like HasElement but also returns the matching set element found.
func (val Value) findElement(elem Value) findElementResult {
if val.IsMarked() || elem.IsMarked() {
val, valMarks := val.Unmark()
elem, elemMarks := elem.Unmark()
return val.HasElement(elem).WithMarks(valMarks, elemMarks)
r := val.findElement(elem)
return findElementResult{
hasElement: r.hasElement.WithMarks(valMarks, elemMarks),
matchingElement: r.matchingElement,
}
}

ty := val.Type()
Expand All @@ -1033,17 +1047,22 @@ func (val Value) HasElement(elem Value) Value {
panic("not a set type")
}
if !val.IsKnown() || !elem.IsKnown() {
return UnknownVal(Bool).RefineNotNull()
return findElementResult{hasElement: UnknownVal(Bool).RefineNotNull()}
}
if val.IsNull() {
panic("can't call HasElement on a null value")
}
if !ty.ElementType().Equals(elem.Type()) {
return False
return findElementResult{hasElement: False}
}

s := val.v.(set.Set[interface{}])
return BoolVal(s.Has(elem.v))
if !s.Has(elem.v) {
return findElementResult{hasElement: False}
}
return findElementResult{
hasElement: True,
matchingElement: elem,
}
}

// Length returns the length of the receiver, which must be a collection type
Expand Down