From f0b9ab0d63710d7ceacda5ae56302c214486c8b0 Mon Sep 17 00:00:00 2001 From: Andinus Date: Sun, 24 Nov 2024 14:01:23 +0530 Subject: [PATCH] Fix handling of numeric types --- README.org | 8 ++++++++ template_nest.go | 29 ++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/README.org b/README.org index 99c5ce2..55d503e 100644 --- a/README.org +++ b/README.org @@ -10,6 +10,14 @@ https://pypi.org/project/template-nest/. * 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 + Initial Release. diff --git a/template_nest.go b/template_nest.go index 466e5c1..aeafb5d 100644 --- a/template_nest.go +++ b/template_nest.go @@ -8,6 +8,7 @@ import ( "path/filepath" "reflect" "regexp" + "strconv" "strings" "time" ) @@ -295,8 +296,30 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) { } return html.EscapeString(v), nil - case float64, int, int64: - return fmt.Sprintf("%v", v), nil + case float32: + 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: return nest.renderHash(v) @@ -305,7 +328,7 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) { return nest.renderHash(v) default: - return "", fmt.Errorf("unsupported template hash value type: %+v", v) + return "", fmt.Errorf("unsupported template hash value type: %T: %+v", v, v) } }