mirror of
https://gitee.com/johng/gf
synced 2026-07-05 13:22:16 +08:00
improve package gtime/gconv for map converting
This commit is contained in:
@ -14,7 +14,7 @@ import (
|
||||
|
||||
// Time is a wrapper for time.Time for additional features.
|
||||
type Time struct {
|
||||
TimeWrapper
|
||||
wrapper
|
||||
}
|
||||
|
||||
// apiUnixNano is an interface definition commonly for custom time.Time wrapper.
|
||||
@ -47,21 +47,21 @@ func New(param ...interface{}) *Time {
|
||||
}
|
||||
}
|
||||
return &Time{
|
||||
TimeWrapper{time.Time{}},
|
||||
wrapper{time.Time{}},
|
||||
}
|
||||
}
|
||||
|
||||
// Now creates and returns a time object of now.
|
||||
func Now() *Time {
|
||||
return &Time{
|
||||
TimeWrapper{time.Now()},
|
||||
wrapper{time.Now()},
|
||||
}
|
||||
}
|
||||
|
||||
// NewFromTime creates and returns a Time object with given time.Time object.
|
||||
func NewFromTime(t time.Time) *Time {
|
||||
return &Time{
|
||||
TimeWrapper{t},
|
||||
wrapper{t},
|
||||
}
|
||||
}
|
||||
|
||||
@ -112,7 +112,7 @@ func NewFromTimeStamp(timestamp int64) *Time {
|
||||
sec = timestamp
|
||||
}
|
||||
return &Time{
|
||||
TimeWrapper{time.Unix(sec, nano)},
|
||||
wrapper{time.Unix(sec, nano)},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -10,14 +10,14 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// TimeWrapper is a wrapper for stdlib struct time.Time.
|
||||
// wrapper is a wrapper for stdlib struct time.Time.
|
||||
// It's used for overwriting some functions of time.Time, for example: String.
|
||||
type TimeWrapper struct {
|
||||
type wrapper struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
// String overwrites the String function of time.Time.
|
||||
func (t TimeWrapper) String() string {
|
||||
func (t wrapper) String() string {
|
||||
if t.IsZero() {
|
||||
return ""
|
||||
}
|
||||
|
||||
@ -75,7 +75,7 @@ func Test_String(t *testing.T) {
|
||||
|
||||
t2 := *t1
|
||||
t.Assert(t2.String(), "2006-01-02 15:04:05")
|
||||
t.Assert(fmt.Sprintf("%s", t2), "{2006-01-02 15:04:05}")
|
||||
t.Assert(fmt.Sprintf("{%s}", t2.String()), "{2006-01-02 15:04:05}")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -117,8 +117,14 @@ func doMapConvert(value interface{}, recursive bool, tags ...string) map[string]
|
||||
dataMap[k] = v
|
||||
}
|
||||
case map[string]interface{}:
|
||||
for k, v := range r {
|
||||
dataMap[k] = doMapConvertForMapOrStructValue(false, v, recursive, newTags...)
|
||||
if recursive {
|
||||
// A copy of current map.
|
||||
for k, v := range r {
|
||||
dataMap[k] = doMapConvertForMapOrStructValue(false, v, recursive, newTags...)
|
||||
}
|
||||
} else {
|
||||
// It returns the map directly without any changing.
|
||||
return r
|
||||
}
|
||||
case map[int]interface{}:
|
||||
for k, v := range r {
|
||||
|
||||
Reference in New Issue
Block a user