improve package gtime/gconv for map converting

This commit is contained in:
John
2020-10-25 11:33:30 +08:00
parent 0caf4bfcec
commit 8e380c0d9d
4 changed files with 17 additions and 11 deletions

View File

@ -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)},
}
}

View File

@ -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 ""
}

View File

@ -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}")
})
}

View File

@ -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 {