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 c:JSONAPI.View.get_field/3 #273

Merged
merged 4 commits into from
Dec 28, 2022
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
30 changes: 27 additions & 3 deletions lib/jsonapi/view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ defmodule JSONAPI.View do
In order to use [sparse fieldsets](https://jsonapi.org/format/#fetching-sparse-fieldsets)
you must include the `JSONAPI.QueryParser` plug.

If you want to fetch fields from the given data *dynamically*, you can use the
`c:get_field/3` callback.

defmodule UserView do
use JSONAPI.View

def fields, do: [:id, :username, :email]

def type, do: "user"

def get_field(field, data, _conn) do
Map.fetch!(data, field)
end
end

## Relationships

Currently the relationships callback expects that a map is returned
Expand Down Expand Up @@ -127,6 +142,7 @@ defmodule JSONAPI.View do
@callback attributes(data(), Conn.t() | nil) :: map()
@callback id(data()) :: resource_id() | nil
@callback fields() :: [field()]
@callback get_field(field(), data(), Conn.t()) :: any()
@callback hidden(data()) :: [field()]
@callback links(data(), Conn.t()) :: links()
@callback meta(data(), Conn.t()) :: meta() | nil
Expand All @@ -141,6 +157,8 @@ defmodule JSONAPI.View do
@callback url_for_rel(term(), String.t(), Conn.t() | nil) :: String.t()
@callback visible_fields(data(), Conn.t() | nil) :: list(atom)

@optional_callbacks [get_field: 3]

defmacro __using__(opts \\ []) do
{type, opts} = Keyword.pop(opts, :type)
{namespace, opts} = Keyword.pop(opts, :namespace)
Expand Down Expand Up @@ -168,9 +186,15 @@ defmodule JSONAPI.View do

Enum.reduce(visible_fields, %{}, fn field, intermediate_map ->
value =
case function_exported?(__MODULE__, field, 2) do
true -> apply(__MODULE__, field, [data, conn])
false -> Map.get(data, field)
cond do
function_exported?(__MODULE__, field, 2) ->
apply(__MODULE__, field, [data, conn])

function_exported?(__MODULE__, :get_field, 3) ->
apply(__MODULE__, :get_field, [field, data, conn])

true ->
Map.get(data, field)
end

Map.put(intermediate_map, field, value)
Expand Down
24 changes: 24 additions & 0 deletions test/jsonapi/view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ defmodule JSONAPI.ViewTest do
use JSONAPI.View, type: "cars", namespace: ""
end

defmodule DynamicView do
use JSONAPI.View

def type, do: "dyns"

def fields, do: [:static_fun, :static_field, :dynamic_1, :dynamic_2]

def static_fun(_data, _conn), do: "static_fun/2"

def get_field(field, _data, _conn), do: "#{field}!"
end

setup do
Application.put_env(:jsonapi, :field_transformation, :underscore)
Application.put_env(:jsonapi, :namespace, "/other-api")
Expand Down Expand Up @@ -290,4 +302,16 @@ defmodule JSONAPI.ViewTest do

assert %{body: "Chunky"} == PostView.attributes(data, conn)
end

test "attributes/2 can return dynamic fields" do
data = %{static_field: "static_field from the map"}
conn = %Plug.Conn{assigns: %{jsonapi_query: %JSONAPI.Config{}}}

assert %{
dynamic_1: "dynamic_1!",
dynamic_2: "dynamic_2!",
static_field: "static_field!",
static_fun: "static_fun/2"
} == DynamicView.attributes(data, conn)
end
end