Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion health/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c CheckStatus) String() string {
b := strings.Builder{}

for _, result := range c.Results {
b.WriteString(fmt.Sprintf("%s\n", result.String()))
b.WriteString(fmt.Sprintf("%s\n", result.String())) //nolint:revive
}
return b.String()
}
Expand Down
12 changes: 11 additions & 1 deletion resource/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@ func CopyObjectInto[T any](out T, in T) error {
}

var errCannotSetValue = errors.New("cannot set value")
var reflectTypeTime = reflect.TypeOf(time.Time{})

// nolint:gocognit,gocritic
// nolint:gocognit,gocritic,funlen
func copyReflectValueInto(dst reflect.Value, src reflect.Value) error {
// Check if we can set the value (Set panics if this is false)
if !dst.CanSet() {
Expand All @@ -239,6 +240,10 @@ func copyReflectValueInto(dst reflect.Value, src reflect.Value) error {
typ := src.Type().Elem()
switch src.Type().Elem().Kind() {
case reflect.Struct:
if src.Type() == reflectTypeTime {
dst.Set(src)
return nil
}
dstPtr := reflect.New(typ).Interface()
err := CopyObjectInto(dstPtr, src.Interface())
if err != nil {
Expand All @@ -259,6 +264,11 @@ func copyReflectValueInto(dst reflect.Value, src reflect.Value) error {
dst.Set(src)
}
case reflect.Struct:
// Special case for time.Time:
if src.Type() == reflectTypeTime {
dst.Set(src)
return nil
}
// Recursively copy the struct
dstStruct := reflect.New(dst.Type()).Interface()
err := CopyObjectInto(dstStruct, src.Interface())
Expand Down
3 changes: 3 additions & 0 deletions resource/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resource
import (
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -17,6 +18,7 @@ type ComplexTestObject struct {
IntPointer *int
SlicePointer *[]int // Weird, but valid, types
MapPointer *map[string]int // Weird, but valid, types
Timestamp time.Time // Times won't be copied right if you try to copy them as a struct (they'll become 0)
}

type ComplexTestObjectChild struct {
Expand Down Expand Up @@ -119,6 +121,7 @@ func TestCopyObjectInto(t *testing.T) {
IntPointer: &i,
SlicePointer: &si,
MapPointer: &mi,
Timestamp: time.Now(),
},
out: &ComplexTestObject{},
}}
Expand Down
Loading