2020-05-05 21:58:44 +00:00
|
|
|
defmodule RDF.XSD do
|
|
|
|
@moduledoc """
|
|
|
|
An implementation of the XML Schema (XSD) datatype system for use within `RDF.Literal.Datatype` system.
|
|
|
|
|
2020-06-01 13:43:38 +00:00
|
|
|
It consists of
|
|
|
|
|
|
|
|
- `RDF.XSD.Datatype`: a more specialized `RDF.Literal.Datatype` behaviour for XSD datatypes
|
|
|
|
- `RDF.XSD.Datatype.Primitive`: macros for the definition of `RDF.Literal.Datatype` and
|
|
|
|
`RDF.XSD.Datatype` implementations for primitive XSD datatypes
|
|
|
|
- `RDF.XSD.Datatype.Restriction`: macros for the definition of `RDF.Literal.Datatype` and
|
|
|
|
`RDF.XSD.Datatype` implementations for derived XSD datatypes
|
|
|
|
- `RDF.XSD.Facet`: a behaviour for XSD facets which can be used to constrain values on
|
|
|
|
datatype derivations
|
|
|
|
|
|
|
|
see <https://www.w3.org/TR/xmlschema11-2/>
|
2020-05-05 21:58:44 +00:00
|
|
|
"""
|
|
|
|
|
2020-05-15 15:13:31 +00:00
|
|
|
import RDF.Utils.Guards
|
2020-05-05 21:58:44 +00:00
|
|
|
|
2020-05-15 15:13:31 +00:00
|
|
|
alias __MODULE__
|
2020-05-05 21:58:44 +00:00
|
|
|
|
|
|
|
@facets [
|
|
|
|
XSD.Facets.MinInclusive,
|
|
|
|
XSD.Facets.MaxInclusive
|
|
|
|
]
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
The list of all XSD facets.
|
|
|
|
"""
|
|
|
|
@spec facets() :: Enum.t()
|
|
|
|
def facets(), do: @facets
|
|
|
|
|
|
|
|
@facets_by_name Map.new(@facets, fn facet -> {facet.name(), facet} end)
|
|
|
|
|
2020-06-01 13:43:38 +00:00
|
|
|
@doc """
|
|
|
|
Get a `RDF.XSD.Facet` by its name.
|
|
|
|
"""
|
|
|
|
def facet(name)
|
2020-05-15 15:13:31 +00:00
|
|
|
def facet(name) when is_ordinary_atom(name), do: @facets_by_name[to_string(name)]
|
2020-05-05 21:58:44 +00:00
|
|
|
def facet(name), do: @facets_by_name[name]
|
|
|
|
|
|
|
|
@doc """
|
2020-05-15 15:13:31 +00:00
|
|
|
Returns if the given value is a `RDF.XSD.Datatype` struct or `RDF.Literal` with a `RDF.XSD.Datatype`.
|
2020-05-05 21:58:44 +00:00
|
|
|
"""
|
2020-05-15 15:13:31 +00:00
|
|
|
defdelegate datatype?(value), to: RDF.Literal.Datatype.Registry, as: :xsd_datatype?
|
2020-05-05 21:58:44 +00:00
|
|
|
|
2020-05-15 15:13:31 +00:00
|
|
|
for datatype <- RDF.Literal.Datatype.Registry.builtin_xsd_datatypes() do
|
2020-05-05 21:58:44 +00:00
|
|
|
defdelegate unquote(String.to_atom(datatype.name))(value), to: datatype, as: :new
|
|
|
|
defdelegate unquote(String.to_atom(datatype.name))(value, opts), to: datatype, as: :new
|
|
|
|
|
|
|
|
elixir_name = Macro.underscore(datatype.name)
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2020-05-05 21:58:44 +00:00
|
|
|
unless datatype.name == elixir_name do
|
|
|
|
defdelegate unquote(String.to_atom(elixir_name))(value), to: datatype, as: :new
|
|
|
|
defdelegate unquote(String.to_atom(elixir_name))(value, opts), to: datatype, as: :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defdelegate datetime(value), to: XSD.DateTime, as: :new
|
|
|
|
defdelegate datetime(value, opts), to: XSD.DateTime, as: :new
|
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate unquote(true)(), to: XSD.Boolean.Value
|
2020-05-05 21:58:44 +00:00
|
|
|
defdelegate unquote(false)(), to: XSD.Boolean.Value
|
|
|
|
end
|