Allow namespaces to be given as RDF.Vocabulary.Namespaces in PrefixMaps
This commit is contained in:
parent
86ca2a866c
commit
14f7bb5693
2 changed files with 39 additions and 6 deletions
|
@ -38,10 +38,20 @@ defmodule RDF.PrefixMap do
|
||||||
do: normalize({String.to_atom(prefix), namespace})
|
do: normalize({String.to_atom(prefix), namespace})
|
||||||
|
|
||||||
defp normalize({prefix, namespace}) when is_binary(namespace),
|
defp normalize({prefix, namespace}) when is_binary(namespace),
|
||||||
do: normalize({prefix, IRI.new(namespace)})
|
do: normalize({prefix, IRI.new(namespace)})
|
||||||
|
|
||||||
defp normalize({prefix, _}),
|
defp normalize({prefix, namespace}) when is_atom(namespace) do
|
||||||
do: raise("Invalid prefix on PrefixMap: #{inspect(prefix)}}")
|
if function_exported?(namespace, :__base_iri__, 0) do
|
||||||
|
normalize({prefix, apply(namespace, :__base_iri__, [])})
|
||||||
|
else
|
||||||
|
raise ArgumentError,
|
||||||
|
"Invalid prefix mapping for #{inspect(prefix)}, #{inspect(namespace)} is not a vocabulary namespace"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp normalize({prefix, namespace}),
|
||||||
|
do:
|
||||||
|
raise(ArgumentError, "Invalid prefix mapping: #{inspect(prefix)} => #{inspect(namespace)}")
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Adds a prefix mapping the given `RDF.PrefixMap`.
|
Adds a prefix mapping the given `RDF.PrefixMap`.
|
||||||
|
@ -92,7 +102,8 @@ defmodule RDF.PrefixMap do
|
||||||
{:ok, %__MODULE__{map: Map.merge(map1, map2)}}
|
{:ok, %__MODULE__{map: Map.merge(map1, map2)}}
|
||||||
else
|
else
|
||||||
conflicts ->
|
conflicts ->
|
||||||
{:error, "conflicting prefix mappings: #{conflicts |> Stream.map(&inspect/1) |> Enum.join(", ")}"}
|
{:error,
|
||||||
|
"conflicting prefix mappings: #{conflicts |> Stream.map(&inspect/1) |> Enum.join(", ")}"}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,11 @@ defmodule RDF.PrefixMapTest do
|
||||||
ex3: @ex_ns3
|
ex3: @ex_ns3
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
@example4 %PrefixMap{map: %{
|
||||||
|
ex1: @ex_ns1,
|
||||||
|
ex: RDF.iri(EX.__base_iri__)
|
||||||
|
}}
|
||||||
|
|
||||||
test "new/0" do
|
test "new/0" do
|
||||||
assert PrefixMap.new() == %PrefixMap{}
|
assert PrefixMap.new() == %PrefixMap{}
|
||||||
end
|
end
|
||||||
|
@ -38,6 +43,13 @@ defmodule RDF.PrefixMapTest do
|
||||||
ex2: "http://example.com/bar#"
|
ex2: "http://example.com/bar#"
|
||||||
) == @example2
|
) == @example2
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "when the IRI namespace is given as a RDF.Vocabulary.Namespace" do
|
||||||
|
assert PrefixMap.new(
|
||||||
|
ex1: "http://example.com/foo/",
|
||||||
|
ex: EX
|
||||||
|
) == @example4
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "add/3" do
|
describe "add/3" do
|
||||||
|
@ -45,14 +57,24 @@ defmodule RDF.PrefixMapTest do
|
||||||
assert PrefixMap.add(@example1, :ex2, @ex_ns2) == {:ok, @example2}
|
assert PrefixMap.add(@example1, :ex2, @ex_ns2) == {:ok, @example2}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "with the prefix is given as a string" do
|
test "when the prefix is given as a string" do
|
||||||
assert PrefixMap.add(@example1, "ex2", @ex_ns2) == {:ok, @example2}
|
assert PrefixMap.add(@example1, "ex2", @ex_ns2) == {:ok, @example2}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "with the IRI namespace is given as a string" do
|
test "when the IRI namespace is given as a string" do
|
||||||
assert PrefixMap.add(@example1, :ex2, "http://example.com/bar#") == {:ok, @example2}
|
assert PrefixMap.add(@example1, :ex2, "http://example.com/bar#") == {:ok, @example2}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "when the IRI namespace is given as a RDF.Vocabulary.Namespace" do
|
||||||
|
assert PrefixMap.add(@example1, :ex, EX) == {:ok, @example4}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "when the IRI namespace is given as an atom" do
|
||||||
|
assert_raise ArgumentError, "Invalid prefix mapping for :ex, :foo is not a vocabulary namespace", fn ->
|
||||||
|
PrefixMap.add(@example1, :ex, :foo)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
test "when a mapping of the given prefix to the same namespace already exists" do
|
test "when a mapping of the given prefix to the same namespace already exists" do
|
||||||
assert PrefixMap.add(@example2, :ex2, "http://example.com/bar#") == {:ok, @example2}
|
assert PrefixMap.add(@example2, :ex2, "http://example.com/bar#") == {:ok, @example2}
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue