network-infra-utility 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +21 -0
  3. data/Gemfile +2 -0
  4. data/Gemfile.lock +70 -0
  5. data/Rakefile +1 -1
  6. data/bin/dns-query +834 -0
  7. data/bin/geo-doc +135 -0
  8. data/bin/geo-get +1 -1
  9. data/document/ASNum/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +242 -0
  10. data/document/DNSQuery/345/267/245/345/205/267/344/275/277/347/224/250/346/226/271/346/263/225.md +248 -0
  11. data/document/Geo/345/221/275/344/273/244/345/267/245/345/205/267/344/275/277/347/224/250/346/226/271/346/263/225.md +441 -0
  12. data/document/IP/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +297 -0
  13. data/document/MAC/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +296 -0
  14. data/document/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/344/275/277/347/224/250/346/226/271/346/263/225.md +764 -0
  15. data/network-infra-utility.gemspec +4 -2
  16. data/network.rb +3 -1
  17. data/service/geodb/geodb.rb +278 -1
  18. data/service/ssh/README.md +942 -0
  19. data/service/ssh/bin/ssh-client +198 -0
  20. data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/345/212/237/350/203/275/351/234/200/346/261/202/346/226/207/346/241/243.md +292 -0
  21. data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/350/257/246/347/273/206/350/256/276/350/256/241/346/226/207/346/241/243.md +1521 -0
  22. data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/350/275/257/344/273/266/350/256/276/350/256/241/346/226/207/346/241/243.md +2493 -0
  23. data/service/ssh/ext/ssh_core/bin/ssh_core.cmd +28 -0
  24. data/service/ssh/ext/ssh_core/config/sys.config +0 -0
  25. data/service/ssh/ext/ssh_core/config/vm.args +0 -0
  26. data/service/ssh/ext/ssh_core/local_deps/jsx/CHECKSUM +1 -0
  27. data/service/ssh/ext/ssh_core/local_deps/jsx/LICENSE +21 -0
  28. data/service/ssh/ext/ssh_core/local_deps/jsx/README.md +696 -0
  29. data/service/ssh/ext/ssh_core/local_deps/jsx/VERSION +1 -0
  30. data/service/ssh/ext/ssh_core/local_deps/jsx/contents.tar.gz +0 -0
  31. data/service/ssh/ext/ssh_core/local_deps/jsx/metadata.config +15 -0
  32. data/service/ssh/ext/ssh_core/local_deps/jsx/rebar.config +17 -0
  33. data/service/ssh/ext/ssh_core/local_deps/jsx/rebar.lock +1 -0
  34. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx.app.src +10 -0
  35. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx.erl +506 -0
  36. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_config.erl +393 -0
  37. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_config.hrl +18 -0
  38. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_consult.erl +81 -0
  39. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_decoder.erl +1909 -0
  40. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_encoder.erl +116 -0
  41. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_parser.erl +1214 -0
  42. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_to_json.erl +408 -0
  43. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_to_term.erl +389 -0
  44. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_verify.erl +121 -0
  45. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx.erl +506 -0
  46. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_config.erl +393 -0
  47. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_config.hrl +18 -0
  48. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_consult.erl +81 -0
  49. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_decoder.erl +1909 -0
  50. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_encoder.erl +116 -0
  51. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_parser.erl +1214 -0
  52. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_to_json.erl +408 -0
  53. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_to_term.erl +389 -0
  54. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_verify.erl +121 -0
  55. data/service/ssh/ext/ssh_core/rebar.config +24 -0
  56. data/service/ssh/ext/ssh_core/rebar.lock +1 -0
  57. data/service/ssh/ext/ssh_core/src/ssh_auth_engine.erl +156 -0
  58. data/service/ssh/ext/ssh_core/src/ssh_channel_stm.erl +232 -0
  59. data/service/ssh/ext/ssh_core/src/ssh_codec.erl +83 -0
  60. data/service/ssh/ext/ssh_core/src/ssh_conn_sup.erl +48 -0
  61. data/service/ssh/ext/ssh_core/src/ssh_conn_worker.erl +535 -0
  62. data/service/ssh/ext/ssh_core/src/ssh_core.app.src +36 -0
  63. data/service/ssh/ext/ssh_core/src/ssh_core_app.erl +11 -0
  64. data/service/ssh/ext/ssh_core/src/ssh_core_sup.erl +46 -0
  65. data/service/ssh/ext/ssh_core/src/ssh_infra_sup.erl +104 -0
  66. data/service/ssh/ext/ssh_core/src/ssh_ipc.hrl +80 -0
  67. data/service/ssh/ext/ssh_core/src/ssh_ipc_coalesce.erl +94 -0
  68. data/service/ssh/ext/ssh_core/src/ssh_ipc_gateway.erl +467 -0
  69. data/service/ssh/ext/ssh_core/src/ssh_ipc_proto.erl +95 -0
  70. data/service/ssh/ext/ssh_core/src/ssh_jump_chain.erl +101 -0
  71. data/service/ssh/ext/ssh_core/src/ssh_keepalive_mgr.erl +222 -0
  72. data/service/ssh/ext/ssh_core/src/ssh_known_hosts_proxy.erl +67 -0
  73. data/service/ssh/ext/ssh_core/src/ssh_port_fwd.erl +225 -0
  74. data/service/ssh/ext/ssh_core/src/ssh_sftp_session.erl +250 -0
  75. data/service/ssh/ext/ssh_core/src/ssh_sftp_sup.erl +62 -0
  76. data/service/ssh/ext/ssh_core_rs/Cargo.lock +2345 -0
  77. data/service/ssh/ext/ssh_core_rs/Cargo.toml +30 -0
  78. data/service/ssh/ext/ssh_core_rs/bin/ssh_core_rs +34 -0
  79. data/service/ssh/ext/ssh_core_rs/bin/ssh_core_rs.cmd +40 -0
  80. data/service/ssh/ext/ssh_core_rs/src/channel.rs +296 -0
  81. data/service/ssh/ext/ssh_core_rs/src/coalesce.rs +143 -0
  82. data/service/ssh/ext/ssh_core_rs/src/codec.rs +71 -0
  83. data/service/ssh/ext/ssh_core_rs/src/conn.rs +628 -0
  84. data/service/ssh/ext/ssh_core_rs/src/gateway.rs +389 -0
  85. data/service/ssh/ext/ssh_core_rs/src/handler.rs +293 -0
  86. data/service/ssh/ext/ssh_core_rs/src/keepalive.rs +194 -0
  87. data/service/ssh/ext/ssh_core_rs/src/main.rs +351 -0
  88. data/service/ssh/ext/ssh_core_rs/src/portfwd.rs +378 -0
  89. data/service/ssh/ext/ssh_core_rs/src/proto.rs +198 -0
  90. data/service/ssh/ext/ssh_core_rs/src/sftp.rs +294 -0
  91. data/service/ssh/lib/network_infra_utility/ssh/automation/macro_engine.rb +213 -0
  92. data/service/ssh/lib/network_infra_utility/ssh/client.rb +257 -0
  93. data/service/ssh/lib/network_infra_utility/ssh/config/schema.rb +90 -0
  94. data/service/ssh/lib/network_infra_utility/ssh/config/settings.rb +103 -0
  95. data/service/ssh/lib/network_infra_utility/ssh/config/store.rb +90 -0
  96. data/service/ssh/lib/network_infra_utility/ssh/ipc/coalesce.rb +83 -0
  97. data/service/ssh/lib/network_infra_utility/ssh/ipc/errors.rb +36 -0
  98. data/service/ssh/lib/network_infra_utility/ssh/ipc/router.rb +212 -0
  99. data/service/ssh/lib/network_infra_utility/ssh/ipc/transport.rb +81 -0
  100. data/service/ssh/lib/network_infra_utility/ssh/security/host_key.rb +211 -0
  101. data/service/ssh/lib/network_infra_utility/ssh/security/vault.rb +211 -0
  102. data/service/ssh/lib/network_infra_utility/ssh/session/history.rb +56 -0
  103. data/service/ssh/lib/network_infra_utility/ssh/session/manager.rb +92 -0
  104. data/service/ssh/lib/network_infra_utility/ssh/session/session.rb +109 -0
  105. data/service/ssh/lib/network_infra_utility/ssh/session/tree.rb +95 -0
  106. data/service/ssh/lib/network_infra_utility/ssh/terminal/ansi_parser.rb +435 -0
  107. data/service/ssh/lib/network_infra_utility/ssh/terminal/buffer.rb +78 -0
  108. data/service/ssh/lib/network_infra_utility/ssh/terminal/emulator.rb +159 -0
  109. data/service/ssh/lib/network_infra_utility/ssh/terminal/logger.rb +195 -0
  110. data/service/ssh/lib/network_infra_utility/ssh/terminal/screen.rb +212 -0
  111. data/service/ssh/lib/network_infra_utility/ssh/terminal/theme.rb +127 -0
  112. data/service/ssh/lib/network_infra_utility/ssh/version.rb +7 -0
  113. data/service/ssh/lib/network_infra_utility/ssh.rb +44 -0
  114. data/support/basic/as_num.rb +221 -0
  115. data/support/basic/mac_address.rb +281 -0
  116. data/version.rb +1 -1
  117. metadata +138 -1
