Skip to content

Commit

Permalink
Appsignal.Utils.ParamsEncoder.preprocess/1 handles structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Kreeftmeijer committed Oct 27, 2016
1 parent ba0b447 commit 8295aba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/appsignal/utils/params_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ defmodule Appsignal.Utils.ParamsEncoder do
@doc """
Makes sure the keys are valid for JSON encoding
"""
def preprocess(value = %{__struct__: _}) do
value
|> Map.from_struct
|> preprocess
end
def preprocess(value) when is_map(value) do
Enum.map(value, fn({k, v}) ->
{safe_key(k), preprocess(v)}
Expand Down
5 changes: 5 additions & 0 deletions test/utils/param_encoder_test.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
defmodule ExampleStruct do
defstruct a: "b"
end

defmodule Appsignal.Utils.ParamsEncoderTest do
use ExUnit.Case

Expand All @@ -8,6 +12,7 @@ defmodule Appsignal.Utils.ParamsEncoderTest do
assert %{"a" => "b"} == ParamsEncoder.preprocess(%{"a" => "b"})
assert %{:a => "b"} == ParamsEncoder.preprocess(%{:a => "b"})
assert %{"{:weird, :key}" => "b"} == ParamsEncoder.preprocess(%{{:weird, :key} => "b"})
assert %{:a => "b"} == ParamsEncoder.preprocess(%ExampleStruct{})
end

test "deep nesting" do
Expand Down

0 comments on commit 8295aba

Please sign in to comment.