json_ld: solve the list reference problem with an agent

This commit is contained in:
Marcel Otto 2017-03-19 14:51:28 +01:00
parent 2405d7d32b
commit a43205deaa
2 changed files with 57 additions and 33 deletions

View file

@ -132,16 +132,23 @@ defmodule JSON.LD.Flattening do
node_map node_map
end end
else else
# TODO: list a reference! We'll have to rewrite this to work without references append_to_list(list, element)
list = Map.update(list, "@list", [element], fn l -> l ++ [element] end)
node_map node_map
end end
# 5) # 5)
Map.has_key?(element, "@list") -> Map.has_key?(element, "@list") ->
result = %{"@list" => []} {:ok, result_list} = new_list
node_map = generate_node_map(element["@list"], node_map, node_id_map, {node_map, result} =
active_graph, active_subject, active_property, result) try do
{
generate_node_map(element["@list"], node_map, node_id_map,
active_graph, active_subject, active_property, result_list),
get_list(result_list)
}
after
terminate_list(result_list)
end
if node do if node do
update_in(node_map, [active_graph, active_subject, active_property], fn update_in(node_map, [active_graph, active_subject, active_property], fn
nil -> [result] nil -> [result]
@ -204,8 +211,7 @@ defmodule JSON.LD.Flattening do
end) end)
# 6.6.3) TODO: Spec fixme: specs says to add ELEMENT to @list member, should be REFERENCE # 6.6.3) TODO: Spec fixme: specs says to add ELEMENT to @list member, should be REFERENCE
else else
# TODO: list a reference! We'll have to rewrite this to work without references append_to_list(list, reference)
list = Map.update(list, "@list", [reference], fn l -> l ++ [reference] end)
end end
end end
end end
@ -289,4 +295,23 @@ defmodule JSON.LD.Flattening do
defp deep_compare(v, v), do: true defp deep_compare(v, v), do: true
defp deep_compare(_, _), do: false defp deep_compare(_, _), do: false
defp new_list do
Agent.start_link fn -> %{"@list" => []} end
end
defp terminate_list(pid) do
Agent.stop pid
end
defp get_list(pid) do
Agent.get pid, fn list_node -> list_node end
end
defp append_to_list(pid, element) do
Agent.update pid, fn list_node ->
Map.update(list_node, "@list", [element], fn list -> list ++ [element] end)
end
end
end end

View file

@ -211,32 +211,31 @@ defmodule JSON.LD.FlatteningTest do
}] }]
""") """)
}, },
# TODO: @list don't work yet, since the reference-based implementation in the spec is not possible in Elixir "Test Manifest (shortened)" => %{
# "Test Manifest (shortened)" => %{ input: Poison.Parser.parse!("""
# input: Poison.Parser.parse!(""" {
# { "@id": "",
# "@id": "", "http://example/sequence": {"@list": [
# "http://example/sequence": {"@list": [ {
# { "@id": "#t0001",
# "@id": "#t0001", "http://example/name": "Keywords cannot be aliased to other keywords",
# "http://example/name": "Keywords cannot be aliased to other keywords", "http://example/input": {"@id": "error-expand-0001-in.jsonld"}
# "http://example/input": {"@id": "error-expand-0001-in.jsonld"} }
# } ]}
# ]} }
# } """),
# """), output: Poison.Parser.parse!("""
# output: Poison.Parser.parse!(""" [{
# [{ "@id": "",
# "@id": "", "http://example/sequence": [{"@list": [{"@id": "#t0001"}]}]
# "http://example/sequence": [{"@list": [{"@id": "#t0001"}]}] }, {
# }, { "@id": "#t0001",
# "@id": "#t0001", "http://example/input": [{"@id": "error-expand-0001-in.jsonld"}],
# "http://example/input": [{"@id": "error-expand-0001-in.jsonld"}], "http://example/name": [{"@value": "Keywords cannot be aliased to other keywords"}]
# "http://example/name": [{"@value": "Keywords cannot be aliased to other keywords"}] }]
# }] """),
# """), options: %{}
# options: %{} },
# },
"@reverse bnode issue (0045)" => %{ "@reverse bnode issue (0045)" => %{
input: Poison.Parser.parse!(""" input: Poison.Parser.parse!("""
{ {