Skip to content

Commit 042ad00

Browse files
authored
Merge pull request #75 from sile/efmt-v0.3.0
Update `rebar3_efmt` to v0.3.0 and make it a project plugin
2 parents d49e284 + 7c593d8 commit 042ad00

File tree

9 files changed

+307
-232
lines changed

9 files changed

+307
-232
lines changed

rebar.config

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
%% -*- erlang -*-
2-
{erl_opts,
3-
[warnings_as_errors,
4-
warn_export_all,
5-
warn_untyped_record,
6-
inline,
7-
{platform_define, "^R[01][0-9]", 'NO_MAP_TYPE'},
8-
{platform_define, "^(R|17)", 'NO_DIALYZER_SPEC'}]}.
2+
{erl_opts, [warnings_as_errors,
3+
warn_export_all,
4+
warn_untyped_record,
5+
inline,
6+
{platform_define, "^R[01][0-9]", 'NO_MAP_TYPE'},
7+
{platform_define, "^(R|17)", 'NO_DIALYZER_SPEC'}]}.
98

109
{xref_checks, [fail_on_warning, undefined_function_calls]}.
1110

1211
{clean_files, [".eunit/*", "ebin/*.beam"]}.
1312

1413
{cover_enabled, true}.
1514

16-
{edoc_opts,
17-
[{dialyzer_specs, all},
18-
{report_missing_type, true},
19-
{report_type_mismatch, true},
20-
{pretty_print, erl_pp},
21-
{preprocess, true}]}.
15+
{edoc_opts, [{dialyzer_specs, all},
16+
{report_missing_type, true},
17+
{report_type_mismatch, true},
18+
{pretty_print, erl_pp},
19+
{preprocess, true}]}.
2220
{validate_app_modules, true}.
2321

2422
{shell, [{apps, [jsone]}]}.
2523

2624
{dialyzer, [{warnings, [error_handling, race_conditions, unmatched_returns, unknown, no_improper_lists]}]}.
2725

28-
{profiles,
29-
[{native, [{erl_opts, [{d, 'ENABLE_HIPE'}]}]}, {edown, [{edoc_opts, [{doclet, edown_doclet}]}, {deps, [edown]}]}]}.
26+
{profiles, [{native, [{erl_opts, [{d, 'ENABLE_HIPE'}]}]},
27+
{edown, [{edoc_opts, [{doclet, edown_doclet}]}, {deps, [edown]}]}]}.
3028

31-
{project_plugins, [covertool]}.
29+
{project_plugins, [covertool, rebar3_efmt]}.
3230
{cover_export_enabled, true}.
3331
{covertool, [{coverdata_files, ["ct.coverdata", "eunit.coverdata"]}]}.
34-
35-
{plugins, [rebar3_efmt]}.

src/jsone.app.src

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
{application,
2-
jsone,
3-
[{description, "Erlang JSON Library"},
4-
{vsn, "1.7.0"},
5-
{registered, []},
6-
{applications, [kernel, stdlib]},
7-
{licenses, ["MIT"]},
8-
{links, [{"GitHub", "https://github.com/sile/jsone"}]},
9-
{env, []}]}.
1+
{application, jsone,
2+
[{description, "Erlang JSON Library"},
3+
{vsn, "1.7.0"},
4+
{registered, []},
5+
{applications, [kernel, stdlib]},
6+
{licenses, ["MIT"]},
7+
{links, [{"GitHub", "https://github.com/sile/jsone"}]},
8+
{env, []}]}.

