helper/schema: only use ~ on first char of code

This commit is contained in:
Mitchell Hashimoto 2015-04-23 17:20:54 +02:00
parent 77314a01d2
commit 707aa36aec
2 changed files with 66 additions and 5 deletions

View File

@ -17,7 +17,6 @@ import (
"reflect" "reflect"
"sort" "sort"
"strconv" "strconv"
"strings"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
@ -812,12 +811,18 @@ func (m schemaMap) diffSet(
} }
for _, code := range ns.listCode() { 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) { switch t := schema.Elem.(type) {
case *Resource: case *Resource:
// This is a complex resource // This is a complex resource
for k2, schema := range t.Schema { for k2, schema := range t.Schema {
subK := fmt.Sprintf("%s.%d.%s", k, code, k2) subK := fmt.Sprintf("%s.%s.%s", k, codeStr, k2)
subK = strings.Replace(subK, "-", "~", -1)
err := m.diff(subK, schema, diff, d, true) err := m.diff(subK, schema, diff, d, true)
if err != nil { if err != nil {
return err return err
@ -831,8 +836,7 @@ func (m schemaMap) diffSet(
// This is just a primitive element, so go through each and // This is just a primitive element, so go through each and
// just diff each. // just diff each.
subK := fmt.Sprintf("%s.%d", k, code) subK := fmt.Sprintf("%s.%s", k, codeStr)
subK = strings.Replace(subK, "-", "~", -1)
err := m.diff(subK, &t2, diff, d, true) err := m.diff(subK, &t2, diff, d, true)
if err != nil { if err != nil {
return err return err

View File

@ -2224,6 +2224,63 @@ func TestSchemaMap_Diff(t *testing.T) {
Err: false, 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 { for i, tc := range cases {