Always resolve datatype IRIs through the registration protocol

It's actually faster (although just slightly) than the map access!
This commit is contained in:
Marcel Otto 2020-05-09 00:54:35 +02:00
parent c22bab6c74
commit afc6f28cf4
25 changed files with 27 additions and 63 deletions

View file

@ -1,7 +1,7 @@
defmodule RDF.Literal.Datatype.Registry do defmodule RDF.Literal.Datatype.Registry do
@moduledoc false @moduledoc false
alias RDF.{Literal, IRI, XSD} alias RDF.{Literal, IRI, XSD, Namespace}
alias RDF.Literal.Datatype.Registry.Registration alias RDF.Literal.Datatype.Registry.Registration
import RDF.Guards import RDF.Guards
@ -46,15 +46,9 @@ defmodule RDF.Literal.Datatype.Registry do
""" """
@spec get(Literal.t | IRI.t | String.t) :: Literal.Datatype.t @spec get(Literal.t | IRI.t | String.t) :: Literal.Datatype.t
def get(%Literal{} = literal), do: Literal.datatype(literal) def get(%Literal{} = literal), do: Literal.datatype(literal)
def get(id) when is_binary(id), do: id |> IRI.new() |> get() def get(%IRI{} = id), do: id |> to_string() |> get()
def get(id) when maybe_ns_term(id), do: id |> IRI.new() |> get() def get(id) when maybe_ns_term(id), do: id |> Namespace.resolve_term!() |> get()
def get(id), do: @mapping[id] || get_custom_datatype(id) def get(id) when is_binary(id), do: Registration.datatype(id)
defp get_custom_datatype(id) do
id
|> to_string()
|> Registration.datatype()
end
defp implements_datatype_behaviour?(module) do defp implements_datatype_behaviour?(module) do
module.module_info[:attributes] module.module_info[:attributes]

View file

@ -7,8 +7,7 @@ defmodule RDF.LangString do
use RDF.Literal.Datatype, use RDF.Literal.Datatype,
name: "langString", name: "langString",
id: RDF.Utils.Bootstrapping.rdf_iri("langString"), id: RDF.Utils.Bootstrapping.rdf_iri("langString")
register: false # core datatypes don't need to be registered
alias RDF.Literal.Datatype alias RDF.Literal.Datatype
alias RDF.Literal alias RDF.Literal

View file

@ -11,8 +11,7 @@ defmodule RDF.XSD.AnyURI do
use RDF.XSD.Datatype.Primitive, use RDF.XSD.Datatype.Primitive,
name: "anyURI", name: "anyURI",
id: RDF.Utils.Bootstrapping.xsd_iri("anyURI"), id: RDF.Utils.Bootstrapping.xsd_iri("anyURI")
register: false # core datatypes don't need to be registered
@impl RDF.XSD.Datatype @impl RDF.XSD.Datatype
@spec lexical_mapping(String.t(), Keyword.t()) :: valid_value @spec lexical_mapping(String.t(), Keyword.t()) :: valid_value

View file

@ -8,8 +8,7 @@ defmodule RDF.XSD.Boolean do
use RDF.XSD.Datatype.Primitive, use RDF.XSD.Datatype.Primitive,
name: "boolean", name: "boolean",
id: RDF.Utils.Bootstrapping.xsd_iri("boolean"), id: RDF.Utils.Bootstrapping.xsd_iri("boolean")
register: false # core datatypes don't need to be registered
@impl RDF.XSD.Datatype @impl RDF.XSD.Datatype
def lexical_mapping(lexical, _) do def lexical_mapping(lexical, _) do

View file

@ -2,8 +2,7 @@ defmodule RDF.XSD.Byte do
use RDF.XSD.Datatype.Restriction, use RDF.XSD.Datatype.Restriction,
name: "byte", name: "byte",
id: RDF.Utils.Bootstrapping.xsd_iri("byte"), id: RDF.Utils.Bootstrapping.xsd_iri("byte"),
base: RDF.XSD.Short, base: RDF.XSD.Short
register: false # core datatypes don't need to be registered
def_facet_constraint RDF.XSD.Facets.MinInclusive, -128 def_facet_constraint RDF.XSD.Facets.MinInclusive, -128
def_facet_constraint RDF.XSD.Facets.MaxInclusive, 127 def_facet_constraint RDF.XSD.Facets.MaxInclusive, 127

View file

@ -11,8 +11,7 @@ defmodule RDF.XSD.Date do
use RDF.XSD.Datatype.Primitive, use RDF.XSD.Datatype.Primitive,
name: "date", name: "date",
id: RDF.Utils.Bootstrapping.xsd_iri("date"), id: RDF.Utils.Bootstrapping.xsd_iri("date")
register: false # core datatypes don't need to be registered
# TODO: Are GMT/UTC actually allowed? Maybe because it is supported by Elixir's Datetime ... # TODO: Are GMT/UTC actually allowed? Maybe because it is supported by Elixir's Datetime ...

