1 |
|
-module(mongoose_graphql_scalar). |
2 |
|
-export([input/2, output/2]). |
3 |
|
-ignore_xref([input/2, output/2]). |
4 |
|
|
5 |
|
-spec input(Type, Value) -> {ok, Coerced} | {error, Reason} |
6 |
|
when |
7 |
|
Type :: binary(), |
8 |
|
Value :: binary(), |
9 |
|
Coerced :: any(), |
10 |
|
Reason :: term(). |
11 |
:-( |
input(<<"DateTime">>, DT) -> binary_to_microseconds(DT); |
12 |
:-( |
input(<<"Stanza">>, Value) -> exml:parse(Value); |
13 |
17 |
input(<<"JID">>, Jid) -> jid_from_binary(Jid); |
14 |
|
input(Ty, V) -> |
15 |
:-( |
error_logger:info_report({coercing_generic_scalar, Ty, V}), |
16 |
:-( |
{ok, V}. |
17 |
|
|
18 |
|
-spec output(Type, Value) -> {ok, Coerced} | {error, Reason} |
19 |
|
when |
20 |
|
Type :: binary(), |
21 |
|
Value :: binary(), |
22 |
|
Coerced :: any(), |
23 |
|
Reason :: term(). |
24 |
:-( |
output(<<"DateTime">>, DT) -> {ok, microseconds_to_binary(DT)}; |
25 |
:-( |
output(<<"Stanza">>, Elem) -> {ok, exml:to_binary(Elem)}; |
26 |
8 |
output(<<"JID">>, Jid) -> {ok, jid:to_binary(Jid)}; |
27 |
|
output(Ty, V) -> |
28 |
:-( |
error_logger:info_report({output_generic_scalar, Ty, V}), |
29 |
:-( |
{ok, V}. |
30 |
|
|
31 |
|
jid_from_binary(Value) -> |
32 |
17 |
case jid:from_binary(Value) of |
33 |
|
error -> |
34 |
:-( |
{error, failed_to_parse_jid}; |
35 |
|
Jid -> |
36 |
17 |
{ok, Jid} |
37 |
|
end. |
38 |
|
|
39 |
|
binary_to_microseconds(DT) -> |
40 |
:-( |
case mod_mam_utils:maybe_microseconds(DT) of |
41 |
|
undefined -> |
42 |
:-( |
{error, failed_to_parse_datetime}; |
43 |
|
Microseconds -> |
44 |
:-( |
{ok, Microseconds} |
45 |
|
end. |
46 |
|
|
47 |
|
microseconds_to_binary(Microseconds) -> |
48 |
:-( |
Opts = [{offset, "Z"}, {unit, microsecond}], |
49 |
:-( |
list_to_binary(calendar:system_time_to_rfc3339(Microseconds, Opts)). |