Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/hocon_cli.erl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ main(Args) ->
now_time();
docgen ->
docgen(ParsedArgs);
pp ->
pretty_print(ParsedArgs);
_Other ->
print_help()
end.
Expand Down Expand Up @@ -141,6 +143,11 @@ get(ParsedArgs, [Query | _]) ->
?STDOUT("~0p", [hocon_schema:get_value(Query, NewConf)]),
stop_ok().

pretty_print(ParsedArgs) ->
Conf0 = load_conf(ParsedArgs, fun log/3),
Conf = hocon_util:richmap_to_map(Conf0),
?STDOUT("~ts", [hocon_pp:do(Conf, #{})]).

docgen(ParsedArgs) ->
case load_schema(ParsedArgs) of
undefined ->
Expand Down
21 changes: 16 additions & 5 deletions src/hocon_pp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
%%
%% `newline': string, by default `"\n"' is used, for generating web-content
%% it should be `"<br>"' instead.
%%
%% `no_obj_nl': boolean, default to `false'. When set to `true' no new line
%% is added after objects.
-spec do(term(), map()) -> iodata().
do(Value, Opts) when is_map(Value) ->
%% Root level map should not have outter '{' '}' pair
Expand Down Expand Up @@ -61,8 +64,11 @@ gen(S, Opts) when is_list(S) ->
gen_list(S, Opts)
end;
gen(M, Opts) when is_map(M) ->
gen_map(M, Opts).

NL = case maps:get(no_obj_nl, Opts, false) of
true -> "";
false -> ?NL
end,
[gen_map(M, Opts), NL].

gen_list(L, Opts) ->
case is_oneliner(L) of
Expand All @@ -73,12 +79,17 @@ gen_list(L, Opts) ->
do_gen_list(L, Opts)
end.

do_gen_list(L, Opts) ->
do_gen_list([_ | _] = L, Opts) ->
[ ["[", ?NL]
, [{indent, [gen(I, Opts), ",", ?NL]} || I <- L]
, do_gen_list_loop(L, Opts#{no_obj_nl => true})
, ["]", ?NL]
].

do_gen_list_loop([I], Opts) ->
[{indent, gen(I, Opts)}];
do_gen_list_loop([H | T], Opts) ->
[{indent, [gen(H, Opts), ","]} | do_gen_list_loop(T, Opts)].

is_oneliner(L) when is_list(L) ->
lists:all(fun(X) -> is_number(X) orelse is_binary(X) orelse is_atom(X) end, L);
is_oneliner(M) when is_map(M) ->
Expand All @@ -87,7 +98,7 @@ is_oneliner(M) when is_map(M) ->
gen_map(M, Opts) ->
case is_oneliner(M) of
true -> ["{", infix(gen_map_fields(M, Opts, ""), ", "), "}"];
false -> [ ["{", ?NL], {indent, gen_map_fields(M, Opts, ?NL)} , ["}", ?NL] ]
false -> [["{", ?NL], {indent, gen_map_fields(M, Opts, ?NL)}, "}"]
end.

gen_map_fields(M, Opts, NL) ->
Expand Down
27 changes: 2 additions & 25 deletions src/hocon_schema.erl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
, desc => iodata()
}.

-type struct_meta() :: #{desc => iodata()}.
-type field() :: {name(), typefunc() | field_schema()}.
-type fields() :: [field()] | #{fields := [field()],
desc => iodata()
Expand Down Expand Up @@ -1070,30 +1069,8 @@ put_rich(Opts, [Name | Path], Value, Box) ->
boxit(NewBoxV, Box).

%% @doc Convert richmap to plain-map.
richmap_to_map(RichMap) when is_map(RichMap) ->
richmap_to_map(maps:iterator(RichMap), #{});
richmap_to_map(Array) when is_list(Array) ->
[richmap_to_map(R) || R <- Array];
richmap_to_map(Other) ->
Other.

richmap_to_map(Iter, Map) ->
case maps:next(Iter) of
{?METADATA, _, I} ->
richmap_to_map(I, Map);
{?HOCON_T, _, I} ->
richmap_to_map(I, Map);
{?HOCON_V, M, _} when is_map(M) ->
richmap_to_map(maps:iterator(M), #{});
{?HOCON_V, A, _} when is_list(A) ->
[richmap_to_map(R) || R <- A];
{?HOCON_V, V, _} ->
V;
{K, V, I} ->
richmap_to_map(I, Map#{K => richmap_to_map(V)});
none ->
Map
end.
richmap_to_map(MaybeRichMap) ->
hocon_util:richmap_to_map(MaybeRichMap).

%% treat 'null' as absence
drop_nulls(_Opts, undefined) -> undefined;
Expand Down
29 changes: 29 additions & 0 deletions src/hocon_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
-export([pipeline_fun/1, pipeline/3]).
-export([stack_multiple_push/2, stack_push/2, get_stack/2, top_stack/2]).
-export([is_same_file/2, real_file_name/1]).
-export([richmap_to_map/1]).

-include("hocon_private.hrl").

deep_merge(M1, M2) when is_map(M1), is_map(M2) ->
maps:fold(fun(K, V2, Acc) ->
Expand Down Expand Up @@ -62,3 +65,29 @@ real_file_name(F) ->
{ok, Real} -> Real;
{error, _} -> F
end.

%% @doc Convert richmap to plain-map.
richmap_to_map(RichMap) when is_map(RichMap) ->
richmap_to_map(maps:iterator(RichMap), #{});
richmap_to_map(Array) when is_list(Array) ->
[richmap_to_map(R) || R <- Array];
richmap_to_map(Other) ->
Other.

richmap_to_map(Iter, Map) ->
case maps:next(Iter) of
{?METADATA, _, I} ->
richmap_to_map(I, Map);
{?HOCON_T, _, I} ->
richmap_to_map(I, Map);
{?HOCON_V, M, _} when is_map(M) ->
richmap_to_map(maps:iterator(M), #{});
{?HOCON_V, A, _} when is_list(A) ->
[richmap_to_map(R) || R <- A];
{?HOCON_V, V, _} ->
V;
{K, V, I} ->
richmap_to_map(I, Map#{K => richmap_to_map(V)});
none ->
Map
end.