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