-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add missing template functions (#724)
* fix: adding missing template functions * refactor: simplify function Co-authored-by: Yash Bhardwaj <imyashbhardwaj@gmail.com>
- Loading branch information
1 parent
cdcb2be
commit cdfecee
Showing
2 changed files
with
96 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package compiler | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"text/template" | ||
"time" | ||
) | ||
|
||
func OptimusFuncMap() template.FuncMap { | ||
return map[string]any{ | ||
"Date": Date, | ||
"replace": Replace, | ||
"trunc": Trunc, | ||
"date": date, | ||
"date_modify": dateModify, | ||
"toDate": toDate, | ||
"unixEpoch": unixEpoch, | ||
"list": list, | ||
"join": join, | ||
} | ||
} | ||
|
||
func Date(timeStr string) (string, error) { | ||
t, err := time.Parse(ISOTimeFormat, timeStr) | ||
if err != nil { | ||
return "", err | ||
} | ||
return t.Format(ISODateFormat), nil | ||
} | ||
|
||
func Replace(old, newStr, name string) string { | ||
return strings.ReplaceAll(name, old, newStr) | ||
} | ||
|
||
func Trunc(c int, s string) string { | ||
if c >= 0 && len(s) > c { | ||
return s[:c] | ||
} | ||
return s | ||
} | ||
|
||
func date(fmt string, date interface{}) string { | ||
return dateInZone(fmt, date, "Local") | ||
} | ||
|
||
func dateInZone(fmt string, date interface{}, zone string) string { | ||
var t time.Time | ||
switch date := date.(type) { | ||
default: | ||
t = time.Now() | ||
case time.Time: | ||
t = date | ||
case *time.Time: | ||
t = *date | ||
case int64: | ||
t = time.Unix(date, 0) | ||
case int: | ||
t = time.Unix(int64(date), 0) | ||
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) | ||
} | ||
|
||
func dateModify(fmt string, date time.Time) time.Time { | ||
d, err := time.ParseDuration(fmt) | ||
if err != nil { | ||
return date | ||
} | ||
return date.Add(d) | ||
} | ||
|
||
func toDate(fmt, str string) time.Time { | ||
t, _ := time.ParseInLocation(fmt, str, time.Local) | ||
return t | ||
} | ||
|
||
func unixEpoch(date time.Time) string { | ||
return strconv.FormatInt(date.Unix(), 10) //nolint | ||
} | ||
|
||
func list(v ...string) []string { | ||
return v | ||
} | ||
|
||
func join(sep string, v []string) string { | ||
return strings.Join(v, sep) | ||
} |