From 290ab064149efb87cc3533326b56658b1c86419a Mon Sep 17 00:00:00 2001 From: Mariusz Morawski Date: Wed, 22 Mar 2023 12:41:44 +0100 Subject: [PATCH 1/5] Do not raise on unsuccessful cast --- lib/geo_postgis/geometry.ex | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/geo_postgis/geometry.ex b/lib/geo_postgis/geometry.ex index 2946f17..c76e159 100644 --- a/lib/geo_postgis/geometry.ex +++ b/lib/geo_postgis/geometry.ex @@ -78,15 +78,31 @@ if Code.ensure_loaded?(Ecto.Type) do def cast(%struct{} = geom) when struct in @geometries, do: {:ok, geom} def cast(%{"type" => type, "coordinates" => _} = geom) when type in @types do - {:ok, Geo.JSON.decode!(geom)} + do_cast(geom) end def cast(%{"type" => "GeometryCollection", "geometries" => _} = geom) do - {:ok, Geo.JSON.decode!(geom)} + do_cast(geom) end def cast(geom) when is_binary(geom) do - {:ok, geom |> Geo.PostGIS.Config.json_library().decode!() |> Geo.JSON.decode!()} + do_cast(geom) + end + + defp do_cast(geom) when is_binary(geom) do + with {:ok, geom} <- Geo.PostGIS.Config.json_library().decode(), + {:ok, result} <- Geo.JSON.decode(geom) do + {:ok, result} + else + _ -> :error + end + end + + defp do_cast(geom) do + case Geo.JSON.decode(geom) do + {:ok, result} -> {:ok, result} + {:error, _} -> :error + end end def cast(_), do: :error From 5124c88051fb6f2054d60e45c22ccdd1c8e02cf6 Mon Sep 17 00:00:00 2001 From: Mariusz Morawski Date: Wed, 22 Mar 2023 12:58:17 +0100 Subject: [PATCH 2/5] Fix binary casting --- lib/geo_postgis/geometry.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/geo_postgis/geometry.ex b/lib/geo_postgis/geometry.ex index c76e159..3df257e 100644 --- a/lib/geo_postgis/geometry.ex +++ b/lib/geo_postgis/geometry.ex @@ -90,7 +90,7 @@ if Code.ensure_loaded?(Ecto.Type) do end defp do_cast(geom) when is_binary(geom) do - with {:ok, geom} <- Geo.PostGIS.Config.json_library().decode(), + with {:ok, geom} <- Geo.PostGIS.Config.json_library().decode(geom), {:ok, result} <- Geo.JSON.decode(geom) do {:ok, result} else From 43c3ca0cb39556d3c863ecb0716383d4801d7ed3 Mon Sep 17 00:00:00 2001 From: Mariusz Morawski Date: Mon, 29 Apr 2024 23:45:42 +0200 Subject: [PATCH 3/5] Update lib/geo_postgis/geometry.ex --- lib/geo_postgis/geometry.ex | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/geo_postgis/geometry.ex b/lib/geo_postgis/geometry.ex index 3df257e..3a9f15d 100644 --- a/lib/geo_postgis/geometry.ex +++ b/lib/geo_postgis/geometry.ex @@ -90,11 +90,9 @@ if Code.ensure_loaded?(Ecto.Type) do end defp do_cast(geom) when is_binary(geom) do - with {:ok, geom} <- Geo.PostGIS.Config.json_library().decode(geom), - {:ok, result} <- Geo.JSON.decode(geom) do - {:ok, result} - else - _ -> :error + case Geo.PostGIS.Config.json_library().decode(geom) do + {:ok, geom} when is_map(geom) -> do_cast(geom) + {:error, reason} -> {:error, [message: "failed to decode JSON", reason: reason]} end end From e9a0eb3de728f57d567d236937957aa1f0fd5630 Mon Sep 17 00:00:00 2001 From: Mariusz Morawski Date: Tue, 30 Apr 2024 11:15:30 +0200 Subject: [PATCH 4/5] Return error from `Geo.JSON.decode/1` --- lib/geo_postgis/geometry.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/geo_postgis/geometry.ex b/lib/geo_postgis/geometry.ex index 3a9f15d..0b90a1c 100644 --- a/lib/geo_postgis/geometry.ex +++ b/lib/geo_postgis/geometry.ex @@ -99,7 +99,7 @@ if Code.ensure_loaded?(Ecto.Type) do defp do_cast(geom) do case Geo.JSON.decode(geom) do {:ok, result} -> {:ok, result} - {:error, _} -> :error + {:error, reason} -> {:error, [message: "failed to decode GeoJSON", reason: reason]} end end From 57fb90fb026114d046539e01a97a7b9ccba33e9e Mon Sep 17 00:00:00 2001 From: Mariusz Morawski Date: Tue, 30 Apr 2024 15:34:18 +0200 Subject: [PATCH 5/5] Group `cast/1` together --- lib/geo_postgis/geometry.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/geo_postgis/geometry.ex b/lib/geo_postgis/geometry.ex index 0b90a1c..a1c8ce2 100644 --- a/lib/geo_postgis/geometry.ex +++ b/lib/geo_postgis/geometry.ex @@ -89,6 +89,8 @@ if Code.ensure_loaded?(Ecto.Type) do do_cast(geom) end + def cast(_), do: :error + defp do_cast(geom) when is_binary(geom) do case Geo.PostGIS.Config.json_library().decode(geom) do {:ok, geom} when is_map(geom) -> do_cast(geom) @@ -103,8 +105,6 @@ if Code.ensure_loaded?(Ecto.Type) do end end - def cast(_), do: :error - def embed_as(_), do: :self def equal?(a, b), do: a == b