Merge remote-tracking branch 'upstream/master' into refactor-keys

This commit is contained in:
flc1125 2025-05-21 19:35:36 +08:00
commit 9335fa5cb0
4 changed files with 28 additions and 22 deletions

View File

@ -397,6 +397,11 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
timeFormat = time.RFC3339 timeFormat = time.RFC3339
} }
if val == "" {
value.Set(reflect.ValueOf(time.Time{}))
return nil
}
switch tf := strings.ToLower(timeFormat); tf { switch tf := strings.ToLower(timeFormat); tf {
case "unix", "unixmilli", "unixmicro", "unixnano": case "unix", "unixmilli", "unixmicro", "unixnano":
tv, err := strconv.ParseInt(val, 10, 64) tv, err := strconv.ParseInt(val, 10, 64)
@ -420,11 +425,6 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
return nil return nil
} }
if val == "" {
value.Set(reflect.ValueOf(time.Time{}))
return nil
}
l := time.Local l := time.Local
if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC { if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC {
l = time.UTC l = time.UTC

View File

@ -186,6 +186,8 @@ func TestMappingTime(t *testing.T) {
Time time.Time Time time.Time
LocalTime time.Time `time_format:"2006-01-02"` LocalTime time.Time `time_format:"2006-01-02"`
ZeroValue time.Time ZeroValue time.Time
ZeroUnixTime time.Time `time_format:"unix"`
ZeroUnixNanoTime time.Time `time_format:"unixnano"`
CSTTime time.Time `time_format:"2006-01-02" time_location:"Asia/Shanghai"` CSTTime time.Time `time_format:"2006-01-02" time_location:"Asia/Shanghai"`
UTCTime time.Time `time_format:"2006-01-02" time_utc:"1"` UTCTime time.Time `time_format:"2006-01-02" time_utc:"1"`
} }
@ -198,6 +200,8 @@ func TestMappingTime(t *testing.T) {
"Time": {"2019-01-20T16:02:58Z"}, "Time": {"2019-01-20T16:02:58Z"},
"LocalTime": {"2019-01-20"}, "LocalTime": {"2019-01-20"},
"ZeroValue": {}, "ZeroValue": {},
"ZeroUnixTime": {},
"ZeroUnixNanoTime": {},
"CSTTime": {"2019-01-20"}, "CSTTime": {"2019-01-20"},
"UTCTime": {"2019-01-20"}, "UTCTime": {"2019-01-20"},
}) })
@ -207,6 +211,8 @@ func TestMappingTime(t *testing.T) {
assert.Equal(t, "2019-01-20 00:00:00 +0100 CET", s.LocalTime.String()) assert.Equal(t, "2019-01-20 00:00:00 +0100 CET", s.LocalTime.String())
assert.Equal(t, "2019-01-19 23:00:00 +0000 UTC", s.LocalTime.UTC().String()) assert.Equal(t, "2019-01-19 23:00:00 +0000 UTC", s.LocalTime.UTC().String())
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", s.ZeroValue.String()) assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", s.ZeroValue.String())
assert.Equal(t, "1970-01-01 00:00:00 +0000 UTC", s.ZeroUnixTime.UTC().String())
assert.Equal(t, "1970-01-01 00:00:00 +0000 UTC", s.ZeroUnixNanoTime.UTC().String())
assert.Equal(t, "2019-01-20 00:00:00 +0800 CST", s.CSTTime.String()) assert.Equal(t, "2019-01-20 00:00:00 +0800 CST", s.CSTTime.String())
assert.Equal(t, "2019-01-19 16:00:00 +0000 UTC", s.CSTTime.UTC().String()) assert.Equal(t, "2019-01-19 16:00:00 +0000 UTC", s.CSTTime.UTC().String())
assert.Equal(t, "2019-01-20 00:00:00 +0000 UTC", s.UTCTime.String()) assert.Equal(t, "2019-01-20 00:00:00 +0000 UTC", s.UTCTime.String())

View File

@ -923,10 +923,10 @@ func TestContextGetCookie(t *testing.T) {
} }
func TestContextBodyAllowedForStatus(t *testing.T) { func TestContextBodyAllowedForStatus(t *testing.T) {
assert.False(t, false, bodyAllowedForStatus(http.StatusProcessing)) assert.False(t, bodyAllowedForStatus(http.StatusProcessing))
assert.False(t, false, bodyAllowedForStatus(http.StatusNoContent)) assert.False(t, bodyAllowedForStatus(http.StatusNoContent))
assert.False(t, false, bodyAllowedForStatus(http.StatusNotModified)) assert.False(t, bodyAllowedForStatus(http.StatusNotModified))
assert.True(t, true, bodyAllowedForStatus(http.StatusInternalServerError)) assert.True(t, bodyAllowedForStatus(http.StatusInternalServerError))
} }
type TestRender struct{} type TestRender struct{}
@ -1180,7 +1180,7 @@ func TestContextRenderNoContentXML(t *testing.T) {
assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type")) assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))
} }
// TestContextString tests that the response is returned // TestContextRenderString tests that the response is returned
// with Content-Type set to text/plain // with Content-Type set to text/plain
func TestContextRenderString(t *testing.T) { func TestContextRenderString(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -1205,7 +1205,7 @@ func TestContextRenderNoContentString(t *testing.T) {
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type")) assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
} }
// TestContextString tests that the response is returned // TestContextRenderHTMLString tests that the response is returned
// with Content-Type set to text/html // with Content-Type set to text/html
func TestContextRenderHTMLString(t *testing.T) { func TestContextRenderHTMLString(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()

View File

@ -203,7 +203,7 @@ func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
assert.Equal(t, "ACB", signature) assert.Equal(t, "ACB", signature)
} }
// TestFailHandlersChain - ensure that Fail interrupt used middleware in fifo order as // TestMiddlewareFailHandlersChain - ensure that Fail interrupt used middleware in fifo order as
// as well as Abort // as well as Abort
func TestMiddlewareFailHandlersChain(t *testing.T) { func TestMiddlewareFailHandlersChain(t *testing.T) {
// SETUP // SETUP