./ct_report/coverage/mongoose_graphql_scalar.COVER.html

1 -module(mongoose_graphql_scalar).
2 -export([input/2, output/2]).
3 -ignore_xref([input/2, output/2]).
4
5 -include_lib("jid/include/jid.hrl").
6
7 -spec input(Type, Value) -> {ok, Coerced} | {error, Reason}
8 when
9 Type :: binary(),
10 Value :: binary() | pos_integer(),
11 Coerced :: any(),
12 Reason :: term().
13
:-(
input(<<"DateTime">>, DT) -> binary_to_microseconds(DT);
14
:-(
input(<<"XmlElement">>, Value) -> exml:parse(Value);
15
:-(
input(<<"JID">>, Jid) -> jid_from_binary(Jid);
16
:-(
input(<<"BareJID">>, Jid) -> bare_jid_from_binary(Jid);
17
:-(
input(<<"FullJID">>, Jid) -> full_jid_from_binary(Jid);
18
:-(
input(<<"UserName">>, User) -> user_from_binary(User);
19
:-(
input(<<"RoomName">>, Room) -> room_from_binary(Room);
20
:-(
input(<<"DomainName">>, Domain) -> domain_from_binary(Domain);
21
:-(
input(<<"ResourceName">>, Res) -> resource_from_binary(Res);
22
:-(
input(<<"NodeName">>, Node) -> node_from_binary(Node);
23
:-(
input(<<"NonEmptyString">>, Value) -> non_empty_string_to_binary(Value);
24
:-(
input(<<"PosInt">>, Value) -> validate_pos_integer(Value);
25
:-(
input(<<"NonNegInt">>, Value) -> validate_non_neg_integer(Value);
26 input(Ty, V) ->
27
:-(
error_logger:info_report({coercing_generic_scalar, Ty, V}),
28
:-(
{ok, V}.
29
30 -spec output(Type, Value) -> {ok, Coerced} | {error, Reason}
31 when
32 Type :: binary(),
33 Value :: binary() | pos_integer(),
34 Coerced :: any(),
35 Reason :: term().
36
:-(
output(<<"DateTime">>, DT) -> {ok, microseconds_to_binary(DT)};
37
:-(
output(<<"XmlElement">>, Elem) -> {ok, exml:to_binary(Elem)};
38
:-(
output(<<"JID">>, Jid) -> {ok, jid:to_binary(Jid)};
39
:-(
output(<<"UserName">>, User) -> {ok, User};
40
:-(
output(<<"DomainName">>, Domain) -> {ok, Domain};
41
:-(
output(<<"ResourceName">>, Res) -> {ok, Res};
42
:-(
output(<<"NonEmptyString">>, Value) -> binary_to_non_empty_string(Value);
43
:-(
output(<<"PosInt">>, Value) -> validate_pos_integer(Value);
44
:-(
output(<<"NonNegInt">>, Value) -> validate_non_neg_integer(Value);
45 output(Ty, V) ->
46
:-(
error_logger:info_report({output_generic_scalar, Ty, V}),
47
:-(
{ok, V}.
48
49 jid_from_binary(Value) ->
50
:-(
case jid:from_binary(Value) of
51 error ->
52
:-(
{error, failed_to_parse_jid};
53 #jid{luser = <<>>} ->
54
:-(
{error, jid_without_local_part};
55 Jid ->
56
:-(
{ok, Jid}
57 end.
58
59 bare_jid_from_binary(Value) ->
60
:-(
case jid:from_binary(Value) of
61 error ->
62
:-(
{error, failed_to_parse_jid};
63 #jid{luser = <<>>} ->
64
:-(
{error, jid_without_local_part};
65 Jid = #jid{lresource = <<>>} ->
66
:-(
{ok, Jid};
67 #jid{} ->
68
:-(
{error, jid_with_resource}
69 end.
70
71 full_jid_from_binary(Value) ->
72
:-(
case jid:from_binary(Value) of
73 error ->
74
:-(
{error, failed_to_parse_jid};
75 #jid{luser = <<>>} ->
76
:-(
{error, jid_without_local_part};
77 #jid{lresource = <<>>} ->
78
:-(
{error, jid_without_resource};
79 Jid ->
80
:-(
{ok, Jid}
81 end.
82
83 user_from_binary(<<>>) ->
84
:-(
{error, empty_user_name};
85 user_from_binary(Value) ->
86
:-(
case jid:nodeprep(Value) of
87 error ->
88
:-(
{error, failed_to_parse_user_name};
89 User ->
90
:-(
{ok, User}
91 end.
92
93 room_from_binary(<<>>) ->
94
:-(
{error, empty_room_name};
95 room_from_binary(Value) ->
96
:-(
case jid:nodeprep(Value) of
97 error ->
98
:-(
{error, failed_to_parse_room_name};
99 Room ->
100
:-(
{ok, Room}
101 end.
102
103 domain_from_binary(<<>>) ->
104
:-(
{error, empty_domain_name};
105 domain_from_binary(Value) ->
106
:-(
case jid:nameprep(Value) of
107 error ->
108
:-(
{error, failed_to_parse_domain_name};
109 Domain ->
110
:-(
{ok, Domain}
111 end.
112
113 resource_from_binary(<<>>) ->
114
:-(
{error, empty_resource_name};
115 resource_from_binary(Value) ->
116
:-(
case jid:resourceprep(Value) of
117 error ->
118
:-(
{error, failed_to_parse_resource_name};
119 Res ->
120
:-(
{ok, Res}
121 end.
122
123 node_from_binary(<<>>) ->
124
:-(
{error, empty_node_name};
125 node_from_binary(NodeName) ->
126
:-(
case string:lexemes(binary_to_list(NodeName), "@") of
127 [_Name, _Host] ->
128
:-(
{ok, binary_to_atom(NodeName)};
129 ["self"] ->
130
:-(
{ok, node()};
131 _ ->
132
:-(
{error, incorrect_node_name}
133 end.
134
135 binary_to_microseconds(DT) ->
136
:-(
case mod_mam_utils:maybe_microseconds(DT) of
137 undefined ->
138
:-(
{error, failed_to_parse_datetime};
139 Microseconds ->
140
:-(
{ok, Microseconds}
141 end.
142
143 non_empty_string_to_binary(<<>>) ->
144
:-(
{error, "Given string is empty"};
145 non_empty_string_to_binary(String) ->
146
:-(
{ok, String}.
147
148 binary_to_non_empty_string(<<>>) ->
149
:-(
{error, "Empty binary cannot be converted to NonEmptyString"};
150 binary_to_non_empty_string(Val) ->
151
:-(
{ok, Val}.
152
153 validate_pos_integer(PosInt) when is_integer(PosInt), PosInt > 0 ->
154
:-(
{ok, PosInt};
155 validate_pos_integer(_Value) ->
156
:-(
{error, "Value is not a positive integer"}.
157
158 validate_non_neg_integer(NonNegInt) when is_integer(NonNegInt), NonNegInt >= 0 ->
159
:-(
{ok, NonNegInt};
160 validate_non_neg_integer(_Value) ->
161
:-(
{error, "Value is not a non-negative integer"}.
162
163 microseconds_to_binary(Microseconds) ->
164
:-(
Opts = [{offset, "Z"}, {unit, microsecond}],
165
:-(
list_to_binary(calendar:system_time_to_rfc3339(Microseconds, Opts)).
Line Hits Source