From 1568c3be0ec72914b556a9b0d002257043031b10 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Thu, 11 Aug 2022 22:39:54 +0800 Subject: [PATCH] fix: time repr wrapper (#2255) --- core/stores/sqlx/utils.go | 9 +++++++++ core/stores/sqlx/utils_test.go | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/core/stores/sqlx/utils.go b/core/stores/sqlx/utils.go index d10ae88b8..58428ecc5 100644 --- a/core/stores/sqlx/utils.go +++ b/core/stores/sqlx/utils.go @@ -6,6 +6,7 @@ import ( "fmt" "strconv" "strings" + "time" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/mapping" @@ -152,6 +153,14 @@ func writeValue(buf *strings.Builder, arg interface{}) { buf.WriteByte('\'') buf.WriteString(escape(v)) buf.WriteByte('\'') + case time.Time: + buf.WriteByte('\'') + buf.WriteString(v.String()) + buf.WriteByte('\'') + case *time.Time: + buf.WriteByte('\'') + buf.WriteString(v.String()) + buf.WriteByte('\'') default: buf.WriteString(mapping.Repr(v)) } diff --git a/core/stores/sqlx/utils_test.go b/core/stores/sqlx/utils_test.go index c3761c5e4..f09e5ae4f 100644 --- a/core/stores/sqlx/utils_test.go +++ b/core/stores/sqlx/utils_test.go @@ -3,6 +3,7 @@ package sqlx import ( "strings" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -138,3 +139,14 @@ func TestFormat(t *testing.T) { }) } } + +func TestWriteValue(t *testing.T) { + var buf strings.Builder + tm := time.Now() + writeValue(&buf, &tm) + assert.Equal(t, "'"+tm.String()+"'", buf.String()) + + buf.Reset() + writeValue(&buf, tm) + assert.Equal(t, "'"+tm.String()+"'", buf.String()) +}