core: Allow "." character in map keys

Fixes #2143 and fixes #7130.
This commit is contained in:
James Nugent 2016-07-14 12:01:45 -06:00
parent b688512133
commit 340655d56c
1 changed files with 21 additions and 9 deletions

View File

@ -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 {
@ -180,7 +172,8 @@ 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
}
@ -188,12 +181,23 @@ func (c *ResourceConfig) get(
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 {
@ -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()))
}