Skip to content

Commit

Permalink
Merge pull request #7 from rustra/fix_dialyzer_warnings
Browse files Browse the repository at this point in the history
Fix the rest dialyzer warnings
  • Loading branch information
marcelotto committed Mar 10, 2020
2 parents 2244c36 + 8115554 commit 625cec9
Show file tree
Hide file tree
Showing 25 changed files with 159 additions and 54 deletions.
29 changes: 17 additions & 12 deletions lib/rdf/datatype.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule RDF.Datatype do
@doc """
Produces the lexical form of a value.
"""
@callback canonical_lexical(any) :: String.t | nil
@callback canonical_lexical(Literal.literal_value) :: String.t | nil

@doc """
Produces the lexical form of an invalid value of a typed Literal.
Expand All @@ -51,7 +51,7 @@ defmodule RDF.Datatype do
of the lexical value space of an `xsd:boolean`, so the `RDF.Boolean`
implementation of this datatype calls `super`.
"""
@callback convert(any, keyword) :: any
@callback convert(any, map) :: any


@doc """
Expand All @@ -60,13 +60,13 @@ defmodule RDF.Datatype do
If the given literal is invalid or can not be converted into this datatype
`nil` is returned.
"""
@callback cast(Literal.t) :: Literal.t | nil
@callback cast(Literal.t | any) :: Literal.t | nil


@doc """
Determines if the value of a `RDF.Literal` is a member of lexical value space of its datatype.
"""
@callback valid?(literal :: Literal.t) :: boolean | nil
@callback valid?(Literal.t) :: boolean | nil

@doc """
Checks if the value of two `RDF.Literal`s of this datatype are equal.
Expand All @@ -78,7 +78,7 @@ defmodule RDF.Datatype do
The default implementation of the `_using__` macro compares the values of the
`canonical/1` forms of the given literals of this datatype.
"""
@callback equal_value?(literal1 :: Literal.t, literal2 :: Literal.t) :: boolean | nil
@callback equal_value?(Literal.t, Literal.t) :: boolean | nil

@doc """
Compares two `RDF.Literal`s.
Expand All @@ -94,7 +94,7 @@ defmodule RDF.Datatype do
The default implementation of the `_using__` macro compares the values of the
`canonical/1` forms of the given literals of this datatype.
"""
@callback compare(literal1 :: Literal.t, literal2 :: Literal.t) :: :lt | :gt | :eq | :indeterminate | nil
@callback compare(Literal.t, Literal.t) :: :lt | :gt | :eq | :indeterminate | nil


@lang_string RDF.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString")
Expand Down Expand Up @@ -122,7 +122,7 @@ defmodule RDF.Datatype do
The IRIs of all datatypes with a `RDF.Datatype` defined.
"""
@spec ids :: [IRI.t]
def ids, do: Map.keys(@mapping)
def ids, do: Map.keys(@mapping)

@doc """
All defined `RDF.Datatype` modules.
Expand Down Expand Up @@ -151,6 +151,7 @@ defmodule RDF.Datatype do
def id, do: @id


@spec new(any, map | keyword) :: Literal.t
def new(value, opts \\ %{})

def new(value, opts) when is_list(opts),
Expand All @@ -170,6 +171,8 @@ defmodule RDF.Datatype do
end


@dialyzer {:nowarn_function, build_literal_by_value: 2}
@spec build_literal_by_value(any, map) :: Literal.t
def build_literal_by_value(value, opts) do
case convert(value, opts) do
nil ->
Expand All @@ -179,6 +182,7 @@ defmodule RDF.Datatype do
end
end

@dialyzer {:nowarn_function, build_literal_by_lexical: 2}
def build_literal_by_lexical(lexical, opts) do
case convert(lexical, opts) do
nil ->
Expand All @@ -192,6 +196,7 @@ defmodule RDF.Datatype do
end
end

@spec build_literal(Literal.literal_value | nil, String.t | nil, map) :: Literal.t
def build_literal(value, lexical, %{canonicalize: true} = opts) do
build_literal(value, lexical, Map.delete(opts, :canonicalize))
|> canonical
Expand All @@ -208,11 +213,11 @@ defmodule RDF.Datatype do
@impl unquote(__MODULE__)
def lexical(literal)

def lexical(%RDF.Literal{value: value, uncanonical_lexical: nil}) do
def lexical(%Literal{value: value, uncanonical_lexical: nil}) do
canonical_lexical(value)
end

def lexical(%RDF.Literal{uncanonical_lexical: lexical}) do
def lexical(%Literal{uncanonical_lexical: lexical}) do
lexical
end

Expand Down Expand Up @@ -250,7 +255,7 @@ defmodule RDF.Datatype do
canonical(literal1).value == canonical(literal2).value
end

def equal_value?(%RDF.Literal{} = left, right) when not is_nil(right) do
def equal_value?(%Literal{} = left, right) when not is_nil(right) do
unless RDF.Term.term?(right) do
equal_value?(left, RDF.Term.coerce(right))
end
Expand All @@ -259,9 +264,9 @@ defmodule RDF.Datatype do
def equal_value?(_, _), do: nil


