فهرست منبع

Refactor a little

binwiederhier 7 ماه پیش
والد
کامیت
1f34c39eb0
5فایلهای تغییر یافته به همراه8 افزوده شده و 22 حذف شده
  1. 3 11
      util/sprig/date.go
  2. 3 6
      util/sprig/defaults.go
  3. 1 3
      util/sprig/dict.go
  4. 1 1
      util/sprig/functions.go
  5. 0 1
      util/sprig/list.go

+ 3 - 11
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

+ 3 - 6
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

+ 1 - 3
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

+ 1 - 1
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,

+ 0 - 1
util/sprig/list.go

@@ -20,7 +20,6 @@ func push(list any, v any) []any {
 	if err != nil {
 		panic(err)
 	}
-
 	return l
 }