From a3dd7d04f9bb7718d4830ebb7c06db82f9034d52 Mon Sep 17 00:00:00 2001 From: James Yean Date: Tue, 17 Oct 2023 16:15:35 +0800 Subject: [PATCH] fix: template does not follow RFC3339 time format (#639) Signed-off-by: James Yean --- pkg/template/json/helper.go | 13 ++++++++++--- pkg/template/json/helper_test.go | 16 +++++++++++++++- pkg/template/json/template_test.go | 10 ++++++++++ pkg/template/text/helper.go | 4 ++++ 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/pkg/template/json/helper.go b/pkg/template/json/helper.go index 26bce774b..d9e021c97 100644 --- a/pkg/template/json/helper.go +++ b/pkg/template/json/helper.go @@ -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} }, } @@ -57,7 +60,9 @@ func writeJSONInJSONString(w io.Writer, v any) error { func writeInJSONString(w io.Writer, v any) error { switch v.(type) { - case nil, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string: + case nil, bool, int, int8, int16, int32, int64, + uint, uint8, uint16, uint32, uint64, + float32, float64, string, time.Time: var ba [32]byte // underlying array of byte buffer in stack. buf := unsafe.Slice((*byte)(noescape(unsafe.Pointer(&ba[0]))), len(ba)) data := appendInJSONString(buf[:0], v) @@ -101,6 +106,8 @@ func appendInJSONString(dst []byte, v any) []byte { return generate.AppendFloat64(dst, val) case string: return generate.AppendRawString(dst, val) + case time.Time: + return val.AppendFormat(dst, time.RFC3339) } // TODO return nil diff --git a/pkg/template/json/helper_test.go b/pkg/template/json/helper_test.go index 58b2994ee..abf9b560d 100644 --- a/pkg/template/json/helper_test.go +++ b/pkg/template/json/helper_test.go @@ -20,11 +20,12 @@ import ( "math" "runtime" "testing" + "time" // third-party libraries. . "github.com/smartystreets/goconvey/convey" - // this project. + // first-party libraries. "github.com/vanus-labs/vanus/lib/bytes" ) @@ -108,6 +109,12 @@ func TestHelper(t *testing.T) { str := appendInJSONString(nil, "\"foo\"\n") So(string(str), ShouldEqual, `\"foo\"\n`) }) + + Convey("append time", func() { + tt := time.Now() + str := appendInJSONString(nil, tt) + So(string(str), ShouldEqual, tt.Format(time.RFC3339)) + }) }) Convey("write in JSON string", t, func() { @@ -269,5 +276,12 @@ func TestHelper(t *testing.T) { So(stats1.Mallocs-stats0.Mallocs, ShouldEqual, 0) So(stats1.HeapAlloc-stats0.HeapAlloc, ShouldEqual, 0) }) + + Convey("write time", func() { + tt := time.Now() + err := writeInJSONString(&buf, tt) + So(err, ShouldBeNil) + So(buf.String(), ShouldEqual, tt.Format(time.RFC3339)) + }) }) } diff --git a/pkg/template/json/template_test.go b/pkg/template/json/template_test.go index 9e48312fd..5c14c6893 100644 --- a/pkg/template/json/template_test.go +++ b/pkg/template/json/template_test.go @@ -17,6 +17,7 @@ package json import ( // standard libraries. "testing" + "time" // third-party libraries. . "github.com/smartystreets/goconvey/convey" @@ -135,5 +136,14 @@ func newTestExecFunc(tp template.Template, model any, variables map[string]any, So(err, ShouldBeNil) So(string(v), ShouldEqual, `{"key":"","key2":""}`) }) + + 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"}`) + }) } } diff --git a/pkg/template/text/helper.go b/pkg/template/text/helper.go index a880db35b..f25271774 100644 --- a/pkg/template/text/helper.go +++ b/pkg/template/text/helper.go @@ -18,10 +18,12 @@ import ( // standard libraries. "io" "sync" + "time" // third-party libraries. "github.com/ohler55/ojg/oj" + // first-party libraries. "github.com/vanus-labs/vanus/lib/bytes" ) @@ -43,6 +45,8 @@ func write(w io.Writer, v any) error { return writeJSON(w, v) case string: return ignoreCount(w.Write(bytes.UnsafeFromString(val))) + case time.Time: + return ignoreCount(w.Write(bytes.UnsafeFromString(val.Format(time.RFC3339)))) default: return writeJSON(w, v) }