Add alias base_iri for the base option of the RDF.Turtle.Encoder
This commit is contained in:
parent
14b1dc5825
commit
58d21a3405
3 changed files with 19 additions and 18 deletions
|
@ -17,9 +17,9 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
|
|||
|
||||
### Changed
|
||||
|
||||
- `RDF.Turtle.Decoder` saves the base IRI now
|
||||
- `RDF.Turtle.Decoder` saves the base IRI in the `RDF.Graph` now
|
||||
- `RDF.Turtle.Encoder` now takes the base IRI to be serialized from the graph when
|
||||
no base IRI is given with the `base` option
|
||||
no base IRI is given with the `base` option or its new alias `base_iri`
|
||||
|
||||
|
||||
[Compare v0.6.1...HEAD](https://github.com/marcelotto/rdf-ex/compare/v0.6.1...HEAD)
|
||||
|
|
|
@ -28,8 +28,10 @@ defmodule RDF.Turtle.Encoder do
|
|||
|
||||
@impl RDF.Serialization.Encoder
|
||||
def encode(data, opts \\ []) do
|
||||
with base = Keyword.get(opts, :base) |> base_iri(data) |> init_base(),
|
||||
prefixes = Keyword.get(opts, :prefixes) |> prefixes(data) |> init_prefixes(),
|
||||
with base = Keyword.get(opts, :base, Keyword.get(opts, :base_iri))
|
||||
|> base_iri(data) |> init_base_iri(),
|
||||
prefixes = Keyword.get(opts, :prefixes)
|
||||
|> prefixes(data) |> init_prefixes(),
|
||||
{:ok, state} = State.start_link(data, base, prefixes) do
|
||||
try do
|
||||
State.preprocess(state)
|
||||
|
@ -48,16 +50,15 @@ defmodule RDF.Turtle.Encoder do
|
|||
defp base_iri(nil, %RDF.Graph{base_iri: base_iri}), do: base_iri
|
||||
defp base_iri(base_iri, _), do: RDF.iri(base_iri)
|
||||
|
||||
defp init_base(nil), do: nil
|
||||
defp init_base_iri(nil), do: nil
|
||||
|
||||
defp init_base(base) do
|
||||
with base = to_string(base) do
|
||||
if String.ends_with?(base, ~w[/ #]) do
|
||||
{:ok, base}
|
||||
else
|
||||
IO.warn("invalid base: #{base}")
|
||||
{:bad, base}
|
||||
end
|
||||
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
|
||||
|
||||
|
|
|
@ -97,7 +97,7 @@ defmodule RDF.Turtle.EncoderTest do
|
|||
|
||||
test "when a base IRI is given, it has used instead of the base IRI of the given graph" do
|
||||
assert Turtle.Encoder.encode!(Graph.new([{EX.S1, EX.p1, EX.O1}], prefixes: %{},
|
||||
base_iri: EX.other), base: EX.__base_iri__) ==
|
||||
base_iri: EX.other), base_iri: EX.__base_iri__) ==
|
||||
"""
|
||||
@base <#{to_string(EX.__base_iri__)}> .
|
||||
<S1>
|
||||
|
@ -164,7 +164,7 @@ defmodule RDF.Turtle.EncoderTest do
|
|||
{EX.S2, RDF.type, RDFS.Class},
|
||||
{EX.S3, RDF.type, RDF.Property},
|
||||
]),
|
||||
base: EX.__base_iri__,
|
||||
base_iri: EX.__base_iri__,
|
||||
prefixes: %{
|
||||
rdf: RDF.__base_iri__,
|
||||
rdfs: RDFS.__base_iri__,
|
||||
|
@ -240,7 +240,7 @@ defmodule RDF.Turtle.EncoderTest do
|
|||
"relative IRIs with base" => %{
|
||||
input: "<http://a/b> <http://a/c> <http://a/d> .",
|
||||
matches: [ ~r(@base\s+<http://a/>\s+\.), ~r(<b>\s+<c>\s+<d>\s+\.)m],
|
||||
base: "http://a/"
|
||||
base_iri: "http://a/"
|
||||
},
|
||||
"pname IRIs with prefix" => %{
|
||||
input: "<http://example.com/b> <http://example.com/c> <http://example.com/d> .",
|
||||
|
@ -676,12 +676,12 @@ defmodule RDF.Turtle.EncoderTest do
|
|||
|
||||
defp assert_serialization(graph, opts) do
|
||||
with prefixes = Keyword.get(opts, :prefixes, %{}),
|
||||
base = Keyword.get(opts, :base),
|
||||
base_iri = Keyword.get(opts, :base_iri),
|
||||
matches = Keyword.get(opts, :matches, []),
|
||||
neg_matches = Keyword.get(opts, :neg_matches, [])
|
||||
do
|
||||
assert {:ok, serialized} =
|
||||
Turtle.write_string(graph, prefixes: prefixes, base: base)
|
||||
Turtle.write_string(graph, prefixes: prefixes, base_iri: base_iri)
|
||||
|
||||
matches
|
||||
|> Stream.map(fn
|
||||
|
|
Loading…
Reference in a new issue