diff --git a/lib/protox/define_message.ex b/lib/protox/define_message.ex index a6c8739..33613e5 100644 --- a/lib/protox/define_message.ex +++ b/lib/protox/define_message.ex @@ -107,12 +107,12 @@ defmodule Protox.DefineMessage do @spec json_decode!(iodata(), keyword()) :: struct() | no_return() def json_decode!(input, opts \\ []) do - {json_library_wrapper, json_library} = Protox.JsonLibrary.get_library(opts, :decode) + json_library_wrapper = Protox.JsonLibrary.get_library(opts, :decode) Protox.JsonDecode.decode!( input, unquote(msg_name), - &json_library_wrapper.decode!(json_library, &1) + &json_library_wrapper.decode!(&1) ) end @@ -128,9 +128,9 @@ defmodule Protox.DefineMessage do @spec json_encode!(struct(), keyword()) :: iodata() | no_return() def json_encode!(msg, opts \\ []) do - {json_library_wrapper, json_library} = Protox.JsonLibrary.get_library(opts, :encode) + json_library_wrapper = Protox.JsonLibrary.get_library(opts, :encode) - Protox.JsonEncode.encode!(msg, &json_library_wrapper.encode!(json_library, &1)) + Protox.JsonEncode.encode!(msg, &json_library_wrapper.encode!(&1)) end end end diff --git a/lib/protox/jason.ex b/lib/protox/jason.ex index 5fa379a..532eac3 100644 --- a/lib/protox/jason.ex +++ b/lib/protox/jason.ex @@ -4,12 +4,10 @@ defmodule Protox.Jason do if Code.ensure_loaded?(Jason) do @impl true - def load() do - {:ok, Jason} - end + def load(), do: :ok @impl true - def decode!(_jason_module, iodata) do + def decode!(iodata) do try do Jason.decode!(iodata) rescue @@ -19,7 +17,7 @@ defmodule Protox.Jason do end @impl true - def encode!(_jason_module, term) do + def encode!(term) do try do Jason.encode!(term) rescue @@ -29,19 +27,17 @@ defmodule Protox.Jason do end else @impl true - def load() do - :error - end + def load(), do: :error @impl true - def decode!(_jason_module, _iodata) do + def decode!(_iodata) do raise Protox.JsonDecodingError.new( "Jason library not loaded. Please check your project dependencies." ) end @impl true - def encode!(_jason_module, _term) do + def encode!(_term) do raise Protox.JsonEncodingError.new( "Jason library not loaded. Please check your project dependencies." ) diff --git a/lib/protox/json_library.ex b/lib/protox/json_library.ex index e8e61eb..3b9f394 100644 --- a/lib/protox/json_library.ex +++ b/lib/protox/json_library.ex @@ -3,25 +3,26 @@ defmodule Protox.JsonLibrary do The behaviour to implement when wrapping a JSON library. """ - @callback load() :: {:ok, atom()} | :error + @callback load() :: :ok | :error @doc """ Should wrap any exception of the underlying library in Protox.JsonDecodingError. """ - @callback decode!(atom(), iodata()) :: term() | no_return() + @callback decode!(iodata()) :: term() | no_return() @doc """ Should wrap any exception of the underlying library in Protox.JsonEncodingError. """ - @callback encode!(atom(), term()) :: iodata() | no_return() + @callback encode!(term()) :: iodata() | no_return() @doc false def get_library(opts, decoding_or_encoding) do - json_library_wrapper = Keyword.get(opts, :json_library, Protox.Jason) + json_library_name = Keyword.get(opts, :json_library, Jason) + json_library_wrapper = Module.concat(Protox, json_library_name) case json_library_wrapper.load() do - {:ok, json_library} -> - {json_library_wrapper, json_library} + :ok -> + json_library_wrapper :error -> message = "cannot load JSON library. Please check your project dependencies." diff --git a/lib/protox/poison.ex b/lib/protox/poison.ex index c8eb94e..92e7111 100644 --- a/lib/protox/poison.ex +++ b/lib/protox/poison.ex @@ -4,14 +4,12 @@ defmodule Protox.Poison do if Code.ensure_loaded?(Poison) do @impl true - def load() do - {:ok, Poison} - end + def load(), do: :ok @impl true - def decode!(poison_module, iodata) do + def decode!(iodata) do try do - poison_module.decode!(iodata) + Poison.decode!(iodata) rescue e in [Poison.DecodeError, Poison.ParseError] -> reraise Protox.JsonDecodingError.new(Exception.message(e)), __STACKTRACE__ @@ -19,9 +17,9 @@ defmodule Protox.Poison do end @impl true - def encode!(poison_module, term) do + def encode!(term) do try do - poison_module.encode!(term) + Poison.encode!(term) rescue e in Poison.EncodeError -> reraise Protox.JsonEncodingError.new(Exception.message(e)), __STACKTRACE__ @@ -29,19 +27,17 @@ defmodule Protox.Poison do end else @impl true - def load() do - :error - end + def load(), do: :error @impl true - def decode!(_poison_module, _iodata) do + def decode!(_iodata) do raise Protox.JsonDecodingError.new( "Poison library not loaded. Please check your project dependencies." ) end @impl true - def encode!(_poison_module, _term) do + def encode!(_term) do raise Protox.JsonEncodingError.new( "Poison library not loaded. Please check your project dependencies." ) diff --git a/test/protox/json_decode_test.exs b/test/protox/json_decode_test.exs index f9838c7..badcf48 100644 --- a/test/protox/json_decode_test.exs +++ b/test/protox/json_decode_test.exs @@ -692,11 +692,11 @@ defmodule Protox.JsonDecodeTest do end test "Success: jason", %{json: json, expected: expected} do - assert Protox.json_decode!(json, Sub, json_library: Protox.Jason) == expected + assert Protox.json_decode!(json, Sub, json_library: Jason) == expected end test "Success: poison", %{json: json, expected: expected} do - assert Protox.json_decode!(json, Sub, json_library: Protox.Poison) == expected + assert Protox.json_decode!(json, Sub, json_library: Poison) == expected end test "Failure: poison", %{} do @@ -704,7 +704,7 @@ defmodule Protox.JsonDecodeTest do Protox.json_decode!( "{\"mapInt32Int32\": 1", ProtobufTestMessages.Proto3.TestAllTypesProto3, - json_library: Protox.Poison + json_library: Poison ) end end diff --git a/test/protox/json_encode_test.exs b/test/protox/json_encode_test.exs index 0d1fed9..ae57397 100644 --- a/test/protox/json_encode_test.exs +++ b/test/protox/json_encode_test.exs @@ -394,7 +394,7 @@ defmodule Protox.JsonEncodeTest do describe "JSON libraries" do test "Success: Jason" do msg = %Msg{msg_k: %{1 => "a", 2 => "b"}} - json = Protox.json_encode!(msg, json_library: Protox.Jason) + json = Protox.json_encode!(msg, json_library: Jason) assert json == [ "{", @@ -405,7 +405,7 @@ defmodule Protox.JsonEncodeTest do test "Success: Poison" do msg = %Msg{msg_k: %{1 => "a", 2 => "b"}} - json = Protox.json_encode!(msg, json_library: Protox.Poison) + json = Protox.json_encode!(msg, json_library: Poison) assert json == [ "{", @@ -418,7 +418,7 @@ defmodule Protox.JsonEncodeTest do invalid_field = %ProtobufTestMessages.Proto3.TestAllTypesProto3{optional_int32: make_ref()} assert_raise Protox.JsonEncodingError, fn -> - Protox.json_encode!(invalid_field, json_library: Protox.Poison) + Protox.json_encode!(invalid_field, json_library: Poison) end end end