Skip to content

Commit e269ae1

Browse files
authored
Fix resource.CopyObject for *time.Time elements. (#747)
Previous PR to fix `resource.CopyObject` and `resource.CopyObjectInto` for `time.Time` elements (#744) had a small bug with `*time.Time` types, and the addition to the test only tested `time.Time`. Added a `*time.Time` element to the test, and fixed the bug where it improperly checked if the pointer is of type `time.Time`, rather than the dereferenced pointer.
1 parent a71966e commit e269ae1

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

resource/object.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func copyReflectValueInto(dst reflect.Value, src reflect.Value) error {
240240
typ := src.Type().Elem()
241241
switch src.Type().Elem().Kind() {
242242
case reflect.Struct:
243-
if src.Type() == reflectTypeTime {
243+
if src.Elem().Type() == reflectTypeTime {
244244
dst.Set(src)
245245
return nil
246246
}

resource/object_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type ComplexTestObject struct {
1919
SlicePointer *[]int // Weird, but valid, types
2020
MapPointer *map[string]int // Weird, but valid, types
2121
Timestamp time.Time // Times won't be copied right if you try to copy them as a struct (they'll become 0)
22+
TimePointer *time.Time
2223
}
2324

2425
type ComplexTestObjectChild struct {
@@ -36,6 +37,7 @@ func TestCopyObjectInto(t *testing.T) {
3637
"b": 2,
3738
}
3839
var ncto *ComplexTestObject
40+
tm := time.Date(2020, time.January, 1, 2, 3, 4, 5, time.UTC)
3941
tests := []struct {
4042
name string
4143
in any
@@ -122,6 +124,7 @@ func TestCopyObjectInto(t *testing.T) {
122124
SlicePointer: &si,
123125
MapPointer: &mi,
124126
Timestamp: time.Now(),
127+
TimePointer: &tm,
125128
},
126129
out: &ComplexTestObject{},
127130
}}

0 commit comments

Comments
 (0)