Handle other slice and map types
This commit is contained in:
parent
07adf2d49e
commit
4a49e901ac
@ -6,6 +6,7 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
@ -256,6 +257,10 @@ func (nest *TemplateNest) MustRender(toRender interface{}) string {
|
||||
}
|
||||
|
||||
func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
|
||||
if reflect.TypeOf(toRender).Kind() == reflect.Slice {
|
||||
return nest.renderSlice(toRender)
|
||||
}
|
||||
|
||||
switch v := toRender.(type) {
|
||||
case nil:
|
||||
return "", nil
|
||||
@ -272,19 +277,37 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
|
||||
case float64, int, int64:
|
||||
return fmt.Sprintf("%v", v), nil
|
||||
|
||||
case []Hash:
|
||||
case Hash:
|
||||
return nest.renderHash(v)
|
||||
|
||||
case map[string]interface{}:
|
||||
return nest.renderHash(v)
|
||||
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported template hash value type: %+v", v)
|
||||
}
|
||||
}
|
||||
|
||||
func (nest *TemplateNest) renderSlice(toRender interface{}) (string, error) {
|
||||
val := reflect.ValueOf(toRender)
|
||||
if val.Kind() != reflect.Slice {
|
||||
return "", fmt.Errorf("expected slice, got: %T", toRender)
|
||||
}
|
||||
|
||||
var rendered strings.Builder
|
||||
for _, item := range v {
|
||||
renderedItem, err := nest.Render(item)
|
||||
for i := 0; i < val.Len(); i++ {
|
||||
element := val.Index(i).Interface()
|
||||
subRender, err := nest.Render(element)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
rendered.WriteString(renderedItem)
|
||||
rendered.WriteString(subRender)
|
||||
}
|
||||
return rendered.String(), nil
|
||||
}
|
||||
|
||||
case Hash:
|
||||
tLabel, ok := v[nest.option.NameLabel]
|
||||
func (nest *TemplateNest) renderHash(hash map[string]interface{}) (string, error) {
|
||||
tLabel, ok := hash[nest.option.NameLabel]
|
||||
if !ok {
|
||||
return "", fmt.Errorf(
|
||||
"encountered hash with no name label (name label: `%s`)", nest.option.NameLabel,
|
||||
@ -316,7 +339,7 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
|
||||
}
|
||||
|
||||
if nest.option.DieOnBadParams {
|
||||
for k, _ := range v {
|
||||
for k, _ := range hash {
|
||||
// If a variable in template hash is not present in template
|
||||
// file and it's not the template label then it's a bad param.
|
||||
_, exists := tIndex.VariableNames[k]
|
||||
@ -340,7 +363,7 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
|
||||
// with an empty string.
|
||||
replacement := ""
|
||||
|
||||
value, exists := v[variable.Name]
|
||||
value, exists := hash[variable.Name]
|
||||
defaultValue, defaultExists := nest.defaultsFlat[variable.Name]
|
||||
|
||||
if exists || defaultExists {
|
||||
@ -378,8 +401,4 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
|
||||
}
|
||||
|
||||
return strings.TrimSpace(rendered), nil
|
||||
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported template hash value type: %+v", v)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user