Skip to content

Private unions#1159

Merged
rrousselGit merged 1 commit intomasterfrom
private-unions
Feb 24, 2025
Merged

Private unions#1159
rrousselGit merged 1 commit intomasterfrom
private-unions

Conversation

@rrousselGit
Copy link
Copy Markdown
Owner

@rrousselGit rrousselGit commented Feb 24, 2025

Summary by CodeRabbit

  • New Features

    • Added support for private constructors in union types, enabling enhanced encapsulation and more flexible design patterns.
  • Refactor

    • Streamlined constructor management and runtime checks to improve safety and reliability.
  • Tests

    • Expanded test cases with new sealed class examples and removed outdated constructs for improved consistency.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Feb 24, 2025

Walkthrough

This pull request updates the Freezed package by adding support for private constructors in union types. It introduces extension methods (isManualCtor and manualConstructor) to streamline constructor validation and improves null safety in code generation by modifying the use of superCall. Additionally, a new sealed class with private union factories is added to integration tests, and obsolete classes are removed with corresponding renames. These changes enhance encapsulation, reduce error-prone validations, and simplify both the API and internal logic.

Changes

File(s) Change Summary
packages/freezed/CHANGELOG.md Updated changelog to document the new support for private constructors in union types with a code example for Result<T>.
packages/freezed/lib/src/models.dart Added extension methods isManualCtor and manualConstructor to encapsulate logic for identifying manual constructors and streamline validation.
packages/freezed/lib/src/templates/con.../concrete_template.dart Modified _concreteConstructor to remove force-unwrapping of superCall and added proper null checks to prevent runtime null dereference errors.
packages/freezed/test/integration/multiple_constructors.dart Introduced new sealed class PrivateUnion featuring two factory constructors with private naming, expanding available union types for testing.
packages/freezed/test/source_gen_src.dart Removed obsolete Mixed classes and renamed AstractClass to AbstractClass (and its implementation), updating the related factory constructor accordingly.

Sequence Diagram(s)

sequenceDiagram
    participant Dev as Developer
    participant Gen as Freezed Generator
    participant Mod as Models Extension
    participant Temp as Template Engine

    Dev->>Gen: Define sealed class with private union constructors
    Gen->>Mod: Identify manual constructors using isManualCtor
    Mod-->>Gen: Return manual constructor status
    Gen->>Temp: Generate concrete constructor with safe superCall handling
    Temp-->>Gen: Return generated code
    Gen-->>Dev: Output updated union features
Loading

Possibly related PRs

  • External factory #1158: Enhancements to constructor behavior and factory constructor improvements directly relate to the new support for private constructors in union types.

Poem

I hop through lines of shining code,
Revealing secrets that were once in ode,
With private paths now safe and sound,
My little constructors joyfully abound!
Hoppy coding days, where bugs are down! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 92b563f and 5008868.

📒 Files selected for processing (5)
  • packages/freezed/CHANGELOG.md (1 hunks)
  • packages/freezed/lib/src/models.dart (5 hunks)
  • packages/freezed/lib/src/templates/concrete_template.dart (1 hunks)
  • packages/freezed/test/integration/multiple_constructors.dart (1 hunks)
  • packages/freezed/test/source_gen_src.dart (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: freezed (packages/freezed, master, get)
🔇 Additional comments (7)
packages/freezed/test/integration/multiple_constructors.dart (1)

5-11: LGTM! Well-structured test case for private unions.

The new PrivateUnion class effectively demonstrates the private unions feature with two private constructors. The implementation is clean and follows best practices.

packages/freezed/CHANGELOG.md (1)

164-175: LGTM! Clear and well-documented changelog entry.

The changelog entry effectively communicates the new private unions feature with a clear example. The documentation helps users understand how to use private constructors in unions.

packages/freezed/lib/src/templates/concrete_template.dart (1)

120-127: LGTM! Improved null safety handling.

The changes enhance code safety by:

  1. Removing the force-unwrap operator
  2. Adding proper null checks before accessing superCall properties
packages/freezed/test/source_gen_src.dart (1)

137-143: LGTM! Fixed class name typo.

Corrected the spelling from AstractClass to AbstractClass and updated all related references.

packages/freezed/lib/src/models.dart (3)

1139-1141: LGTM! Clean implementation of manual constructor detection.

The extension methods provide a clear and maintainable way to identify manual constructors, following the single responsibility principle.

Also applies to: 1154-1156


197-198: LGTM! Robust validation for manual constructors.

The validation logic ensures consistency between manual and factory constructors, particularly for positional parameters. The error message is clear and provides actionable guidance.

Also applies to: 206-228


518-522: LGTM! Proper handling of manual constructors in field validation and super calls.

The implementation correctly handles field validation and super call generation for classes with manual constructors, ensuring proper inheritance and parameter handling.

Also applies to: 560-572


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@rrousselGit rrousselGit merged commit 89e2658 into master Feb 24, 2025
4 checks passed
@rrousselGit rrousselGit deleted the private-unions branch February 24, 2025 12:33
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