src/jsone.erl

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,10 @@
2929
%%--------------------------------------------------------------------------------
3030
%% Exported API
3131
%%--------------------------------------------------------------------------------
32-
-export([decode/1,
33-
decode/2,
34-
try_decode/1,
35-
try_decode/2,
36-
encode/1,
37-
encode/2,
38-
try_encode/1,
39-
try_encode/2,
32+
-export([decode/1, decode/2,
33+
try_decode/1, try_decode/2,
34+
encode/1, encode/2,
35+
try_encode/1, try_encode/2,
4036
term_to_json_string/1,
4137
ip_address_to_json_string/1]).
4238

@@ -203,7 +199,7 @@
203199
{object_key_type, string | scalar | value} |
204200
{space, non_neg_integer()} |
205201
{indent, non_neg_integer()} |
206-
{map_unknown_value, undefined | fun ((term()) -> {ok, json_value()} | error)} |
202+
{map_unknown_value, undefined | fun((term()) -> {ok, json_value()} | error)} |
207203
skip_undefined |
208204
common_option().
209205
%% `native_utf8': <br />
@@ -307,12 +303,13 @@
307303
%% The 'OTP_RELEASE' macro introduced at OTP-21,
308304
%% so we can use it for detecting whether the Erlang compiler supports new try/catch syntax or not.
309305
-define(CAPTURE_STACKTRACE, :__StackTrace).
310-
-define(GET_STACKTRACE, __StackTrace).
306+
-define(GET_STACKTRACE, __StackTrace).
311307
-else.
312308
-define(CAPTURE_STACKTRACE, ).
313-
-define(GET_STACKTRACE, erlang:get_stacktrace()).
309+
-define(GET_STACKTRACE, erlang:get_stacktrace()).
314310
-endif.
315311

312+
316313
%%--------------------------------------------------------------------------------
317314
%% Exported Functions
318315
%%--------------------------------------------------------------------------------
@@ -321,6 +318,7 @@
321318
decode(Json) ->
322319
decode(Json, []).
323320

321+
324322
%% @doc Decodes an erlang term from json text (a utf8 encoded binary)
325323
%%
326324
%% Raises an error exception if input is not valid json
@@ -346,11 +344,13 @@ decode(Json, Options) ->
346344
erlang:raise(error, Reason, [StackItem | ?GET_STACKTRACE])
347345
end.
348346

347+
349348
%% @equiv try_decode(Json, [])
350349
-spec try_decode(binary()) -> {ok, json_value(), Remainings :: binary()} | {error, {Reason :: term(), [stack_item()]}}.
351350
try_decode(Json) ->
352351
try_decode(Json, []).
353352

353+
354354
%% @doc Decodes an erlang term from json text (a utf8 encoded binary)
355355
%%
356356
%% ```
@@ -367,11 +367,13 @@ try_decode(Json) ->
367367
try_decode(Json, Options) ->
368368
jsone_decode:decode(Json, Options).
369369

370+
370371
%% @equiv encode(JsonValue, [])
371372
-spec encode(json_value()) -> binary().
372373
encode(JsonValue) ->
373374
encode(JsonValue, []).
374375

376+
375377
%% @doc Encodes an erlang term into json text (a utf8 encoded binary)
376378
%%
377379
%% Raises an error exception if input is not an instance of type `json_value()'
@@ -396,11 +398,13 @@ encode(JsonValue, Options) ->
396398
erlang:raise(error, Reason, [StackItem | ?GET_STACKTRACE])
397399
end.
398400

401+
399402
%% @equiv try_encode(JsonValue, [])
400403
-spec try_encode(json_value()) -> {ok, binary()} | {error, {Reason :: term(), [stack_item()]}}.
401404
try_encode(JsonValue) ->
402405
try_encode(JsonValue, []).
403406

407+
404408
%% @doc Encodes an erlang term into json text (a utf8 encoded binary)
405409
%%
406410
%% ```
@@ -416,11 +420,13 @@ try_encode(JsonValue) ->
416420
try_encode(JsonValue, Options) ->
417421
jsone_encode:encode(JsonValue, Options).
418422

423+
419424
%% @doc Converts the given term `X' to its string representation (i.e., the result of `io_lib:format("~p", [X])').
420425
-spec term_to_json_string(term()) -> {ok, json_string()} | error.
421426
term_to_json_string(X) ->
422427
{ok, list_to_binary(io_lib:format("~p", [X]))}.
423428

429+
424430
%% @doc Convert an IP address into a text representation.
425431
%%
426432
%% This function can be specified as the value of the `map_unknown_value' encoding option.
@@ -448,6 +454,7 @@ term_to_json_string(X) ->
448454
ip_address_to_json_string(X) ->
449455
jsone_inet:ip_address_to_json_string(X).
450456

457+
451458
%%--------------------------------------------------------------------------------
452459
%% Internal Functions
453460
%%--------------------------------------------------------------------------------

src/jsone_decode.erl

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,32 @@
6363

6464
-type decode_result() :: {ok, jsone:json_value(), Rest :: binary()} | {error, {Reason :: term(), [jsone:stack_item()]}}.
6565

66-
-record(decode_opt_v2,
67-
{object_format = ?DEFAULT_OBJECT_FORMAT :: tuple | proplist | map,
68-
allow_ctrl_chars = false :: boolean(),
69-
reject_invalid_utf8 = false :: boolean(),
70-
keys = binary :: 'binary' | 'atom' | 'existing_atom' | 'attempt_atom',
71-
undefined_as_null = false :: boolean(),
72-
duplicate_map_keys = first :: first | last}).
66+
-record(decode_opt_v2, {
67+
object_format = ?DEFAULT_OBJECT_FORMAT :: tuple | proplist | map,
68+
allow_ctrl_chars = false :: boolean(),
69+
reject_invalid_utf8 = false :: boolean(),
70+
keys = binary :: 'binary' | 'atom' | 'existing_atom' | 'attempt_atom',
71+
undefined_as_null = false :: boolean(),
72+
duplicate_map_keys = first :: first | last
73+
}).
7374
-define(OPT, #decode_opt_v2).
7475
-type opt() :: #decode_opt_v2{}.
7576

77+
7678
%%--------------------------------------------------------------------------------
7779
%% Exported Functions
7880
%%--------------------------------------------------------------------------------
7981
-spec decode(binary()) -> decode_result().
8082
decode(Json) ->
8183
decode(Json, []).
8284

85+
8386
-spec decode(binary(), [jsone:decode_option()]) -> decode_result().
8487
decode(<<Json/binary>>, Options) ->
8588
Opt = parse_options(Options),
8689
whitespace(Json, value, [], <<"">>, Opt).
8790

91+
8892
%%--------------------------------------------------------------------------------
8993
%% Internal Functions
9094
%%--------------------------------------------------------------------------------
@@ -101,6 +105,7 @@ next(<<Bin/binary>>, Value, [Next | Nexts], Buf, Opt) ->
101105
whitespace(Bin, {object_next, [{Key, Value} | Members]}, Nexts, Buf, Opt)
102106
end.
103107

108+
104109
-spec whitespace(binary(), whitespace_next(), [next()], binary(), opt()) -> decode_result().
105110
whitespace(<<$ , Bin/binary>>, Next, Nexts, Buf, Opt) ->
106111
whitespace(Bin, Next, Nexts, Buf, Opt);
@@ -128,6 +133,7 @@ whitespace(<<Bin/binary>>, Next, Nexts, Buf, Opt) ->
128133
object_next(Bin, Members, Nexts, Buf, Opt)
129134
end.
130135

136+
131137
-spec value(binary(), [next()], binary(), opt()) -> decode_result().
132138
value(<<"false", Bin/binary>>, Nexts, Buf, Opt) ->
133139
next(Bin, false, Nexts, Buf, Opt);
@@ -146,12 +152,14 @@ value(<<$", Bin/binary>>, Nexts, Buf, Opt) ->
146152
value(<<Bin/binary>>, Nexts, Buf, Opt) ->
147153
number(Bin, Nexts, Buf, Opt).
148154

155+
149156
-spec array(binary(), [next()], binary(), opt()) -> decode_result().
150157
array(<<$], Bin/binary>>, Nexts, Buf, Opt) ->
151158
next(Bin, [], Nexts, Buf, Opt);
152159
array(<<Bin/binary>>, Nexts, Buf, Opt) ->
153160
value(Bin, [{array_next, []} | Nexts], Buf, Opt).
154161

162+
155163
-spec array_next(binary(), [jsone:json_value()], [next()], binary(), opt()) -> decode_result().
156164
array_next(<<$], Bin/binary>>, Values, Nexts, Buf, Opt) ->
157165
next(Bin, lists:reverse(Values), Nexts, Buf, Opt);
@@ -160,18 +168,21 @@ array_next(<<$,, Bin/binary>>, Values, Nexts, Buf, Opt) ->
160168
array_next(Bin, Values, Nexts, Buf, Opt) ->
161169
?ERROR(array_next, [Bin, Values, Nexts, Buf, Opt]).
162170

171+
163172
-spec object(binary(), [next()], binary(), opt()) -> decode_result().
164173
object(<<$}, Bin/binary>>, Nexts, Buf, Opt) ->
165174
next(Bin, make_object([], Opt), Nexts, Buf, Opt);
166175
object(<<Bin/binary>>, Nexts, Buf, Opt) ->
167176
object_key(Bin, [], Nexts, Buf, Opt).
168177

178+
169179
-spec object_key(binary(), jsone:json_object_members(), [next()], binary(), opt()) -> decode_result().
170180
object_key(<<$", Bin/binary>>, Members, Nexts, Buf, Opt) ->
171181
string(Bin, byte_size(Buf), [{object_value, Members} | Nexts], Buf, Opt);
172182
object_key(<<Bin/binary>>, Members, Nexts, Buf, Opt) ->
173183
?ERROR(object_key, [Bin, Members, Nexts, Buf, Opt]).
174184

185+
175186
-spec object_value(binary(), jsone:json_string(), jsone:json_object_members(), [next()], binary(), opt()) ->
176187
decode_result().
177188
object_value(<<$:, Bin/binary>>, Key, Members, Nexts, Buf, Opt) ->
@@ -180,6 +191,8 @@ object_value(Bin, Key, Members, Nexts, Buf, Opt) ->
180191
?ERROR(object_value, [Bin, Key, Members, Nexts, Buf, Opt]).
181192

182193
-compile({inline, [object_key/2]}).
194+
195+
183196
object_key(Key, ?OPT{keys = binary}) ->
184197
Key;
185198
object_key(Key, ?OPT{keys = atom}) ->
@@ -194,6 +207,7 @@ object_key(Key, ?OPT{keys = attempt_atom}) ->
194207
Key
195208
end.
196209

210+
197211
-spec object_next(binary(), jsone:json_object_members(), [next()], binary(), opt()) -> decode_result().
198212
object_next(<<$}, Bin/binary>>, Members, Nexts, Buf, Opt) ->
199213
next(Bin, make_object(Members, Opt), Nexts, Buf, Opt);
@@ -202,10 +216,12 @@ object_next(<<$,, Bin/binary>>, Members, Nexts, Buf, Opt) ->
202216
object_next(Bin, Members, Nexts, Buf, Opt) ->
203217
?ERROR(object_next, [Bin, Members, Nexts, Buf, Opt]).
204218

219+
205220
-spec string(binary(), non_neg_integer(), [next()], binary(), opt()) -> decode_result().
206221
string(<<Bin/binary>>, Start, Nexts, Buf, Opt) ->
207222
string(Bin, Bin, Start, Nexts, Buf, Opt).
208223

224+
209225
-spec string(binary(), binary(), non_neg_integer(), [next()], binary(), opt()) -> decode_result().
210226
string(<<$", Bin/binary>>, Base, Start, Nexts, Buf, Opt) ->
211227
Prefix = binary:part(Base, 0, byte_size(Base) - byte_size(Bin) - 1),
@@ -252,6 +268,7 @@ string(<<C/utf8, Bin/binary>>, Base, Start, Nexts, Buf, Opt) when 16#20 =< C ->
252268
string(Bin, Base, Start, Nexts, Buf, Opt) ->
253269
?ERROR(string, [Bin, Base, Start, Nexts, Buf, Opt]).
254270

271+
255272
-spec unicode_string(binary(), non_neg_integer(), [next()], binary(), opt()) -> decode_result().
256273
unicode_string(<<N:4/binary, Bin/binary>>, Start, Nexts, Buf, Opt) ->
257274
try
@@ -290,12 +307,14 @@ unicode_string(<<N:4/binary, Bin/binary>>, Start, Nexts, Buf, Opt) ->
290307
unicode_string(Bin, Start, Nexts, Buf, Opt) ->
291308
?ERROR(unicode_string, [Bin, Start, Nexts, Buf, Opt]).
292309

310+
293311
-spec number(binary(), [next()], binary(), opt()) -> decode_result().
294312
number(<<$-, Bin/binary>>, Nexts, Buf, Opt) ->
295313
number_integer_part(Bin, -1, Nexts, Buf, Opt);
296314
number(<<Bin/binary>>, Nexts, Buf, Opt) ->
297315
number_integer_part(Bin, 1, Nexts, Buf, Opt).
298316

317+
299318
-spec number_integer_part(binary(), 1 | -1, [next()], binary(), opt()) -> decode_result().
300319
number_integer_part(<<$0, Bin/binary>>, Sign, Nexts, Buf, Opt) ->
301320
number_fraction_part(Bin, Sign, 0, Nexts, Buf, Opt);
@@ -304,18 +323,21 @@ number_integer_part(<<C, Bin/binary>>, Sign, Nexts, Buf, Opt) when $1 =< C, C =<
304323
number_integer_part(Bin, Sign, Nexts, Buf, Opt) ->
305324
?ERROR(number_integer_part, [Bin, Sign, Nexts, Buf, Opt]).
306325

326+
307327
-spec number_integer_part_rest(binary(), non_neg_integer(), 1 | -1, [next()], binary(), opt()) -> decode_result().
308328
number_integer_part_rest(<<C, Bin/binary>>, N, Sign, Nexts, Buf, Opt) when $0 =< C, C =< $9 ->
309329
number_integer_part_rest(Bin, N * 10 + C - $0, Sign, Nexts, Buf, Opt);
310330
number_integer_part_rest(<<Bin/binary>>, N, Sign, Nexts, Buf, Opt) ->
311331
number_fraction_part(Bin, Sign, N, Nexts, Buf, Opt).
312332

333+
313334
-spec number_fraction_part(binary(), 1 | -1, non_neg_integer(), [next()], binary(), opt()) -> decode_result().
314335
number_fraction_part(<<$., Bin/binary>>, Sign, Int, Nexts, Buf, Opt) ->
315336
number_fraction_part_rest(Bin, Sign, Int, 0, Nexts, Buf, Opt);
316337
number_fraction_part(<<Bin/binary>>, Sign, Int, Nexts, Buf, Opt) ->
317338
number_exponation_part(Bin, Sign * Int, 0, Nexts, Buf, Opt).
318339

340+
319341
-spec number_fraction_part_rest(binary(), 1 | -1, non_neg_integer(), non_neg_integer(), [next()], binary(), opt()) ->
320342
decode_result().
321343
number_fraction_part_rest(<<C, Bin/binary>>, Sign, N, DecimalOffset, Nexts, Buf, Opt) when $0 =< C, C =< $9 ->
@@ -325,6 +347,7 @@ number_fraction_part_rest(<<Bin/binary>>, Sign, N, DecimalOffset, Nexts, Buf, Op
325347
number_fraction_part_rest(Bin, Sign, N, DecimalOffset, Nexts, Buf, Opt) ->
326348
?ERROR(number_fraction_part_rest, [Bin, Sign, N, DecimalOffset, Nexts, Buf, Opt]).
327349

350+
328351
-spec number_exponation_part(binary(), integer(), non_neg_integer(), [next()], binary(), opt()) -> decode_result().
329352
number_exponation_part(<<$e, $+, Bin/binary>>, N, DecimalOffset, Nexts, Buf, Opt) ->
330353
number_exponation_part(Bin, N, DecimalOffset, 1, 0, true, Nexts, Buf, Opt);
@@ -346,6 +369,7 @@ number_exponation_part(<<Bin/binary>>, N, DecimalOffset, Nexts, Buf, Opt) ->
346369
next(Bin, N / math:pow(10, DecimalOffset), Nexts, Buf, Opt)
347370
end.
348371

372+
349373
-spec number_exponation_part(binary(),
350374
integer(),
351375
non_neg_integer(),
@@ -376,6 +400,7 @@ number_exponation_part(<<Bin/binary>>, N, DecimalOffset, ExpSign, Exp, false, Ne
376400
number_exponation_part(Bin, N, DecimalOffset, ExpSign, Exp, IsFirst, Nexts, Buf, Opt) ->
377401
?ERROR(number_exponation_part, [Bin, N, DecimalOffset, ExpSign, Exp, IsFirst, Nexts, Buf, Opt]).
378402

403+
379404
-spec make_object(jsone:json_object_members(), opt()) -> jsone:json_object().
380405
make_object(Members, ?OPT{object_format = tuple}) ->
381406
{lists:reverse(Members)};
@@ -388,10 +413,12 @@ make_object([], _) ->
388413
make_object(Members, _) ->
389414
lists:reverse(Members).
390415

416+
391417
-spec parse_options([jsone:decode_option()]) -> opt().
392418
parse_options(Options) ->
393419
parse_option(Options, ?OPT{}).
394420

421+
395422
-spec parse_option([jsone:decode_option()], opt()) -> opt().
396423
parse_option([], Opt) ->
397424
Opt;

0 commit comments

Comments
 (0)