Change RDF.Datatype.cast implementations to return nil when not castable

This commit is contained in:
Marcel Otto 2018-09-14 18:00:43 +02:00
parent a2cd1adeeb
commit 7847c76fc9
17 changed files with 50 additions and 1 deletions

View file

@ -54,6 +54,8 @@ defmodule RDF.Boolean do
end
end
def cast(_), do: nil
@doc """
Returns `RDF.true` if the effective boolean value of the given argument is `RDF.false`, or `RDF.false` if it is `RDF.true`.

View file

@ -86,4 +86,6 @@ defmodule RDF.Date do
end
end
def cast(_), do: nil
end

View file

@ -88,6 +88,8 @@ defmodule RDF.DateTime do
end
end
def cast(_), do: nil
def tz(%Literal{value: %NaiveDateTime{}}), do: ""

View file

@ -96,6 +96,8 @@ defmodule RDF.Decimal do
end
end
def cast(_), do: nil
def equal_value?(left, right), do: RDF.Numeric.equal_value?(left, right)

View file

@ -128,6 +128,8 @@ defmodule RDF.Double do
end
end
def cast(_), do: nil
def equal_value?(left, right), do: RDF.Numeric.equal_value?(left, right)

View file

@ -58,6 +58,8 @@ defmodule RDF.Integer do
end
end
def cast(_), do: nil
def equal_value?(left, right), do: RDF.Numeric.equal_value?(left, right)

View file

@ -53,8 +53,9 @@ defmodule RDF.LangString do
end
end
def cast(_) do
raise "not supported"
nil
end
end

View file

@ -91,4 +91,6 @@ defmodule RDF.String do
end
end
def cast(_), do: nil
end

View file

@ -141,4 +141,6 @@ defmodule RDF.Time do
end
end
def cast(_), do: nil
end

View file

@ -112,6 +112,10 @@ defmodule RDF.BooleanTest do
test "with literals of unsupported datatypes" do
assert RDF.DateTime.now() |> RDF.Boolean.cast() == nil
end
test "with non-RDF terms" do
assert RDF.Boolean.cast(:foo) == nil
end
end

View file

@ -109,6 +109,10 @@ defmodule RDF.DateTest do
assert RDF.integer(1) |> RDF.Date.cast() == nil
assert RDF.decimal(3.14) |> RDF.Date.cast() == nil
end
test "with non-RDF terms" do
assert RDF.Date.cast(:foo) == nil
end
end
end

View file

@ -163,6 +163,10 @@ defmodule RDF.DateTimeTest do
assert RDF.integer(1) |> RDF.DateTime.cast() == nil
assert RDF.decimal(3.14) |> RDF.DateTime.cast() == nil
end
test "with non-RDF terms" do
assert RDF.DateTime.cast(:foo) == nil
end
end

View file

@ -117,6 +117,10 @@ defmodule RDF.DecimalTest do
test "with literals of unsupported datatypes" do
assert RDF.DateTime.now() |> RDF.Decimal.cast() == nil
end
test "with non-RDF terms" do
assert RDF.Decimal.cast(:foo) == nil
end
end

View file

@ -105,6 +105,10 @@ defmodule RDF.DoubleTest do
test "with literals of unsupported datatypes" do
assert RDF.DateTime.now() |> RDF.Double.cast() == nil
end
test "with non-RDF terms" do
assert RDF.Double.cast(:foo) == nil
end
end
end

View file

@ -70,6 +70,10 @@ defmodule RDF.IntegerTest do
test "with literals of unsupported datatypes" do
assert RDF.DateTime.now() |> RDF.Integer.cast() == nil
end
test "with non-RDF terms" do
assert RDF.Integer.cast(:foo) == nil
end
end

View file

@ -122,6 +122,10 @@ defmodule RDF.StringTest do
assert RDF.decimal("NAN") |> RDF.String.cast() == nil
assert RDF.double(true) |> RDF.String.cast() == nil
end
test "with non-RDF terms" do
assert RDF.String.cast(:foo) == nil
end
end
end

View file

@ -96,6 +96,10 @@ defmodule RDF.TimeTest do
assert RDF.integer(1) |> RDF.Time.cast() == nil
assert RDF.decimal(3.14) |> RDF.Time.cast() == nil
end
test "with non-RDF terms" do
assert RDF.Time.cast(:foo) == nil
end
end
end