From bfc2b27be381ac520e39c391f34632d5f653345b Mon Sep 17 00:00:00 2001 From: Mauro Date: Tue, 5 Apr 2022 11:41:23 +0200 Subject: [PATCH] Align bridge_ex with improvements made on Peano (#43) * Align bridge_ex with improvements made on Peano this update allows the library to be in sync with the Peano version of `graphql_utils.ex`, so that it can be implemented there more easily in any use case. More specifically, `normalize_inner_fields` now handle cases where the root value object is a number or a boolean. Before it would break ad the map pattern match * fix spec and pattern match * format * format a mano che dio non so come entrarci * test --- lib/graphql/utils.ex | 11 +++++++---- test/graphql/utils_test.exs | 8 ++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/graphql/utils.ex b/lib/graphql/utils.ex index df22772..24d19bf 100644 --- a/lib/graphql/utils.ex +++ b/lib/graphql/utils.ex @@ -69,10 +69,13 @@ defmodule BridgeEx.Graphql.Utils do def parse_response({:ok, %{data: data}}), do: {:ok, data} - @spec normalize_inner_fields(%{atom() => any()} | String.t()) :: %{atom() => any()} | String.t() - def normalize_inner_fields(binary) when is_binary(binary), do: binary - def normalize_inner_fields(map = %{}), do: Enum.reduce(map, %{}, &do_normalize_inner_fields/2) - @spec do_normalize_inner_fields({atom(), any()}, map()) :: %{atom() => any()} + @spec normalize_inner_fields(any()) :: any() + def normalize_inner_fields(map) when is_map(map), + do: Enum.reduce(map, %{}, &do_normalize_inner_fields/2) + + def normalize_inner_fields(value), do: value + + @spec do_normalize_inner_fields({atom() | String.t(), any()}, map()) :: %{atom() => any()} defp do_normalize_inner_fields({key, value}, acc) when is_map(value) do Map.merge(acc, %{to_snake_case(key) => normalize_inner_fields(value)}) end diff --git a/test/graphql/utils_test.exs b/test/graphql/utils_test.exs index 68f5223..c31784c 100644 --- a/test/graphql/utils_test.exs +++ b/test/graphql/utils_test.exs @@ -8,6 +8,14 @@ defmodule BridgeEx.Graphql.UtilsTest do assert "some-string" == Utils.normalize_inner_fields("some-string") end + test "does not change integers" do + assert 5 == Utils.normalize_inner_fields(5) + end + + test "does not change booleans" do + assert true == Utils.normalize_inner_fields(true) + end + test "does not change empty maps" do assert %{} == Utils.normalize_inner_fields(%{}) end