View file

@ -7,8 +7,7 @@ defmodule RDF.XSD.DateTime do
use RDF.XSD.Datatype.Primitive, use RDF.XSD.Datatype.Primitive,
name: "dateTime", name: "dateTime",
id: RDF.Utils.Bootstrapping.xsd_iri("dateTime"), id: RDF.Utils.Bootstrapping.xsd_iri("dateTime")
register: false # core datatypes don't need to be registered
@impl RDF.XSD.Datatype @impl RDF.XSD.Datatype
def lexical_mapping(lexical, opts) do def lexical_mapping(lexical, opts) do

View file

@ -7,8 +7,7 @@ defmodule RDF.XSD.Decimal do
use RDF.XSD.Datatype.Primitive, use RDF.XSD.Datatype.Primitive,
name: "decimal", name: "decimal",
id: RDF.Utils.Bootstrapping.xsd_iri("decimal"), id: RDF.Utils.Bootstrapping.xsd_iri("decimal")
register: false # core datatypes don't need to be registered
alias Elixir.Decimal, as: D alias Elixir.Decimal, as: D

View file

@ -8,8 +8,7 @@ defmodule RDF.XSD.Double do
use RDF.XSD.Datatype.Primitive, use RDF.XSD.Datatype.Primitive,
name: "double", name: "double",
id: RDF.Utils.Bootstrapping.xsd_iri("double"), id: RDF.Utils.Bootstrapping.xsd_iri("double")
register: false # core datatypes don't need to be registered
@special_values ~W[positive_infinity negative_infinity nan]a @special_values ~W[positive_infinity negative_infinity nan]a

View file

@ -9,6 +9,5 @@ defmodule RDF.XSD.Float do
use RDF.XSD.Datatype.Restriction, use RDF.XSD.Datatype.Restriction,
name: "float", name: "float",
id: RDF.Utils.Bootstrapping.xsd_iri("float"), id: RDF.Utils.Bootstrapping.xsd_iri("float"),
base: RDF.XSD.Double, base: RDF.XSD.Double
register: false # core datatypes don't need to be registered
end end

View file

@ -2,8 +2,7 @@ defmodule RDF.XSD.Int do
use RDF.XSD.Datatype.Restriction, use RDF.XSD.Datatype.Restriction,
name: "int", name: "int",
id: RDF.Utils.Bootstrapping.xsd_iri("int"), id: RDF.Utils.Bootstrapping.xsd_iri("int"),
base: RDF.XSD.Long, base: RDF.XSD.Long
register: false # core datatypes don't need to be registered
def_facet_constraint RDF.XSD.Facets.MinInclusive, -2_147_483_648 def_facet_constraint RDF.XSD.Facets.MinInclusive, -2_147_483_648
def_facet_constraint RDF.XSD.Facets.MaxInclusive, 2_147_483_647 def_facet_constraint RDF.XSD.Facets.MaxInclusive, 2_147_483_647

View file

@ -10,8 +10,7 @@ defmodule RDF.XSD.Integer do
use RDF.XSD.Datatype.Primitive, use RDF.XSD.Datatype.Primitive,
name: "integer", name: "integer",
id: RDF.Utils.Bootstrapping.xsd_iri("integer"), id: RDF.Utils.Bootstrapping.xsd_iri("integer")
register: false # core datatypes don't need to be registered
def_applicable_facet RDF.XSD.Facets.MinInclusive def_applicable_facet RDF.XSD.Facets.MinInclusive
def_applicable_facet RDF.XSD.Facets.MaxInclusive def_applicable_facet RDF.XSD.Facets.MaxInclusive

View file

@ -2,8 +2,7 @@ defmodule RDF.XSD.Long do
use RDF.XSD.Datatype.Restriction, use RDF.XSD.Datatype.Restriction,
name: "long", name: "long",
id: RDF.Utils.Bootstrapping.xsd_iri("long"), id: RDF.Utils.Bootstrapping.xsd_iri("long"),
base: RDF.XSD.Integer, base: RDF.XSD.Integer
register: false # core datatypes don't need to be registered
def_facet_constraint RDF.XSD.Facets.MinInclusive, -9_223_372_036_854_775_808 def_facet_constraint RDF.XSD.Facets.MinInclusive, -9_223_372_036_854_775_808
def_facet_constraint RDF.XSD.Facets.MaxInclusive, 9_223_372_036_854_775_807 def_facet_constraint RDF.XSD.Facets.MaxInclusive, 9_223_372_036_854_775_807

