Make RDF.Data.equal?/2 implementations commutative
This commit is contained in:
parent
af5cc26d7f
commit
706de8b954
4 changed files with 47 additions and 13 deletions
|
@ -9,8 +9,8 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- `RDF.Data.merge/3` is now commutative, i.e. structs which implement the
|
- `RDF.Data.merge/2` and `RDF.Data.equal?/2` are now commutative, i.e. structs
|
||||||
`RDF.Data` protocol can be given also as the second argument
|
which implement the `RDF.Data` protocol can be given also as the second argument
|
||||||
(previously custom structs with `RDF.Data` protocol implementations always
|
(previously custom structs with `RDF.Data` protocol implementations always
|
||||||
had to be given as the first argument)
|
had to be given as the first argument)
|
||||||
- several performance improvements
|
- several performance improvements
|
||||||
|
|
|
@ -237,6 +237,14 @@ defimpl RDF.Data, for: RDF.Description do
|
||||||
RDF.Data.equal?(dataset, description)
|
RDF.Data.equal?(dataset, description)
|
||||||
end
|
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
|
def equal?(_, _), do: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -331,6 +339,14 @@ defimpl RDF.Data, for: RDF.Graph do
|
||||||
def equal?(graph, %Dataset{} = dataset),
|
def equal?(graph, %Dataset{} = dataset),
|
||||||
do: RDF.Data.equal?(dataset, graph)
|
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
|
def equal?(_, _), do: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -433,5 +449,13 @@ defimpl RDF.Data, for: RDF.Dataset do
|
||||||
)
|
)
|
||||||
end
|
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
|
def equal?(_, _), do: false
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,6 +27,8 @@ defmodule External do
|
||||||
def values(_external, _opts \\ []), do: nil
|
def values(_external, _opts \\ []), do: nil
|
||||||
def map(_external, _fun), 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
|
||||||
end
|
end
|
||||||
|
|
|
@ -626,20 +626,28 @@ defmodule RDF.DataTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "external RDF.Data protocol implementations" do
|
describe "external RDF.Data protocol implementations" do
|
||||||
description = EX.S |> EX.p(EX.O2)
|
test "merge/2" do
|
||||||
|
description = EX.S |> EX.p(EX.O2)
|
||||||
|
|
||||||
assert RDF.Data.merge(description, %External{}) ==
|
assert RDF.Data.merge(description, %External{}) ==
|
||||||
Description.add(description, External.data())
|
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{}) ==
|
assert RDF.Data.merge(graph, %External{}) ==
|
||||||
Graph.add(graph, External.data())
|
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{}) ==
|
assert RDF.Data.merge(dataset, %External{}) ==
|
||||||
Dataset.add(dataset, External.data())
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue