Skip to content

Commit

Permalink
mix format
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer authored and jherdman committed Mar 18, 2020
1 parent 08c0965 commit 5e14139
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 78 deletions.
4 changes: 2 additions & 2 deletions lib/jsonapi/plugs/deserializer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ defmodule JSONAPI.Deserializer do
import Plug.Conn
alias JSONAPI.Utils.DataToParams

@spec init(Keyword.t) :: Keyword.t
@spec init(Keyword.t()) :: Keyword.t()
def init(opts), do: opts

@spec call(Plug.Conn.t, Keyword.t) :: Plug.Conn.t
@spec call(Plug.Conn.t(), Keyword.t()) :: Plug.Conn.t()
def call(conn, _opts) do
content_type = get_req_header(conn, "content-type")

Expand Down
31 changes: 24 additions & 7 deletions lib/jsonapi/utils/data_to_params.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ defmodule JSONAPI.Utils.DataToParams do

@spec process(map) :: map
def process(%{"data" => nil}), do: nil

def process(%{"data" => _} = incoming) do
incoming
|> flatten_incoming()
|> process_included()
|> process_relationships()
|> process_attributes()
end

def process(incoming), do: incoming

defp flatten_incoming(%{"data" => data}) when is_list(data) do
data
end

defp flatten_incoming(%{"data" => data} = incoming) do
incoming
|> Map.merge(data)
Expand All @@ -30,35 +33,46 @@ defmodule JSONAPI.Utils.DataToParams do
defp process_attributes(%{"attributes" => nil} = data) do
Map.drop(data, ["attributes"])
end

defp process_attributes(%{"attributes" => attributes} = data) do
data
|> Map.merge(attributes)
|> Map.drop(["attributes"])
end

defp process_attributes(data), do: data

## Relationships

defp process_relationships(%{"relationships" => nil} = data) do
Map.drop(data, ["relationships"])
end

defp process_relationships(%{"relationships" => relationships} = data) do
relationships
|> Enum.reduce(%{}, &transform_relationship/2)
|> Map.merge(data)
|> Map.drop(["relationships"])
end

defp process_relationships(data), do: data

defp transform_relationship({key, %{"data" => nil}}, acc) do
Map.put(acc, transform_fields("#{key}-id"), nil)
end

defp transform_relationship({key, %{"data" => %{"id" => id}}}, acc) do
Map.put(acc, transform_fields("#{key}-id"), id)
end

defp transform_relationship({_key, %{"data" => list}}, acc) when is_list(list) do
Enum.reduce(list, acc, fn %{"id" => id, "type" => type}, inner_acc ->
{_val, new_map} = Map.get_and_update(inner_acc, transform_fields("#{type}-id"), &(update_list_relationship(&1, id)))
{_val, new_map} =
Map.get_and_update(
inner_acc,
transform_fields("#{type}-id"),
&update_list_relationship(&1, id)
)

new_map
end)
Expand All @@ -77,17 +91,20 @@ defmodule JSONAPI.Utils.DataToParams do
defp process_included(%{"included" => nil} = incoming) do
Map.drop(incoming, ["included"])
end

defp process_included(%{"included" => included} = incoming) do
included
|> Enum.reduce(incoming, fn (%{"data" => %{"type" => type}} = params, acc) ->
flattened = process(params)
case Map.has_key?(acc, type) do
false -> Map.put(acc, type, [flattened])
true -> Map.update(acc, type, flattened, &([flattened | &1]))
end
|> Enum.reduce(incoming, fn %{"data" => %{"type" => type}} = params, acc ->
flattened = process(params)

case Map.has_key?(acc, type) do
false -> Map.put(acc, type, [flattened])
true -> Map.update(acc, type, flattened, &[flattened | &1])
end
end)
|> Map.drop(["included"])
end

defp process_included(incoming), do: incoming

defp transform_fields(fields) do
Expand Down
1 change: 1 addition & 0 deletions lib/jsonapi/utils/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ defmodule JSONAPI.Utils.String do

@doc false
def field_transformation(nil), do: nil

def field_transformation(transformation) when transformation in @allowed_transformations,
do: transformation
end
20 changes: 11 additions & 9 deletions test/jsonapi/plugs/deserializer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,25 @@ defmodule JSONAPI.DeserializerTest do
end

test "works with basic list of data" do
req_body = Jason.encode!(%{
"data" => [
%{"id" => "1", "type" => "car"},
%{"id" => "2", "type" => "car"}
]
})
req_body =
Jason.encode!(%{
"data" => [
%{"id" => "1", "type" => "car"},
%{"id" => "2", "type" => "car"}
]
})

conn =
Plug.Test.conn("POST", "/", req_body)
|> put_req_header("content-type", @ct)
|> put_req_header("accept", @ct)

result = ExamplePlug.call(conn, [])

assert result.params == [
%{"id" => "1", "type" => "car"},
%{"id" => "2", "type" => "car"}
]
%{"id" => "1", "type" => "car"},
%{"id" => "2", "type" => "car"}
]
end

