Fix dialyzer warning

This commit is contained in:
Marcel Otto 2020-11-02 15:59:14 +01:00
parent 8d98461e0b
commit e3454f73ce
5 changed files with 27 additions and 15 deletions

View file

@ -4,7 +4,7 @@ defmodule RDF.Serialization.Encoder do
`RDF.Serialization` format.
"""
alias RDF.{Dataset, Graph}
alias RDF.{Dataset, Graph, Description}
@doc """
Encodes a `RDF.Graph` or `RDF.Dataset`.
@ -12,7 +12,8 @@ defmodule RDF.Serialization.Encoder do
It returns an `{:ok, string}` tuple, with `string` being the serialized
`RDF.Graph` or `RDF.Dataset`, or `{:error, reason}` if an error occurs.
"""
@callback encode(Graph.t() | Dataset.t(), keyword | map) :: {:ok, String.t()} | {:error, any}
@callback encode(Description.t() | Graph.t() | Dataset.t(), keyword | map) ::
{:ok, String.t()} | {:error, any}
@doc """
Encodes a `RDF.Graph` or `RDF.Dataset`.
@ -22,7 +23,7 @@ defmodule RDF.Serialization.Encoder do
Note: The `__using__` macro automatically provides an overridable default
implementation based on the non-bang `encode` function.
"""
@callback encode!(Graph.t() | Dataset.t(), keyword | map) :: String.t()
@callback encode!(Description.t() | Graph.t() | Dataset.t(), keyword | map) :: String.t()
defmacro __using__(_) do
quote bind_quoted: [], unquote: true do
@ -30,7 +31,7 @@ defmodule RDF.Serialization.Encoder do
@impl unquote(__MODULE__)
@dialyzer {:nowarn_function, encode!: 2}
@spec encode!(Graph.t() | Dataset.t(), keyword) :: String.t()
@spec encode!(Description.t() | Graph.t() | Dataset.t(), keyword) :: String.t()
def encode!(data, opts \\ []) do
case encode(data, opts) do
{:ok, data} -> data

View file

@ -28,7 +28,7 @@ defmodule RDF.Serialization.Format do
`decoder/0` functions in your `RDF.Serialization.Format` module.
"""
alias RDF.{Dataset, Graph}
alias RDF.{Dataset, Graph, Description}
@doc """
An IRI of the serialization format.
@ -99,19 +99,21 @@ defmodule RDF.Serialization.Format do
def read_file!(file, opts \\ []),
do: RDF.Serialization.Reader.read_file!(decoder(), file, opts)
@spec write_string(Graph.t() | Dataset.t(), keyword) :: {:ok, String.t()} | {:error, any}
@spec write_string(Description.t() | Graph.t() | Dataset.t(), keyword) ::
{:ok, String.t()} | {:error, any}
def write_string(data, opts \\ []),
do: RDF.Serialization.Writer.write_string(encoder(), data, opts)
@spec write_string!(Graph.t() | Dataset.t(), keyword) :: String.t()
@spec write_string!(Description.t() | Graph.t() | Dataset.t(), keyword) :: String.t()
def write_string!(data, opts \\ []),
do: RDF.Serialization.Writer.write_string!(encoder(), data, opts)
@spec write_file(Graph.t() | Dataset.t(), Path.t(), keyword) :: :ok | {:error, any}
@spec write_file(Description.t() | Graph.t() | Dataset.t(), Path.t(), keyword) ::
:ok | {:error, any}
def write_file(data, path, opts \\ []),
do: RDF.Serialization.Writer.write_file(encoder(), data, path, opts)
@spec write_file!(Graph.t() | Dataset.t(), Path.t(), keyword) :: :ok
@spec write_file!(Description.t() | Graph.t() | Dataset.t(), Path.t(), keyword) :: :ok
def write_file!(data, path, opts \\ []),
do: RDF.Serialization.Writer.write_file!(encoder(), data, path, opts)

View file

@ -7,7 +7,7 @@ defmodule RDF.Serialization.Writer do
use the proper `RDF.Serialization.Encoder` module.
"""
alias RDF.{Dataset, Graph}
alias RDF.{Dataset, Graph, Description}
@doc """
Encodes and writes a graph or dataset to a string.
@ -15,7 +15,7 @@ defmodule RDF.Serialization.Writer do
It returns an `{:ok, string}` tuple, with `string` being the serialized graph or
dataset, or `{:error, reason}` if an error occurs.
"""
@spec write_string(module, Graph.t() | Dataset.t(), keyword) ::
@spec write_string(module, Description.t() | Graph.t() | Dataset.t(), keyword) ::
{:ok, String.t()} | {:error, any}
def write_string(encoder, data, opts \\ []) do
encoder.encode(data, opts)
@ -26,7 +26,7 @@ defmodule RDF.Serialization.Writer do
As opposed to `write_string`, it raises an exception if an error occurs.
"""
@spec write_string!(module, Graph.t() | Dataset.t(), keyword) :: String.t()
@spec write_string!(module, Description.t() | Graph.t() | Dataset.t(), keyword) :: String.t()
def write_string!(encoder, data, opts \\ []) do
encoder.encode!(data, opts)
end
@ -43,7 +43,8 @@ defmodule RDF.Serialization.Writer do
It returns `:ok` if successful or `{:error, reason}` if an error occurs.
"""
@spec write_file(module, Graph.t() | Dataset.t(), Path.t(), keyword) :: :ok | {:error, any}
@spec write_file(module, Description.t() | Graph.t() | Dataset.t(), Path.t(), keyword) ::
:ok | {:error, any}
def write_file(encoder, data, path, opts \\ []) do
with {:ok, encoded_string} <- write_string(encoder, data, opts) do
File.write(path, encoded_string, file_mode(encoder, opts))
@ -57,7 +58,7 @@ defmodule RDF.Serialization.Writer do
As opposed to `write_file`, it raises an exception if an error occurs.
"""
@spec write_file!(module, Graph.t() | Dataset.t(), Path.t(), keyword) :: :ok
@spec write_file!(module, Description.t() | Graph.t() | Dataset.t(), Path.t(), keyword) :: :ok
def write_file!(encoder, data, path, opts \\ []) do
with encoded_string = write_string!(encoder, data, opts) do
File.write!(path, encoded_string, file_mode(encoder, opts))

View file

@ -36,7 +36,8 @@ defmodule RDF.Turtle.Encoder do
@ordered_properties MapSet.new(@predicate_order)
@impl RDF.Serialization.Encoder
@spec encode(Graph.t() | Dataset.t(), keyword) :: {:ok, String.t()} | {:error, any}
@spec encode(Description.t() | Graph.t() | Dataset.t(), keyword) ::
{:ok, String.t()} | {:error, any}
def encode(data, opts \\ []) do
base =
Keyword.get(opts, :base, Keyword.get(opts, :base_iri))

View file

@ -289,6 +289,13 @@ defmodule RDF.Turtle.EncoderTest do
end
end
test "serializing a description" do
description = EX.S |> EX.p(EX.O)
assert Turtle.Encoder.encode!(description) ==
description |> Graph.new() |> Turtle.Encoder.encode!()
end
describe "serializing a dataset" do
test "prefixes of the graphs are merged properly" do
dataset =