Implement Prototype pattern source generator with safe cloning semantics#99
Conversation
…ing strategies Co-authored-by: JerrettDavis <2610199+JerrettDavis@users.noreply.github.com>
Co-authored-by: JerrettDavis <2610199+JerrettDavis@users.noreply.github.com>
Co-authored-by: JerrettDavis <2610199+JerrettDavis@users.noreply.github.com>
Co-authored-by: JerrettDavis <2610199+JerrettDavis@users.noreply.github.com>
- Created docs/generators/prototype.md with complete generator documentation - Created docs/examples/prototype-demo.md with real-world game character example - Follows existing PatternKit documentation patterns and style - Includes quickstart, configuration, diagnostics, best practices - Demonstrates deep cloning, mutation chains, and prototype registry Co-authored-by: JerrettDavis <2610199+JerrettDavis@users.noreply.github.com>
Co-authored-by: JerrettDavis <2610199+JerrettDavis@users.noreply.github.com>
…and documentation Co-authored-by: JerrettDavis <2610199+JerrettDavis@users.noreply.github.com>
…on checking, and custom method validation Co-authored-by: JerrettDavis <2610199+JerrettDavis@users.noreply.github.com>
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
Test Results268 tests 268 ✅ 1m 20s ⏱️ Results for commit 46c1ebb. ♻️ This comment has been updated with latest results. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #99 +/- ##
==========================================
+ Coverage 85.20% 87.31% +2.10%
==========================================
Files 152 166 +14
Lines 14002 15754 +1752
Branches 1923 2199 +276
==========================================
+ Hits 11931 13755 +1824
- Misses 1576 1999 +423
+ Partials 495 0 -495
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
🔍 PR Validation ResultsVersion: `` ✅ Validation Steps
📊 ArtifactsDry-run artifacts have been uploaded and will be available for 7 days. This comment was automatically generated by the PR validation workflow. |
There was a problem hiding this comment.
Pull request overview
This PR introduces a new Prototype pattern source generator plus supporting attributes, tests, and documentation to provide deterministic, reflection-free clone methods with configurable cloning strategies.
Changes:
- Add
PrototypeGeneratorincremental source generator that analyzes[Prototype]-annotated types and emits clone methods using recordwithexpressions, copy constructors, or parameterless constructors plus member assignments. - Add Prototype-related attribute types and enums (
[Prototype],[PrototypeIgnore],[PrototypeInclude],[PrototypeStrategy],PrototypeMode,PrototypeCloneStrategy) in the abstractions package for consumer use. - Add tests and extensive documentation (generator guide and a game-character prototype demo) and register new diagnostics in the analyzer release metadata.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
src/PatternKit.Generators/PrototypeGenerator.cs |
Implements the core Prototype incremental generator: attribute parsing, member analysis, construction strategy selection, clone strategy resolution, diagnostic reporting, and clone method emission. |
src/PatternKit.Generators.Abstractions/Prototype/PrototypeAttribute.cs |
Defines the public Prototype attributes and enums that consumers apply to types and members to control cloning behavior. |
src/PatternKit.Generators/AnalyzerReleases.Unshipped.md |
Registers the new PKPRO00x diagnostic IDs for the Prototype generator and their severities/messages. |
test/PatternKit.Generators.Tests/PrototypeGeneratorTests.cs |
Adds initial tests for Prototype generation across classes, structs, records, inclusion/ignore attributes, custom method name, and the partial-type and mutable-reference diagnostics. |
docs/generators/toc.yml |
Adds the Prototype generator article to the generators documentation TOC. |
docs/generators/prototype.md |
Provides a comprehensive guide to the Prototype generator’s configuration, semantics, diagnostics, and usage patterns, including many examples. |
docs/examples/toc.yml |
Adds the Prototype demo entry to the examples documentation TOC. |
docs/examples/prototype-demo.md |
Documents a full game-character factory example using the runtime Prototype API, tying into the generator as an automation aid. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot apply changes based on the comments in this thread |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot apply changes based on the comments in this thread |
…ssibility, add static Clone() test, document enum sync Co-authored-by: JerrettDavis <2610199+JerrettDavis@users.noreply.github.com>
Code Coverage |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot apply changes based on the comments in this thread |
… diagnostic, add comprehensive tests Co-authored-by: JerrettDavis <2610199+JerrettDavis@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot apply changes based on the comments in this thread |
…add Custom/DeepWhenPossible tests Co-authored-by: JerrettDavis <2610199+JerrettDavis@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Prototype Pattern Generator - Fifth Review Fixes ✅
Changes in this commit:
Added missing diagnostic documentation:
Fixed CustomStrategyMissingDescriptor message:
Added comprehensive tests:
SucceedWithCustomStrategy: Validates successful Custom strategy with user-provided partial methodSucceedWithDeepWhenPossibleMode: Tests DeepWhenPossible mode behavior (auto-promotes to Clone when possible, falls back to by-reference otherwise)Test Results:
✅ All 29 tests passing (27 previous + 2 new)
Documentation Quality:
Original prompt
This section details on the original issue you should resolve
<issue_title>Generator: Implement Prototype Pattern</issue_title>
<issue_description>## Summary
Add a source generator that produces boilerplate-free, GoF-consistent Prototype pattern implementations, generating safe cloning APIs with configurable strategies for mutable references.
The generator lives in
PatternKit.Generatorsand emits self-contained C# with no runtime PatternKit dependency.Primary goals:
Clone()/Copy()methods that are correct and explicit.Motivation / Problem
Cloning is where bugs go to reproduce:
We want generator-emitted clones that are:
Supported Targets (must-have)
The generator must support:
partial classpartial structpartial record classpartial record structProposed User Experience
A) Default clone (safe-by-default)
Generated (representative shape):
Default behavior:
B) Per-member strategy
Generator emits
new List<string>(Tags)forClonestrategy when T is cloneable or has copy ctor patterns (rules below).C) Custom clone hook
Attributes / Surface Area
Namespace:
PatternKit.Generators.Prototype[Prototype]on the prototype typePrototypeMode Mode(default:ShallowWithWarnings)string CloneMethodName(default:Clone)[PrototypeIgnore]exclude a member[PrototypeInclude]include only marked members (if IncludeExplicit mode)[PrototypeStrategy]per-member strategyEnums:
PrototypeMode:ShallowWithWarnings,Shallow,DeepWhenPossiblePrototypeCloneStrategy:ByReference,ShallowCopy,Clone,DeepCopy,CustomSemantics (must-have)
Member selection
[Prototype(IncludeExplicit=true)](optional if you prefer to split; but consistent with other generators).Clone construction
For records: prefer
with { ... }when feasible.Otherwise:
If none are possible, emit a diagnostic and do not generate broken code.
Strategies
Baseline rules:
value types: copy
string: copy
reference types:
ByReferencewith warning underShallowWithWarningsStrategy
Clone(v1):Supported when:
ICloneable(discouraged but common), ORClone()method returning same type, ORT(T other)Collections:
List<T>with Clone strategy:new List<T>(old)Trequires clone, deep behavior is v2 unless explicitly configured.Strategy
DeepCopy:Strategy
Custom:Clone<MemberName>(T value).Diagnostics (must-have)
Stable IDs, actionable:
PKPRO001Type marked[Prototype]must bepartial.PKPRO002Cannot construct clone target (no supported clone construction path).PKPRO003Unsafe reference capture: member cloned by reference (warning).PKPRO004Requested Clone strategy but no clone mechanism found.PKPRO005Custom strategy requires partial clone hook, but none found.PKPRO006Include/Ignore attribute misuse.Generated Code Layout
TypeName.Prototype.g.csDeterminism:
Testing Expectations
Records clone with
withwhen possible.Class clone via ctor+assignment works.
Diagnostics:
Strategy behavior:
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.