2010-09-20 19:11:31 -07:00
|
|
|
%% The MIT License
|
|
|
|
|
2013-03-10 20:30:24 -07:00
|
|
|
%% Copyright (c) 2010-2013 alisdair sullivan <alisdairsullivan@yahoo.ca>
|
2010-09-20 19:11:31 -07:00
|
|
|
|
|
|
|
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
%% of this software and associated documentation files (the "Software"), to deal
|
|
|
|
%% in the Software without restriction, including without limitation the rights
|
|
|
|
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
%% copies of the Software, and to permit persons to whom the Software is
|
|
|
|
%% furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
%% The above copyright notice and this permission notice shall be included in
|
|
|
|
%% all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
%% THE SOFTWARE.
|
|
|
|
|
|
|
|
|
2013-03-06 01:37:08 -08:00
|
|
|
-module(jsx_config).
|
2010-09-20 19:11:31 -07:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
-export([parse_config/1]).
|
2013-03-04 23:48:34 -08:00
|
|
|
-export([config_to_list/1]).
|
2013-02-12 11:54:42 -08:00
|
|
|
-export([extract_config/1, valid_flags/0]).
|
2011-09-14 06:44:52 -07:00
|
|
|
|
2013-03-04 17:00:34 -08:00
|
|
|
-ifdef(TEST).
|
2013-03-04 20:40:39 -08:00
|
|
|
-export([fake_error_handler/3]).
|
2013-03-04 17:00:34 -08:00
|
|
|
-endif.
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
-include("jsx_config.hrl").
|
2010-09-20 19:11:31 -07:00
|
|
|
|
2010-09-22 23:22:55 -07:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
%% parsing of jsx config
|
|
|
|
parse_config(Config) ->
|
|
|
|
parse_config(Config, #config{}).
|
2011-10-19 06:51:36 -07:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([], Config) ->
|
|
|
|
Config;
|
|
|
|
parse_config([replaced_bad_utf8|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{replaced_bad_utf8=true});
|
|
|
|
parse_config([escaped_forward_slashes|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{escaped_forward_slashes=true});
|
2013-07-10 05:41:19 +00:00
|
|
|
parse_config([stream|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{stream=true});
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([single_quoted_strings|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{single_quoted_strings=true});
|
|
|
|
parse_config([unescaped_jsonp|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{unescaped_jsonp=true});
|
|
|
|
parse_config([comments|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{comments=true});
|
|
|
|
parse_config([escaped_strings|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{escaped_strings=true});
|
|
|
|
parse_config([dirty_strings|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{dirty_strings=true});
|
|
|
|
parse_config([ignored_bad_escapes|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{ignored_bad_escapes=true});
|
|
|
|
parse_config([relax|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{
|
2012-03-31 21:58:18 -07:00
|
|
|
replaced_bad_utf8 = true,
|
|
|
|
single_quoted_strings = true,
|
2012-03-29 00:24:31 -07:00
|
|
|
comments = true,
|
2012-03-31 21:58:18 -07:00
|
|
|
ignored_bad_escapes = true
|
2012-03-29 00:24:31 -07:00
|
|
|
});
|
2013-03-04 20:40:39 -08:00
|
|
|
parse_config([{error_handler, ErrorHandler}|Rest] = Options, Config) when is_function(ErrorHandler, 3) ->
|
2013-03-04 17:00:34 -08:00
|
|
|
case Config#config.error_handler of
|
|
|
|
false -> parse_config(Rest, Config#config{error_handler=ErrorHandler})
|
|
|
|
; _ -> erlang:error(badarg, [Options, Config])
|
|
|
|
end;
|
2013-03-04 23:13:23 -08:00
|
|
|
parse_config([{incomplete_handler, IncompleteHandler}|Rest] = Options, Config) when is_function(IncompleteHandler, 3) ->
|
|
|
|
case Config#config.incomplete_handler of
|
|
|
|
false -> parse_config(Rest, Config#config{incomplete_handler=IncompleteHandler})
|
|
|
|
; _ -> erlang:error(badarg, [Options, Config])
|
|
|
|
end;
|
2012-03-31 21:58:18 -07:00
|
|
|
%% deprecated flags
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([loose_unicode|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{replaced_bad_utf8=true});
|
|
|
|
parse_config([escape_forward_slash|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{escaped_forward_slashes=true});
|
|
|
|
parse_config([single_quotes|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{single_quoted_strings=true});
|
|
|
|
parse_config([no_jsonp_escapes|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{unescaped_jsonp=true});
|
|
|
|
parse_config([json_escape|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{escaped_strings=true});
|
|
|
|
parse_config([ignore_bad_escapes|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{ignored_bad_escapes=true});
|
|
|
|
parse_config(Options, Config) ->
|
|
|
|
erlang:error(badarg, [Options, Config]).
|
2011-10-19 06:51:36 -07:00
|
|
|
|
|
|
|
|
2013-03-04 23:48:34 -08:00
|
|
|
config_to_list(Config) ->
|
|
|
|
lists:map(
|
2013-03-11 01:13:01 -07:00
|
|
|
fun ({error_handler, F}) -> {error_handler, F};
|
2013-03-04 23:48:34 -08:00
|
|
|
({incomplete_handler, F}) -> {incomplete_handler, F};
|
|
|
|
({Key, true}) -> Key
|
|
|
|
end,
|
|
|
|
lists:filter(
|
|
|
|
fun({_, false}) -> false; (_) -> true end,
|
|
|
|
lists:zip(record_info(fields, config), tl(tuple_to_list(Config)))
|
|
|
|
)
|
|
|
|
).
|
|
|
|
|
|
|
|
|
2012-03-15 23:06:19 -07:00
|
|
|
valid_flags() ->
|
|
|
|
[
|
2012-03-31 21:58:18 -07:00
|
|
|
replaced_bad_utf8,
|
|
|
|
escaped_forward_slashes,
|
|
|
|
single_quoted_strings,
|
|
|
|
unescaped_jsonp,
|
2012-03-24 19:42:00 -07:00
|
|
|
comments,
|
2012-03-31 21:58:18 -07:00
|
|
|
escaped_strings,
|
2012-03-29 00:18:53 -07:00
|
|
|
dirty_strings,
|
2012-03-31 21:58:18 -07:00
|
|
|
ignored_bad_escapes,
|
2013-07-10 05:41:19 +00:00
|
|
|
stream,
|
2012-03-31 21:58:18 -07:00
|
|
|
relax,
|
2013-03-04 17:00:34 -08:00
|
|
|
error_handler,
|
2013-03-04 23:48:34 -08:00
|
|
|
incomplete_handler,
|
2012-03-31 21:58:18 -07:00
|
|
|
%% deprecated flags
|
|
|
|
loose_unicode, %% replaced_bad_utf8
|
|
|
|
escape_forward_slash, %% escaped_forward_slashes
|
2013-02-05 20:25:41 -08:00
|
|
|
single_quotes, %% single_quoted_strings
|
2012-03-31 21:58:18 -07:00
|
|
|
no_jsonp_escapes, %% unescaped_jsonp
|
|
|
|
json_escape, %% escaped_strings
|
|
|
|
ignore_bad_escapes %% ignored_bad_escapes
|
2012-03-15 23:06:19 -07:00
|
|
|
].
|
2012-11-21 23:04:14 -08:00
|
|
|
|
2012-03-15 23:06:19 -07:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
extract_config(Config) ->
|
|
|
|
extract_parser_config(Config, []).
|
2012-03-04 18:40:00 -08:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
extract_parser_config([], Acc) -> Acc;
|
|
|
|
extract_parser_config([{K,V}|Rest], Acc) ->
|
2012-03-15 23:06:19 -07:00
|
|
|
case lists:member(K, valid_flags()) of
|
2013-02-12 11:54:42 -08:00
|
|
|
true -> extract_parser_config(Rest, [{K,V}] ++ Acc)
|
|
|
|
; false -> extract_parser_config(Rest, Acc)
|
2012-03-04 18:40:00 -08:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
extract_parser_config([K|Rest], Acc) ->
|
2012-03-15 23:06:19 -07:00
|
|
|
case lists:member(K, valid_flags()) of
|
2013-02-12 11:54:42 -08:00
|
|
|
true -> extract_parser_config(Rest, [K] ++ Acc)
|
|
|
|
; false -> extract_parser_config(Rest, Acc)
|
2012-03-04 18:40:00 -08:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-09-20 19:11:31 -07:00
|
|
|
%% eunit tests
|
|
|
|
-ifdef(TEST).
|
2011-08-10 01:44:38 -07:00
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
2010-09-20 19:11:31 -07:00
|
|
|
|
2011-09-14 06:44:52 -07:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
config_test_() ->
|
2012-03-29 06:03:14 -07:00
|
|
|
[
|
|
|
|
{"all flags",
|
|
|
|
?_assertEqual(
|
2013-02-12 11:54:42 -08:00
|
|
|
#config{
|
2012-03-31 21:58:18 -07:00
|
|
|
replaced_bad_utf8=true,
|
|
|
|
escaped_forward_slashes=true,
|
2013-07-10 05:41:19 +00:00
|
|
|
stream=true,
|
2012-03-31 21:58:18 -07:00
|
|
|
single_quoted_strings=true,
|
|
|
|
unescaped_jsonp=true,
|
2012-03-29 06:03:14 -07:00
|
|
|
comments=true,
|
|
|
|
dirty_strings=true,
|
2012-03-31 21:58:18 -07:00
|
|
|
ignored_bad_escapes=true
|
2013-02-05 20:29:38 -08:00
|
|
|
},
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([
|
2013-02-05 20:29:38 -08:00
|
|
|
replaced_bad_utf8,
|
|
|
|
escaped_forward_slashes,
|
2013-07-10 05:41:19 +00:00
|
|
|
stream,
|
2013-02-05 20:29:38 -08:00
|
|
|
single_quoted_strings,
|
|
|
|
unescaped_jsonp,
|
|
|
|
comments,
|
|
|
|
dirty_strings,
|
|
|
|
ignored_bad_escapes
|
|
|
|
])
|
2012-03-29 06:03:14 -07:00
|
|
|
)
|
|
|
|
},
|
|
|
|
{"relax flag",
|
|
|
|
?_assertEqual(
|
2013-02-12 11:54:42 -08:00
|
|
|
#config{
|
2012-03-31 21:58:18 -07:00
|
|
|
replaced_bad_utf8=true,
|
|
|
|
single_quoted_strings=true,
|
2012-03-29 06:03:14 -07:00
|
|
|
comments=true,
|
2012-03-31 21:58:18 -07:00
|
|
|
ignored_bad_escapes=true
|
2013-02-05 20:29:38 -08:00
|
|
|
},
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([relax])
|
2012-03-29 06:03:14 -07:00
|
|
|
)
|
2013-02-05 20:12:29 -08:00
|
|
|
},
|
2013-02-05 20:25:41 -08:00
|
|
|
{"deprecated flags", ?_assertEqual(
|
2013-02-12 11:54:42 -08:00
|
|
|
#config{
|
2013-02-05 20:25:41 -08:00
|
|
|
replaced_bad_utf8=true,
|
|
|
|
escaped_forward_slashes=true,
|
|
|
|
single_quoted_strings=true,
|
|
|
|
unescaped_jsonp=true,
|
|
|
|
escaped_strings=true,
|
|
|
|
ignored_bad_escapes=true
|
2013-02-05 20:29:38 -08:00
|
|
|
},
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([
|
2013-02-05 20:29:38 -08:00
|
|
|
loose_unicode,
|
|
|
|
escape_forward_slash,
|
|
|
|
single_quotes,
|
|
|
|
no_jsonp_escapes,
|
|
|
|
json_escape,
|
|
|
|
ignore_bad_escapes
|
|
|
|
])
|
2013-02-05 20:25:41 -08:00
|
|
|
)},
|
2013-03-04 17:00:34 -08:00
|
|
|
{"error_handler flag", ?_assertEqual(
|
2013-03-04 20:40:39 -08:00
|
|
|
#config{error_handler=fun ?MODULE:fake_error_handler/3},
|
|
|
|
parse_config([{error_handler, fun ?MODULE:fake_error_handler/3}])
|
2013-03-04 17:00:34 -08:00
|
|
|
)},
|
|
|
|
{"two error_handlers defined", ?_assertError(
|
|
|
|
badarg,
|
|
|
|
parse_config([
|
|
|
|
{error_handler, fun(_) -> true end},
|
|
|
|
{error_handler, fun(_) -> false end}
|
|
|
|
])
|
|
|
|
)},
|
2013-03-04 23:13:23 -08:00
|
|
|
{"incomplete_handler flag", ?_assertEqual(
|
|
|
|
#config{incomplete_handler=fun ?MODULE:fake_error_handler/3},
|
|
|
|
parse_config([{incomplete_handler, fun ?MODULE:fake_error_handler/3}])
|
|
|
|
)},
|
|
|
|
{"two incomplete_handlers defined", ?_assertError(
|
|
|
|
badarg,
|
|
|
|
parse_config([
|
|
|
|
{incomplete_handler, fun(_) -> true end},
|
|
|
|
{incomplete_handler, fun(_) -> false end}
|
|
|
|
])
|
|
|
|
)},
|
2013-02-12 11:54:42 -08:00
|
|
|
{"bad option flag", ?_assertError(badarg, parse_config([error]))}
|
2012-03-29 06:03:14 -07:00
|
|
|
].
|
2013-03-04 23:48:34 -08:00
|
|
|
|
|
|
|
|
|
|
|
config_to_list_test_() ->
|
|
|
|
[
|
|
|
|
{"empty config", ?_assertEqual(
|
|
|
|
[],
|
|
|
|
config_to_list(#config{})
|
|
|
|
)},
|
|
|
|
{"all flags", ?_assertEqual(
|
|
|
|
[
|
|
|
|
replaced_bad_utf8,
|
|
|
|
escaped_forward_slashes,
|
|
|
|
single_quoted_strings,
|
|
|
|
unescaped_jsonp,
|
|
|
|
comments,
|
|
|
|
dirty_strings,
|
|
|
|
ignored_bad_escapes,
|
2013-07-10 05:41:19 +00:00
|
|
|
stream
|
2013-03-04 23:48:34 -08:00
|
|
|
],
|
|
|
|
config_to_list(
|
|
|
|
#config{
|
|
|
|
replaced_bad_utf8=true,
|
|
|
|
escaped_forward_slashes=true,
|
2013-07-10 05:41:19 +00:00
|
|
|
stream=true,
|
2013-03-04 23:48:34 -08:00
|
|
|
single_quoted_strings=true,
|
|
|
|
unescaped_jsonp=true,
|
|
|
|
comments=true,
|
|
|
|
dirty_strings=true,
|
|
|
|
ignored_bad_escapes=true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)},
|
|
|
|
{"error handler", ?_assertEqual(
|
|
|
|
[{error_handler, fun ?MODULE:fake_error_handler/3}],
|
|
|
|
config_to_list(#config{error_handler=fun ?MODULE:fake_error_handler/3})
|
|
|
|
)},
|
|
|
|
{"incomplete handler", ?_assertEqual(
|
|
|
|
[{incomplete_handler, fun ?MODULE:fake_error_handler/3}],
|
|
|
|
config_to_list(#config{incomplete_handler=fun ?MODULE:fake_error_handler/3})
|
|
|
|
)}
|
|
|
|
].
|
|
|
|
|
2013-03-04 17:00:34 -08:00
|
|
|
|
2013-03-04 20:40:39 -08:00
|
|
|
fake_error_handler(_, _, _) -> ok.
|
2012-03-29 06:03:14 -07:00
|
|
|
|
|
|
|
|
2010-09-20 19:11:31 -07:00
|
|
|
-endif.
|