Fix: Generate AddTo methods for array properties with child builders - #115
Conversation
- Added ArrayCollectionMetadata class to represent array collections - Updated CollectionMethodDetector to detect and handle array types - Implemented array-specific AddTo method generation for both regular and child builder scenarios - Added ParentWithChildArrayEntity test entity - Added comprehensive integration tests for array AddTo methods - Updated CHANGELOG.md with fix details Co-authored-by: pmrogala <9459432+pmrogala@users.noreply.github.com>
Co-authored-by: pmrogala <9459432+pmrogala@users.noreply.github.com>
| {{ | ||
| var existingArray = {fieldName}.Value.Object; | ||
| array = new {elementTypeName}[existingArray.Length + newItems.Length]; | ||
| System.Array.Copy(existingArray, 0, array, 0, existingArray.Length); |
There was a problem hiding this comment.
I would prefer to use List of items and then in the build method we can use .ToArray().
THis copying mechanism sounds bad
There was a problem hiding this comment.
Implemented as requested in commit 5eab549. Arrays now use List internally and convert to array via .ToArray() in the Build method.
Note: This creates new array instances in With methods, breaking reference equality. See my comment on line 265 for details on the tradeoff.
| {{ | ||
| var existingArray = {fieldName}.Value.Object; | ||
| array = new {elementTypeName}[existingArray.Length + items.Length]; | ||
| System.Array.Copy(existingArray, 0, array, 0, existingArray.Length); |
There was a problem hiding this comment.
I would prefer to use List of items and then in the build method we can use .ToArray().
THis copying mechanism sounds bad
There was a problem hiding this comment.
I've refactored the implementation to use List internally with .ToArray() conversion in the Build method (commit 5eab549).
However, this approach has a semantic tradeoff: the With methods now create new array instances instead of preserving reference equality. This causes 8 existing tests to fail because they expect BeSameAs (reference equality).
Tradeoffs:
- List approach (current): Cleaner code, better performance for multiple AddTo calls, but breaks reference equality
- Array.Copy approach (original): Preserves reference equality, but more complex code
The 4 new array child builder tests all pass. Should I:
- Keep the List approach and update the failing tests?
- Revert to Array.Copy to preserve existing semantics?
- Hybrid: Use Array.Copy for With methods but List for AddTo methods?
Updated approach per code review feedback: - Arrays now use List<T> for internal storage - Conversion to array happens in Build() method via ToArray() - AddTo methods use List.Add/AddRange instead of Array.Copy - Simpler and more performant for multiple AddTo calls Note: This changes semantics - With methods now create new array instances rather than preserving reference equality. 8 tests fail due to this change. Co-authored-by: pmrogala <9459432+pmrogala@users.noreply.github.com>
pmrogala
left a comment
There was a problem hiding this comment.
Let's revert back to the Array backing field solution
| var methodName = CreateAddToMethodName(typedSymbol); | ||
| var fieldName = typedSymbol.UnderScoreName; | ||
|
|
||
| // For array types, use List<T> internally |
There was a problem hiding this comment.
Is it possible to reuse the same geneartion like for [concrete/interface]collectionmetada?
It looks super similar.
There was a problem hiding this comment.
Consolidated the array and concrete collection generation in commit 4e00e9a. Both types now use a unified pattern with conditional logic to handle the differences (Array.Copy vs collection.Add).
| }}"; | ||
| } | ||
|
|
||
| // For array types, use List<T> internally |
There was a problem hiding this comment.
Is it possible to reuse the same geneartion like for [concrete/interface]collectionmetada?
It looks super similar.
There was a problem hiding this comment.
Consolidated the array and concrete collection generation in commit 4e00e9a. Both types now use a unified pattern with conditional logic to handle the differences (Array.Copy vs collection.Add).
Reverted changes from commit 5eab549 to restore the original Array.Copy implementation from commit ac3a84b. This preserves reference equality semantics for With methods while still supporting AddTo methods for arrays. All 111 tests now pass (previously 8 were failing with List approach). Co-authored-by: pmrogala <9459432+pmrogala@users.noreply.github.com>
Unified the code generation for arrays and concrete collections to reduce duplication. Both types now share the same pattern with conditional logic to handle the differences (Array.Copy vs collection.Add). This addresses code review feedback about similar-looking generation code between ArrayCollectionMetadata and ConcreteCollectionMetadata. Co-authored-by: pmrogala <9459432+pmrogala@users.noreply.github.com>
Reverted to array backing field with Array.Copy approach in commit dd74d64. All 111 tests now pass (the List approach had 8 failing tests due to reference equality issues). |
Extracted the inline conditional for whitespace formatting into a clearer methodBody variable. This makes the code more maintainable and easier to understand. Co-authored-by: pmrogala <9459432+pmrogala@users.noreply.github.com>
…115) * Initial plan * Add AddTo method generation for arrays with child builders - Added ArrayCollectionMetadata class to represent array collections - Updated CollectionMethodDetector to detect and handle array types - Implemented array-specific AddTo method generation for both regular and child builder scenarios - Added ParentWithChildArrayEntity test entity - Added comprehensive integration tests for array AddTo methods - Updated CHANGELOG.md with fix details Co-authored-by: pmrogala <9459432+pmrogala@users.noreply.github.com> * Remove unused imports from ParentWithChildArrayEntity Co-authored-by: pmrogala <9459432+pmrogala@users.noreply.github.com> * Refactor array handling to use List internally with ToArray() conversion Updated approach per code review feedback: - Arrays now use List<T> for internal storage - Conversion to array happens in Build() method via ToArray() - AddTo methods use List.Add/AddRange instead of Array.Copy - Simpler and more performant for multiple AddTo calls Note: This changes semantics - With methods now create new array instances rather than preserving reference equality. 8 tests fail due to this change. Co-authored-by: pmrogala <9459432+pmrogala@users.noreply.github.com> * Revert to array backing field with Array.Copy approach Reverted changes from commit 5eab549 to restore the original Array.Copy implementation from commit ac3a84b. This preserves reference equality semantics for With methods while still supporting AddTo methods for arrays. All 111 tests now pass (previously 8 were failing with List approach). Co-authored-by: pmrogala <9459432+pmrogala@users.noreply.github.com> * Refactor: Consolidate array and concrete collection AddTo generation Unified the code generation for arrays and concrete collections to reduce duplication. Both types now share the same pattern with conditional logic to handle the differences (Array.Copy vs collection.Add). This addresses code review feedback about similar-looking generation code between ArrayCollectionMetadata and ConcreteCollectionMetadata. Co-authored-by: pmrogala <9459432+pmrogala@users.noreply.github.com> * Improve code readability in child builder AddTo generation Extracted the inline conditional for whitespace formatting into a clearer methodBody variable. This makes the code more maintainable and easier to understand. Co-authored-by: pmrogala <9459432+pmrogala@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: pmrogala <9459432+pmrogala@users.noreply.github.com> Co-authored-by: Cranter <progala@ecovadis.com>
Arrays were excluded from
AddTomethod generation whenuseChildBuilders: true, despite Lists and other collections receiving both traditional and Func-based overloads.Changes
ArrayCollectionMetadata- New metadata class for array types, following existing collection metadata patternCollectionMethodDetector- DetectIArrayTypeSymboland returnArrayCollectionMetadataPropertiesStringGenerator- Generate array-specific AddTo methods:AddTo(params T[] items)usingArray.Copyfor concatenationAddTo(params Func<TBuilder, TBuilder>[] configures)ParentWithChildArrayEntitywith 4 test cases covering both constructor and settable array propertiesImplementation Details
Arrays use
System.Array.Copyto concatenate existing arrays with new items, preserving reference equality semantics. The implementation consolidates array and concrete collection code generation patterns while maintaining the distinct behavior for each (Array.Copy vs collection.Add).This approach ensures:
WithmethodsAddTocalls properly accumulate itemsExample
Arrays now behave consistently with other collection types - multiple
AddTocalls accumulate items using array concatenation.Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.