helper/schema: NewComputed values should be nil

When working on this initially, I think I thought that since NewComputed
values in the diff were empty strings, that it was using the zero value.
After review, it doesn't seem like this is the case - so I have adjusted
NewComputed to pass nil values. There is also a guard now that keeps the
new value writer from accepting computed fields with non-nil values.
This commit is contained in:
Chris Marchesi 2017-05-30 21:34:02 -07:00 committed by Martin Atkins
parent 6f422d8c44
commit 7d5f9ed6b1
1 changed files with 10 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package schema package schema
import ( import (
"errors"
"fmt" "fmt"
"reflect" "reflect"
"strings" "strings"
@ -26,6 +27,12 @@ type newValueWriter struct {
// WriteField overrides MapValueWriter's WriteField, adding the ability to flag // WriteField overrides MapValueWriter's WriteField, adding the ability to flag
// the address as computed. // the address as computed.
func (w *newValueWriter) WriteField(address []string, value interface{}, computed bool) error { func (w *newValueWriter) WriteField(address []string, value interface{}, computed bool) error {
// Fail the write if we have a non-nil value and computed is true.
// NewComputed values should not have a value when written.
if value != nil && computed {
return errors.New("Non-nil value with computed set")
}
if err := w.MapFieldWriter.WriteField(address, value); err != nil { if err := w.MapFieldWriter.WriteField(address, value); err != nil {
return err return err
} }
@ -261,12 +268,12 @@ func (d *ResourceDiff) SetNew(key string, value interface{}) error {
return d.SetDiff(key, d.get(strings.Split(key, "."), "state").Value, value, false) return d.SetDiff(key, d.get(strings.Split(key, "."), "state").Value, value, false)
} }
// SetNewComputed functions like SetNew, except that it sets the new value to // SetNewComputed functions like SetNew, except that it blanks out a new value
// the zero value and flags the attribute's diff as computed. // and marks it as computed.
// //
// This function is only allowed on computed keys. // This function is only allowed on computed keys.
func (d *ResourceDiff) SetNewComputed(key string) error { func (d *ResourceDiff) SetNewComputed(key string) error {
return d.SetDiff(key, d.get(strings.Split(key, "."), "state").Value, d.schema[key].ZeroValue(), true) return d.SetDiff(key, d.get(strings.Split(key, "."), "state").Value, nil, true)
} }
// SetDiff allows the setting of both old and new values for the diff // SetDiff allows the setting of both old and new values for the diff