From d0bb8f09738d23a79688cf5e69730b1d81b37b08 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Tue, 12 Dec 2023 15:44:54 +0100 Subject: [bugfix] Let templates deref pointers, as a treat (#2448) --- internal/router/template.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'internal/router/template.go') diff --git a/internal/router/template.go b/internal/router/template.go index d5e36d6f2..d1f6f297c 100644 --- a/internal/router/template.go +++ b/internal/router/template.go @@ -22,6 +22,7 @@ import ( "html/template" "os" "path/filepath" + "reflect" "strings" "time" "unsafe" @@ -180,6 +181,19 @@ func isNil(i interface{}) bool { return (*eface)(unsafe.Pointer(&i)).data == nil } +// deref returns the dereferenced value of +// its input. To ensure you don't pass nil +// pointers into this func, use isNil first. +func deref(i any) any { + vOf := reflect.ValueOf(i) + if vOf.Kind() != reflect.Pointer { + // Not a pointer. + return i + } + + return vOf.Elem() +} + func LoadTemplateFunctions(engine *gin.Engine) { engine.SetFuncMap(template.FuncMap{ "escape": escape, @@ -194,5 +208,6 @@ func LoadTemplateFunctions(engine *gin.Engine) { "acctInstance": acctInstance, "increment": increment, "isNil": isNil, + "deref": deref, }) } -- cgit v1.2.3