How to define a ip field in Ecto?
You can use some 3rd party library like this: https://github.com/jerel/ecto_fields
Or write it yourself https://github.com/jerel/ecto_fields/blob/master/lib/fields/ip4.ex
Here’s a tutorial how to write such custom Field http://pedroassumpcao.ghost.io/using-postgres-range-data-type-in-ecto/
You can also use the 32-bit integer type in the database, then just binary match it for the most compact representation (if you do not already have it as an integer:
iex> <<packed::unsigned-integer-32>> = <<192,168,1,1>> <<192, 168, 1, 1>> iex> <<ip0::unsigned-integer-8, ip1::unsigned-integer-8, ip2::unsigned-integer-8, ip3::unsigned-integer-8>> = <<packed::unsigned-integer-32>> <<192, 168, 1, 1>>
Bit-twiddling might be more efficient too.
Presuming you’re using Postgres, my package EctoNetwork, will give you INET, CIDR, and MACADDR types.