edgurgel / edgurgel/solid

Structs should raise Protocol.UndefinedError when used as context in Solid.render!/3

Open
#185 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Elixir
Stars
259
Forks
58
Avg merge
9d 18h
Merged PRs (30d)
2

Description

Hi! I'm making this issue to discuss updating the response for unsupported types in the Solid.Matcher protocol. I encountered this issue when passing a struct into `Solid.render!/3`, using the `[strict_filters: true, strict_variables: true]` filters. The function was returning an `{:error, :not_found}` response, which was a little confusing since maps **are** a natively implemented type in Solid, and [Elixir treats structs as maps under the hood](https://hexdocs.pm/elixir/structs.html#structs-are-bare-maps-underneath). I was able to encounter a similar error by replacing the string keys in a map with `:atom` keys, leading me to believe the issue was due to the expectation that maps would adhere to the `%{"key" => "value"}` syntax, rather than the `%{key: :value}` syntax found in structs due to how variables are passed as strings into `Map.fetch/2`. I totally agree (after "RTFM"-ing) that the implementing application should handle how a proprietary struct implements the `Solid.Matcher` protocol, but I think it would've been more helpful to receive a `**Protocol.UndefinedError` message during development, instead of `1: Undefined variable`. This matches native Elixir patterns seen in situations like:
```elixir
1 |> Enum.map(& &1)
# ** (Protocol.UndefinedError) protocol Enumerable not implemented for 1 of type Integer
# (elixir 1.17.2) lib/enum.ex:1: Enumerable.impl_for!/1
# (elixir 1.17.2) lib/enum.ex:166: Enumerable.reduce/3
# (elixir 1.17.2) lib/enum.ex:4423: Enum.map/2
```

I have a local branch with this behavior tested and ready for a PR if this behavior is worth implementing.

Thanks!

Contributor guide

Open the contributing guide

Research direction

Start by reading the Solid.Matcher protocol and the Solid.render!/3 path when strict_filters and strict_variables are enabled. Reproduce the struct and atom-key map cases, then compare the current {:error, :not_found} or undefined-variable response with the requested Protocol.UndefinedError behavior and verify the existing local tests cover it.

Written by the indexing model from the issue text.

Assessment

Tech stack
elixir
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.