Repository: vanus-labs/vanus
Pull request #639 (2023-10-17 07:30:14)

fix: template does not follow RFC3339 time format
<!--

Thank you for contributing to Vanus!

PR Title Format: 

feat/fix/refactor/docs/...: what's changed

Note: see https://github.com/vanus-labs/vanus/blob/main/CONTRIBUTING.md to know details about title format

-->

### What problem does this PR solve?

<!--

Please create an issue first to describe the problem

There MUST be one line starting with "Issue Number:  ".

-->

template does not follow the RFC3339 time format.

### Problem Summary

### What is changed and how does it work?

### Check List

<!-- At least one of them must be included. -->

#### Tests

- [x] Unit test
- [ ] Integration test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No code


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs) Report
> Merging [#639](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs) (f3da796) into [main](https://app.codecov.io/gh/vanus-labs/vanus/commit/ee76217182ba60efbcbb62fd374b8e75127d5133?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs) (ee76217) will **decrease** coverage by `5.84%`.
> Report is 101 commits behind head on main.
> The diff coverage is `3.63%`.

[![Impacted file tree graph](https://app.codecov.io/gh/vanus-labs/vanus/pull/639/graphs/tree.svg?width=650&height=150&src=pr&token=RSXSIMEY4V&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs)](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs)

```diff
@@            Coverage Diff             @@
##             main     #639      +/-   ##
==========================================
- Coverage   57.07%   51.24%   -5.84%     
==========================================
  Files         190      259      +69     
  Lines       15191    19514    +4323     
==========================================
+ Hits         8670     9999    +1329     
- Misses       5884     8698    +2814     
- Partials      637      817     +180     
```

| [Flag](https://app.codecov.io/gh/vanus-labs/vanus/pull/639/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs) | Coverage Δ | |
|---|---|---|
| [unittests](https://app.codecov.io/gh/vanus-labs/vanus/pull/639/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs) | `51.24% <3.63%> (?)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Files](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs) | Coverage Δ | |
|---|---|---|
| [api/errors/app.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-YXBpL2Vycm9ycy9hcHAuZ28=) | `30.90% <ø> (ø)` | |
| [lib/bytes/escape.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2J5dGVzL2VzY2FwZS5nbw==) | `54.46% <ø> (ø)` | |
| [lib/bytes/io.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2J5dGVzL2lvLmdv) | `0.00% <ø> (ø)` | |
| [lib/bytes/parse.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2J5dGVzL3BhcnNlLmdv) | `7.14% <ø> (ø)` | |
| [lib/bytes/unsafe.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2J5dGVzL3Vuc2FmZS5nbw==) | `100.00% <ø> (ø)` | |
| [lib/bytes/utf8.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2J5dGVzL3V0ZjguZ28=) | `11.11% <ø> (ø)` | |
| [lib/json/path/parse.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2pzb24vcGF0aC9wYXJzZS5nbw==) | `49.41% <ø> (ø)` | |
| [lib/json/path/path.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2pzb24vcGF0aC9wYXRoLmdv) | `0.00% <ø> (ø)` | |
| [lib/json/path/segment.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2pzb24vcGF0aC9zZWdtZW50Lmdv) | `0.00% <ø> (ø)` | |
| [lib/json/path/selector.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2pzb24vcGF0aC9zZWxlY3Rvci5nbw==) | `0.00% <ø> (ø)` | |
| ... and [30 more](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs) | |

... and [393 files with indirect coverage changes](https://app.codecov.io/gh/vanus-labs/vanus/pull/639/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs)

------

[Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs). Last update [3c31d91...f3da796](https://app.codecov.io/gh/vanus-labs/vanus/pull/639?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs).


Patch:
diff --git a/pkg/template/json/helper.go b/pkg/template/json/helper.go
index 26bce77..d9e021c 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/text/helper.go b/pkg/template/text/helper.go
index a880db3..f252717 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)
 	}


Test patch:
diff --git a/pkg/template/json/helper_test.go b/pkg/template/json/helper_test.go
index 58b2994..abf9b56 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 9e48312..5c14c68 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":"<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"}`)
+		})
 	}
 }