Skip to content

Commit

Permalink
fix: template does not follow RFC3339 time format
Browse files Browse the repository at this point in the history
Signed-off-by: James Yean <[email protected]>
  • Loading branch information
ifplusor committed Oct 17, 2023
1 parent 3c31d91 commit f3da796
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
7 changes: 5 additions & 2 deletions pkg/template/json/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ import (
"io"
"runtime"
"sync"
"time"
"unsafe"

// third-party libraries.
"github.com/ohler55/ojg/oj"

// this project.
// first-party libraries.
"github.com/vanus-labs/vanus/lib/json/generate"
)

var writerPool = sync.Pool{
New: func() any {
return &oj.Writer{Options: oj.DefaultOptions}
opts := oj.DefaultOptions
opts.TimeFormat = time.RFC3339
return &oj.Writer{Options: opts}
},
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/template/json/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package json
import (
// standard libraries.
"testing"
"time"

// third-party libraries.
. "github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -135,5 +136,14 @@ func newTestExecFunc(tp template.Template, model any, variables map[string]any,
So(err, ShouldBeNil)
So(string(v), ShouldEqual, `{"key":"<a\r\nb>","key2":"<b\r\na>"}`)
})

Convey("time value", func() {
tt, _ := time.Parse(time.RFC3339, "2018-04-05T17:31:00Z")
m["var"] = tt
m["var2"] = tt
v, err := tp.Execute(model, variables)
So(err, ShouldBeNil)
So(string(v), ShouldEqual, `{"key":"2018-04-05T17:31:00Z","key2":"\"2018-04-05T17:31:00Z\""}`)
})
}
}
6 changes: 5 additions & 1 deletion pkg/template/text/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@ import (
// standard libraries.
"io"
"sync"
"time"

// third-party libraries.
"github.com/ohler55/ojg/oj"

// first-party libraries.
"github.com/vanus-labs/vanus/lib/bytes"
)

var writerPool = sync.Pool{
New: func() any {
return &oj.Writer{Options: oj.DefaultOptions}
opts := oj.DefaultOptions
opts.TimeFormat = time.RFC3339
return &oj.Writer{Options: opts}
},
}

Expand Down

0 comments on commit f3da796

Please sign in to comment.