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
|
||||
|
||||
- `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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -626,7 +626,8 @@ defmodule RDF.DataTest do
|
|||
end
|
||||
end
|
||||
|
||||
test "external RDF.Data protocol implementations" do
|
||||
describe "external RDF.Data protocol implementations" do
|
||||
test "merge/2" do
|
||||
description = EX.S |> EX.p(EX.O2)
|
||||
|
||||
assert RDF.Data.merge(description, %External{}) ==
|
||||
|
@ -642,4 +643,11 @@ defmodule RDF.DataTest do
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue