Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support casting to atom from strings #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

cheerfulstoic
Copy link

Ecto types can be so hard for me to wrap my head around sometimes, but I'm pretty sure this is right 😅

Here's an example from the docs with comments that I think are really helpful (I think they might be relatively new):
https://hexdocs.pm/ecto/Ecto.Type.html#module-example

But basically, dump takes what's on the Ecto schema struct and turns it into the database format. So cast is to take potentially supported values (e.g. in forms) and turn them into the data type that's expected to be on the Ecto schema struct. Basically load while is loading from the DB, cast is kind of like "loading" from the outside world, and thus potentially needs to support different types of data.

In that example in the docs, there should always be a %URI{} on the schema struct. So cast takes strings and %URI{} values and assigns them (I could imagine it also taking maps with the URL parts, depending on what you wanted to do).

@cheerfulstoic
Copy link
Author

I realize this may seem picky for such a small library, but I imagine this being a prime example that people might look at when trying to figure out how Ecto types work.

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