Handle conflicting terms for the same IRI in a PropertyMap

This commit is contained in:
Marcel Otto 2020-10-11 02:35:58 +02:00
parent 95a69d9007
commit 875908eb39
2 changed files with 35 additions and 5 deletions

View file

@ -71,9 +71,13 @@ defmodule RDF.PropertyMap do
do_set(property_map, op, term, iri, Map.get(property_map.iris, term)) do_set(property_map, op, term, iri, Map.get(property_map.iris, term))
end end
defp do_set(property_map, _, _, iri, iri), do: {:ok, property_map} defp do_set(property_map, op, term, new_iri, old_iri) do
do_set(property_map, op, term, new_iri, old_iri, Map.get(property_map.terms, new_iri))
end
defp do_set(property_map, _, term, iri, nil) do defp do_set(property_map, _, _, iri, iri, _), do: {:ok, property_map}
defp do_set(property_map, _, term, iri, nil, nil) do
{:ok, {:ok,
%__MODULE__{ %__MODULE__{
property_map property_map
@ -82,13 +86,23 @@ defmodule RDF.PropertyMap do
}} }}
end end
defp do_set(_context, :add, term, new_iri, old_iri) do defp do_set(_context, :add, term, new_iri, old_iri, nil) do
{:error, "conflicting mapping for #{term}: #{new_iri}; already mapped to #{old_iri}"} {:error, "conflicting mapping for #{term}: #{new_iri}; already mapped to #{old_iri}"}
end end
defp do_set(property_map, :put, term, new_iri, old_iri) do defp do_set(_context, :add, term, iri, _, old_term) do
{:error,
"conflicting mapping for #{term}: #{iri}; IRI already mapped to #{inspect(old_term)}"}
end
defp do_set(property_map, :put, term, new_iri, old_iri, nil) do
%__MODULE__{property_map | terms: Map.delete(property_map.terms, old_iri)} %__MODULE__{property_map | terms: Map.delete(property_map.terms, old_iri)}
|> do_set(:put, term, new_iri, nil) |> do_set(:put, term, new_iri, nil, nil)
end
defp do_set(property_map, :put, term, new_iri, old_iri, old_term) do
%__MODULE__{property_map | iris: Map.delete(property_map.iris, old_term)}
|> do_set(:put, term, new_iri, old_iri, nil)
end end
def delete(%__MODULE__{} = property_map, term) do def delete(%__MODULE__{} = property_map, term) do

View file

@ -118,6 +118,12 @@ defmodule RDF.PropertyMapTest do
{:error, {:error,
"conflicting mapping for foo: http://example.com/test/other; already mapped to http://example.com/test/foo"} "conflicting mapping for foo: http://example.com/test/other; already mapped to http://example.com/test/foo"}
end end
test "when another term mapping to the IRI exists" do
assert PropertyMap.add(@example_property_map, other: ~I<http://example.com/test/foo>) ==
{:error,
"conflicting mapping for other: http://example.com/test/foo; IRI already mapped to :foo"}
end
end end
describe "put/2" do describe "put/2" do
@ -150,6 +156,16 @@ defmodule RDF.PropertyMapTest do
quux: EX.quux() quux: EX.quux()
) )
end end
test "when another term mapping to the IRI exists" do
{:ok, expected_result} =
@example_property_map
|> PropertyMap.delete(:foo)
|> PropertyMap.add(other: ~I<http://example.com/test/foo>)
assert PropertyMap.put(@example_property_map, other: ~I<http://example.com/test/foo>) ==
expected_result
end
end end
describe "delete/2" do describe "delete/2" do