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