From 6f968a125eb23684387b721a8b2b0df2ac31ec22 Mon Sep 17 00:00:00 2001 From: John Guo Date: Sun, 17 Jan 2021 21:39:17 +0800 Subject: [PATCH] improve time converting for package gconv --- os/gtime/gtime_time.go | 19 +++++++++++-------- os/gtime/gtime_z_unit_time_test.go | 2 +- util/gconv/gconv_struct.go | 1 - util/gconv/gconv_z_unit_time_test.go | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/os/gtime/gtime_time.go b/os/gtime/gtime_time.go index 1137a6799..997cf43ad 100644 --- a/os/gtime/gtime_time.go +++ b/os/gtime/gtime_time.go @@ -8,6 +8,7 @@ package gtime import ( "bytes" + "github.com/gogf/gf/errors/gerror" "strconv" "time" ) @@ -444,11 +445,13 @@ func (t *Time) UnmarshalJSON(b []byte) error { return nil } -//// UnmarshalValue is an interface implement which sets any type of value for Time. -//func (t *Time) UnmarshalValue(value interface{}) error { -// vTime := New(value) -// if vTime != nil { -// *t = *vTime -// } -// return gerror.Newf(`invalid time value: %v`, value) -//} +// UnmarshalText implements the encoding.TextUnmarshaler interface. +// Note that it overwrites the same implementer of `time.Time`. +func (t *Time) UnmarshalText(data []byte) error { + vTime := New(data) + if vTime != nil { + *t = *vTime + return nil + } + return gerror.Newf(`invalid time value: %s`, data) +} diff --git a/os/gtime/gtime_z_unit_time_test.go b/os/gtime/gtime_z_unit_time_test.go index 5eb2809bd..2f3ca95ec 100644 --- a/os/gtime/gtime_z_unit_time_test.go +++ b/os/gtime/gtime_z_unit_time_test.go @@ -1,4 +1,4 @@ -// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, diff --git a/util/gconv/gconv_struct.go b/util/gconv/gconv_struct.go index ecf6b7f50..130d87477 100644 --- a/util/gconv/gconv_struct.go +++ b/util/gconv/gconv_struct.go @@ -131,7 +131,6 @@ func doStruct(params interface{}, pointer interface{}, mapping ...map[string]str e := reflect.New(pointerElemReflectValue.Type().Elem()).Elem() pointerElemReflectValue.Set(e.Addr()) } - fmt.Println(pointerElemReflectValue.Type()) //if v, ok := pointerElemReflectValue.Interface().(apiUnmarshalValue); ok { // return v.UnmarshalValue(params) //} diff --git a/util/gconv/gconv_z_unit_time_test.go b/util/gconv/gconv_z_unit_time_test.go index 608ad6b67..1d41b04cd 100644 --- a/util/gconv/gconv_z_unit_time_test.go +++ b/util/gconv/gconv_z_unit_time_test.go @@ -7,6 +7,7 @@ package gconv_test import ( + "github.com/gogf/gf/frame/g" "testing" "time" @@ -42,3 +43,21 @@ func Test_Time(t *testing.T) { t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time) }) } + +func Test_Time_Slice_Attribute(t *testing.T) { + type SelectReq struct { + Arr []*gtime.Time + One *gtime.Time + } + gtest.C(t, func(t *gtest.T) { + var s *SelectReq + err := gconv.Struct(g.Map{ + "arr": g.Slice{"2021-01-12 12:34:56", "2021-01-12 12:34:57"}, + "one": "2021-01-12 12:34:58", + }, &s) + t.Assert(err, nil) + t.Assert(s.One, "2021-01-12 12:34:58") + t.Assert(s.Arr[0], "2021-01-12 12:34:56") + t.Assert(s.Arr[1], "2021-01-12 12:34:57") + }) +}