Skip to content

Commit e0beb36

Browse files
committed
Accept additional_macros option, which higher priority than definitions in the file
1 parent c718206 commit e0beb36

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/yconf.erl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ parse(Path0, Validators, Opts) ->
108108
Path = unicode:characters_to_binary(Path0),
109109
{Opts1, Opts2} = proplists:split(
110110
proplists:compact(Opts),
111-
[replace_macros, include_files, plain_as_atom]),
111+
[replace_macros, include_files, plain_as_atom, additional_macros]),
112112
Opts3 = lists:flatten(Opts1),
113113
try
114114
Y1 = read_yaml(prep_path(Path), Opts3, []),
115-
Y2 = replace_macros(Y1, Opts3),
115+
Y2 = replace_macros(define_additional_macros(Y1, Opts3), Opts3),
116116
Validators1 = maps:merge(Validators, validators(Opts3)),
117117
{ok, (options(Validators1, Opts2))(Y2)}
118118
catch _:{?MODULE, Why, Ctx} ->
@@ -888,6 +888,19 @@ replace_macros(Y, Opts) ->
888888
Y
889889
end.
890890

891+
define_additional_macros(Yi, Opts) ->
892+
CurrentMacros = lists:flatten(proplists:get_all_values(define_macro, Yi)),
893+
AdditionalMacros = proplists:get_value(additional_macros, Opts, []),
894+
NewMacros = lists:foldl(fun({Key, Value}, CMs) ->
895+
lists:keystore(Key, 1, CMs, {Key, Value})
896+
end,
897+
CurrentMacros,
898+
AdditionalMacros),
899+
case NewMacros of
900+
[] -> proplists:delete(define_macro, Yi);
901+
_ -> [{define_macro, NewMacros} | proplists:delete(define_macro, Yi)]
902+
end.
903+
891904
-spec check_duplicated_macros([macro()]) -> [macro()].
892905
check_duplicated_macros(Macros) ->
893906
lists:foldl(

0 commit comments

Comments
 (0)