Skip to content

Commit

Permalink
fix: add missing template functions (#724)
Browse files Browse the repository at this point in the history
* fix: adding missing template functions

* refactor: simplify function

Co-authored-by: Yash Bhardwaj <imyashbhardwaj@gmail.com>
  • Loading branch information
sbchaos and Mryashbhardwaj authored Jan 18, 2023
1 parent cdcb2be commit cdfecee
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 11 deletions.
12 changes: 1 addition & 11 deletions internal/compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ type Engine struct {
func NewEngine() *Engine {
baseTemplate := template.
New("optimus_template_engine").
Funcs(map[string]any{
"Date": dateFn,
})
Funcs(OptimusFuncMap())

return &Engine{
baseTemplate: baseTemplate,
Expand Down Expand Up @@ -65,11 +63,3 @@ func (e *Engine) CompileString(input string, context map[string]any) (string, er
}
return strings.TrimSpace(buf.String()), nil
}

func dateFn(timeStr string) (string, error) {
t, err := time.Parse(ISOTimeFormat, timeStr)
if err != nil {
return "", err
}
return t.Format(ISODateFormat), nil
}
95 changes: 95 additions & 0 deletions internal/compiler/template_func.go
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)
}

0 comments on commit cdfecee

Please sign in to comment.