@@ -0,0 +1,393 @@
1
+ %% The MIT License
2
+
3
+ %% Copyright (c) 2010-2013 alisdair sullivan <alisdairsullivan@yahoo.ca>
4
+
5
+ %% Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ %% of this software and associated documentation files (the "Software"), to deal
7
+ %% in the Software without restriction, including without limitation the rights
8
+ %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ %% copies of the Software, and to permit persons to whom the Software is
10
+ %% furnished to do so, subject to the following conditions:
11
+
12
+ %% The above copyright notice and this permission notice shall be included in
13
+ %% all copies or substantial portions of the Software.
14
+
15
+ %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ %% THE SOFTWARE.
22
+
23
+
24
+ -module(jsx_config).
25
+
26
+ -export([parse_config/1]).
27
+ -export([config_to_list/1]).
28
+ -export([extract_config/1, valid_flags/0]).
29
+
30
+ -ifdef(TEST).
31
+ -export([fake_error_handler/3]).
32
+ -endif.
33
+
34
+ -include("jsx_config.hrl").
35
+
36
+ -type handler_type(Handler) ::
37
+ fun((jsx:json_text() | end_stream |
38
+ jsx:json_term(),
39
+ {decoder, any(), module(), null | list(), list()} |
40
+ {parser, any(), module(), list()} |
41
+ {encoder, any(), module()},
42
+ list({pre_encode, fun((any()) -> any())} |
43
+ {error_handler, Handler} |
44
+ {incomplete_handler, Handler} |
45
+ atom())) -> any()).
46
+ -type handler() :: handler_type(handler()).
47
+ -export_type([handler/0]).
48
+
49
+ -type config() :: #config{}.
50
+ -export_type([config/0]).
51
+
52
+ -type option() :: valid_flag()
53
+ | {valid_flag(), boolean()}
54
+ | {strict, [strict_option()]}
55
+ | {error_handler, fun((any(), any(), any()) -> ok)}
56
+ | {incomplete_handler, fun((any(), any(), any()) -> ok)}
57
+ | {return_maps, boolean()}
58
+ | {labels, label_option()}
59
+ | {space, non_neg_integer()}
60
+ | {indent, non_neg_integer()}
61
+ | {depth, non_neg_integer()}
62
+ | {newline, binary()}
63
+ | legacy_option()
64
+ | {legacy_option(), boolean()}.
65
+ -type legacy_option() :: strict_comments
66
+ | strict_commas
67
+ | strict_utf8
68
+ | strict_single_quotes
69
+ | strict_escapes
70
+ | strict_control_codes.
71
+
72
+ -type options() :: [option()].
73
+ -export_type([options/0]).
74
+
75
+ -type strict_option() :: comments
76
+ | trailing_commas
77
+ | utf8
78
+ | single_quotes
79
+ | escapes
80
+ | control_codes.
81
+ -type label_option() :: binary
82
+ | atom
83
+ | existing_atom
84
+ | attempt_atom.
85
+
86
+ -type valid_flag() :: escaped_forward_slashes
87
+ | escaped_strings
88
+ | unescaped_jsonp
89
+ | dirty_strings
90
+ | multi_term
91
+ | return_tail
92
+ | repeat_keys
93
+ | strict
94
+ | stream
95
+ | uescape
96
+ | error_handler
97
+ | incomplete_handler.
98
+
99
+ %% parsing of jsx config
100
+ -spec parse_config(Config::options()) -> config().
101
+
102
+ parse_config(Config) -> parse_config(Config, #config{}).
103
+
104
+ parse_config([], Config) -> Config;
105
+ parse_config([escaped_forward_slashes|Rest], Config) ->
106
+ parse_config(Rest, Config#config{escaped_forward_slashes=true});
107
+ parse_config([escaped_strings|Rest], Config) ->
108
+ parse_config(Rest, Config#config{escaped_strings=true});
109
+ parse_config([unescaped_jsonp|Rest], Config) ->
110
+ parse_config(Rest, Config#config{unescaped_jsonp=true});
111
+ parse_config([dirty_strings|Rest], Config) ->
112
+ parse_config(Rest, Config#config{dirty_strings=true});
113
+ parse_config([multi_term|Rest], Config) ->
114
+ parse_config(Rest, Config#config{multi_term=true});
115
+ parse_config([return_tail|Rest], Config) ->
116
+ parse_config(Rest, Config#config{return_tail=true});
117
+ %% retained for backwards compat, now does nothing however
118
+ parse_config([repeat_keys|Rest], Config) ->
119
+ parse_config(Rest, Config);
120
+ parse_config([uescape|Rest], Config) ->
121
+ parse_config(Rest, Config#config{uescape=true});
122
+ parse_config([strict|Rest], Config) ->
123
+ parse_config(Rest, Config#config{
124
+ strict_comments=true,
125
+ strict_commas=true,
126
+ strict_utf8=true,
127
+ strict_single_quotes=true,
128
+ strict_escapes=true,
129
+ strict_control_codes=true
130
+ });
131
+ parse_config([{strict, Strict}|Rest], Config) ->
132
+ parse_strict(Strict, Rest, Config);
133
+ parse_config([stream|Rest], Config) ->
134
+ parse_config(Rest, Config#config{stream=true});
135
+ parse_config([{error_handler, ErrorHandler}|Rest] = Options, Config) when is_function(ErrorHandler, 3) ->
136
+ case Config#config.error_handler of
137
+ false -> parse_config(Rest, Config#config{error_handler=ErrorHandler})
138
+ ; _ -> erlang:error(badarg, [Options, Config])
139
+ end;
140
+ parse_config([{incomplete_handler, IncompleteHandler}|Rest] = Options, Config) when is_function(IncompleteHandler, 3) ->
141
+ case Config#config.incomplete_handler of
142
+ false -> parse_config(Rest, Config#config{incomplete_handler=IncompleteHandler})
143
+ ; _ -> erlang:error(badarg, [Options, Config])
144
+ end;
145
+ parse_config(_Options, _Config) -> erlang:error(badarg).
146
+
147
+
148
+ parse_strict([], Rest, Config) -> parse_config(Rest, Config);
149
+ parse_strict([comments|Strict], Rest, Config) ->
150
+ parse_strict(Strict, Rest, Config#config{strict_comments=true});
151
+ parse_strict([trailing_commas|Strict], Rest, Config) ->
152
+ parse_strict(Strict, Rest, Config#config{strict_commas=true});
153
+ parse_strict([utf8|Strict], Rest, Config) ->
154
+ parse_strict(Strict, Rest, Config#config{strict_utf8=true});
155
+ parse_strict([single_quotes|Strict], Rest, Config) ->
156
+ parse_strict(Strict, Rest, Config#config{strict_single_quotes=true});
157
+ parse_strict([escapes|Strict], Rest, Config) ->
158
+ parse_strict(Strict, Rest, Config#config{strict_escapes=true});
159
+ parse_strict([control_codes|Strict], Rest, Config) ->
160
+ parse_strict(Strict, Rest, Config#config{strict_control_codes=true});
161
+ parse_strict(_Strict, _Rest, _Config) ->
162
+ erlang:error(badarg).
163
+
164
+
165
+
166
+ -spec config_to_list(Config::config()) -> options().
167
+
168
+ config_to_list(Config) ->
169
+ reduce_config(lists:map(
170
+ fun ({error_handler, F}) -> {error_handler, F};
171
+ ({incomplete_handler, F}) -> {incomplete_handler, F};
172
+ ({Key, true}) -> Key
173
+ end,
174
+ lists:filter(
175
+ fun({_, false}) -> false; (_) -> true end,
176
+ lists:zip(record_info(fields, config), tl(tuple_to_list(Config)))
177
+ )
178
+ )).
179
+
180
+
181
+ reduce_config(Input) -> reduce_config(Input, [], []).
182
+
183
+ reduce_config([], Output, Strict) ->
184
+ case length(Strict) of
185
+ 0 -> lists:reverse(Output);
186
+ 5 -> lists:reverse(Output) ++ [strict];
187
+ _ -> lists:reverse(Output) ++ [{strict, lists:reverse(Strict)}]
188
+ end;
189
+ reduce_config([strict_comments|Input], Output, Strict) ->
190
+ reduce_config(Input, Output, [comments] ++ Strict);
191
+ reduce_config([strict_utf8|Input], Output, Strict) ->
192
+ reduce_config(Input, Output, [utf8] ++ Strict);
193
+ reduce_config([strict_single_quotes|Input], Output, Strict) ->
194
+ reduce_config(Input, Output, [single_quotes] ++ Strict);
195
+ reduce_config([strict_escapes|Input], Output, Strict) ->
196
+ reduce_config(Input, Output, [escapes] ++ Strict);
197
+ reduce_config([strict_control_codes|Input], Output, Strict) ->
198
+ reduce_config(Input, Output, [control_codes] ++ Strict);
199
+ reduce_config([Else|Input], Output, Strict) ->
200
+ reduce_config(Input, [Else] ++ Output, Strict).
201
+
202
+
203
+ -spec valid_flags() -> [valid_flag(), ...].
204
+
205
+ valid_flags() ->
206
+ [
207
+ escaped_forward_slashes,
208
+ escaped_strings,
209
+ unescaped_jsonp,
210
+ dirty_strings,
211
+ multi_term,
212
+ return_tail,
213
+ repeat_keys,
214
+ strict,
215
+ stream,
216
+ uescape,
217
+ error_handler,
218
+ incomplete_handler
219
+ ].
220
+
221
+
222
+ -spec extract_config(Config::options()) -> options().
223
+
224
+ extract_config(Config) ->
225
+ extract_parser_config(Config, []).
226
+
227
+ extract_parser_config([], Acc) -> Acc;
228
+ extract_parser_config([{K,V}|Rest], Acc) ->
229
+ case lists:member(K, valid_flags()) of
230
+ true -> extract_parser_config(Rest, [{K,V}] ++ Acc)
231
+ ; false -> extract_parser_config(Rest, Acc)
232
+ end;
233
+ extract_parser_config([K|Rest], Acc) ->
234
+ case lists:member(K, valid_flags()) of
235
+ true -> extract_parser_config(Rest, [K] ++ Acc)
236
+ ; false -> extract_parser_config(Rest, Acc)
237
+ end.
238
+
239
+
240
+ %% eunit tests
241
+ -ifdef(TEST).
242
+ -include_lib("eunit/include/eunit.hrl").
243
+
244
+
245
+ config_test_() ->
246
+ [
247
+ {"all flags",
248
+ ?_assertEqual(
249
+ #config{escaped_forward_slashes = true,
250
+ escaped_strings = true,
251
+ unescaped_jsonp = true,
252
+ dirty_strings = true,
253
+ multi_term = true,
254
+ return_tail = true,
255
+ strict_comments = true,
256
+ strict_commas = true,
257
+ strict_utf8 = true,
258
+ strict_single_quotes = true,
259
+ strict_escapes = true,
260
+ strict_control_codes = true,
261
+ stream = true,
262
+ uescape = true
263
+ },
264
+ parse_config([dirty_strings,
265
+ escaped_forward_slashes,
266
+ escaped_strings,
267
+ unescaped_jsonp,
268
+ multi_term,
269
+ return_tail,
270
+ repeat_keys,
271
+ strict,
272
+ stream,
273
+ uescape
274
+ ])
275
+ )
276
+ },
277
+ {"strict flag",
278
+ ?_assertEqual(
279
+ #config{strict_comments = true,
280
+ strict_commas = true,
281
+ strict_utf8 = true,
282
+ strict_single_quotes = true,
283
+ strict_escapes = true,
284
+ strict_control_codes = true
285
+ },
286
+ parse_config([strict])
287
+ )
288
+ },
289
+ {"strict selective",
290
+ ?_assertEqual(
291
+ #config{strict_comments = true},
292
+ parse_config([{strict, [comments]}])
293
+ )
294
+ },
295
+ {"strict expanded",
296
+ ?_assertEqual(
297
+ #config{strict_comments = true,
298
+ strict_utf8 = true,
299
+ strict_single_quotes = true,
300
+ strict_escapes = true
301
+ },
302
+ parse_config([{strict, [comments, utf8, single_quotes, escapes]}])
303
+ )
304
+ },
305
+ {"error_handler flag", ?_assertEqual(
306
+ #config{error_handler=fun ?MODULE:fake_error_handler/3},
307
+ parse_config([{error_handler, fun ?MODULE:fake_error_handler/3}])
308
+ )},
309
+ {"two error_handlers defined", ?_assertError(
310
+ badarg,
311
+ parse_config([
312
+ {error_handler, fun(_, _, _) -> true end},
313
+ {error_handler, fun(_, _, _) -> false end}
314
+ ])
315
+ )},
316
+ {"incomplete_handler flag", ?_assertEqual(
317
+ #config{incomplete_handler=fun ?MODULE:fake_error_handler/3},
318
+ parse_config([{incomplete_handler, fun ?MODULE:fake_error_handler/3}])
319
+ )},
320
+ {"two incomplete_handlers defined", ?_assertError(
321
+ badarg,
322
+ parse_config([
323
+ {incomplete_handler, fun(_, _, _) -> true end},
324
+ {incomplete_handler, fun(_, _, _) -> false end}
325
+ ])
326
+ )},
327
+ {"bad option flag", ?_assertError(badarg, parse_config([this_flag_does_not_exist]))}
328
+ ].
329
+
330
+
331
+ config_to_list_test_() ->
332
+ [
333
+ {"empty config", ?_assertEqual(
334
+ [],
335
+ config_to_list(#config{})
336
+ )},
337
+ {"all flags", ?_assertEqual(
338
+ [dirty_strings,
339
+ escaped_forward_slashes,
340
+ escaped_strings,
341
+ multi_term,
342
+ stream,
343
+ uescape,
344
+ unescaped_jsonp,
345
+ strict
346
+ ],
347
+ config_to_list(
348
+ #config{escaped_forward_slashes = true,
349
+ escaped_strings = true,
350
+ unescaped_jsonp = true,
351
+ dirty_strings = true,
352
+ multi_term = true,
353
+ strict_comments = true,
354
+ strict_utf8 = true,
355
+ strict_single_quotes = true,
356
+ strict_escapes = true,
357
+ strict_control_codes = true,
358
+ stream = true,
359
+ uescape = true
360
+ }
361
+ )
362
+ )},
363
+ {"single strict", ?_assertEqual(
364
+ [{strict, [comments]}],
365
+ config_to_list(#config{strict_comments = true})
366
+ )},
367
+ {"multiple strict", ?_assertEqual(
368
+ [{strict, [utf8, single_quotes, escapes]}],
369
+ config_to_list(#config{strict_utf8 = true, strict_single_quotes = true, strict_escapes = true})
370
+ )},
371
+ {"all strict", ?_assertEqual(
372
+ [strict],
373
+ config_to_list(#config{strict_comments = true,
374
+ strict_utf8 = true,
375
+ strict_single_quotes = true,
376
+ strict_escapes = true,
377
+ strict_control_codes = true})
378
+ )},
379
+ {"error handler", ?_assertEqual(
380
+ [{error_handler, fun ?MODULE:fake_error_handler/3}],
381
+ config_to_list(#config{error_handler=fun ?MODULE:fake_error_handler/3})
382
+ )},
383
+ {"incomplete handler", ?_assertEqual(
384
+ [{incomplete_handler, fun ?MODULE:fake_error_handler/3}],
385
+ config_to_list(#config{incomplete_handler=fun ?MODULE:fake_error_handler/3})
386
+ )}
387
+ ].
388
+
389
+
390
+ fake_error_handler(_, _, _) -> ok.
391
+
392
+
393
+ -endif.
@@ -0,0 +1,18 @@
1
+ -record(config, {
2
+ dirty_strings = false :: boolean(),
3
+ escaped_forward_slashes = false :: boolean(),
4
+ escaped_strings = false :: boolean(),
5
+ multi_term = false :: boolean(),
6
+ strict_comments = false :: boolean(),
7
+ strict_commas = false :: boolean(),
8
+ strict_utf8 = false :: boolean(),
9
+ strict_single_quotes = false :: boolean(),
10
+ strict_escapes = false :: boolean(),
11
+ strict_control_codes = false :: boolean(),
12
+ stream = false :: boolean(),
13
+ return_tail = false :: boolean(),
14
+ uescape = false :: boolean(),
15
+ unescaped_jsonp = false :: boolean(),
16
+ error_handler = false :: false | jsx_config:handler(),
17
+ incomplete_handler = false :: false | jsx_config:handler()
18
+ }).
@@ -0,0 +1,81 @@
1
+ %% The MIT License
2
+
3
+ %% Copyright (c) 2010-2015 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
4
+
5
+ %% Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ %% of this software and associated documentation files (the "Software"), to deal
7
+ %% in the Software without restriction, including without limitation the rights
8
+ %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ %% copies of the Software, and to permit persons to whom the Software is
10
+ %% furnished to do so, subject to the following conditions:
11
+
12
+ %% The above copyright notice and this permission notice shall be included in
13
+ %% all copies or substantial portions of the Software.
14
+
15
+ %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ %% THE SOFTWARE.
22
+
23
+
24
+ -module(jsx_consult).
25
+
26
+ -export([consult/2]).
27
+ -export([init/1, reset/1, handle_event/2]).
28
+
29
+
30
+ -record(config, {
31
+ labels = binary,
32
+ return_maps = false
33
+ }).
34
+
35
+ -type config() :: proplists:proplist().
36
+ -export_type([config/0]).
37
+
38
+ -type json_value() :: list(json_value())
39
+ | map()
40
+ | true
41
+ | false
42
+ | null
43
+ | integer()
44
+ | float()
45
+ | binary().
46
+ -export_type([json_value/0]).
47
+
48
+ opts(Opts) -> [return_maps, multi_term] ++ Opts.
49
+
50
+ -spec consult(File::file:name_all(), Config::config()) -> [json_value()].
51
+
52
+ consult(File, Config) when is_list(Config) ->
53
+ case file:read_file(File) of
54
+ {ok, Bin} ->
55
+ {Final, _, _} = (jsx:decoder(
56
+ ?MODULE,
57
+ opts(Config),
58
+ jsx_config:extract_config(opts(Config))
59
+ ))(Bin),
60
+ lists:reverse(Final);
61
+ {error, _} -> erlang:error(badarg)
62
+ end.
63
+
64
+
65
+ -type state() :: {[], config(), {list(), #config{}}}.
66
+ -spec init(Config::config()) -> state().
67
+
68
+ init(Config) -> {[], Config, jsx_to_term:start_term(Config)}.
69
+
70
+
71
+ -spec reset(State::state()) -> state().
72
+
73
+ reset({Acc, Config, _}) -> {Acc, Config, jsx_to_term:start_term(Config)}.
74
+
75
+
76
+ -spec handle_event(Event::any(), State::state()) -> state().
77
+
78
+ handle_event(end_json, {Acc, Config, State}) ->
79
+ {[jsx_to_term:get_value(State)] ++ Acc, Config, State};
80
+ handle_event(Event, {Acc, Config, State}) ->
81
+ {Acc, Config, jsx_to_term:handle_event(Event, State)}.