Fix handling of numeric types

This commit is contained in:
Andinus 2024-11-24 14:01:23 +05:30
parent 8e83ac046b
commit f0b9ab0d63
Signed by: andinus
SSH Key Fingerprint: SHA256:rop/w9c/gwtEJhUWS5curN3KJEwRr+Fm1ojM3liQTqo
2 changed files with 34 additions and 3 deletions

@ -10,6 +10,14 @@ https://pypi.org/project/template-nest/.
* News * News
** v0.1.3 - 2024-11-24
+ Fix handling of numeric types.
Earlier numeric values would result in an error (value type not supported),
this was due to a programming error. I've fixed the error and added support
for more numeric types.
** v0.1.0 - 2024-11-18 ** v0.1.0 - 2024-11-18
+ Initial Release. + Initial Release.

@ -8,6 +8,7 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"regexp" "regexp"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -295,8 +296,30 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
} }
return html.EscapeString(v), nil return html.EscapeString(v), nil
case float64, int, int64: case float32:
return fmt.Sprintf("%v", v), nil return strconv.FormatFloat(float64(v), 'f', -1, 32), nil
case float64:
return strconv.FormatFloat(v, 'f', -1, 64), nil
case int:
return strconv.Itoa(v), nil
case int8:
return strconv.FormatInt(int64(v), 10), nil
case int16:
return strconv.FormatInt(int64(v), 10), nil
case int32:
return strconv.FormatInt(int64(v), 10), nil
case int64:
return strconv.FormatInt(v, 10), nil
case uint:
return strconv.FormatUint(uint64(v), 10), nil
case uint8:
return strconv.FormatUint(uint64(v), 10), nil
case uint16:
return strconv.FormatUint(uint64(v), 10), nil
case uint32:
return strconv.FormatUint(uint64(v), 10), nil
case uint64:
return strconv.FormatUint(v, 10), nil
case Hash: case Hash:
return nest.renderHash(v) return nest.renderHash(v)
@ -305,7 +328,7 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
return nest.renderHash(v) return nest.renderHash(v)
default: default:
return "", fmt.Errorf("unsupported template hash value type: %+v", v) return "", fmt.Errorf("unsupported template hash value type: %T: %+v", v, v)
} }
} }