Fix some of the BlankNode representations to be Turtle-serializable

This commit is contained in:
Marcel Otto 2020-11-01 04:03:21 +01:00
parent 3ca4207a14
commit 8e74e9a9d2
7 changed files with 46 additions and 19 deletions

View file

@ -18,6 +18,10 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
- more compact Inspect form for `RDF.PrefixMap`
- the `RDF.Turtle.Encoder` accepts `RDF.Vocabulary.Namespace` modules as `base`
### Fixed
- `RDF.BlankNode`s based on refs weren't serializable to Turtle
[Compare v0.9.0...HEAD](https://github.com/rdf-elixir/rdf-ex/compare/v0.9.0...HEAD)

View file

@ -31,14 +31,14 @@ defmodule RDF.BlankNode do
@spec new(reference | String.t() | atom | integer) :: t
def new(value)
def new(value) when is_binary(value),
do: %__MODULE__{value: value}
def new(string) when is_binary(string), do: %__MODULE__{value: string}
def new(atom) when is_atom(atom), do: atom |> to_string() |> new()
def new(integer) when is_integer(integer), do: new("b#{integer}")
def new(value) when is_reference(value),
do: value |> :erlang.ref_to_list() |> to_string |> String.replace(~r/\<|\>/, "") |> new
def new(value) when is_atom(value) or is_integer(value),
do: value |> to_string |> new
def new(ref) when is_reference(ref) do
"#Ref<" <> value = ref |> :erlang.ref_to_list() |> to_string()
value |> String.trim_trailing(">") |> new()
end
@doc """
Returns the internal string representation of a blank node.

View file

@ -15,7 +15,7 @@ defmodule RDF.BlankNodeTest do
end
test "with a integer" do
assert BlankNode.new(42) == %BlankNode{value: "42"}
assert BlankNode.new(42) == %BlankNode{value: "b42"}
end
test "with a ref" do
@ -23,4 +23,27 @@ defmodule RDF.BlankNodeTest do
assert is_binary(value)
end
end
test "internal representation are valid Turtle blank node" do
[
BlankNode.new(),
BlankNode.new("foo"),
BlankNode.new(:foo),
BlankNode.new(42),
BlankNode.new(-42),
BlankNode.new(make_ref())
]
|> Enum.each(fn bnode ->
assert {:ok, graph} =
[
{EX.S1, EX.p1(), bnode},
{EX.S2, EX.p1(), bnode}
]
|> Graph.new()
|> RDF.Turtle.write_string!()
|> RDF.Turtle.read_string()
assert Graph.triple_count(graph) == 2
end)
end
end

View file

@ -62,7 +62,7 @@ defmodule RDF.NQuads.EncoderTest do
])
) ==
"""
<http://example.org/#S1> <http://example.org/#p1> _:1 .
<http://example.org/#S1> <http://example.org/#p1> _:b1 .
<http://example.org/#S1> <http://example.org/#p1> _:bar .
<http://example.org/#S1> <http://example.org/#p1> _:foo .
"""
@ -117,7 +117,7 @@ defmodule RDF.NQuads.EncoderTest do
])
) ==
"""
<http://example.org/#S1> <http://example.org/#p1> _:1 .
<http://example.org/#S1> <http://example.org/#p1> _:b1 .
<http://example.org/#S1> <http://example.org/#p1> _:bar .
<http://example.org/#S1> <http://example.org/#p1> _:foo <http://example.org/#G> .
"""

View file

@ -62,7 +62,7 @@ defmodule RDF.NTriples.EncoderTest do
])
) ==
"""
<http://example.org/#S1> <http://example.org/#p1> _:1 .
<http://example.org/#S1> <http://example.org/#p1> _:b1 .
<http://example.org/#S1> <http://example.org/#p1> _:bar .
<http://example.org/#S1> <http://example.org/#p1> _:foo .
"""

View file

@ -247,14 +247,14 @@ defmodule RDF.Query.BuilderTest do
test "element count > 3" do
assert Builder.path([EX.s(), EX.p1(), EX.p2(), EX.o()]) ==
ok_bgp_struct([
{EX.s(), EX.p1(), RDF.bnode("0")},
{RDF.bnode("0"), EX.p2(), EX.o()}
{EX.s(), EX.p1(), RDF.bnode("b0")},
{RDF.bnode("b0"), EX.p2(), EX.o()}
])
assert Builder.path([:s?, :p1?, :p2?, :o?]) ==
ok_bgp_struct([
{:s, :p1, RDF.bnode("0")},
{RDF.bnode("0"), :p2, :o}
{:s, :p1, RDF.bnode("b0")},
{RDF.bnode("b0"), :p2, :o}
])
end
@ -280,8 +280,8 @@ defmodule RDF.Query.BuilderTest do
assert Builder.path([EX.s(), :p1, :p2, EX.o()], context: property_map) ==
ok_bgp_struct([
{EX.s(), EX.p1(), RDF.bnode("0")},
{RDF.bnode("0"), EX.p2(), EX.o()}
{EX.s(), EX.p1(), RDF.bnode("b0")},
{RDF.bnode("b0"), EX.p2(), EX.o()}
])
assert Builder.path([EX.s(), :p1, :p2, :o?], context: property_map, with_elements: true) ==

View file

@ -177,10 +177,10 @@ defmodule RDF.Turtle.EncoderTest do
) ==
"""
<http://example.org/#S1>
<http://example.org/#p1> _:1, _:bar, _:foo .
<http://example.org/#p1> _:b1, _:bar, _:foo .
<http://example.org/#S2>
<http://example.org/#p1> _:1, _:bar, _:foo .
<http://example.org/#p1> _:b1, _:bar, _:foo .
"""
end