Skip to content

Commit 32d7a68

Browse files
committed
fix: compilation error when no JSON libraries are listed in deps
1 parent f49b997 commit 32d7a68

File tree

4 files changed

+69
-40
lines changed

4 files changed

+69
-40
lines changed

lib/protox/define_message.ex

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
defmodule Protox.DefineMessage do
22
@moduledoc false
33

4-
@default_json_library Jason
5-
64
alias Protox.Field
75

86
def define(messages, opts \\ []) do
97
keep_unknown_fields = Keyword.get(opts, :keep_unknown_fields, true)
10-
json_library = Keyword.get(opts, :json_library, @default_json_library)
118

129
for msg = %Protox.Message{} <- messages do
1310
fields = Enum.sort(msg.fields, &(&1.tag < &2.tag))
@@ -19,7 +16,7 @@ defmodule Protox.DefineMessage do
1916
unknown_fields_funs = make_unknown_fields_funs(unknown_fields, keep_unknown_fields)
2017
required_fields_fun = make_required_fields_fun(required_fields)
2118
fields_access_funs = make_fields_access_funs(fields)
22-
json_funs = make_json_funs(msg.name, json_library)
19+
json_funs = make_json_funs(msg.name, Protox.JsonLibrary.get_wrapper(opts))
2320
default_fun = make_default_funs(fields)
2421
syntax_fun = make_syntax_fun(msg.syntax)
2522
file_options_fun = make_file_options_fun(msg)
@@ -96,9 +93,31 @@ defmodule Protox.DefineMessage do
9693
end
9794
end
9895

99-
defp make_json_funs(msg_name, json_library) do
100-
json_library_wrapper = Protox.JsonLibrary.get_wrapper(json_library)
96+
defp make_json_funs(_msg_name, nil = _json_library_wrapper) do
97+
quote do
98+
@spec json_decode(iodata()) :: {:error, any()}
99+
def json_decode(_input) do
100+
{:error, Protox.JsonLibraryError.new()}
101+
end
102+
103+
@spec json_decode!(iodata()) :: no_return()
104+
def json_decode!(_input) do
105+
raise Protox.JsonLibraryError.new()
106+
end
107+
108+
@spec json_encode(struct()) :: {:error, any()}
109+
def json_encode(_msg) do
110+
{:error, Protox.JsonLibraryError.new()}
111+
end
112+
113+
@spec json_encode!(struct()) :: no_return()
114+
def json_encode!(_msg) do
115+
raise Protox.JsonLibraryError.new()
116+
end
117+
end
118+
end
101119

120+
defp make_json_funs(msg_name, json_library_wrapper) do
102121
quote do
103122
@spec json_decode(iodata()) :: {:ok, struct()} | {:error, any()}
104123
def json_decode(input) do

lib/protox/jason.ex

+19-16
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
defmodule Protox.Jason do
22
@moduledoc false
3-
@behaviour Protox.JsonLibrary
43

5-
@impl true
6-
def decode!(iodata) do
7-
try do
8-
Jason.decode!(iodata)
9-
rescue
10-
e in [Jason.DecodeError, Protocol.UndefinedError] ->
11-
reraise Protox.JsonDecodingError.new(Exception.message(e)), __STACKTRACE__
4+
if Code.ensure_loaded?(Jason) do
5+
@behaviour Protox.JsonLibrary
6+
7+
@impl true
8+
def decode!(iodata) do
9+
try do
10+
Jason.decode!(iodata)
11+
rescue
12+
e in [Jason.DecodeError, Protocol.UndefinedError] ->
13+
reraise Protox.JsonDecodingError.new(Exception.message(e)), __STACKTRACE__
14+
end
1215
end
13-
end
1416

15-
@impl true
16-
def encode!(term) do
17-
try do
18-
Jason.encode_to_iodata!(term)
19-
rescue
20-
e in Jason.EncodeError ->
21-
reraise Protox.JsonEncodingError.new(Exception.message(e)), __STACKTRACE__
17+
@impl true
18+
def encode!(term) do
19+
try do
20+
Jason.encode_to_iodata!(term)
21+
rescue
22+
e in Jason.EncodeError ->
23+
reraise Protox.JsonEncodingError.new(Exception.message(e)), __STACKTRACE__
24+
end
2225
end
2326
end
2427
end

lib/protox/json_library.ex

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule Protox.JsonLibrary do
33
The behaviour to implement when wrapping a JSON library.
44
"""
55

6+
@default_json_library Jason
7+
68
@doc """
79
Should wrap any exception of the underlying library in Protox.JsonDecodingError.
810
"""
@@ -14,11 +16,13 @@ defmodule Protox.JsonLibrary do
1416
@callback encode!(term()) :: iodata() | no_return()
1517

1618
@doc false
17-
def get_wrapper(json_library) do
19+
def get_wrapper(opts) do
20+
json_library = Keyword.get(opts, :json_library, @default_json_library)
21+
1822
if Code.ensure_loaded?(json_library) do
1923
Module.concat(Protox, json_library)
2024
else
21-
raise Protox.JsonLibraryError.new()
25+
nil
2226
end
2327
end
2428
end

lib/protox/poison.ex

+19-16
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
defmodule Protox.Poison do
22
@moduledoc false
3-
@behaviour Protox.JsonLibrary
43

5-
@impl true
6-
def decode!(iodata) do
7-
try do
8-
Poison.decode!(iodata)
9-
rescue
10-
e in [Poison.DecodeError, Poison.ParseError] ->
11-
reraise Protox.JsonDecodingError.new(Exception.message(e)), __STACKTRACE__
4+
if Code.ensure_loaded?(Poison) do
5+
@behaviour Protox.JsonLibrary
6+
7+
@impl true
8+
def decode!(iodata) do
9+
try do
10+
Poison.decode!(iodata)
11+
rescue
12+
e in [Poison.DecodeError, Poison.ParseError] ->
13+
reraise Protox.JsonDecodingError.new(Exception.message(e)), __STACKTRACE__
14+
end
1215
end
13-
end
1416

15-
@impl true
16-
def encode!(term) do
17-
try do
18-
Poison.encode!(term)
19-
rescue
20-
e in Poison.EncodeError ->
21-
reraise Protox.JsonEncodingError.new(Exception.message(e)), __STACKTRACE__
17+
@impl true
18+
def encode!(term) do
19+
try do
20+
Poison.encode!(term)
21+
rescue
22+
e in Poison.EncodeError ->
23+
reraise Protox.JsonEncodingError.new(Exception.message(e)), __STACKTRACE__
24+
end
2225
end
2326
end
2427
end

0 commit comments

Comments
 (0)