Skip to content

Commit

Permalink
stacks: use UnknownForEachRepetitionData for instance map repetition …
Browse files Browse the repository at this point in the history
…data in for_each
  • Loading branch information
DanielMSchmidt committed Apr 29, 2024
1 parent 91a18a6 commit d224414
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
11 changes: 3 additions & 8 deletions internal/stacks/stackruntime/internal/stackeval/for_each.go
Expand Up @@ -157,7 +157,7 @@ func instancesMap[T any](maybeForEachVal cty.Value, makeInst func(addrs.Instance
case !maybeForEachVal.IsKnown():
// This is temporary to gradually rollout support for unknown for_each values
if allowsUnknown {
return unknownForEachInstancesMap(makeInst)
return unknownForEachInstancesMap(maybeForEachVal.Type(), makeInst)
} else {
return nil
}
Expand Down Expand Up @@ -242,14 +242,9 @@ func noForEachInstancesMap[T any](makeInst func(addrs.InstanceKey, instances.Rep
}
}

func unknownForEachInstancesMap[T any](makeInst func(addrs.InstanceKey, instances.RepetitionData) T) map[addrs.InstanceKey]T {
func unknownForEachInstancesMap[T any](ty cty.Type, makeInst func(addrs.InstanceKey, instances.RepetitionData) T) map[addrs.InstanceKey]T {
return map[addrs.InstanceKey]T{
addrs.WildcardKey: makeInst(addrs.WildcardKey, instances.RepetitionData{
// As we don't know the for_each value, we can only provide dynamic values
// for the repetition symbols.
EachKey: cty.DynamicVal,
EachValue: cty.DynamicVal,
}),
addrs.WildcardKey: makeInst(addrs.WildcardKey, instances.UnknownForEachRepetitionData(ty)),
}
}

Expand Down
27 changes: 19 additions & 8 deletions internal/stacks/stackruntime/internal/stackeval/for_each_test.go
Expand Up @@ -211,6 +211,7 @@ func TestInstancesMap(t *testing.T) {
}

tests := []struct {
Name string
Input cty.Value
Want Expectation

Expand All @@ -220,6 +221,7 @@ func TestInstancesMap(t *testing.T) {
}{
// No for_each at all
{
"nil",
cty.NilVal,
Expectation{
UnknownForEachSupported: map[addrs.InstanceKey]InstanceObj{
Expand All @@ -243,13 +245,14 @@ func TestInstancesMap(t *testing.T) {

// Unknowns
{
"unknown empty object",
cty.UnknownVal(cty.EmptyObject),
Expectation{
UnknownForEachSupported: map[addrs.InstanceKey]InstanceObj{
addrs.WildcardKey: {
Key: addrs.WildcardKey,
Rep: instances.RepetitionData{
EachKey: cty.DynamicVal,
EachKey: cty.UnknownVal(cty.String),
EachValue: cty.DynamicVal,
},
},
Expand All @@ -258,29 +261,31 @@ func TestInstancesMap(t *testing.T) {
},
},
{
"unknown bool map",
cty.UnknownVal(cty.Map(cty.Bool)),
Expectation{
UnknownForEachSupported: map[addrs.InstanceKey]InstanceObj{
addrs.WildcardKey: {
Key: addrs.WildcardKey,
Rep: instances.RepetitionData{
EachKey: cty.DynamicVal,
EachValue: cty.DynamicVal,
EachKey: cty.UnknownVal(cty.String),
EachValue: cty.UnknownVal(cty.Bool),
},
},
},
UnknownForEachUnsupported: nil, // a nil map means "unknown" for this function
},
},
{
"unknown set of strings",
cty.UnknownVal(cty.Set(cty.String)),
Expectation{
UnknownForEachSupported: map[addrs.InstanceKey]InstanceObj{
addrs.WildcardKey: {
Key: addrs.WildcardKey,
Rep: instances.RepetitionData{
EachKey: cty.DynamicVal,
EachValue: cty.DynamicVal,
EachKey: cty.UnknownVal(cty.String),
EachValue: cty.UnknownVal(cty.String),
},
},
},
Expand All @@ -290,6 +295,7 @@ func TestInstancesMap(t *testing.T) {

// Empties
{
"empty object",
cty.EmptyObjectVal,
Expectation{
UnknownForEachSupported: map[addrs.InstanceKey]InstanceObj{
Expand All @@ -305,6 +311,7 @@ func TestInstancesMap(t *testing.T) {
},
},
{
"empty string map",
cty.MapValEmpty(cty.String),
Expectation{
UnknownForEachSupported: map[addrs.InstanceKey]InstanceObj{
Expand All @@ -320,6 +327,7 @@ func TestInstancesMap(t *testing.T) {
},
},
{
"empty string set",
cty.SetValEmpty(cty.String),
Expectation{
UnknownForEachSupported: map[addrs.InstanceKey]InstanceObj{
Expand All @@ -337,6 +345,7 @@ func TestInstancesMap(t *testing.T) {

// Known and not empty
{
"object",
cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("beep"),
"b": cty.StringVal("boop"),
Expand Down Expand Up @@ -377,6 +386,7 @@ func TestInstancesMap(t *testing.T) {
},
},
{
"map",
cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("beep"),
"b": cty.StringVal("boop"),
Expand Down Expand Up @@ -417,6 +427,7 @@ func TestInstancesMap(t *testing.T) {
},
},
{
"set",
cty.SetVal([]cty.Value{
cty.StringVal("beep"),
cty.StringVal("boop"),
Expand Down Expand Up @@ -459,15 +470,15 @@ func TestInstancesMap(t *testing.T) {
}

for _, test := range tests {
t.Run(fmt.Sprintf("%s", test.Input), func(t *testing.T) {
t.Run("unknown for_each supported", func(t *testing.T) {
t.Run(fmt.Sprintf("%s", test.Name), func(t *testing.T) {

Check failure on line 473 in internal/stacks/stackruntime/internal/stackeval/for_each_test.go

View workflow job for this annotation

GitHub Actions / Code Consistency Checks

the argument is already a string, there's no need to use fmt.Sprintf (S1025)

Check failure on line 473 in internal/stacks/stackruntime/internal/stackeval/for_each_test.go

View workflow job for this annotation

GitHub Actions / Code Consistency Checks

the argument is already a string, there's no need to use fmt.Sprintf (S1025)
t.Run("unknown for_each supported", func(t *testing.T) {
got := instancesMap(test.Input, makeObj, true)

if diff := cmp.Diff(test.Want.UnknownForEachSupported, got, ctydebug.CmpOptions); diff != "" {
t.Errorf("wrong result\ninput: %#v\n%s", test.Input, diff)
}
})
t.Run("unknown for_each unsupported", func(t *testing.T) {
t.Run("unknown for_each unsupported", func(t *testing.T) {
got := instancesMap(test.Input, makeObj, false)

if diff := cmp.Diff(test.Want.UnknownForEachUnsupported, got, ctydebug.CmpOptions); diff != "" {
Expand Down

0 comments on commit d224414

Please sign in to comment.