From 61fd5481cc45c15cc321b79499379096da4dfe04 Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Sun, 2 Feb 2025 13:02:46 +0000 Subject: [PATCH] Update custom API --- internal/glance/widget-custom-api.go | 34 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/internal/glance/widget-custom-api.go b/internal/glance/widget-custom-api.go index b7acce3..4c029f6 100644 --- a/internal/glance/widget-custom-api.go +++ b/internal/glance/widget-custom-api.go @@ -100,7 +100,7 @@ func fetchAndParseCustomAPI(req *http.Request, tmpl *template.Template) (templat var templateBuffer bytes.Buffer - data := CustomAPITemplateData{ + data := customAPITemplateData{ JSON: decoratedGJSONResult{gjson.Parse(body)}, Response: resp, } @@ -117,7 +117,7 @@ type decoratedGJSONResult struct { gjson.Result } -type CustomAPITemplateData struct { +type customAPITemplateData struct { JSON decoratedGJSONResult Response *http.Response } @@ -180,28 +180,28 @@ var customAPITemplateFuncs = func() template.FuncMap { "toInt": func(a float64) int64 { return int64(a) }, - "mathexpr": func(left float64, op string, right float64) float64 { - if right == 0 { + "add": func(a, b float64) float64 { + return a + b + }, + "sub": func(a, b float64) float64 { + return a - b + }, + "mul": func(a, b float64) float64 { + return a * b + }, + "div": func(a, b float64) float64 { + if b == 0 { return 0 } - switch op { - case "+": - return left + right - case "-": - return left - right - case "*": - return left * right - case "/": - return left / right - default: - return 0 - } + return a / b }, } for key, value := range globalTemplateFunctions { - funcs[key] = value + if _, exists := funcs[key]; !exists { + funcs[key] = value + } } return funcs