Commit 95d086fd
95d086fd8164Merged
Svyatoslav Dumchenko <[email protected]> ยท 4 years ago ยท 1 file ยท ~1 modified
Description
Fix some functions for templates
Details
- Commit
- 95d086fd8164
- Parent
- 119ef1b87b23
pkg/template/extra.gomodifiedView file
@@ -118,31 +118,34 @@
118118
components := strings.Split(path, ".")119119
pathToMap := components[:len(components)-1]120120
key := components[len(components)-1]121+
121122
for _, k := range pathToMap {122-
var elem interface{}123-
124123
switch typedCurrent := current.(type) {124+
case Values:125+
v, exists := typedCurrent[k]126+
if !exists {127+
return nil, fmt.Errorf("failed to set value at path \"%s\": value for key \"%s\" does not exist", path, k)128+
}129+
current = v125130
case map[string]interface{}:126131
v, exists := typedCurrent[k]127132
if !exists {128133
return nil, fmt.Errorf("failed to set value at path \"%s\": value for key \"%s\" does not exist", path, k)129134
}130-
elem = v135+
current = v131136
case map[interface{}]interface{}:132137
v, exists := typedCurrent[k]133138
if !exists {134139
return nil, fmt.Errorf("failed to set value at path \"%s\": value for key \"%s\" does not exist", path, k)135140
}136-
elem = v141+
current = v137142
default:138-
return nil, fmt.Errorf("failed to set value at path \"%s\": value for key \"%s\" was not a map", path, k)139-
}140-
141-
switch typedElem := elem.(type) {142-
case map[string]interface{}, map[interface{}]interface{}:143-
current = typedElem144-
default:145-
return nil, fmt.Errorf("failed to set value at path \"%s\": value for key \"%s\" was not a map", path, k)143+
return nil, fmt.Errorf(144+
"failed to walk over path \"%s\": value for key \"%s\" is not a map: %+v",145+
path,146+
k,147+
reflect.TypeOf(current),148+
)146149
}147150
}148151
@@ -151,8 +154,15 @@
151154
typedCurrent[key] = value152155
case map[interface{}]interface{}:153156
typedCurrent[key] = value157+
case Values:158+
typedCurrent[key] = value154159
default:155-
return nil, fmt.Errorf("failed to set value at path \"%s\": value for key \"%s\" was not a map", path, key)160+
return nil, fmt.Errorf(161+
"failed to set value at path \"%s\": value for key \"%s\" is not a map: %+v",162+
path,163+
key,164+
reflect.TypeOf(current),165+
)156166
}157167
158168
return values, nil@@ -236,6 +246,15 @@
236246
var v interface{}237247
var ok bool238248
switch typedObj := obj.(type) {249+
case Values:250+
v, ok = typedObj[keys[0]]251+
if !ok {252+
if defSet {253+
return def, nil254+
}255+
256+
return nil, &noValueError{fmt.Sprintf("no value exist for key %q in %v", keys[0], typedObj)}257+
}239258
case map[string]interface{}:240259
v, ok = typedObj[keys[0]]241260
if !ok {@@ -317,6 +336,11 @@
317336
var v interface{}318337
var ok bool319338
switch typedObj := obj.(type) {339+
case Values:340+
v, ok = typedObj[keys[0]]341+
if !ok {342+
return defSet, nil343+
}320344
case map[string]interface{}:321345
v, ok = typedObj[keys[0]]322346
if !ok {