Handle base URIs without trailing slash or hash in Turtle encoder properly

This commit is contained in:
Marcel Otto 2021-03-04 15:07:28 +01:00
parent 19ff9def6f
commit f9bd3b9476
3 changed files with 27 additions and 13 deletions

View file

@ -14,13 +14,15 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
### Changed
- improvement of the Inspect forms RDF data structures: the content is now
enclosed in angle brackets and indented
- improvement of the Inspect forms of the RDF data structures: the content is
now enclosed in angle brackets and indented
### Fixed
- strings of the form `".0"` and `"0."` weren't recognized as valid XSD float
and double literals
- the Turtle encoder handle base URIs without a trailing slash or hash properly
(no longer raising a warning and ignoring them)
[Compare v0.9.2...HEAD](https://github.com/rdf-elixir/rdf-ex/compare/v0.9.2...HEAD)

View file

@ -102,17 +102,7 @@ defmodule RDF.Turtle.Encoder do
defp base_iri(base_iri, _), do: IRI.coerce_base(base_iri)
defp init_base_iri(nil), do: nil
defp init_base_iri(base_iri) do
base_iri = to_string(base_iri)
if String.ends_with?(base_iri, ~w[/ #]) do
{:ok, base_iri}
else
IO.warn("invalid base_iri: #{base_iri}")
{:bad, base_iri}
end
end
defp init_base_iri(base_iri), do: {:ok, to_string(base_iri)}
defp prefixes(nil, %Graph{prefixes: prefixes}) when not is_nil(prefixes), do: prefixes

View file

@ -107,6 +107,28 @@ defmodule RDF.Turtle.EncoderTest do
<S1>
<p1> <O1> .
"""
base_without_hash = "http://example.com/foo"
assert Turtle.Encoder.encode!(
Graph.new(
[
{
RDF.iri(base_without_hash <> "#S1"),
RDF.iri(base_without_hash <> "#p1"),
RDF.iri(base_without_hash <> "#O1")
}
],
prefixes: %{},
base_iri: base_without_hash
)
) ==
"""
@base <#{base_without_hash}> .
<#S1>
<#p1> <#O1> .
"""
end
test "when a base IRI is given, it has used instead of the base IRI of the given graph" do