def less_than?(literal1, literal2), do: RDF.Literal.less_than?(literal1, literal2)
def less_than?(literal1, literal2), do: Literal.less_than?(literal1, literal2)

def greater_than?(literal1, literal2), do: RDF.Literal.greater_than?(literal1, literal2)
def greater_than?(literal1, literal2), do: Literal.greater_than?(literal1, literal2)


@impl unquote(__MODULE__)
Expand Down
55 changes: 32 additions & 23 deletions lib/rdf/datatypes/boolean.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ defmodule RDF.Boolean do

use RDF.Datatype, id: RDF.Datatype.NS.XSD.boolean

import RDF.Literal.Guards
import Literal.Guards

@type value :: boolean
@type input :: value | String.t | number


@impl RDF.Datatype
@spec convert(input | any, map) :: value | nil
def convert(value, opts)

def convert(value, _) when is_boolean(value), do: value
Expand All @@ -35,9 +37,9 @@ defmodule RDF.Boolean do
@impl RDF.Datatype
def cast(literal)

def cast(%RDF.Literal{datatype: datatype} = literal) do
def cast(%Literal{datatype: datatype} = literal) do
cond do
not RDF.Literal.valid?(literal) ->
not Literal.valid?(literal) ->
nil

is_xsd_boolean(datatype) ->
Expand Down Expand Up @@ -92,11 +94,12 @@ defmodule RDF.Boolean do
see <https://www.w3.org/TR/xpath-functions/#func-not>
"""
@spec fn_not(Literal.t | any) :: Literal.t | nil
def fn_not(value) do
case ebv(value) do
%RDF.Literal{value: true} -> RDF.Boolean.Value.false
%RDF.Literal{value: false} -> RDF.Boolean.Value.true
nil -> nil
%Literal{value: true} -> RDF.Boolean.Value.false
%Literal{value: false} -> RDF.Boolean.Value.true
nil -> nil
end
end

Expand All @@ -123,20 +126,22 @@ defmodule RDF.Boolean do
see <https://www.w3.org/TR/sparql11-query/#func-logical-and>
"""
@spec logical_and(Literal.t | any, Literal.t | any) ::
Literal.t | nil
def logical_and(left, right) do
case ebv(left) do
%RDF.Literal{value: false} ->
%Literal{value: false} ->
RDF.false

%RDF.Literal{value: true} ->
%Literal{value: true} ->
case ebv(right) do
%RDF.Literal{value: true} -> RDF.true
%RDF.Literal{value: false} -> RDF.false
nil -> nil
%Literal{value: true} -> RDF.true
%Literal{value: false} -> RDF.false
nil -> nil
end

nil ->
if match?(%RDF.Literal{value: false}, ebv(right)) do
if match?(%Literal{value: false}, ebv(right)) do
RDF.false
end
end
Expand Down Expand Up @@ -165,20 +170,22 @@ defmodule RDF.Boolean do
see <https://www.w3.org/TR/sparql11-query/#func-logical-or>
"""
@spec logical_or(Literal.t | any, Literal.t | any) ::
Literal.t | nil
def logical_or(left, right) do
case ebv(left) do
%RDF.Literal{value: true} ->
%Literal{value: true} ->
RDF.true

%RDF.Literal{value: false} ->
%Literal{value: false} ->
case ebv(right) do
%RDF.Literal{value: true} -> RDF.true
%RDF.Literal{value: false} -> RDF.false
%Literal{value: true} -> RDF.true
%Literal{value: false} -> RDF.false
nil -> nil
end

nil ->
if match?(%RDF.Literal{value: true}, ebv(right)) do
if match?(%Literal{value: true}, ebv(right)) do
RDF.true
end
end
Expand All @@ -201,23 +208,24 @@ defmodule RDF.Boolean do
- <https://www.w3.org/TR/sparql11-query/#ebv>
"""
@spec ebv(Literal.t | any) :: Literal.t | nil
def ebv(value)

def ebv(true), do: RDF.Boolean.Value.true
def ebv(false), do: RDF.Boolean.Value.false

def ebv(%RDF.Literal{value: nil, datatype: @xsd_boolean}), do: RDF.Boolean.Value.false
def ebv(%RDF.Literal{datatype: @xsd_boolean} = literal), do: literal
def ebv(%Literal{value: nil, datatype: @xsd_boolean}), do: RDF.Boolean.Value.false
def ebv(%Literal{datatype: @xsd_boolean} = literal), do: literal

def ebv(%RDF.Literal{datatype: datatype} = literal) do
def ebv(%Literal{datatype: datatype} = literal) do
cond do
RDF.Numeric.type?(datatype) ->
if RDF.Literal.valid?(literal) and
if Literal.valid?(literal) and
not (literal.value == 0 or literal.value == :nan),
do: RDF.Boolean.Value.true,
else: RDF.Boolean.Value.false