test "deserializes attribute key names" do
Expand Down
122 changes: 62 additions & 60 deletions test/utils/data_to_params_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ defmodule JSONAPI.DataToParamsTest do
result = JSONAPI.Utils.DataToParams.process(incoming)

assert result == %{
"id" => "1",
"type" => "user",
"foo-bar" => true,
"baz-id" => "2",
"boo-id" => nil
}
"id" => "1",
"type" => "user",
"foo-bar" => true,
"baz-id" => "2",
"boo-id" => nil
}
end

test "converts to many relationship" do
Expand All @@ -56,11 +56,11 @@ defmodule JSONAPI.DataToParamsTest do
result = JSONAPI.Utils.DataToParams.process(incoming)

assert result == %{
"id" => "1",
"type" => "user",
"foo-bar" => true,
"baz-id" => ["2", "3"]
}
"id" => "1",
"type" => "user",
"foo-bar" => true,
"baz-id" => ["2", "3"]
}
end

test "converts polymorphic" do
Expand All @@ -85,12 +85,12 @@ defmodule JSONAPI.DataToParamsTest do
result = JSONAPI.Utils.DataToParams.process(incoming)

assert result == %{
"id" => "1",
"type" => "user",
"foo-bar" => true,
"baz-id" => "2",
"yooper-id" => "3"
}
"id" => "1",
"type" => "user",
"foo-bar" => true,
"baz-id" => "2",
"yooper-id" => "3"
}
end

test "processes single includes" do
Expand Down Expand Up @@ -118,15 +118,17 @@ defmodule JSONAPI.DataToParamsTest do
result = JSONAPI.Utils.DataToParams.process(incoming)

assert result == %{
"friend" => [%{
"name" => "Tara",
"id" => "234",
"type" => "friend"
}],
"id" => "1",
"type" => "user",
"name" => "Jerome"
}
"friend" => [
%{
"name" => "Tara",
"id" => "234",
"type" => "friend"
}
],
"id" => "1",
"type" => "user",
"name" => "Jerome"
}
end

test "processes has many includes" do
Expand Down Expand Up @@ -183,31 +185,31 @@ defmodule JSONAPI.DataToParamsTest do
result = JSONAPI.Utils.DataToParams.process(incoming)

assert result == %{
"friend" => [
%{
"name" => "Wild Bill",
"id" => "0012",
"type" => "friend"
},
%{
"name" => "Tara",
"id" => "234",
"type" => "friend",
"baz-id" => "2",
"boo-id" => nil
}
],
"organization" => [
%{
"title" => "Sr",
"id" => "456",
"type" => "organization"
}
],
"id" => "1",
"type" => "user",
"name" => "Jerome"
}
"friend" => [
%{
"name" => "Wild Bill",
"id" => "0012",
"type" => "friend"
},
%{
"name" => "Tara",
"id" => "234",
"type" => "friend",
"baz-id" => "2",
"boo-id" => nil
}
],
"organization" => [
%{
"title" => "Sr",
"id" => "456",
"type" => "organization"
}
],
"id" => "1",
"type" => "user",
"name" => "Jerome"
}
end

test "processes simple array of data" do
Expand All @@ -221,9 +223,9 @@ defmodule JSONAPI.DataToParamsTest do
result = JSONAPI.Utils.DataToParams.process(incoming)

assert result == [
%{"id" => "1", "type" => "user"},
%{"id" => "2", "type" => "user"}
]
%{"id" => "1", "type" => "user"},
%{"id" => "2", "type" => "user"}
]
end

test "processes empty keys" do
Expand All @@ -240,9 +242,9 @@ defmodule JSONAPI.DataToParamsTest do
result = JSONAPI.Utils.DataToParams.process(incoming)

assert result == %{
"id" => "1",
"type" => "user"
}
"id" => "1",
"type" => "user"
}
end

test "processes empty data" do
Expand All @@ -256,9 +258,9 @@ defmodule JSONAPI.DataToParamsTest do
result = JSONAPI.Utils.DataToParams.process(incoming)

assert result == %{
"id" => "1",
"type" => "user"
}
"id" => "1",
"type" => "user"
}
end

test "processes nil data" do
Expand Down

0 comments on commit 5e14139

Please sign in to comment.