Skip to content

Commit cf50404

Browse files
committed
check that all values in a list of coordinates are either a binary or a number
1 parent 762874e commit cf50404

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

lib/value_formatters.ex

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,35 @@ defmodule ValueFormatters do
8484

8585
defp determine_value_type(value) do
8686
case value do
87-
%Date{} -> "date"
88-
%DateTime{} -> "date"
89-
%NaiveDateTime{} -> "date"
90-
%Time{} -> "date"
91-
[lat, _lng, _radius] when is_number(lat) or is_binary(lat) -> "coordinates"
92-
[lat, _lng] when is_number(lat) or is_binary(lat) -> "coordinates"
93-
%{"lat" => _lat, "lng" => _lng, "radius" => _radius} -> "coordinates"
94-
%{"lat" => _lat, "lng" => _lng} -> "coordinates"
95-
_ -> "The type of value #{inspect(value)} is not supported."
87+
%Date{} ->
88+
"date"
89+
90+
%DateTime{} ->
91+
"date"
92+
93+
%NaiveDateTime{} ->
94+
"date"
95+
96+
%Time{} ->
97+
"date"
98+
99+
[lat, lng, radius]
100+
when (is_number(lat) or is_binary(lat)) and
101+
(is_number(lng) or is_binary(lng)) and (is_number(radius) or is_binary(radius)) ->
102+
"coordinates"
103+
104+
[lat, lng]
105+
when (is_number(lat) or is_binary(lat)) and (is_number(lng) or is_binary(lng)) ->
106+
"coordinates"
107+
108+
%{"lat" => _lat, "lng" => _lng, "radius" => _radius} ->
109+
"coordinates"
110+
111+
%{"lat" => _lat, "lng" => _lng} ->
112+
"coordinates"
113+
114+
_ ->
115+
"The type of value #{inspect(value)} is not supported."
96116
end
97117
end
98118

test/value_formatters_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,15 @@ defmodule ValueFormattersTest do
563563
{:ok, "123.1345°, 34.123°, 2 m"}
564564
end
565565

566+
test "mixed coordinates with radius" do
567+
assert ValueFormatters.to_string(
568+
[123.1345, "34.123", 2],
569+
%{"format" => "coordinates"},
570+
@opts
571+
) ==
572+
{:ok, "123.1345°, 34.123°, 2 m"}
573+
end
574+
566575
test "inference object with radius" do
567576
assert ValueFormatters.to_string(
568577
%{"lat" => 43.1298, "lng" => 54.1234, "radius" => 1},
@@ -644,6 +653,12 @@ defmodule ValueFormattersTest do
644653
"Unsupported format The type of value [%{\"foo\" => \"bar\"}, %{\"bar\" => \"foo\"}, %{\"baz\" => \"qux\"}] is not supported."
645654
}
646655
end
656+
657+
test "doesn't format an array with only one map" do
658+
assert ValueFormatters.to_string([123, 456, %{"foo" => "bar"}], %{}, @opts) ==
659+
{:error,
660+
"Unsupported format The type of value [123, 456, %{\"foo\" => \"bar\"}] is not supported."}
661+
end
647662
end
648663

649664
describe "render" do

0 commit comments

Comments
 (0)