View file

@ -2,8 +2,7 @@ defmodule RDF.XSD.NegativeInteger do
use RDF.XSD.Datatype.Restriction, use RDF.XSD.Datatype.Restriction,
name: "negativeInteger", name: "negativeInteger",
id: RDF.Utils.Bootstrapping.xsd_iri("negativeInteger"), id: RDF.Utils.Bootstrapping.xsd_iri("negativeInteger"),
base: RDF.XSD.NonPositiveInteger, base: RDF.XSD.NonPositiveInteger
register: false # core datatypes don't need to be registered
def_facet_constraint RDF.XSD.Facets.MaxInclusive, -1 def_facet_constraint RDF.XSD.Facets.MaxInclusive, -1
end end

View file

@ -2,8 +2,7 @@ defmodule RDF.XSD.NonNegativeInteger do
use RDF.XSD.Datatype.Restriction, use RDF.XSD.Datatype.Restriction,
name: "nonNegativeInteger", name: "nonNegativeInteger",
id: RDF.Utils.Bootstrapping.xsd_iri("nonNegativeInteger"), id: RDF.Utils.Bootstrapping.xsd_iri("nonNegativeInteger"),
base: RDF.XSD.Integer, base: RDF.XSD.Integer
register: false # core datatypes don't need to be registered
def_facet_constraint RDF.XSD.Facets.MinInclusive, 0 def_facet_constraint RDF.XSD.Facets.MinInclusive, 0
end end

View file

@ -2,8 +2,7 @@ defmodule RDF.XSD.NonPositiveInteger do
use RDF.XSD.Datatype.Restriction, use RDF.XSD.Datatype.Restriction,
name: "nonPositiveInteger", name: "nonPositiveInteger",
id: RDF.Utils.Bootstrapping.xsd_iri("nonPositiveInteger"), id: RDF.Utils.Bootstrapping.xsd_iri("nonPositiveInteger"),
base: RDF.XSD.Integer, base: RDF.XSD.Integer
register: false # core datatypes don't need to be registered
def_facet_constraint RDF.XSD.Facets.MaxInclusive, 0 def_facet_constraint RDF.XSD.Facets.MaxInclusive, 0
end end

View file

@ -2,8 +2,7 @@ defmodule RDF.XSD.PositiveInteger do
use RDF.XSD.Datatype.Restriction, use RDF.XSD.Datatype.Restriction,
name: "positiveInteger", name: "positiveInteger",
id: RDF.Utils.Bootstrapping.xsd_iri("positiveInteger"), id: RDF.Utils.Bootstrapping.xsd_iri("positiveInteger"),
base: RDF.XSD.NonNegativeInteger, base: RDF.XSD.NonNegativeInteger
register: false # core datatypes don't need to be registered
def_facet_constraint RDF.XSD.Facets.MinInclusive, 1 def_facet_constraint RDF.XSD.Facets.MinInclusive, 1
end end

View file

@ -2,8 +2,7 @@ defmodule RDF.XSD.Short do
use RDF.XSD.Datatype.Restriction, use RDF.XSD.Datatype.Restriction,
name: "short", name: "short",
id: RDF.Utils.Bootstrapping.xsd_iri("short"), id: RDF.Utils.Bootstrapping.xsd_iri("short"),
base: RDF.XSD.Int, base: RDF.XSD.Int
register: false # core datatypes don't need to be registered
def_facet_constraint RDF.XSD.Facets.MinInclusive, -32768 def_facet_constraint RDF.XSD.Facets.MinInclusive, -32768
def_facet_constraint RDF.XSD.Facets.MaxInclusive, 32767 def_facet_constraint RDF.XSD.Facets.MaxInclusive, 32767

View file

@ -7,8 +7,7 @@ defmodule RDF.XSD.String do
use RDF.XSD.Datatype.Primitive, use RDF.XSD.Datatype.Primitive,
name: "string", name: "string",
id: RDF.Utils.Bootstrapping.xsd_iri("string"), id: RDF.Utils.Bootstrapping.xsd_iri("string")
register: false # core datatypes don't need to be registered
@impl RDF.XSD.Datatype @impl RDF.XSD.Datatype
@spec lexical_mapping(String.t(), Keyword.t()) :: valid_value @spec lexical_mapping(String.t(), Keyword.t()) :: valid_value

View file

