From 707aa36aec5bcdb047dd37ab4e185c43cf3acc7e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 23 Apr 2015 17:20:54 +0200 Subject: [PATCH] helper/schema: only use ~ on first char of code --- helper/schema/schema.go | 14 +++++---- helper/schema/schema_test.go | 57 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/helper/schema/schema.go b/helper/schema/schema.go index ed8b0ac94a..94ba865a56 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -17,7 +17,6 @@ import ( "reflect" "sort" "strconv" - "strings" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/mapstructure" @@ -812,12 +811,18 @@ func (m schemaMap) diffSet( } for _, code := range ns.listCode() { + // If the code is negative (first character is -) then + // replace it with "~" for our computed set stuff. + codeStr := strconv.Itoa(code) + if codeStr[0] == '-' { + codeStr = string('~') + codeStr[1:] + } + switch t := schema.Elem.(type) { case *Resource: // This is a complex resource for k2, schema := range t.Schema { - subK := fmt.Sprintf("%s.%d.%s", k, code, k2) - subK = strings.Replace(subK, "-", "~", -1) + subK := fmt.Sprintf("%s.%s.%s", k, codeStr, k2) err := m.diff(subK, schema, diff, d, true) if err != nil { return err @@ -831,8 +836,7 @@ func (m schemaMap) diffSet( // This is just a primitive element, so go through each and // just diff each. - subK := fmt.Sprintf("%s.%d", k, code) - subK = strings.Replace(subK, "-", "~", -1) + subK := fmt.Sprintf("%s.%s", k, codeStr) err := m.diff(subK, &t2, diff, d, true) if err != nil { return err diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 52980ad80a..7ec4e5dbb8 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -2224,6 +2224,63 @@ func TestSchemaMap_Diff(t *testing.T) { Err: false, }, + + // #58 Set with hyphen keys + { + Schema: map[string]*Schema{ + "route": &Schema{ + Type: TypeSet, + Optional: true, + Elem: &Resource{ + Schema: map[string]*Schema{ + "index": &Schema{ + Type: TypeInt, + Required: true, + }, + + "gateway-name": &Schema{ + Type: TypeString, + Optional: true, + }, + }, + }, + Set: func(v interface{}) int { + m := v.(map[string]interface{}) + return m["index"].(int) + }, + }, + }, + + State: nil, + + Config: map[string]interface{}{ + "route": []map[string]interface{}{ + map[string]interface{}{ + "index": "1", + "gateway-name": "hello", + }, + }, + }, + + Diff: &terraform.InstanceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "route.#": &terraform.ResourceAttrDiff{ + Old: "0", + New: "1", + }, + "route.1.index": &terraform.ResourceAttrDiff{ + Old: "", + New: "1", + }, + "route.1.gateway-name": &terraform.ResourceAttrDiff{ + Old: "", + New: "hello", + }, + }, + }, + + Err: false, + }, } for i, tc := range cases {