Skip to content

feat: copy through Clone: interfaces that declare it, and copy:"clone" - #9

Merged
darxnet merged 1 commit into
mainfrom
feat/clone
Nov 13, 2026
Merged

darxnet merged 1 commit into
mainfrom
feat/clone

Conversation

@darxnet

@darxnet darxnet commented Nov 13, 2026

Copy link
Copy Markdown
Owner

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 a Clone() on the value itself, and gocopy now uses it.

  • An interface that declares Clone() returning itself (type Shape interface { ...; Clone() Shape }) is copied as dst.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 has Clone() returning its own type, such as *tls.Config, http.Header or 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.
  • Fields copied through Clone no longer keep dst's old value around the bulk assignment.

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) and TestGenerateCloneMissing.

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>
@darxnet
darxnet enabled auto-merge (squash) November 13, 2026 10:46
@darxnet
darxnet merged commit 3e390f0 into main Nov 13, 2026
8 checks passed
@darxnet
darxnet deleted the feat/clone branch November 13, 2026 10:47
@codecov

codecov Bot commented Nov 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant