Enable deep copying of time.TIme values
This commit is contained in:
parent
21c7cb2341
commit
1200b6c209
2 changed files with 39 additions and 0 deletions
|
@ -15,6 +15,8 @@
|
||||||
package deep
|
package deep
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -57,6 +59,11 @@ func copy(prime, clone reflect.Value) {
|
||||||
clone.Set(alike)
|
clone.Set(alike)
|
||||||
|
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
|
t, ok := prime.Interface().(time.Time)
|
||||||
|
if ok {
|
||||||
|
clone.Set(reflect.ValueOf(t))
|
||||||
|
return
|
||||||
|
}
|
||||||
for i := 0; i < prime.NumField(); i++ {
|
for i := 0; i < prime.NumField(); i++ {
|
||||||
if prime.Type().Field(i).PkgPath != "" {
|
if prime.Type().Field(i).PkgPath != "" {
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package deep
|
package deep
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
|
@ -62,6 +64,12 @@ func TestSimple(t *testing.T) {
|
||||||
So(item, ShouldResemble, done)
|
So(item, ShouldResemble, done)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("Can copy time.Time", t, func() {
|
||||||
|
item := time.Now()
|
||||||
|
done := Copy(item)
|
||||||
|
So(item, ShouldResemble, done)
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSlices(t *testing.T) {
|
func TestSlices(t *testing.T) {
|
||||||
|
@ -105,12 +113,23 @@ func TestSlices(t *testing.T) {
|
||||||
So(item, ShouldResemble, done)
|
So(item, ShouldResemble, done)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("Can copy slice of time.Times", t, func() {
|
||||||
|
item := []time.Time{
|
||||||
|
time.Now(),
|
||||||
|
time.Now(),
|
||||||
|
time.Now(),
|
||||||
|
}
|
||||||
|
done := Copy(item)
|
||||||
|
So(item, ShouldResemble, done)
|
||||||
|
})
|
||||||
|
|
||||||
Convey("Can copy slice of interfaces", t, func() {
|
Convey("Can copy slice of interfaces", t, func() {
|
||||||
item := []interface{}{
|
item := []interface{}{
|
||||||
nil,
|
nil,
|
||||||
int64(1),
|
int64(1),
|
||||||
float64(2),
|
float64(2),
|
||||||
"str",
|
"str",
|
||||||
|
time.Now(),
|
||||||
}
|
}
|
||||||
done := Copy(item)
|
done := Copy(item)
|
||||||
So(item, ShouldResemble, done)
|
So(item, ShouldResemble, done)
|
||||||
|
@ -159,12 +178,23 @@ func TestObjects(t *testing.T) {
|
||||||
So(item, ShouldResemble, done)
|
So(item, ShouldResemble, done)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("Can copy map of time.Times", t, func() {
|
||||||
|
item := map[string]time.Time{
|
||||||
|
"a": time.Now(),
|
||||||
|
"b": time.Now(),
|
||||||
|
"c": time.Now(),
|
||||||
|
}
|
||||||
|
done := Copy(item)
|
||||||
|
So(item, ShouldResemble, done)
|
||||||
|
})
|
||||||
|
|
||||||
Convey("Can copy map of interfaces", t, func() {
|
Convey("Can copy map of interfaces", t, func() {
|
||||||
item := map[interface{}]interface{}{
|
item := map[interface{}]interface{}{
|
||||||
1: nil,
|
1: nil,
|
||||||
"b": int64(1),
|
"b": int64(1),
|
||||||
true: float64(2),
|
true: float64(2),
|
||||||
"d": "str",
|
"d": "str",
|
||||||
|
"e": time.Now(),
|
||||||
}
|
}
|
||||||
done := Copy(item)
|
done := Copy(item)
|
||||||
So(item, ShouldResemble, done)
|
So(item, ShouldResemble, done)
|
||||||
|
@ -184,6 +214,7 @@ func TestStructs(t *testing.T) {
|
||||||
"a": 1,
|
"a": 1,
|
||||||
"b": "str",
|
"b": "str",
|
||||||
"c": true,
|
"c": true,
|
||||||
|
"d": time.Now(),
|
||||||
},
|
},
|
||||||
privateNumber: 1,
|
privateNumber: 1,
|
||||||
privateString: "str",
|
privateString: "str",
|
||||||
|
@ -194,6 +225,7 @@ func TestStructs(t *testing.T) {
|
||||||
"a": 1,
|
"a": 1,
|
||||||
"b": "str",
|
"b": "str",
|
||||||
"c": true,
|
"c": true,
|
||||||
|
"d": time.Now(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue