Compare commits

..

6 Commits

Author SHA1 Message Date
39314e3403
Handle all pointer types generically 2025-02-22 11:18:50 +05:30
62a63d786a
Handle *string 2025-02-08 18:36:36 +05:30
0c7fd9a814
Release v0.1.4 2025-01-09 21:48:44 +05:30
b82d9089bd
Fix nil pointer dereference 2025-01-09 21:42:22 +05:30
f0b9ab0d63
Fix handling of numeric types 2024-11-24 14:20:42 +05:30
8e83ac046b
Update test file name 2024-11-18 22:25:17 +05:30
5 changed files with 57 additions and 5 deletions

@ -10,6 +10,22 @@ https://pypi.org/project/template-nest/.
* News * News
** v0.1.5 - 2025-02-22
+ Handle all pointer types generically.
** v0.1.4 - 2025-01-09
+ Fix nil pointer dereference error.
** 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"
) )
@ -278,6 +279,19 @@ func (nest *TemplateNest) MustRender(toRender interface{}) string {
} }
func (nest *TemplateNest) Render(toRender interface{}) (string, error) { func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
if toRender == nil {
return "", nil
}
// Handle pointer types generically
if reflect.TypeOf(toRender).Kind() == reflect.Ptr {
if reflect.ValueOf(toRender).IsNil() {
return "", nil
}
// Dereference the pointer and recursively call Render
return nest.Render(reflect.ValueOf(toRender).Elem().Interface())
}
if reflect.TypeOf(toRender).Kind() == reflect.Slice { if reflect.TypeOf(toRender).Kind() == reflect.Slice {
return nest.renderSlice(toRender) return nest.renderSlice(toRender)
} }
@ -295,8 +309,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 +341,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)
} }
} }

@ -79,10 +79,10 @@ func TestRenderWithEscapedVariableAtStart01(t *testing.T) {
} }
page := templatenest.Hash{ page := templatenest.Hash{
"TEMPLATE": "03-var-at-begin-with-space", "TEMPLATE": "03-var-at-begin-with-escape",
} }
outputPath := "templates/output/10-var-at-begin-with-space.html" outputPath := "templates/output/10-var-at-begin-with-escape.html"
outputContents, err := ioutil.ReadFile(outputPath) outputContents, err := ioutil.ReadFile(outputPath)
if err != nil { if err != nil {
t.Fatalf("error reading file (`%s`): %+v", outputPath, err) t.Fatalf("error reading file (`%s`): %+v", outputPath, err)