Skip to content

Commit

Permalink
Backend - Added modern time formatters (#5464)
Browse files Browse the repository at this point in the history
* Backend - Added modern time formatters

Placeholder format: `{{$.scheduledTime.strftime('%Y-%m-%d')}}`

* Update go.mod

* Update go.sum

* Update parameter_formatter_test.go

* Update parameter_formatter.go

* Update parameter_formatter.go

* Update parameter_formatter.go

* Fixed regexp escaping

* Fixed typo

* Fixed formatting

* Fixed the current time placeholder

* Fixed the test

* Added error logging
  • Loading branch information
Ark-kun committed May 20, 2021
1 parent b7084f2 commit 584d5e5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
27 changes: 26 additions & 1 deletion backend/src/common/util/parameter_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
"regexp"
"strings"
"time"

"github.com/golang/glog"
"github.com/lestrrat-go/strftime"
)

const (
Expand All @@ -30,6 +33,10 @@ const (
currentTimePrefix = "[[CurrentTime."
defaultTimeFormat = "20060102150405"
suffix = "]]"

scheduledTimePrefix2 = "{{$.scheduledTime.strftime('"
currentTimePrefix2 = "{{$.currentTime.strftime('"
suffix2 = "')}}"
)

const (
Expand Down Expand Up @@ -79,7 +86,7 @@ func (p *ParameterFormatter) FormatWorkflowParameters(

// Format substitutes special strings in the provided string.
func (p *ParameterFormatter) Format(s string) string {
re := regexp.MustCompile(`\[\[(.*?)\]\]`)
re := regexp.MustCompile(`\[\[(.*?)\]\]|\{\{\$\.(.*?)\}\}`)
matches := re.FindAllString(s, -1)
if matches == nil {
return s
Expand Down Expand Up @@ -113,6 +120,24 @@ func (p *ParameterFormatter) createSubstitutes(match string) string {
match = strings.Replace(match, currentTimePrefix, "", 1)
match = strings.Replace(match, suffix, "", 1)
return time.Unix(p.nowEpoch, 0).UTC().Format(match)
} else if p.scheduledEpoch != disabledField && strings.HasPrefix(match, scheduledTimePrefix2) {
format := strings.Replace(match, scheduledTimePrefix2, "", 1)
format = strings.Replace(format, suffix2, "", 1)
formatter, err := strftime.New(format, strftime.WithUnixSeconds('s'))
if err != nil {
glog.Errorf("Could not create the strftime formatter from '%v'. Error: %v", format, err)
return match
}
return formatter.FormatString(time.Unix(p.scheduledEpoch, 0).UTC())
} else if p.nowEpoch != disabledField && strings.HasPrefix(match, currentTimePrefix2) {
format := strings.Replace(match, currentTimePrefix2, "", 1)
format = strings.Replace(format, suffix2, "", 1)
formatter, err := strftime.New(format, strftime.WithUnixSeconds('s'))
if err != nil {
glog.Errorf("Could not create the strftime formatter from '%v'. Error: %v", format, err)
return match
}
return formatter.FormatString(time.Unix(p.nowEpoch, 0).UTC())
} else {
return match
}
Expand Down
4 changes: 4 additions & 0 deletions backend/src/common/util/parameter_formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ func TestParameterFormatter_Format(t *testing.T) {

// Test empty string
assert.Equal(t, "", formatter.Format(""))

// Test modern time formatters
assert.Equal(t, "FOO 1970-01-01 00:00:25 FOO", formatter.Format("FOO {{$.scheduledTime.strftime('%Y-%m-%d %H:%M:%S')}} FOO"))
assert.Equal(t, "FOO 1970-01-01 00:00:26 FOO", formatter.Format("FOO {{$.currentTime.strftime('%Y-%m-%d %H:%M:%S')}} FOO"))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/jinzhu/gorm v1.9.1
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect
github.com/jinzhu/now v0.0.0-20181116074157-8ec929ed50c3 // indirect
github.com/lestrrat-go/strftime v1.0.4
github.com/mattn/go-sqlite3 v1.9.0
github.com/minio/minio-go v6.0.14+incompatible
github.com/peterhellberg/duration v0.0.0-20191119133758-ec6baeebcd10
Expand Down
4 changes: 4 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 584d5e5

Please sign in to comment.