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

Update specs #157

Merged
merged 1 commit into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/geo/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule Geo.JSON do
@doc """
Takes a map representing GeoJSON and returns a Geometry.
"""
@spec decode!(map()) :: Geo.geometry() | no_return
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no_return should be used when the function never returns (always raises, throws, endlessly loops, etc). Otherwise we'd need to annotate almost every function as they may e.g. raise an exception for invalid args, function clause, etc!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah got it. That makes sense

@spec decode!(map()) :: Geo.geometry()
defdelegate decode!(geo_json), to: Decoder

@doc """
Expand All @@ -42,7 +42,7 @@ defmodule Geo.JSON do
@doc """
Takes a Geometry and returns a map representing the GeoJSON.
"""
@spec encode!(Geo.geometry()) :: map() | no_return
@spec encode!(Geo.geometry()) :: map()
defdelegate encode!(geom), to: Encoder
defdelegate encode!(geom, opts), to: Encoder

Expand Down
2 changes: 1 addition & 1 deletion lib/geo/json/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Geo.JSON.Decoder do
@doc """
Takes a map representing GeoJSON and returns a Geometry.
"""
@spec decode!(map()) :: Geo.geometry() | no_return
@spec decode!(map()) :: Geo.geometry()
def decode!(geo_json) do
cond do
Map.has_key?(geo_json, "geometries") ->
Expand Down
16 changes: 9 additions & 7 deletions lib/geo/wkb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ defmodule Geo.WKB do
Takes a Geometry and returns a WKB string. The endian decides
what the byte order will be.
"""
@spec encode!(Geo.geometry(), Geo.endian()) :: binary | no_return
@spec encode!(Geo.geometry(), Geo.endian()) :: binary
def encode!(geom, endian \\ :xdr) do
geom |> Encoder.encode!(endian) |> IO.iodata_to_binary() |> Base.encode16()
end

@doc """
Takes a Geometry and returns a WKB string. The endian decides
what the byte order will be.
Takes a Geometry and returns a WKB string.

The endian decides what the byte order will be.
"""
@spec encode(binary, Geo.endian()) :: {:ok, binary} | {:error, Exception.t()}
def encode(geom, endian \\ :xdr) do
Expand All @@ -39,10 +40,11 @@ defmodule Geo.WKB do
end

@doc """
Takes a Geometry and returns a WKB as iodata. The endian decides
what the byte order will be.
Takes a Geometry and returns a WKB as iodata.

The endian decides what the byte order will be.
"""
@spec encode_to_iodata(Geo.geometry(), Geo.endian()) :: iodata() | no_return
@spec encode_to_iodata(Geo.geometry(), Geo.endian()) :: iodata()
def encode_to_iodata(geom, endian \\ :xdr) do
Encoder.encode!(geom, endian)
end
Expand Down Expand Up @@ -79,7 +81,7 @@ defmodule Geo.WKB do
@doc """
Takes a WKB string and returns a Geometry.
"""
@spec decode!(binary | iodata()) :: Geo.geometry() | no_return
@spec decode!(iodata()) :: Geo.geometry()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't think there's a use case for accepting an iodata, I'm happy to send a PR which removes the IO.iodata_to_binary call and thus this function just accepts a binary. And if there's a use case, we could always add it then. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the IO.iodata_to_binary call makes sense to me. I can see by updating the postgrex extension it's not needed there. I can accept a PR to remove that 👍🏿

def decode!(wkb)

def decode!("00" <> _ = wkb) do
Expand Down
2 changes: 1 addition & 1 deletion lib/geo/wkt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ defmodule Geo.WKT do
@doc """
Takes a WKT string and returns a Geo.geometry struct or list of Geo.geometry.
"""
@spec decode!(binary) :: Geo.geometry() | no_return
@spec decode!(binary) :: Geo.geometry()
defdelegate decode!(wkt), to: Decoder
end
2 changes: 1 addition & 1 deletion lib/geo/wkt/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Geo.WKT.Decoder do
@doc """
Takes a WKT string and returns a Geo.geometry struct or list of Geo.geometry.
"""
@spec decode!(binary) :: Geo.geometry() | no_return
@spec decode!(binary) :: Geo.geometry()
def decode!(wkt) do
wkt_split = String.split(wkt, ";")

Expand Down