Add RDF.Data.t type for the RDF data structures

This commit is contained in:
Marcel Otto 2020-11-03 11:39:38 +01:00
parent 1cf84a0e84
commit 0dc8e383ce
5 changed files with 12 additions and 13 deletions

View file

@ -3,6 +3,8 @@ defprotocol RDF.Data do
An abstraction over the different data structures for collections of RDF statements. An abstraction over the different data structures for collections of RDF statements.
""" """
@type t :: RDF.Description.t() | RDF.Graph.t() | RDF.Dataset.t()
@doc """ @doc """
Adds statements to a RDF data structure. Adds statements to a RDF data structure.

View file

@ -31,7 +31,7 @@ defmodule RDF.Serialization.Encoder do
@impl unquote(__MODULE__) @impl unquote(__MODULE__)
@dialyzer {:nowarn_function, encode!: 2} @dialyzer {:nowarn_function, encode!: 2}
@spec encode!(Description.t() | Graph.t() | Dataset.t(), keyword) :: String.t() @spec encode!(RDF.Data.t(), keyword) :: String.t()
def encode!(data, opts \\ []) do def encode!(data, opts \\ []) do
case encode(data, opts) do case encode(data, opts) do
{:ok, data} -> data {:ok, data} -> data

View file

@ -192,7 +192,7 @@ defmodule RDF.Serialization do
It returns an `{:ok, string}` tuple, with `string` being the serialized graph or It returns an `{:ok, string}` tuple, with `string` being the serialized graph or
dataset, or `{:error, reason}` if an error occurs. dataset, or `{:error, reason}` if an error occurs.
""" """
@spec write_string(Graph.t() | Dataset.t(), keyword) :: {:ok, String.t()} | {:error, any} @spec write_string(RDF.Data.t(), keyword) :: {:ok, String.t()} | {:error, any}
def write_string(data, opts) do def write_string(data, opts) do
with {:ok, format} <- string_format(opts) do with {:ok, format} <- string_format(opts) do
format.write_string(data, opts) format.write_string(data, opts)
@ -207,7 +207,7 @@ defmodule RDF.Serialization do
As opposed to `write_string`, it raises an exception if an error occurs. As opposed to `write_string`, it raises an exception if an error occurs.
""" """
@spec write_string!(Graph.t() | Dataset.t(), keyword) :: String.t() @spec write_string!(RDF.Data.t(), keyword) :: String.t()
def write_string!(data, opts) do def write_string!(data, opts) do
with {:ok, format} <- string_format(opts) do with {:ok, format} <- string_format(opts) do
format.write_string!(data, opts) format.write_string!(data, opts)
@ -232,7 +232,7 @@ defmodule RDF.Serialization do
It returns `:ok` if successful or `{:error, reason}` if an error occurs. It returns `:ok` if successful or `{:error, reason}` if an error occurs.
""" """
@spec write_file(Graph.t() | Dataset.t(), Path.t(), keyword) :: :ok | {:error, any} @spec write_file(RDF.Data.t(), Path.t(), keyword) :: :ok | {:error, any}
def write_file(data, path, opts \\ []) do def write_file(data, path, opts \\ []) do
with {:ok, format} <- file_format(path, opts) do with {:ok, format} <- file_format(path, opts) do
format.write_file(data, path, opts) format.write_file(data, path, opts)
@ -250,7 +250,7 @@ defmodule RDF.Serialization do
As opposed to `write_file`, it raises an exception if an error occurs. As opposed to `write_file`, it raises an exception if an error occurs.
""" """
@spec write_file!(Graph.t() | Dataset.t(), Path.t(), keyword) :: :ok @spec write_file!(RDF.Data.t(), Path.t(), keyword) :: :ok
def write_file!(data, path, opts \\ []) do def write_file!(data, path, opts \\ []) do
with {:ok, format} <- file_format(path, opts) do with {:ok, format} <- file_format(path, opts) do
format.write_file!(data, path, opts) format.write_file!(data, path, opts)

View file

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

View file

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