-
Notifications
You must be signed in to change notification settings - Fork 0
Add Ecto Type for IP Addresses #11
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [ | ||
| exit: "medium", | ||
| format: "txt", | ||
| ignore: [], | ||
| ignore_files: [], | ||
| out: nil, | ||
| private: false, | ||
| router: nil, | ||
| skip: true, | ||
| threshold: :low, | ||
| verbose: true, | ||
| version: false, | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| defmodule Together.IP do | ||
| @moduledoc """ | ||
| Ecto-compatible type for storing IP addresses | ||
|
|
||
| Migrations should use the `:inet` type for the database column. | ||
|
|
||
| ## Example | ||
|
|
||
| # Schema | ||
| schema "example" do | ||
| field :ip_address, Together.IP | ||
| end | ||
|
|
||
| # Migration | ||
| def change do | ||
| create table(:example) do | ||
| add :ip_address, :inet | ||
| end | ||
| end | ||
|
|
||
| """ | ||
| if Code.ensure_loaded?(Ecto.Type) do | ||
| @behaviour Ecto.Type | ||
|
|
||
| @doc false | ||
| @impl Ecto.Type | ||
| def type, do: :string | ||
|
|
||
| @doc false | ||
| @impl Ecto.Type | ||
| def cast(value) when is_binary(value) or is_list(value), do: parse_address(value) | ||
| def cast({_, _, _, _} = value), do: {:ok, value} | ||
| def cast(_invalid), do: :error | ||
|
|
||
| @doc false | ||
| @impl Ecto.Type | ||
| def load(value), do: parse_address(value) | ||
|
|
||
| @doc false | ||
| @impl Ecto.Type | ||
| def dump(value) when is_binary(value) or is_list(value) do | ||
| with {:ok, ip_address} <- parse_address(value) do | ||
| dump(ip_address) | ||
| end | ||
| end | ||
|
|
||
| def dump({_, _, _, _} = value) do | ||
| case :inet.ntoa(value) do | ||
| {:error, :einval} -> :error | ||
| ip_address_charlist -> {:ok, to_string(ip_address_charlist)} | ||
| end | ||
| end | ||
|
|
||
| def dump(_), do: :error | ||
|
|
||
| @doc false | ||
| @impl Ecto.Type | ||
| def equal?(value1, value2) do | ||
| value1 == value2 | ||
| end | ||
|
|
||
| @doc false | ||
| @impl Ecto.Type | ||
| def embed_as(_), do: :self | ||
|
|
||
| @spec parse_address(String.t() | charlist) :: {:ok, :inet.ip_address()} | :error | ||
| defp parse_address(ip_address_string_or_charlist) do | ||
| ip_address_charlist = to_charlist(ip_address_string_or_charlist) | ||
|
|
||
| case :inet.parse_address(ip_address_charlist) do | ||
| {:ok, ip_address} -> {:ok, ip_address} | ||
| {:error, :einval} -> :error | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| defmodule Together.IPTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| alias Together.IP | ||
|
|
||
| describe "cast/1" do | ||
| test "casts a string, charlist, or IP address tuple" do | ||
| assert IP.cast("127.0.0.1") == {:ok, {127, 0, 0, 1}} | ||
| assert IP.cast(~c'127.0.0.1') == {:ok, {127, 0, 0, 1}} | ||
| assert IP.cast({127, 0, 0, 1}) == {:ok, {127, 0, 0, 1}} | ||
| end | ||
|
|
||
| test "returns :error for invalid input" do | ||
| assert IP.cast("invalid_ip") == :error | ||
| assert IP.cast(123) == :error | ||
| assert IP.cast([]) == :error | ||
| end | ||
| end | ||
|
|
||
| describe "load/1" do | ||
| test "parses a string into an IP address tuple" do | ||
| assert IP.load("127.0.0.1") == {:ok, {127, 0, 0, 1}} | ||
| end | ||
| end | ||
|
|
||
| describe "dump/1" do | ||
| test "dumps a string, charlist, or IP address tuple" do | ||
| assert IP.dump("127.0.0.1") == {:ok, "127.0.0.1"} | ||
| assert IP.dump(~c'127.0.0.1') == {:ok, "127.0.0.1"} | ||
| assert IP.dump({127, 0, 0, 1}) == {:ok, "127.0.0.1"} | ||
| end | ||
|
|
||
| test "returns :error for invalid IP address tuples" do | ||
| assert IP.dump({256, 0, 0, 1}) == :error | ||
aj-foster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert IP.dump("invalid_ip") == :error | ||
| end | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I've never done this, but why are we specifying
:stringhere instead of:inet?It should also be possible, we might also consider instead using
INETfrom ecto_network