-
Notifications
You must be signed in to change notification settings - Fork 54
Add Exqlite.TypeExtensions to allow more types to be stored in the database #333
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Exqlite.TypeExtension do | ||
@moduledoc """ | ||
A behaviour that defines the API for extensions providing custom data loaders and dumpers | ||
for Ecto schemas. | ||
""" | ||
|
||
@doc """ | ||
Takes a value and convers it to data suitable for storage in the database. | ||
""" | ||
@callback convert(value :: term) :: term | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -481,5 +481,20 @@ defmodule Exqlite.Sqlite3 do | |
raise ArgumentError, "#{inspect(datetime)} is not in UTC" | ||
end | ||
|
||
defp convert(val), do: val | ||
defp convert(val) do | ||
convert_with_type_extensions(type_extensions(), val) | ||
end | ||
|
||
defp convert_with_type_extensions([], val), do: val | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going back and fourth on allowing On one hand, letting it default to nil, will make the pattern match be faster than matching with an empty list. defp convert_with_type_extensions(nil, val), do: val
defp convert_with_type_extensions([], val), do: val And then changing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went back and forth as well .. I'm not sure the speed gain is really going to be noticeable, but it's easy enough to do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See 704c9bd |
||
|
||
defp convert_with_type_extensions([extension | other_extensions], val) do | ||
case extension.convert(val) do | ||
nil -> convert_with_type_extensions(other_extensions, val) | ||
convert -> convert | ||
end | ||
end | ||
|
||
defp type_extensions() do | ||
aseigo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Application.get_env(:exqlite, :type_extensions, []) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this common for these to simply be
term
that only returnnil
when it can't be converted? Or is a{:ok, term()} | :error
more useful?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could do a tagged tuple, yes. See 7b5d2ee
This also has the nice effect of making it more like the Ecto usage.