Skip to content

Commit

Permalink
helper/schema: skip StateFunc when value is nil
Browse files Browse the repository at this point in the history
fixes #3586
  • Loading branch information
phinze committed Nov 20, 2015
1 parent 9382810 commit 78a6d85
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ func (m schemaMap) diffString(
var originalN interface{}
var os, ns string
o, n, _, _ := d.diffChange(k)
if schema.StateFunc != nil {
if schema.StateFunc != nil && n != nil {
originalN = n
n = schema.StateFunc(n)
}
Expand Down
32 changes: 31 additions & 1 deletion helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func TestSchemaMap_Diff(t *testing.T) {
Err: false,
},

"#7 String with StateFunc": {
"String with StateFunc": {
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Expand Down Expand Up @@ -352,6 +352,36 @@ func TestSchemaMap_Diff(t *testing.T) {
Err: false,
},

"StateFunc not called with nil value": {
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
StateFunc: func(a interface{}) string {
t.Fatalf("should not get here!")
return ""
},
},
},

State: nil,

Config: map[string]interface{}{},

Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"availability_zone": &terraform.ResourceAttrDiff{
Old: "",
New: "",
NewComputed: true,
},
},
},

Err: false,
},

"#8 Variable (just checking)": {
Schema: map[string]*Schema{
"availability_zone": &Schema{
Expand Down

0 comments on commit 78a6d85

Please sign in to comment.