Repository navigation
feat: support generic structs and nested collections, fix imports and lock detection - #8
Merged
Merged
Conversation
…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 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.
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
Behaviour changes
Package.GenerateAllgenerates several types in one run so they see each other's CopyFrom (the CLI uses it).Next *Node) is copied recursively from the first run: cyclic data needscopy:"shallow"(README says so).Testing
go vet,golangci-lint runandgo test -race ./...pass locally.