Make RDF.Data.equal?/2 implementations commutative

This commit is contained in:
Marcel Otto 2022-05-17 00:34:26 +02:00
parent af5cc26d7f
commit 706de8b954
4 changed files with 47 additions and 13 deletions

View file

@ -9,8 +9,8 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
### Changed
- `RDF.Data.merge/3` is now commutative, i.e. structs which implement the
`RDF.Data` protocol can be given also as the second argument
- `RDF.Data.merge/2` and `RDF.Data.equal?/2` are now commutative, i.e. structs
which implement the `RDF.Data` protocol can be given also as the second argument
(previously custom structs with `RDF.Data` protocol implementations always
had to be given as the first argument)
- several performance improvements

View file

@ -237,6 +237,14 @@ defimpl RDF.Data, for: RDF.Description do
RDF.Data.equal?(dataset, description)
end
def equal?(description, %_{} = other) do
if RDF.Data.impl_for(other) do
RDF.Data.equal?(other, description)
else
raise ArgumentError, "no RDF.Data implementation found for #{inspect(other)}"
end
end
def equal?(_, _), do: false
end
@ -331,6 +339,14 @@ defimpl RDF.Data, for: RDF.Graph do
def equal?(graph, %Dataset{} = dataset),
do: RDF.Data.equal?(dataset, graph)
def equal?(graph, %_{} = other) do
if RDF.Data.impl_for(other) do
RDF.Data.equal?(other, graph)
else
raise ArgumentError, "no RDF.Data implementation found for #{inspect(other)}"
end
end
def equal?(_, _), do: false
end
@ -433,5 +449,13 @@ defimpl RDF.Data, for: RDF.Dataset do
)
end
def equal?(dataset, %_{} = other) do
if RDF.Data.impl_for(other) do
RDF.Data.equal?(other, dataset)
else
raise ArgumentError, "no RDF.Data implementation found for #{inspect(other)}"
end
end
def equal?(_, _), do: false
end

View file

@ -27,6 +27,8 @@ defmodule External do
def values(_external, _opts \\ []), do: nil
def map(_external, _fun), do: nil
def equal?(_, _), do: false
def equal?(%External{}, data) do
RDF.Data.equal?(data, RDF.graph(External.data()))
end
end
end

View file

@ -626,20 +626,28 @@ defmodule RDF.DataTest do
end
end
test "external RDF.Data protocol implementations" do
description = EX.S |> EX.p(EX.O2)
describe "external RDF.Data protocol implementations" do
test "merge/2" do
description = EX.S |> EX.p(EX.O2)
assert RDF.Data.merge(description, %External{}) ==
Description.add(description, External.data())
assert RDF.Data.merge(description, %External{}) ==
Description.add(description, External.data())
graph = RDF.graph({EX.S, EX.p(), EX.O2})
graph = RDF.graph({EX.S, EX.p(), EX.O2})
assert RDF.Data.merge(graph, %External{}) ==
Graph.add(graph, External.data())
assert RDF.Data.merge(graph, %External{}) ==
Graph.add(graph, External.data())
dataset = RDF.dataset({EX.S, EX.p(), EX.O2})
dataset = RDF.dataset({EX.S, EX.p(), EX.O2})
assert RDF.Data.merge(dataset, %External{}) ==
Dataset.add(dataset, External.data())
assert RDF.Data.merge(dataset, %External{}) ==
Dataset.add(dataset, External.data())
end
test "equal/2" do
assert EX.S |> EX.p(42) |> RDF.Data.equal?(%External{}) == true
assert {EX.S, EX.p(), 42} |> RDF.graph() |> RDF.Data.equal?(%External{}) == true
assert {EX.S, EX.p(), 42} |> RDF.dataset() |> RDF.Data.equal?(%External{}) == true
end
end
end