Skip to content

Commit

Permalink
core: Allow "." character in map keys
Browse files Browse the repository at this point in the history
Fixes #2143 and fixes #7130.
  • Loading branch information
jen20 committed Jul 14, 2016
1 parent b688512 commit 340655d
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions terraform/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ type Resource struct {
// its a primary instance, a tainted instance, or an orphan.
type ResourceFlag byte

const (
FlagPrimary ResourceFlag = 1 << iota
FlagTainted
FlagOrphan
FlagReplacePrimary
FlagDeposed
)

// InstanceInfo is used to hold information about the instance and/or
// resource being modified.
type InstanceInfo struct {
Expand Down Expand Up @@ -180,20 +172,32 @@ func (c *ResourceConfig) get(
}

var current interface{} = raw
for _, part := range parts {
var previous interface{} = nil
for i, part := range parts {
if current == nil {
return nil, false
}

cv := reflect.ValueOf(current)
switch cv.Kind() {
case reflect.Map:
previous = current
v := cv.MapIndex(reflect.ValueOf(part))
if !v.IsValid() {
if i > 0 && i != (len(parts)-1) {
tryKey := strings.Join(parts[i:], ".")
v := cv.MapIndex(reflect.ValueOf(tryKey))
if !v.IsValid() {
return nil, false
}
return v.Interface(), true
}

return nil, false
}
current = v.Interface()
case reflect.Slice:
previous = current
if part == "#" {
current = cv.Len()
} else {
Expand All @@ -206,6 +210,14 @@ func (c *ResourceConfig) get(
}
current = cv.Index(int(i)).Interface()
}
case reflect.String:
// This happens when map keys contain "." and have a common
// prefix so were split as path components above.
actualKey := strings.Join(parts[i-1:], ".")
if prevMap, ok := previous.(map[string]interface{}); ok {
return prevMap[actualKey], true
}
return nil, false
default:
panic(fmt.Sprintf("Unknown kind: %s", cv.Kind()))
}
Expand Down

0 comments on commit 340655d

Please sign in to comment.