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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -8,8 +8,7 @@ defmodule RDF.XSD.Double do
use RDF.XSD.Datatype.Primitive,
name: "double",
id: RDF.Utils.Bootstrapping.xsd_iri("double"),
register: false # core datatypes don't need to be registered
id: RDF.Utils.Bootstrapping.xsd_iri("double")
@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,
name: "float",
id: RDF.Utils.Bootstrapping.xsd_iri("float"),
base: RDF.XSD.Double,
register: false # core datatypes don't need to be registered
base: RDF.XSD.Double
end

View file

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

View file

@ -2,8 +2,7 @@ defmodule RDF.XSD.Long do
use RDF.XSD.Datatype.Restriction,
name: "long",
id: RDF.Utils.Bootstrapping.xsd_iri("long"),
base: RDF.XSD.Integer,
register: false # core datatypes don't need to be registered
base: RDF.XSD.Integer
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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -7,8 +7,7 @@ defmodule RDF.XSD.String do
use RDF.XSD.Datatype.Primitive,
name: "string",
id: RDF.Utils.Bootstrapping.xsd_iri("string"),
register: false # core datatypes don't need to be registered
id: RDF.Utils.Bootstrapping.xsd_iri("string")
@impl RDF.XSD.Datatype
@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,
name: "time",
id: RDF.Utils.Bootstrapping.xsd_iri("time"),
register: false # core datatypes don't need to be registered
id: RDF.Utils.Bootstrapping.xsd_iri("time")
# 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/

View file

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

View file

@ -2,8 +2,7 @@ defmodule RDF.XSD.UnsignedInt do
use RDF.XSD.Datatype.Restriction,
name: "unsignedInt",
id: RDF.Utils.Bootstrapping.xsd_iri("unsignedInt"),
base: RDF.XSD.UnsignedLong,
register: false # core datatypes don't need to be registered
base: RDF.XSD.UnsignedLong
def_facet_constraint RDF.XSD.Facets.MinInclusive, 0
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,
name: "unsignedLong",
id: RDF.Utils.Bootstrapping.xsd_iri("unsignedLong"),
base: RDF.XSD.NonNegativeInteger,
register: false # core datatypes don't need to be registered
base: RDF.XSD.NonNegativeInteger
def_facet_constraint RDF.XSD.Facets.MinInclusive, 0
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,
name: "unsignedShort",
id: RDF.Utils.Bootstrapping.xsd_iri("unsignedShort"),
base: RDF.XSD.UnsignedInt,
register: false # core datatypes don't need to be registered
base: RDF.XSD.UnsignedInt
def_facet_constraint RDF.XSD.Facets.MinInclusive, 0
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)
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