mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
Fix echoParam
template function to accept string key name
This changes `echoParam` template function behavior to accept not only an array or a slice and its index pair but also a map and its key pair. This also changes the function that float and uint values are treated as a valid result type of it. Fix #771
This commit is contained in:
@@ -785,20 +785,36 @@ func IsSet(a interface{}, key interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func ReturnWhenSet(a interface{}, index int) interface{} {
|
||||
av := reflect.ValueOf(a)
|
||||
func ReturnWhenSet(a, k interface{}) interface{} {
|
||||
av, isNil := indirect(reflect.ValueOf(a))
|
||||
if isNil {
|
||||
return ""
|
||||
}
|
||||
|
||||
var avv reflect.Value
|
||||
switch av.Kind() {
|
||||
case reflect.Array, reflect.Slice:
|
||||
if av.Len() > index {
|
||||
index, ok := k.(int)
|
||||
if ok && av.Len() > index {
|
||||
avv = av.Index(index)
|
||||
}
|
||||
case reflect.Map:
|
||||
kv := reflect.ValueOf(k)
|
||||
if kv.Type().AssignableTo(av.Type().Key()) {
|
||||
avv = av.MapIndex(kv)
|
||||
}
|
||||
}
|
||||
|
||||
avv := av.Index(index)
|
||||
switch avv.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return avv.Int()
|
||||
case reflect.String:
|
||||
return avv.String()
|
||||
}
|
||||
if avv.IsValid() {
|
||||
switch avv.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return avv.Int()
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
return avv.Uint()
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return avv.Float()
|
||||
case reflect.String:
|
||||
return avv.String()
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user