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

Fix nesting for includes #87

Merged
merged 8 commits into from
Jan 9, 2018

Conversation

jlvallelonga
Copy link
Contributor

@jlvallelonga jlvallelonga commented Nov 27, 2017

fixes nested includes. query strings like this should now include data as expected:
?include=user.company.industry.tags

@jeregrine
Copy link

cc @doomspork if I could get a second set of eyes on this

@doomspork
Copy link
Member

@jeregrine I'll take a peek today 👍

Copy link
Member

@doomspork doomspork left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @jlvallelonga 😁

@@ -83,7 +83,14 @@ defmodule JSONAPI.Serializer do
if {rel_view, :include} == valid_include_view && is_data_loaded?(rel_data) do
rel_query_includes =
if is_list(query_includes) do
Keyword.get(query_includes, key, [])
Enum.map(query_includes, fn include ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we refactor this entire pipechain into a Enum.reduce/3?

Enum.reduce(query_includes, [], fn 
  {_key, value}, acc -> acc ++ [value]
  _, acc -> acc
end)

base = view.relationships()
Keyword.get(base, include)
defp get_includes(view, query_includes) do
get_default_includes(view) ++ get_query_includes(view, query_includes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid pipes for single function calls.

includes = get_default_includes(view) ++ get_query_includes(view, query_includes)
Enum.uniq(includes)

defp get_default_includes(view) do
rels = view.relationships()
default_includes = rels |> Enum.filter(fn {k, v} ->
case v do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to use a case here, match on the function head 👍


defp get_query_includes(view, query_includes) do
rels = view.relationships()
Enum.map(query_includes, fn include ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is an opportunity to refactor this. We could probably move to Enum.reduce/3 and we can eliminate the case by matching on the function signature instead.

@@ -9,7 +9,7 @@ defmodule JSONAPITest do
def type, do: "mytype"
def relationships do
[author: {JSONAPITest.UserView, :include},
other_user: {JSONAPITest.UserView, :include}]
other_user: JSONAPITest.UserView]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will these changes result in a breaking change? If not, let's leave the existing tests alone and add new ones to cover these changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this changes the functionality. Although, I think it fixes the original intended functionality of :include meaning that the relationship will be included by default. I'm basing this on this bit from the readme:

  def relationships do
    # The post's author will be included by default
    [author: {MyApp.UserView, :include},
     comments: MyApp.CommentView]
  end

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

@jeregrine jeregrine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @doomspork's suggestions. Especially re-testing, when adding new functionality its best to leave existing tests the same and creating new ones. This will help cover backwards compatibility.

@StareIntoTheBeard
Copy link
Contributor

@doomspork @jeregrine @jlvallelonga bump, any other thoughts on this one? What's needed to get this merged in?

@doomspork
Copy link
Member

Sorry @jlvallelonga, I've been away on a business trip and didn't have too much time to go through GH. Since these are a breaking change, I'm going to have to spend some time looking through them closer. That said, if @jeregrine wants to go forward with it 👍

@doomspork doomspork dismissed their stale review December 9, 2017 17:40

No longer applicable.

@doomspork doomspork merged commit 7f91eba into beam-community:master Jan 9, 2018
@doomspork
Copy link
Member

Thank you @jlvallelonga! @StareIntoTheBeard I will be cutting a new release shortly 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants