From 1f34c39eb04c5e60c30a88c44705cc41c37d2f2f Mon Sep 17 00:00:00 2001 From: binwiederhier Date: Sat, 19 Jul 2025 22:52:08 +0200 Subject: [PATCH] Refactor a little --- util/sprig/date.go | 14 +++----------- util/sprig/defaults.go | 9 +++------ util/sprig/dict.go | 4 +--- util/sprig/functions.go | 2 +- util/sprig/list.go | 1 - 5 files changed, 8 insertions(+), 22 deletions(-) diff --git a/util/sprig/date.go b/util/sprig/date.go index 3fed04e9..f01dcf0b 100644 --- a/util/sprig/date.go +++ b/util/sprig/date.go @@ -38,12 +38,10 @@ func dateInZone(fmt string, date any, zone string) string { case int32: t = time.Unix(int64(date), 0) } - loc, err := time.LoadLocation(zone) if err != nil { loc, _ = time.LoadLocation("UTC") } - return t.In(loc).Format(fmt) } @@ -65,7 +63,6 @@ func mustDateModify(fmt string, date time.Time) (time.Time, error) { func dateAgo(date any) string { var t time.Time - switch date := date.(type) { default: t = time.Now() @@ -76,9 +73,7 @@ func dateAgo(date any) string { case int: t = time.Unix(int64(date), 0) } - // Drop resolution to seconds - duration := time.Since(t).Round(time.Second) - return duration.String() + return time.Since(t).Round(time.Second).String() } func duration(sec any) string { @@ -106,13 +101,10 @@ func durationRound(duration any) string { case time.Time: d = time.Since(duration) } - - u := uint64(d) - neg := d < 0 - if neg { + var u uint64 + if d < 0 { u = -u } - var ( year = uint64(time.Hour) * 24 * 365 month = uint64(time.Hour) * 24 * 30 diff --git a/util/sprig/defaults.go b/util/sprig/defaults.go index 71c3e61b..948747b9 100644 --- a/util/sprig/defaults.go +++ b/util/sprig/defaults.go @@ -7,7 +7,7 @@ import ( "strings" ) -// dfault checks whether `given` is set, and returns default if not set. +// defaultValue checks whether `given` is set, and returns default if not set. // // This returns `d` if `given` appears not to be set, and `given` otherwise. // @@ -17,8 +17,7 @@ import ( // Structs are never considered unset. // // For everything else, including pointers, a nil value is unset. -func dfault(d any, given ...any) any { - +func defaultValue(d any, given ...any) any { if empty(given) || empty(given[0]) { return d } @@ -31,7 +30,6 @@ func empty(given any) bool { if !g.IsValid() { return true } - // Basically adapted from text/template.isTrue switch g.Kind() { default: @@ -140,8 +138,7 @@ func mustToRawJSON(v any) (string, error) { buf := new(bytes.Buffer) enc := json.NewEncoder(buf) enc.SetEscapeHTML(false) - err := enc.Encode(&v) - if err != nil { + if err := enc.Encode(&v); err != nil { return "", err } return strings.TrimSuffix(buf.String(), "\n"), nil diff --git a/util/sprig/dict.go b/util/sprig/dict.go index 97182a97..6485763e 100644 --- a/util/sprig/dict.go +++ b/util/sprig/dict.go @@ -33,7 +33,7 @@ func pluck(key string, d ...map[string]any) []any { } func keys(dicts ...map[string]any) []string { - k := []string{} + var k []string for _, dict := range dicts { for key := range dict { k = append(k, key) @@ -54,12 +54,10 @@ func pick(dict map[string]any, keys ...string) map[string]any { func omit(dict map[string]any, keys ...string) map[string]any { res := map[string]any{} - omit := make(map[string]bool, len(keys)) for _, k := range keys { omit[k] = true } - for k, v := range dict { if _, ok := omit[k]; !ok { res[k] = v diff --git a/util/sprig/functions.go b/util/sprig/functions.go index f7aabfc5..f0232a5b 100644 --- a/util/sprig/functions.go +++ b/util/sprig/functions.go @@ -100,7 +100,7 @@ func TxtFuncMap() template.FuncMap { "sortAlpha": sortAlpha, // Defaults - "default": dfault, + "default": defaultValue, "empty": empty, "coalesce": coalesce, "all": all, diff --git a/util/sprig/list.go b/util/sprig/list.go index 138ecfa5..d8882af0 100644 --- a/util/sprig/list.go +++ b/util/sprig/list.go @@ -20,7 +20,6 @@ func push(list any, v any) []any { if err != nil { panic(err) } - return l }