From c7dc1c10a3fff425637bdde7da8ab2f7729e526d Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Fri, 20 Nov 2015 14:02:20 -0600 Subject: [PATCH] helper/schema: skip StateFunc when value is nil This takes the nil checking burden off of StateFunc. fixes #3586, see that issue for further discussion --- helper/schema/schema.go | 2 +- helper/schema/schema_test.go | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 8ed813526..8c85be2cf 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -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) } diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 024b74c6d..c70827a5f 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -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, @@ -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{