Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Poison 4 #2888

Merged
merged 1 commit into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clients/gax/lib/google_api/gax/data_wrapper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule GoogleApi.Gax.DataWrapper do
@spec decode(GoogleApi.Gax.DataWrapper.t(), keyword()) :: any()
def decode(data_wrapper, options) do
struct = options[:struct]
Poison.Decode.decode(data_wrapper.data, as: struct)
GoogleApi.Gax.ModelBase.poison_transform(data_wrapper.data, %{as: struct})
end
end

Expand Down
29 changes: 26 additions & 3 deletions clients/gax/lib/google_api/gax/model_base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.

defmodule GoogleApi.Gax.ModelBase do
require Poison.Decode

@moduledoc """
This module helps you quickly and concisely define API models.

Expand Down Expand Up @@ -97,17 +99,38 @@ defmodule GoogleApi.Gax.ModelBase do
def decode(value, :map, module) when not is_nil(value) do
value
|> Enum.map(fn {k, v} ->
{k, Poison.Decode.decode(v, as: struct(module))}
{k, poison_transform(v, %{as: struct(module)})}
end)
|> Enum.into(%{})
end

def decode(value, :list, module) do
Poison.Decode.decode(value, as: [struct(module)])
poison_transform(value, %{as: [struct(module)]})
end

def decode(value, _, module) do
Poison.Decode.decode(value, as: struct(module))
poison_transform(value, %{as: struct(module)})
end

if function_exported?(Poison.Decode, :decode, 2) do
def poison_transform(value, options) do
Poison.Decode.decode(value, options)
end
else
# Short-circuit if the value has already been transformed.
# This works around a bug in poison 4 where Poison.decode does an extra
# transformation pass on sub-structs.
def poison_transform(%{__struct__: _} = value, %{as: _}) do
value
end

def poison_transform([%{__struct__: _} | _] = value, %{as: [_]}) do
value
end

def poison_transform(value, options) do
Poison.Decode.transform(value, options)
end
end

@doc """
Expand Down
4 changes: 2 additions & 2 deletions clients/gax/mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule GoogleApi.Gax.MixProject do
use Mix.Project

@version "0.2.0"
@version "0.3.0"

def project do
[
Expand All @@ -27,7 +27,7 @@ defmodule GoogleApi.Gax.MixProject do
defp deps() do
[
{:tesla, "~> 1.2"},
{:poison, ">= 1.0.0 and < 4.0.0"},
{:poison, ">= 3.0.0 and < 5.0.0"},
{:ex_doc, "~> 0.16", only: :dev},
{:dialyxir, "~> 0.5", only: [:dev], runtime: false}
]
Expand Down