www
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYestin L. Harrison <yestin@ylh.io>2022-06-19 20:34:39 -0700
committerYestin L. Harrison <yestin@ylh.io>2022-06-19 20:34:39 -0700
commit242b19d21458b591a4cc663c6e6922f4f7b867b7 (patch)
tree0b6fb1fb6e158ce9d9ef5fe50a34cddd224b7037
parentde7678bfe41a171e1dff61ffaf2d45138577e1a6 (diff)
downloaddemoheader-242b19d21458b591a4cc663c6e6922f4f7b867b7.tar.gz
demoheader-242b19d21458b591a4cc663c6e6922f4f7b867b7.tar.xz
demoheader-242b19d21458b591a4cc663c6e6922f4f7b867b7.zip
switch to jsone (wicked fast)
-rw-r--r--rebar.config5
-rw-r--r--rebar.lock9
-rw-r--r--src/demoheader.erl35
3 files changed, 22 insertions, 27 deletions
diff --git a/rebar.config b/rebar.config
index 738db9d..e8db42a 100644
--- a/rebar.config
+++ b/rebar.config
@@ -1,5 +1,5 @@
{erl_opts, [no_debug_info]}.
-{deps, [{getopt, "1.0.2"}]}.
+{deps, [{getopt, "1.0.2"}, {jsone, "1.7.0"}]}.
{escript_incl_apps,
[demoheader]}.
@@ -9,5 +9,6 @@
%% Profiles
{profiles, [{test,
- [{erl_opts, [debug_info]}
+ [{erl_opts, [debug_info]},
+ {deps, [{eflambe, "0.2.1"}]}
]}]}.
diff --git a/rebar.lock b/rebar.lock
index 761d3f4..63aae3e 100644
--- a/rebar.lock
+++ b/rebar.lock
@@ -1,8 +1,11 @@
{"1.2.0",
-[{<<"getopt">>,{pkg,<<"getopt">>,<<"1.0.2">>},0}]}.
+[{<<"getopt">>,{pkg,<<"getopt">>,<<"1.0.2">>},0},
+ {<<"jsone">>,{pkg,<<"jsone">>,<<"1.7.0">>},0}]}.
[
{pkg_hash,[
- {<<"getopt">>, <<"33D9B44289FE7AD08627DDFE1D798E30B2DA0033B51DA1B3A2D64E72CD581D02">>}]},
+ {<<"getopt">>, <<"33D9B44289FE7AD08627DDFE1D798E30B2DA0033B51DA1B3A2D64E72CD581D02">>},
+ {<<"jsone">>, <<"1E3BD7D5DD44BB2EB0797DDDEA1CBF2DDAB8D9F29E499A467CA171C23F5984EA">>}]},
{pkg_hash_ext,[
- {<<"getopt">>, <<"A0029AEA4322FB82A61F6876A6D9C66DC9878B6CB61FAA13DF3187384FD4EA26">>}]}
+ {<<"getopt">>, <<"A0029AEA4322FB82A61F6876A6D9C66DC9878B6CB61FAA13DF3187384FD4EA26">>},
+ {<<"jsone">>, <<"A3A33712EE6BC8BE10CFA21C7C425A299DE4C5A8533F9F931E577A6D0E8F5DBD">>}]}
].
diff --git a/src/demoheader.erl b/src/demoheader.erl
index c857d36..3c31d4c 100644
--- a/src/demoheader.erl
+++ b/src/demoheader.erl
@@ -38,34 +38,25 @@ args(Args, Opts) ->
{ok, Res} -> Res
end,
lists:member(help, OptArgs) andalso usage(Opts, 0),
- Headers = lists:filter(fun(A) -> lists:member(A, [?FIELDS]) end, OptArgs),
- Show = case lists:member(json, OptArgs) of
- true -> json_of(case Headers of [] -> [?FIELDS]; Else -> Else end);
- _ -> case Headers of
- [One] -> fun(#{One := V}) ->
- io_lib:format(case V of [_|_] -> "~s\n"; _ -> "~w\n" end, [V])
- end;
- _ -> DU("without -j, exactly one field must be requested")
- end
+ Show = case {
+ lists:member(json, OptArgs),
+ lists:filter(fun(A) -> lists:member(A, [?FIELDS]) end, OptArgs)
+ } of
+ {true, []} -> fun(P) -> P end;
+ {true, Else} -> fun(P) -> maps:with(Else, P) end;
+ {false, [One]} -> fun(P) -> #{One := V} = P, V end;
+ {false, _} -> DU("without -j, exactly one field must be requested")
end,
Parsed = case ExtraArgs of
[Single] -> parse(Single);
_ -> DU("exactly one file must be specified")
end,
- io:put_chars(Show(Parsed)).
+ io:put_chars([
+ jsone:encode(Show(Parsed), [{indent, 1}, {float_format, [short]}]),
+ $\n
+ ]).
-undef(X).
-json_of(Keys) ->
- fun(Map) ->
- F = fun(Key, Acc) ->
- #{Key := V} = Map,
- [[$", atom_to_binary(Key), <<"\": ">>, io_lib:format("~0p", [V])]|Acc]
- end,
- [<<"{\n\t">>,
- lists:join(<<",\n\t">>, lists:foldl(F, [], lists:reverse(Keys))),
- <<"\n}\n">>]
- end.
-
-define(X(ATOM, _C, _D, WIDTH, TYPE), fun
({<<Var:WIDTH/TYPE, Rem/binary>>, Map}) -> {Rem, Map#{ATOM => Var}}
end).
@@ -84,7 +75,7 @@ parse(Arg) ->
maps:map(fun
(_, <<S/binary>>) ->
[Head|_] = binary:split(S, <<0>>),
- unicode:characters_to_list(Head);
+ Head;
(_, V) ->
V
end, Map).