Skip to content

Commit

Permalink
optional+computed
Browse files Browse the repository at this point in the history
Simplify the proposedNewAttributes cases, and add another test for
coverage.
  • Loading branch information
jbardin committed Jan 19, 2023
1 parent 470ed22 commit 7ca9abe
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
19 changes: 11 additions & 8 deletions internal/plans/objchange/objchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,7 @@ func proposedNewAttributes(attrs map[string]*configschema.Attribute, prior, conf
// For non-computed NestedType attributes, we need to descend
// into the individual nested attributes to build the final
// value, unless the entire nested attribute is unknown.
if !configV.IsKnown() {
newV = configV
} else {
newV = proposedNewNestedType(attr.NestedType, priorV, configV)
}
newV = proposedNewNestedType(attr.NestedType, priorV, configV)
default:
// For non-computed attributes, we always take the config value,
// even if it is null. If it's _required_ then null values
Expand All @@ -319,12 +315,19 @@ func proposedNewAttributes(attrs map[string]*configschema.Attribute, prior, conf
}

func proposedNewNestedType(schema *configschema.Object, prior, config cty.Value) cty.Value {
// If the config is null or empty, we will be using this default value.
// if the config isn't known at all, then we must use that value
if !config.IsNull() && !config.IsKnown() {
return config
}

// Even if the config is null or empty, we will be using this default value.
newV := config

switch schema.Nesting {
case configschema.NestingSingle:
// if the config is null, we already have our value
// If the config is null, we already have our value. If the attribute
// is optional+computed, we won't reach this branch with a null value
// since the computed case would have been taken.
if config.IsNull() {
break
}
Expand All @@ -334,7 +337,7 @@ func proposedNewNestedType(schema *configschema.Object, prior, config cty.Value)
case configschema.NestingList:
// Nested blocks are correlated by index.
configVLen := 0
if config.IsKnown() && !config.IsNull() {
if !config.IsNull() {
configVLen = config.LengthInt()
}

Expand Down
43 changes: 43 additions & 0 deletions internal/plans/objchange/objchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,49 @@ func TestProposedNew(t *testing.T) {
})),
}),
},

"prior optional computed nested single to null": {
&configschema.Block{
Attributes: map[string]*configschema.Attribute{
"bloop": {
NestedType: &configschema.Object{
Nesting: configschema.NestingSingle,
Attributes: map[string]*configschema.Attribute{
"blop": {
Type: cty.String,
Required: true,
},
"bleep": {
Type: cty.String,
Optional: true,
},
},
},
Optional: true,
Computed: true,
},
},
},
cty.ObjectVal(map[string]cty.Value{
"bloop": cty.ObjectVal(map[string]cty.Value{
"blop": cty.StringVal("glub"),
"bleep": cty.NullVal(cty.String),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"bloop": cty.NullVal(cty.Object(map[string]cty.Type{
"blop": cty.String,
"bleep": cty.String,
})),
}),
cty.ObjectVal(map[string]cty.Value{
"bloop": cty.ObjectVal(map[string]cty.Value{
"blop": cty.StringVal("glub"),
"bleep": cty.NullVal(cty.String),
}),
}),
},

"prior nested list": {
&configschema.Block{
BlockTypes: map[string]*configschema.NestedBlock{
Expand Down

0 comments on commit 7ca9abe

Please sign in to comment.