Skip to content

Commit

Permalink
Fix and support IPv6
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali Rajbi committed Oct 21, 2017
1 parent 453a919 commit 3b0c8ca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
23 changes: 10 additions & 13 deletions lib/ecto_cassandra/inet.ex
Expand Up @@ -4,29 +4,26 @@ defmodule EctoCassandra.INet do
def type, do: :inet

def cast({_, _, _, _} = ip), do: {:ok, ip}
def cast({_, _, _, _, _, _} = ip), do: {:ok, ip}
def cast({_, _, _, _, _, _, _, _} = ip), do: {:ok, ip}

def cast(ip) when is_binary(ip) do
case String.split(ip, ".") do
list when is_list(list) and length(list) == 4 ->
{:ok, List.to_tuple(list)}
_ ->
case String.split(ip, ":") do
list when is_list(list) and length(list) == 6 ->
{:ok, List.to_tuple(list)}
_ ->
:error
end
cast(String.to_charlist(ip))
end

def cast(ip) when is_list(ip) do
case :inet_parse.address(ip) do
{:ok, ip} -> {:ok, ip}
_ -> :error
end
end

def cast(_), do: :error

def load({_, _, _, _} = ip), do: {:ok, ip}
def load({_, _, _, _, _, _} = ip), do: {:ok, ip}
def load({_, _, _, _, _, _, _, _} = ip), do: {:ok, ip}
def load(_), do: :error

def dump({_, _, _, _} = ip), do: {:ok, ip}
def dump({_, _, _, _, _, _} = ip), do: {:ok, ip}
def dump({_, _, _, _, _, _, _, _} = ip), do: {:ok, ip}
def dump(_), do: :error
end
2 changes: 1 addition & 1 deletion mix.exs
Expand Up @@ -27,7 +27,7 @@ defmodule EctoCassandra.Mixfile do

defp deps, do: [
{:ecto, "~> 2.1.0"},
{:cassandra, "~> 1.0.0-rc.1"},
{:cassandra, "~> 1.0.0-rc.2"},
{:excoveralls, "~> 0.6", only: :test},
{:ex_doc, ">= 0.0.0", only: :dev},
{:lz4, github: "szktty/erlang-lz4", override: true}, # TODO check if fixed remove
Expand Down
7 changes: 7 additions & 0 deletions test/integration/repo_test.exs
Expand Up @@ -87,4 +87,11 @@ defmodule EctoCassandra.Integration.RepoTest do
assert %Post{} = TestRepo.insert!(post)
assert [%Post{ip: ^ip}] = TestRepo.all(Post)
end

test "inet version 6" do
ip = {0, 0, 0, 0, 0, 0, 0, 1}
post = %Post{title: "test inet", ip: ip}
assert %Post{} = TestRepo.insert!(post)
assert [%Post{ip: ^ip}] = TestRepo.all(Post)
end
end

0 comments on commit 3b0c8ca

Please sign in to comment.