Skip to content

Conversation

@dvic
Copy link
Collaborator

@dvic dvic commented Jun 24, 2025

Summary

Fixes #52 by properly handling field options when they are passed as variables in macro contexts.

Problem

When field options (opts) are passed as variables in macro contexts, they become AST variable references (tuples like {:opts, [], Module}) instead of literal keyword lists. The original code in syntax_sugar.ex:46 called Keyword.drop/2 directly on opts, which failed with a FunctionClauseError when opts was an AST tuple.

Solution

Modified TypedEctoSchema.SyntaxSugar.transform_expression/2 to:

  1. Check if opts is a compile-time keyword list using Keyword.keyword?/1
  2. If yes, drop keys at compile time (preserving existing behavior for performance)
  3. If no (AST variable reference), generate code to drop keys at runtime

Changes

  • Core Fix: Updated lib/typed_ecto_schema/syntax_sugar.ex to handle both compile-time and runtime opts
  • Test Coverage: Added test case that reproduces the original issue and verifies the fix
  • Documentation: Added CLAUDE.md with development guidelines

Test Plan

  • All existing tests pass (no regressions)
  • New test reproduces the original error when fix is commented out
  • New test passes with the fix applied
  • Code formatted with mix format
  • Dialyzer passes

Backward Compatibility

✅ Fully backward compatible - existing usage with literal keyword lists works exactly as before, with the same compile-time optimization.

Problem: When field options are passed as variables in macro contexts,
they become AST variable references instead of literal keyword lists.
The original code called Keyword.drop/2 directly on opts, which failed
with FunctionClauseError when opts was an AST tuple.

Solution: Check if opts is a compile-time keyword list using
Keyword.keyword?/1. If yes, drop keys at compile time. If no (AST
variable reference), generate code to drop keys at runtime.

Changes:
- Modified TypedEctoSchema.SyntaxSugar.transform_expression/2 to handle
  both compile-time keyword lists and runtime AST variable references
- Added test case that reproduces the issue and verifies the fix
- Added CLAUDE.md with development guidelines

Fixes #52
@dvic dvic requested a review from bamorim June 24, 2025 21:55
@dvic
Copy link
Collaborator Author

dvic commented Jun 24, 2025

@bamorim ngl, this was 99.9% Claude ;)

@elliottneilclark
Copy link

elliottneilclark commented Jun 24, 2025

I tested this branch, and it compiles and passes tests (running our larger suite now). Thank you!

@bamorim
Copy link
Owner

bamorim commented Jun 25, 2025

@dvic who am I to say no to our master Claude???

@bamorim bamorim merged commit 252ffef into master Jun 25, 2025
11 checks 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.

0.4.2 breaks usage in defmacro

4 participants