Skip to content

Add collection manipulation methods for union maps and union lists - #196

Merged
EricWittmann merged 1 commit into
mainfrom
feature/union-collection-methods
Jan 13, 2026
Merged

Add collection manipulation methods for union maps and union lists#196
EricWittmann merged 1 commit into
mainfrom
feature/union-collection-methods

Conversation

@EricWittmann

Copy link
Copy Markdown
Contributor

Summary

Union maps and union lists now generate the same collection manipulation methods as entity maps and entity lists. This provides a consistent API for working with all types of collections in the generated models.

Problem

Previously, union maps and union lists only generated:

  • Factory create methods for each union type
  • A getter method
  • A setter method

Entity maps and entity lists generated:

  • Factory create methods
  • add, clear, insert, remove methods (named after property)
  • A getter method
  • No setter (users manipulate via collection methods)

This inconsistency made union collections harder to use and less intuitive.

Solution

Updated the code generator to treat union maps/lists like entity maps/lists:

Generated Methods (example for property "schemas" with types StandardSchema|MultiFormatSchema):

  • addSchema(String name, MultiFormatSchemaStandardSchemaUnion value) - add to map
  • removeSchema(String name) - remove from map
  • clearSchemas() - clear all
  • getSchemas() - get the map
  • insertSchema(...) - insert at position (for lists)
  • createStandardSchema() - factory for StandardSchema
  • createMultiFormatSchema() - factory for MultiFormatSchema
  • No setter - manipulate via add/clear/remove/insert methods

Methods are named after the property name (singularized), not the union or entity type names.

Changes

  1. AbstractCreateMethodsStage.java

    • Added createAddMethod, createClearMethod, createRemoveMethod, createInsertMethod calls for union maps/lists
    • Removed setter generation for union collections
    • Extended method creation logic to handle union types
  2. CreateImplMethodsStage.java

    • Extended collection method body generation to handle union types
    • Added parent property tracking for entities within union collections
  3. AbstractJavaStage.java

    • Extended JavaType class to support union types in toJavaTypeString() and addImportsTo()
  4. CreateReadersStage.java

    • Updated handleUnionListProperty and handleUnionMapProperty to use add methods instead of setters
    • Singularized property names when generating add method calls
  5. UnionMapTest.java

    • Added comprehensive testCollectionMethods() test validating all new methods work correctly

Testing

All 10 integration tests pass:

  • 9 tests in union-map-test (up from 8) ✅
  • New test validates: add, insert, remove, clear methods for union maps, union lists, and union lists of primitives
  • Verifies serialization/deserialization works correctly after using collection methods
Build Summary:
  Passed: 10, Failed: 0, Errors: 0, Skipped: 0

Union maps and union lists now generate the same collection manipulation methods as entity maps and entity lists:
- add, clear, insert, remove methods named after property (singularized)
- Factory create methods for each entity type in the union
- Removed setters for union collections to match entity collection behavior

Changes:
- AbstractCreateMethodsStage: Added createAddMethod, createClearMethod, createRemoveMethod, createInsertMethod for union types
- CreateImplMethodsStage: Extended collection method implementations to handle union types with parent property tracking
- AbstractJavaStage: Extended JavaType class to support union types in toJavaTypeString() and addImportsTo()
- CreateReadersStage: Updated reader to use add methods instead of setters for union collections, with singularized property names
- UnionMapTest: Added comprehensive test validating all collection methods work correctly

All 10 integration tests pass.
@EricWittmann
EricWittmann merged commit 427c5dc into main Jan 13, 2026
1 check passed
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