Fix resource.CopyObject and resource.CopyObjectInto zeroing time.Time#744
Merged
IfSentient merged 2 commits intomainfrom Apr 15, 2025
Merged
Fix resource.CopyObject and resource.CopyObjectInto zeroing time.Time#744IfSentient merged 2 commits intomainfrom
IfSentient merged 2 commits intomainfrom
Conversation
… values in the copy
marcoabreu
approved these changes
Apr 15, 2025
onprem
approved these changes
Apr 15, 2025
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
resource.CopyObjectandresource.CopyObjectIntoincorrectly zerotime.Timevalues when doing their copy, as it tries to copytime.Timeinstances as structs using reflection. Sincetime.Timehas no exported fields, this does not work. To fix this behavior, check if the struct to be copied istypeOf(time.Time), and, if so, directly copy it withdst.Set(src), rather than recursively callingCopyObjectIntoto copy it as a user-defined struct.This does bring up the issue that any other "special" types which have custom
MarshalJSON/UnmarshalJSONimplementations but no exported fields will also fail with the deep copy performed byresource.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).