core: InstanceDiff.ApplyToValue correctly handle creates

We previously tried to take a shortcut for an empty diff, just returning
the given value directly. This is incorrect in the weird case where we're
creating a new instance but it has no attributes (and thus an empty diff)
because in that case we'd return the given null value, turning the result
into a no-op or destroy change.

To fix this, we just always do the work to construct a new value, even
if we might end up doing all this just to reconstruct the same value we
started with in some cases.
This commit is contained in:
Martin Atkins 2018-09-10 14:44:32 -07:00
parent 27745af0ea
commit e488ff126e
1 changed files with 4 additions and 4 deletions

View File

@ -415,10 +415,10 @@ func (d *InstanceDiff) Unlock() { d.mu.Unlock() }
// This method is intended for shimming old subsystems that still use this // This method is intended for shimming old subsystems that still use this
// legacy diff type to work with the new-style types. // legacy diff type to work with the new-style types.
func (d *InstanceDiff) ApplyToValue(base cty.Value, schema *configschema.Block) (cty.Value, error) { func (d *InstanceDiff) ApplyToValue(base cty.Value, schema *configschema.Block) (cty.Value, error) {
// No diff means the state is unchanged. // We always build a new value here, even if the given diff is "empty",
if d.Empty() { // because we might be planning to create a new instance that happens
return base, nil // to have no attributes set, and so we want to produce an empty object
} // rather than just echoing back the null old value.
// Create an InstanceState attributes from our existing state. // Create an InstanceState attributes from our existing state.
// We can use this to more easily apply the diff changes. // We can use this to more easily apply the diff changes.