-
Notifications
You must be signed in to change notification settings - Fork 8
/
base64_binary.ex
64 lines (51 loc) · 1.61 KB
/
base64_binary.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
defmodule RDF.XSD.Base64Binary do
@moduledoc """
`RDF.XSD.Datatype` for XSD base64Binary.
"""
@type valid_value :: binary()
use RDF.XSD.Datatype.Primitive,
name: "base64Binary",
id: RDF.Utils.Bootstrapping.xsd_iri("base64Binary")
alias RDF.XSD
def_applicable_facet XSD.Facets.MinLength
def_applicable_facet XSD.Facets.MaxLength
def_applicable_facet XSD.Facets.Length
def_applicable_facet XSD.Facets.Pattern
@doc false
def min_length_conform?(min_length, value, _lexical) do
byte_size(value) >= min_length
end
@doc false
def max_length_conform?(max_length, value, _lexical) do
byte_size(value) <= max_length
end
@doc false
def length_conform?(length, value, _lexical) do
byte_size(value) == length
end
@doc false
def pattern_conform?(pattern, value, _lexical) do
XSD.Facets.Pattern.conform?(pattern, value)
end
@impl XSD.Datatype
@spec lexical_mapping(String.t(), Keyword.t()) :: valid_value()
def lexical_mapping(lexical, _) do
case Base.decode64(lexical) do
{:ok, value} ->
value
_ ->
@invalid_value
end
end
@impl XSD.Datatype
@spec elixir_mapping(any, Keyword.t()) :: nil | valid_value()
def elixir_mapping(value, _) when is_binary(value), do: value
def elixir_mapping(_, _), do: @invalid_value
@impl XSD.Datatype
@spec canonical_mapping(valid_value()) :: String.t()
def canonical_mapping(value), do: Base.encode64(value)
@impl RDF.Literal.Datatype
def do_cast(value)
def do_cast(%XSD.String{} = xsd_string), do: new(xsd_string.value, as_value: true)
def do_cast(literal), do: super(literal)
end