Repository navigation
feat: copy through Clone: interfaces that declare it, and copy:"clone" - #9
Merged
Merged
Conversation
An interface field is shared with src, as a CopyFrom(*T) cannot be called through it. An interface that declares Clone() returning itself now copies the value inside, in slices, maps and arrays too. The copy:"clone" tag copies any field whose type has Clone() returning its own type, such as *tls.Config or http.Header. A nil src gives nil. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
An interface field was always shared with src: a
CopyFrom(*T)is tied to one concrete type and cannot be called through an interface. The Prototype pattern's answer is aClone()on the value itself, and gocopy now uses it.Clone()returning itself (type Shape interface { ...; Clone() Shape }) is copied asdst.F = src.F.Clone(), whatever the value inside, in slices, maps and arrays too. A nil src gives nil. Other interfaces are assigned as before; a type parameter is still assigned.copy:"clone"copies any field whose type hasClone()returning its own type, such as*tls.Config,http.Headeror a value type, with a nil check where the type can be nil (a type parameter's Clone is called whatever the value). A type without such a Clone is an error.Behaviour change on regeneration: an interface field whose interface declares
Clone()returning itself was shared and is now cloned.Tests:
TestGenerateClone(bulk, field-wise and generic types, run with go vet and go test in a throwaway module) andTestGenerateCloneMissing.