Skip to content

Commit

Permalink
fix: template does not follow RFC3339 time format (#639)
Browse files Browse the repository at this point in the history
Signed-off-by: James Yean <[email protected]>
  • Loading branch information
ifplusor authored Oct 17, 2023
1 parent 3c31d91 commit a3dd7d0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
13 changes: 10 additions & 3 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 All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion pkg/template/json/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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))
})
})
}
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"}`)
})
}
}
4 changes: 4 additions & 0 deletions pkg/template/text/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand Down

0 comments on commit a3dd7d0

Please sign in to comment.