-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configurable env parser #151
Changes from all commits
5244f23
6659a71
d477cd9
774717b
9320d18
834b5f9
dac0978
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[{elvis, [ | ||
{config, [ | ||
#{dirs => ["src", "src/*", "scenarios"], | ||
filter => "*.erl", | ||
ruleset => erl_files, | ||
rules => [ | ||
{elvis_style, invalid_dynamic_call, #{ignore => [amoc_user]}}, | ||
{elvis_style, export_used_types, disable}, | ||
{elvis_style, no_throw, #{ignore => [{amoc_config, get, 2}] }}, | ||
{elvis_text_style, line_length, #{skip_comments => whole_line }}, | ||
{elvis_style, no_block_expressions, disable} | ||
]}, | ||
#{dirs => ["test"], | ||
filter => "*.erl", | ||
ruleset => erl_files, | ||
rules => [ | ||
{elvis_style, function_naming_convention, #{regex => "^[a-z]([a-z0-9]*_?)*$"}}, | ||
{elvis_style, atom_naming_convention, #{regex => "^[a-z]([a-z0-9]*_?)*(_SUITE)?$"}}, | ||
{elvis_style, dont_repeat_yourself, #{min_complexity => 50}}, | ||
{elvis_style, no_debug_call, disable}, | ||
{elvis_style, no_throw, disable}, | ||
{elvis_style, no_import, disable} | ||
]}, | ||
#{dirs => ["."], | ||
filter => "rebar.config", | ||
ruleset => rebar_config} | ||
]} | ||
]}]. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,14 @@ | |
%%============================================================================== | ||
-module(amoc_config_env). | ||
|
||
-export([get/1, get/2, parse_value/1, format/2]). | ||
-export([get/1, get/2]). | ||
|
||
-include_lib("kernel/include/logger.hrl"). | ||
|
||
-define(DEFAULT_PARSER_MODULE, amoc_config_parser). | ||
|
||
-callback(parse_value(string()) -> {ok, amoc_config:value()} | {error, any()}). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a perfect idea, but it only requires now documentation on how to do so when you want to add a different parser. So some md file in the guides directory, link it in the rebar config for hex like the others, etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need a separate dedicated PR with documentation update. |
||
|
||
%% ------------------------------------------------------------------ | ||
%% API | ||
%% ------------------------------------------------------------------ | ||
|
@@ -24,24 +28,6 @@ get(Name) -> | |
get(Name, Default) when is_atom(Name) -> | ||
get_os_env(Name, Default). | ||
|
||
-spec parse_value(string() | binary()) -> {ok, amoc_config:value()} | {error, any()}. | ||
parse_value(Binary) when is_binary(Binary) -> | ||
parse_value(binary_to_list(Binary)); | ||
parse_value(String) when is_list(String) -> | ||
try | ||
{ok, Tokens, _} = erl_scan:string(String ++ "."), | ||
{ok, _} = erl_parse:parse_term(Tokens) | ||
catch | ||
_:E -> {error, E} | ||
end. | ||
|
||
-spec format(any(), binary) -> binary(); | ||
(any(), string) -> string(). | ||
format(Value, binary) -> | ||
list_to_binary(format(Value, string)); | ||
format(Value, string) -> | ||
lists:flatten(io_lib:format("~tp", [Value])). | ||
|
||
%% ------------------------------------------------------------------ | ||
%% Internal Function Definitions | ||
%% ------------------------------------------------------------------ | ||
|
@@ -51,8 +37,13 @@ get_os_env(Name, Default) -> | |
Value = os:getenv(EnvName), | ||
case parse_value(Value, Default) of | ||
{ok, Term} -> Term; | ||
{error, _} -> | ||
?LOG_ERROR("cannot parse $~p value \"~p\", using default one", [EnvName, Value]), | ||
{error, Error} -> | ||
?LOG_ERROR("cannot parse environment variable, using default value.~n" | ||
" parsing error: '~p'~n" | ||
" variable name: '$~s'~n" | ||
" variable value: '~s'~n" | ||
" default value: '~p'~n", | ||
Comment on lines
+41
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that I see these, can we use structured logging? Like, maps with keys and values. Maybe in a separate PR if there's plenty of these things anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's worth to have a separate dedicated PR with all the logs reworked. |
||
[Error, EnvName, Value, Default]), | ||
Default | ||
end. | ||
|
||
|
@@ -64,4 +55,13 @@ os_env_name(Name) when is_atom(Name) -> | |
parse_value(false, Default) -> {ok, Default}; | ||
parse_value("", Default) -> {ok, Default}; | ||
parse_value(String, _) -> | ||
parse_value(String). | ||
App = application:get_application(?MODULE), | ||
Mod = application:get_env(App, config_parser_mod, ?DEFAULT_PARSER_MODULE), | ||
try Mod:parse_value(String) of | ||
{ok, Value} -> {ok, Value}; | ||
{error, Error} -> {error, Error}; | ||
InvalidRetValue -> {error, {parser_returned_invalid_value, InvalidRetValue}} | ||
catch | ||
Class:Error:Stacktrace -> | ||
{error, {parser_crashed, {Class, Error, Stacktrace}}} | ||
end. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
%%============================================================================== | ||
%% Copyright 2023 Erlang Solutions Ltd. | ||
%% Licensed under the Apache License, Version 2.0 (see LICENSE file) | ||
%%============================================================================== | ||
%% This module implements the default parser for the amoc_config_env module | ||
%%============================================================================== | ||
-module(amoc_config_parser). | ||
-behaviour(amoc_config_env). | ||
|
||
-export([parse_value/1]). | ||
|
||
-ifdef(TEST). | ||
%% exported for testing only | ||
-export([format/2]). | ||
-else. | ||
-ignore_xref([format/2]). | ||
-dialyzer({nowarn_function, [format/2]}). | ||
-endif. | ||
|
||
%% ------------------------------------------------------------------ | ||
%% API | ||
%% ------------------------------------------------------------------ | ||
|
||
-spec parse_value(string() | binary()) -> {ok, amoc_config:value()} | {error, any()}. | ||
parse_value(Binary) when is_binary(Binary) -> | ||
parse_value(binary_to_list(Binary)); | ||
parse_value(String) when is_list(String) -> | ||
try | ||
{ok, Tokens, _} = erl_scan:string(String ++ "."), | ||
{ok, _} = erl_parse:parse_term(Tokens) | ||
catch | ||
_:E -> {error, E} | ||
end. | ||
|
||
-spec format(any(), binary) -> binary(); | ||
(any(), string) -> string(). | ||
format(Value, binary) -> | ||
list_to_binary(format(Value, string)); | ||
format(Value, string) -> | ||
lists:flatten(io_lib:format("~tp", [Value])). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent idea