@ -7,8 +7,7 @@ defmodule RDF.XSD.Time do
use RDF.XSD.Datatype.Primitive, use RDF.XSD.Datatype.Primitive,
name: "time", name: "time",
id: RDF.Utils.Bootstrapping.xsd_iri("time"), id: RDF.Utils.Bootstrapping.xsd_iri("time")
register: false # core datatypes don't need to be registered
# TODO: Are GMT/UTC actually allowed? Maybe because it is supported by Elixir's Datetime ... # TODO: Are GMT/UTC actually allowed? Maybe because it is supported by Elixir's Datetime ...
@grammar ~r/\A(\d{2}:\d{2}:\d{2}(?:\.\d+)?)((?:[\+\-]\d{2}:\d{2})|UTC|GMT|Z)?\Z/ @grammar ~r/\A(\d{2}:\d{2}:\d{2}(?:\.\d+)?)((?:[\+\-]\d{2}:\d{2})|UTC|GMT|Z)?\Z/

View file

@ -2,8 +2,7 @@ defmodule RDF.XSD.UnsignedByte do
use RDF.XSD.Datatype.Restriction, use RDF.XSD.Datatype.Restriction,
name: "unsignedByte", name: "unsignedByte",
id: RDF.Utils.Bootstrapping.xsd_iri("unsignedByte"), id: RDF.Utils.Bootstrapping.xsd_iri("unsignedByte"),
base: RDF.XSD.UnsignedShort, base: RDF.XSD.UnsignedShort
register: false # core datatypes don't need to be registered
def_facet_constraint RDF.XSD.Facets.MinInclusive, 0 def_facet_constraint RDF.XSD.Facets.MinInclusive, 0
def_facet_constraint RDF.XSD.Facets.MaxInclusive, 255 def_facet_constraint RDF.XSD.Facets.MaxInclusive, 255

View file

@ -2,8 +2,7 @@ defmodule RDF.XSD.UnsignedInt do
use RDF.XSD.Datatype.Restriction, use RDF.XSD.Datatype.Restriction,
name: "unsignedInt", name: "unsignedInt",
id: RDF.Utils.Bootstrapping.xsd_iri("unsignedInt"), id: RDF.Utils.Bootstrapping.xsd_iri("unsignedInt"),
base: RDF.XSD.UnsignedLong, base: RDF.XSD.UnsignedLong
register: false # core datatypes don't need to be registered
def_facet_constraint RDF.XSD.Facets.MinInclusive, 0 def_facet_constraint RDF.XSD.Facets.MinInclusive, 0
def_facet_constraint RDF.XSD.Facets.MaxInclusive, 4_294_967_295 def_facet_constraint RDF.XSD.Facets.MaxInclusive, 4_294_967_295

View file

@ -2,8 +2,7 @@ defmodule RDF.XSD.UnsignedLong do
use RDF.XSD.Datatype.Restriction, use RDF.XSD.Datatype.Restriction,
name: "unsignedLong", name: "unsignedLong",
id: RDF.Utils.Bootstrapping.xsd_iri("unsignedLong"), id: RDF.Utils.Bootstrapping.xsd_iri("unsignedLong"),
base: RDF.XSD.NonNegativeInteger, base: RDF.XSD.NonNegativeInteger
register: false # core datatypes don't need to be registered
def_facet_constraint RDF.XSD.Facets.MinInclusive, 0 def_facet_constraint RDF.XSD.Facets.MinInclusive, 0
def_facet_constraint RDF.XSD.Facets.MaxInclusive, 18_446_744_073_709_551_615 def_facet_constraint RDF.XSD.Facets.MaxInclusive, 18_446_744_073_709_551_615

View file

@ -2,8 +2,7 @@ defmodule RDF.XSD.UnsignedShort do
use RDF.XSD.Datatype.Restriction, use RDF.XSD.Datatype.Restriction,
name: "unsignedShort", name: "unsignedShort",
id: RDF.Utils.Bootstrapping.xsd_iri("unsignedShort"), id: RDF.Utils.Bootstrapping.xsd_iri("unsignedShort"),
base: RDF.XSD.UnsignedInt, base: RDF.XSD.UnsignedInt
register: false # core datatypes don't need to be registered
def_facet_constraint RDF.XSD.Facets.MinInclusive, 0 def_facet_constraint RDF.XSD.Facets.MinInclusive, 0
def_facet_constraint RDF.XSD.Facets.MaxInclusive, 65535 def_facet_constraint RDF.XSD.Facets.MaxInclusive, 65535

View file

@ -66,11 +66,4 @@ defmodule RDF.Literal.Datatype.RegistryTest do
assert Age == Datatype.Registry.get(EX.Age) assert Age == Datatype.Registry.get(EX.Age)
end end
end end
test "core datatypes are handled differently and should not be registered (for performance reasons)" do
Enum.each(Datatype.Registry.core_datatypes(), fn datatype ->
refute Datatype.Registry.Registration.datatype(datatype.id)
refute Datatype.Registry.Registration.datatype(to_string(datatype.id))
end)
end
end end