Skip to content

Allow Enum values to be defined as a function#26

Merged
renatomassaro merged 1 commit intomainfrom
allow-enum-values-to-be-defined-as-a-function
Jun 24, 2025
Merged

Allow Enum values to be defined as a function#26
renatomassaro merged 1 commit intomainfrom
allow-enum-values-to-be-defined-as-a-function

Conversation

@renatomassaro
Copy link
Copy Markdown
Owner

@renatomassaro renatomassaro commented Jun 24, 2025

This might be useful for applications that wish to avoid transitive compile-time dependencies.

Summary by CodeRabbit

  • New Features

    • Added support for defining enum values using a function that returns a list, enabling dynamic value generation.
    • Introduced a new enum field that leverages this dynamic value capability.
  • Bug Fixes

    • None.
  • Tests

    • Expanded test coverage to verify correct handling, storage, and retrieval of enum fields defined with dynamic values.
    • Added tests to ensure the new enum field is correctly processed in schema and database operations.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jun 24, 2025

Walkthrough

The changes introduce support for specifying enum values as a zero-arity function in the database type logic, allowing deferred evaluation of possible values. A new database column and schema field (enum_fn) are added to test this feature, and related tests are updated and expanded to verify correct behavior with function-based enum values.

Changes

File(s) Change Summary
lib/feeb/db/type/enum.ex Extended overwrite_opts/3 to accept values as a zero-arity function, invoking it for deferred value evaluation. Added guard for list handling.
priv/test/migrations/test/241020150400_all_types.sql Added new column enum_fn TEXT to the all_types table.
test/support/db/schemas/all_types.ex Added :enum_fn field to schema, using a function to provide enum values.
test/db/type/enum_test.exs Added tests for function-based enum values and updated tests to include enum_fn.
test/db/schema_test.exs Updated expected columns to include :enum_fn in schema tests.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant EnumType

    Caller->>EnumType: overwrite_opts(opts, mod, identifier)
    alt values is a function
        EnumType->>EnumType: Call values_fn()
        EnumType->>EnumType: Use returned list as values
    else values is a list
        EnumType->>EnumType: Use values directly
    end
    EnumType-->>Caller: Return updated opts
Loading

Poem

A hop, a leap, a function call,
Now enums need not list at all!
Deferred with care, their values bloom,
In schema fields, there’s extra room.
Tests abound for every case,
Enum_fn joins the rabbit race!
🐇✨


📜 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 4b0bae6 and 7ae561d.

📒 Files selected for processing (5)
  • lib/feeb/db/type/enum.ex (1 hunks)
  • priv/test/migrations/test/241020150400_all_types.sql (1 hunks)
  • test/db/schema_test.exs (1 hunks)
  • test/db/type/enum_test.exs (2 hunks)
  • test/support/db/schemas/all_types.ex (1 hunks)
🔇 Additional comments (8)
lib/feeb/db/type/enum.ex (2)

12-17: LGTM! Clean implementation of function-based enum values.

The implementation correctly handles function-based enum values by:

  1. Adding clear documentation explaining the use case
  2. Implementing a recursive approach that calls the function and processes the result
  3. Maintaining backward compatibility with existing list-based values

24-24: Good addition of guard clause for type safety.

Adding the when is_list(values) guard clause ensures that the existing logic only processes lists, preventing runtime errors now that values can be either a list or a function.

priv/test/migrations/test/241020150400_all_types.sql (1)

34-34: Migration looks correct.

The new enum_fn column is properly defined as TEXT, consistent with other enum columns in the table.

test/db/schema_test.exs (1)

62-63: Test update is correct.

Adding :enum_fn to the expected list of non-virtual columns ensures the schema test continues to pass with the new field.

test/support/db/schemas/all_types.ex (1)

39-39: Schema definition demonstrates the new functionality well.

The enum_fn field correctly uses a zero-arity function to provide enum values, showcasing the new feature while maintaining proper nullable configuration for testing.

test/db/type/enum_test.exs (3)

10-10: Good integration of new field into existing tests.

The enum_fn field is properly integrated into the existing test case, ensuring it's tested for casting, dumping, and loading functionality.

Also applies to: 16-16, 23-23


26-27: SQL query correctly updated to include new column.

The raw SQL query is properly updated to verify that the new enum field is stored correctly as text in the database.


235-250: Comprehensive test coverage for function-based enum values.

The test case thoroughly covers:

  1. Function-based enum values with explicit format specification
  2. Function-based enum values with format inference for both atoms and strings
  3. Proper verification that the function is called and values are processed correctly
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @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.

@renatomassaro renatomassaro merged commit 472957e into main Jun 24, 2025
3 of 5 checks passed
@renatomassaro renatomassaro deleted the allow-enum-values-to-be-defined-as-a-function branch June 24, 2025 13:30
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