Skip to content

Fix resource.CopyObject and resource.CopyObjectInto zeroing time.Time#744

Merged
IfSentient merged 2 commits intomainfrom
fix-copy-time
Apr 15, 2025
Merged

Fix resource.CopyObject and resource.CopyObjectInto zeroing time.Time#744
IfSentient merged 2 commits intomainfrom
fix-copy-time

Conversation

@IfSentient
Copy link
Copy Markdown
Contributor

resource.CopyObject and resource.CopyObjectInto incorrectly zero time.Time values when doing their copy, as it tries to copy time.Time instances as structs using reflection. Since time.Time has no exported fields, this does not work. To fix this behavior, check if the struct to be copied is typeOf(time.Time), and, if so, directly copy it with dst.Set(src), rather than recursively calling CopyObjectInto to copy it as a user-defined struct.

This does bring up the issue that any other "special" types which have custom MarshalJSON/UnmarshalJSON implementations but no exported fields will also fail with the deep copy performed by resource.CopyObject/resource.CopyObjectInto. I'm not sure if this is a restriction we want to live with, or attempt to solve here.

Ultimately, a better solution would be to avoid using relfection for copying objects entirely, and rely on generated deep copy methods in objects (see #424).

@IfSentient IfSentient added type/bug Something isn't working area/general labels Apr 14, 2025
@IfSentient IfSentient requested a review from a team as a code owner April 14, 2025 16:14
@IfSentient IfSentient requested a review from onprem April 14, 2025 16:14
@IfSentient IfSentient merged commit a71966e into main Apr 15, 2025
10 checks passed
@IfSentient IfSentient deleted the fix-copy-time branch April 15, 2025 12:57
IfSentient added a commit that referenced this pull request Apr 16, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/general type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants