我正在使用GitHub GraphQL API,需要过滤掉一些响应数据,但它是如此深层嵌套。下面是我正在处理的响应的剪切示例。
case "text":
reader.next();
if(reader != null && !"".equals(reader.toString()) && reader.hasText())
writer1.println("Text: " + reader.getText());
break;
我想要实现的是能够根据他们的登录过滤评论者。我怎样才能过滤掉节点中的一些地图以达到以下结果?
%{"name" => "repo",
"pullRequests" => %{
"nodes" => [%{
"reviews" => %{
"nodes" => [
%{"author" => %{"login" => "FOO"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "BAR"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "BAR"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "BAR"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "BAR"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "FOO"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "FOO"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "BAR"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "BAR"}, "state" => "COMMENTED"}
]
}
}]
}
}
答案 0 :(得分:2)
这是Bill_no
的完美用例:
update_in
输出:
map = %{"name" => "repo",
"pullRequests" => %{
"nodes" => [%{
"reviews" => %{
"nodes" => [
%{"author" => %{"login" => "FOO"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "BAR"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "BAR"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "BAR"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "BAR"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "FOO"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "FOO"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "BAR"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "BAR"}, "state" => "COMMENTED"}
]
}
}]
}
}
map2 = update_in(map, ["pullRequests", "nodes", Access.all(), "reviews", "nodes"], fn nodes ->
for %{"author" => %{"login" => "FOO"}} = node <- nodes, do: node
end)
IO.inspect map2
%{"name" => "repo",
"pullRequests" => %{"nodes" => [%{"reviews" => %{"nodes" => [%{"author" => %{"login" => "FOO"},
"state" => "COMMENTED"},
%{"author" => %{"login" => "FOO"}, "state" => "COMMENTED"},
%{"author" => %{"login" => "FOO"}, "state" => "COMMENTED"}]}}]}}
让我们在进行更新时迭代列表的所有元素。这意味着如果您的顶级“节点”有多个项目,则所有项目都将根据我们的逻辑进行过滤。
注意:您的代码语法无效,所以我假设您打算将Access.all()
包装在地图中。