Skip to content

Commit cdfecee

Browse files
fix: add missing template functions (#724)
* fix: adding missing template functions * refactor: simplify function Co-authored-by: Yash Bhardwaj <[email protected]>
1 parent cdcb2be commit cdfecee

File tree

2 files changed

+96
-11
lines changed

2 files changed

+96
-11
lines changed

internal/compiler/engine.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ type Engine struct {
2626
func NewEngine() *Engine {
2727
baseTemplate := template.
2828
New("optimus_template_engine").
29-
Funcs(map[string]any{
30-
"Date": dateFn,
31-
})
29+
Funcs(OptimusFuncMap())
3230

3331
return &Engine{
3432
baseTemplate: baseTemplate,
@@ -65,11 +63,3 @@ func (e *Engine) CompileString(input string, context map[string]any) (string, er
6563
}
6664
return strings.TrimSpace(buf.String()), nil
6765
}
68-
69-
func dateFn(timeStr string) (string, error) {
70-
t, err := time.Parse(ISOTimeFormat, timeStr)
71-
if err != nil {
72-
return "", err
73-
}
74-
return t.Format(ISODateFormat), nil
75-
}

internal/compiler/template_func.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package compiler
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
"text/template"
7+
"time"
8+
)
9+
10+
func OptimusFuncMap() template.FuncMap {
11+
return map[string]any{
12+
"Date": Date,
13+
"replace": Replace,
14+
"trunc": Trunc,
15+
"date": date,
16+
"date_modify": dateModify,
17+
"toDate": toDate,
18+
"unixEpoch": unixEpoch,
19+
"list": list,
20+
"join": join,
21+
}
22+
}
23+
24+
func Date(timeStr string) (string, error) {
25+
t, err := time.Parse(ISOTimeFormat, timeStr)
26+
if err != nil {
27+
return "", err
28+
}
29+
return t.Format(ISODateFormat), nil
30+
}
31+
32+
func Replace(old, newStr, name string) string {
33+
return strings.ReplaceAll(name, old, newStr)
34+
}
35+
36+
func Trunc(c int, s string) string {
37+
if c >= 0 && len(s) > c {
38+
return s[:c]
39+
}
40+
return s
41+
}
42+
43+
func date(fmt string, date interface{}) string {
44+
return dateInZone(fmt, date, "Local")
45+
}
46+
47+
func dateInZone(fmt string, date interface{}, zone string) string {
48+
var t time.Time
49+
switch date := date.(type) {
50+
default:
51+
t = time.Now()
52+
case time.Time:
53+
t = date
54+
case *time.Time:
55+
t = *date
56+
case int64:
57+
t = time.Unix(date, 0)
58+
case int:
59+
t = time.Unix(int64(date), 0)
60+
case int32:
61+
t = time.Unix(int64(date), 0)
62+
}
63+
64+
loc, err := time.LoadLocation(zone)
65+
if err != nil {
66+
loc, _ = time.LoadLocation("UTC")
67+
}
68+
69+
return t.In(loc).Format(fmt)
70+
}
71+
72+
func dateModify(fmt string, date time.Time) time.Time {
73+
d, err := time.ParseDuration(fmt)
74+
if err != nil {
75+
return date
76+
}
77+
return date.Add(d)
78+
}
79+
80+
func toDate(fmt, str string) time.Time {
81+
t, _ := time.ParseInLocation(fmt, str, time.Local)
82+
return t
83+
}
84+
85+
func unixEpoch(date time.Time) string {
86+
return strconv.FormatInt(date.Unix(), 10) //nolint
87+
}
88+
89+
func list(v ...string) []string {
90+
return v
91+
}
92+
93+
func join(sep string, v []string) string {
94+
return strings.Join(v, sep)
95+
}

0 commit comments

Comments
 (0)