Skip to content

feat: support generic structs and nested collections, fix imports and lock detection - #8

Merged
darxnet merged 10 commits into
mainfrom
fix/backfill-2026-11
Nov 12, 2026
Merged

darxnet merged 10 commits into
mainfrom
fix/backfill-2026-11

Conversation

@darxnet

@darxnet darxnet commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Fixes found in a one-off review of competitor issues over their whole history; each bug is reproduced by a regression test that failed before the fix.

Changes

  • fix: import every package a field's type names, alias same-named packages
  • fix: call a nested CopyFrom only when it takes *T
  • fix: detect locks the way go vet copylocks does, skip blank fields
  • fix: generate methods of generic structs with their type parameters
  • fix: make one gocopy run see the CopyFrom it generates
  • fix: copy nested collections element by element
  • fix: keep an empty src slice non-nil in the copy
  • chore: satisfy golangci-lint in the new code and tests
  • test: cover the new branches of the generator and the generated code
  • docs: reflow the new README and package doc paragraphs

Behaviour changes

  • Generic structs are supported; new Package.GenerateAll generates several types in one run so they see each other's CopyFrom (the CLI uses it).
  • Nested slices, maps and arrays are copied element by element at any depth; an array of maps no longer needs a tag.
  • A self-referential pointer field (Next *Node) is copied recursively from the first run: cyclic data needs copy:"shallow" (README says so).
  • A non-nil empty src slice gives a non-nil copy.

Testing

go vet, golangci-lint run and go test -race ./... pass locally.

darxnet and others added 10 commits November 12, 2026 13:07
…ages

typeName kept a single import path per field and overwrote it for each
package it met, so map[a.K]b.V or *box.Box[a.K] lost all but the last
import and the generated file did not compile. Two packages with the same
name (two v1) were imported under one name.

The imports are now collected per file; a package whose name is taken by
another import, a declaration of the package, a predeclared identifier or
a local variable of the generated code is imported under name_N.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hasCopyFrom accepted any CopyFrom with one parameter and no results, so a
CopyFrom(*Inner) promoted into Outer by embedding, or a CopyFrom taking
another type, was called and the generated code did not compile. The
parameter must now be *T.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
isLockType knew only the sync and sync/atomic packages: a struct with a
custom noCopy was copied in bulk and vet reported the generated code, while
a sync.Locker interface field (safe to copy) demanded a tag and forced the
field-wise mode. A lock is now also any type whose pointer is a sync.Locker
and value is not, and interfaces are never locks. Blank fields, which the
generated code cannot refer to (a _ noCopy in field-wise mode, a _ []byte
in bulk mode), are skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A generic struct got func (dst *Box) CopyFrom(src *Box), which does not
compile. The receivers now list the type parameters (a blank one gets a
name, as src's type must refer to it); a field whose type is a type
parameter is assigned as it is, and that is documented.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The package was loaded once, so -type=Request,Inner copied Request.Inner
without the Inner.CopyFrom generated in the same run, and a type referring
to itself (Next *Node) shared the pointer on the first run and copied it
recursively on the second: go generate was not idempotent.

Package.GenerateAll generates several types as one run, treating each of
them (and the type itself) as having its CopyFrom; Generate is GenerateAll
for one type, and the command uses GenerateAll (writing nothing when a type
fails). The self-referential behaviour and the one-directive advice are
documented.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The generated CopyFrom fixed only the top level: the elements of a [][]byte
or of a []Inner (even with Inner.CopyFrom), the values of a
map[string][]byte, and the inner slices of [N][M][]byte or [N][][]T still
shared memory with src, against the promise of an independent copy.

The copy is now built recursively: slices, maps and arrays whose elements
need a copy of their own are copied element by element (a slice into dst's
own elements up to its capacity, a map value as a new copy), at any depth.
An array of maps is copied the same way instead of demanding a tag; a type
that contains itself through slices or maps (type Tree []Tree) is an error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
append(dst.F[:0], src.F...) gave nil for an empty non-nil src when dst's
slice was nil, so Copy turned [] into null in JSON and broke DeepEqual. A
non-nil src slice now always gives a non-nil one (make(T, 0) allocates
nothing), as a non-nil map already did; a nil src still empties dst's slice
and keeps its array for the next copy. Both rules are now documented.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@darxnet
darxnet merged commit a638ae7 into main Nov 12, 2026
9 checks passed
@darxnet
darxnet deleted the fix/backfill-2026-11 branch November 12, 2026 12:59
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