Skip to content

Commit dd479f8

Browse files
bdebinskaStefan Fochler
andauthored
Stricter type checking for coordinates (#4)
* latitude must be a number or a binary to be formatted as a coordinate Co-authored-by: Stefan Fochler <s.fochler@box-id.com> * tests for arrays * fix typo * check that all values in a list of coordinates are either a binary or a number * bump version to 0.1.3 --------- Co-authored-by: Stefan Fochler <s.fochler@box-id.com>
1 parent 992db6c commit dd479f8

File tree

3 files changed

+82
-11
lines changed

3 files changed

+82
-11
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] -> "coordinates"
92-
[_lat, _lng] -> "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

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule ValueFormatters.MixProject do
44
def project do
55
[
66
app: :value_formatters,
7-
version: "0.1.2",
7+
version: "0.1.3",
88
elixir: "~> 1.15",
99
elixirc_paths: elixirc_paths(Mix.env()),
1010
start_permanent: Mix.env() == :prod,

test/value_formatters_test.exs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,24 @@ defmodule ValueFormattersTest do
554554
{:ok, "123.1345°, 34.123°, 2 m"}
555555
end
556556

557+
test "full coordinates as string" do
558+
assert ValueFormatters.to_string(
559+
["123.1345", "34.123", "2"],
560+
%{"format" => "coordinates"},
561+
@opts
562+
) ==
563+
{:ok, "123.1345°, 34.123°, 2 m"}
564+
end
565+
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+
557575
test "inference object with radius" do
558576
assert ValueFormatters.to_string(
559577
%{"lat" => 43.1298, "lng" => 54.1234, "radius" => 1},
@@ -578,6 +596,11 @@ defmodule ValueFormattersTest do
578596
{:ok, "123.1345°, 34.123°"}
579597
end
580598

599+
test "inference list as string" do
600+
assert ValueFormatters.to_string(["123.1345", "34.123"], %{}, @opts) ==
601+
{:ok, "123.1345°, 34.123°"}
602+
end
603+
581604
test "with radius show radius" do
582605
assert ValueFormatters.to_string(
583606
%{"lat" => 123.134567, "lng" => 34.12345, "radius" => 2},
@@ -606,10 +629,38 @@ defmodule ValueFormattersTest do
606629
end
607630
end
608631

609-
test "call with empty object format desription" do
632+
test "call with empty object format description" do
610633
assert ValueFormatters.to_string(3.14244453, %{"precision" => 2}, @opts) == {:ok, "3.14"}
611634
end
612635

636+
describe "array" do
637+
test "doesn't format an arrays of maps with 2 elements" do
638+
assert ValueFormatters.to_string([%{"foo" => "bar"}, %{"bar" => "foo"}], %{}, @opts) ==
639+
{
640+
:error,
641+
"Unsupported format The type of value [%{\"foo\" => \"bar\"}, %{\"bar\" => \"foo\"}] is not supported."
642+
}
643+
end
644+
645+
test "doesn't format an arrays of maps with 3 elements" do
646+
assert ValueFormatters.to_string(
647+
[%{"foo" => "bar"}, %{"bar" => "foo"}, %{"baz" => "qux"}],
648+
%{},
649+
@opts
650+
) ==
651+
{
652+
:error,
653+
"Unsupported format The type of value [%{\"foo\" => \"bar\"}, %{\"bar\" => \"foo\"}, %{\"baz\" => \"qux\"}] is not supported."
654+
}
655+
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
662+
end
663+
613664
describe "render" do
614665
test "render unit html" do
615666
assert ValueFormatters.to_string(

0 commit comments

Comments
 (0)