Skip to content

Fix type specs to accept structs and atom-keyed maps#14

Merged
xu-chris merged 1 commit intomainfrom
fix/type-specs-for-encoder-protocol
Jan 21, 2026
Merged

Fix type specs to accept structs and atom-keyed maps#14
xu-chris merged 1 commit intomainfrom
fix/type-specs-for-encoder-protocol

Conversation

@xu-chris
Copy link
Copy Markdown
Owner

Summary

Fixes Dialyzer warnings when calling Toon.encode!/1 with structs or maps with atom keys.

Problem

The Toon.Types.encodable() type only accepted:

  • %{optional(String.t()) => encodable()} (string-keyed maps)

But users commonly pass:

  • Ecto schemas (structs with atom keys like %Screentour.Festivals.Edition{})
  • Maps with atom keys (%{name: "Alice"})

The Toon.Encoder protocol handles normalization, but Dialyzer didn't know this.

Solution

  • Add Toon.Types.input() as term() - the Encoder protocol accepts any type with an implementation
  • Update encode/1,2 and encode!/1,2 specs to use input() for parameters
  • Keep encodable() for documenting the normalized output format

TDD Process

RED: Created lib/toon/type_check.ex that calls Toon.encode!/1 with a struct. Dialyzer failed:

The call 'Elixir.Toon':'encode!'(#struct) will never return since it differs in the 1st argument

GREEN: Added input() type and updated specs. Dialyzer passes.

REFACTOR: Removed temporary test module, kept behavioral tests in encoder_test.exs.

Test plan

  • 549 tests pass
  • mix quality.ci passes (credo, dialyzer, format)
  • New tests verify struct and atom-key encoding via public API

🤖 Generated with Claude Code

The Toon.Encoder protocol normalizes input before encoding, so the
public API should accept any term() - not just pre-normalized types.

Changes:
- Add Toon.Types.input() type as term() for encoder parameters
- Update encode/1,2 and encode!/1,2 specs to use input()
- Keep encodable() for normalized output (string-keyed maps)
- Add tests for struct and atom-key encoding via public API

This fixes Dialyzer warnings when calling Toon.encode!/1 with:
- Ecto schemas (structs with atom keys)
- Maps with atom keys
- Any type implementing the Toon.Encoder protocol

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@coveralls
Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 45581c8dffdd6a4b56077620d5c1a98ec683ef47-PR-14

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 67.867%

Totals Coverage Status
Change from base Build 039bd55701dad23196695fb4d7e40f621ccfb962: 0.0%
Covered Lines: 716
Relevant Lines: 1055

💛 - Coveralls

@xu-chris xu-chris merged commit 9810e86 into main Jan 21, 2026
17 of 18 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.

2 participants