RDF.Literal.plain?(literal) ->
Literal.plain?(literal) ->
if String.length(literal.value) == 0,
do: RDF.Boolean.Value.false,
else: RDF.Boolean.Value.true
Expand All @@ -228,14 +236,15 @@ defmodule RDF.Boolean do
end

def ebv(value) when is_binary(value) or is_number(value) do
value |> RDF.Literal.new() |> ebv()
value |> Literal.new() |> ebv()
end

def ebv(_), do: nil

@doc """
Alias for `ebv/1`.
"""
@spec effective(Literal.t | any) :: Literal.t | nil
def effective(value), do: ebv(value)

end
Expand Down
3 changes: 3 additions & 0 deletions lib/rdf/datatypes/date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ defmodule RDF.Date do
import RDF.Literal.Guards

@type value :: Date.t | {Date.t, String.t}
@type input :: value | String.t

@grammar ~r/\A(-?\d{4}-\d{2}-\d{2})((?:[\+\-]\d{2}:\d{2})|UTC|GMT|Z)?\Z/
@xsd_datetime RDF.Datatype.NS.XSD.dateTime


@impl RDF.Datatype
@spec convert(input | any, map) :: value | nil
def convert(value, opts)

def convert(%Date{} = value, %{tz: "+00:00"} = opts) do
Expand Down Expand Up @@ -54,6 +56,7 @@ defmodule RDF.Date do


@impl RDF.Datatype
@spec canonical_lexical(value) :: String.t
def canonical_lexical(value)

def canonical_lexical(%Date{} = value) do
Expand Down
6 changes: 6 additions & 0 deletions lib/rdf/datatypes/date_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ defmodule RDF.DateTime do
import RDF.Literal.Guards

@type value :: DateTime.t | NaiveDateTime.t
@type input :: value | String.t

@xsd_date RDF.Datatype.NS.XSD.date


@impl RDF.Datatype
@spec convert(input | any, map) :: value | nil
def convert(value, opts)

# Special case for date and dateTime, for which 0 is not a valid year
Expand Down Expand Up @@ -65,6 +67,7 @@ defmodule RDF.DateTime do


@impl RDF.Datatype
@spec canonical_lexical(value) :: String.t
def canonical_lexical(value)

def canonical_lexical(%DateTime{} = value) do
Expand Down Expand Up @@ -112,6 +115,7 @@ defmodule RDF.DateTime do
@doc """
Builds a `RDF.DateTime` literal for current moment in time.
"""
@spec now :: Literal.t
def now() do
new(DateTime.utc_now())
end
Expand All @@ -120,6 +124,7 @@ defmodule RDF.DateTime do
@doc """
Extracts the timezone string from a `RDF.DateTime` literal.
"""
@spec tz(Literal.t) :: String.t | nil
def tz(literal)

def tz(%Literal{value: %NaiveDateTime{}}), do: ""
Expand All @@ -136,6 +141,7 @@ defmodule RDF.DateTime do
@doc """
Converts a datetime literal to a canonical string, preserving the zone information.
"""
@spec canonical_lexical_with_zone(Literal.t) :: String.t | nil
def canonical_lexical_with_zone(%Literal{datatype: datatype} = literal)
when is_xsd_datetime(datatype) do
case tz(literal) do
Expand Down
1 change: 1 addition & 0 deletions lib/rdf/datatypes/date_time_utils.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule RDF.DateTimeUtils do
@moduledoc false

@spec tz(String.t) :: String.t
def tz(string) do
case Regex.run(~r/([+-])(\d\d:\d\d)/, string) do
[_, sign, zone] ->
Expand Down
6 changes: 5 additions & 1 deletion lib/rdf/datatypes/decimal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ defmodule RDF.Decimal do

alias Elixir.Decimal, as: D

@type value :: Decimal.t | :nan
@type value :: Decimal.t
@type input :: value | number | String.t

@xsd_integer RDF.Datatype.NS.XSD.integer


@impl RDF.Datatype
@spec convert(input | any, map) :: value | nil
def convert(value, opts)

def convert(%D{coef: coef} = value, opts) when coef in ~w[qNaN sNaN inf]a,
Expand Down Expand Up @@ -120,6 +122,7 @@ defmodule RDF.Decimal do
@doc """
The number of digits in the XML Schema canonical form of the literal value.
"""
@spec digit_count(Literal.t) :: non_neg_integer
def digit_count(%RDF.Literal{datatype: @id} = literal) do
if valid?(literal) do
literal
Expand All @@ -137,6 +140,7 @@ defmodule RDF.Decimal do
@doc """
The number of digits to the right of the decimal point in the XML Schema canonical form of the literal value.
"""
@spec fraction_digit_count(Literal.t) :: non_neg_integer
def fraction_digit_count(%RDF.Literal{datatype: @id} = literal) do
if valid?(literal) do
[_, fraction] =
Expand Down
Loading

0 comments on commit 625cec9

Please sign in to comment.