Skip to content
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

chore: ensure Erlang/OTP 27 compatibility #303

Merged
merged 7 commits into from
Jan 2, 2025
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
10 changes: 4 additions & 6 deletions .github/workflows/run_elvis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ jobs:
runs-on: ubuntu-latest

container:
image: erlang:25.3
image: erlang:26

steps:
- uses: actions/checkout@v1
- run: |
./scripts/elvis-check.sh $GITHUB_BASE_REF
- run: |
make erlfmt-check
- uses: actions/checkout@v4
- run: ./scripts/elvis-check.sh $GITHUB_BASE_REF
- run: make erlfmt-check
9 changes: 6 additions & 3 deletions .github/workflows/run_test_case.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ jobs:
run_test_case:

runs-on: ubuntu-latest
strategy:
matrix:
erlang-vsn: ['26', '27']

container:
image: erlang:25.3
image: erlang:${{ matrix.erlang-vsn }}

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: Code dialyzer
run: |
make dialyzer
Expand All @@ -24,5 +27,5 @@ jobs:
make cover
- uses: actions/upload-artifact@v4
with:
name: cover
name: cover-${{ matrix.erlang-vsn }}
path: _build/test/cover
6 changes: 3 additions & 3 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{minimum_otp_vsn, "21.0"}.
{minimum_otp_vsn, "23.0"}.

{deps, [
{getopt, "1.0.1"},
{typerefl, {git, "https://github.com/ieQu1/typerefl.git", {tag, "0.9.1"}}}
{getopt, "1.0.3"},
{typerefl, {git, "https://github.com/ieQu1/typerefl.git", {tag, "0.9.6"}}}
]}.

{edoc_opts, [{preprocess, true}]}.
Expand Down
1 change: 1 addition & 0 deletions src/hocon.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{applications, [
kernel,
stdlib,
compiler,
getopt,
typerefl
]},
Expand Down
2 changes: 1 addition & 1 deletion src/hocon_parser.yrl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ make_array(Line, Array) -> #{'$hcTyp' => array, '$hcVal' => Array, '$hcMeta' =>
make_primitive_value({endstr, Line, Value}) -> #{'$hcTyp' => string, '$hcVal' => Value, '$hcMeta' => #{line => Line}};
make_primitive_value({T, Line, Value}) -> #{'$hcTyp' => T, '$hcVal' => Value, '$hcMeta' => #{line => Line}}.

make_variable({V, Line, {maybe, Value}}) when V =:= variable orelse V =:= endvar ->
make_variable({V, Line, {'maybe', Value}}) when V =:= variable orelse V =:= endvar ->
#{'$hcTyp' => variable, '$hcVal' => Value, name => Value, '$hcMeta' => #{line => Line}, required => false};
make_variable({V, Line, Value}) when V =:= variable orelse V =:= endvar ->
#{'$hcTyp' => variable, '$hcVal' => Value, name => Value, '$hcMeta' => #{line => Line}, required => true}.
Expand Down
3 changes: 2 additions & 1 deletion src/hocon_pp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,9 @@ esc($\") -> "\\\"";
esc($\\) -> "\\\\";
esc(Char) -> Char.

-include_lib("eunit/include/eunit.hrl").
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").

simple_string_test_() ->
[
?_assert(is_simple_string("")),
Expand Down
2 changes: 1 addition & 1 deletion src/hocon_scanner.xrl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Rules.
{Percent} : {token, {string, TokenLine, TokenChars}}.
{Duration} : {token, {string, TokenLine, TokenChars}}.
{Variable} : {token, {variable, TokenLine, var_ref_name(TokenChars)}}.
{MaybeVar} : {token, {variable, TokenLine, {maybe, maybe_var_ref_name(TokenChars)}}}.
{MaybeVar} : {token, {variable, TokenLine, {'maybe', maybe_var_ref_name(TokenChars)}}}.
{Required} : {token, {required, TokenLine}, get_filename_from_required(TokenChars)}.


Expand Down
4 changes: 2 additions & 2 deletions src/hocon_schema_builtin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ convert(Str, Type) when is_list(Str) ->
case typerefl:from_string(Type, Str) of
{ok, V} ->
V;
{error, _} ->
Str
{error, Reason} ->
throw({?MODULE, Reason})
end;
false ->
Str
Expand Down
77 changes: 40 additions & 37 deletions src/hocon_tconf.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@
-type name() :: hocon_schema:name().
-type schema() :: hocon_schema:schema().

-define(VALIDATION_ERRS(Context), [Context#{kind => validation_error}]).
-define(TRANSLATION_ERRS(Context), [Context#{kind => translation_error}]).

-define(DEFAULT_REQUIRED, false).

-define(META_BOX(Tag, Metadata), #{?METADATA => #{Tag => Metadata}}).
Expand Down Expand Up @@ -148,21 +145,21 @@ do_translate([{MappedField, Translator} | More], TrNamespace, Conf, Acc) ->
catch
throw:Reason ->
Error =
{error,
?TRANSLATION_ERRS(#{
reason => Reason,
path => MappedField0
})},
{error, #{
kind => translation_error,
reason => Reason,
path => MappedField0
}},
do_translate(More, TrNamespace, Conf, [Error | Acc]);
Exception:Reason:St ->
Error =
{error,
?TRANSLATION_ERRS(#{
reason => Reason,
stacktrace => St,
path => MappedField0,
exception => Exception
})},
{error, #{
kind => translation_error,
reason => Reason,
stacktrace => St,
path => MappedField0,
exception => Exception
}},
do_translate(More, TrNamespace, Conf, [Error | Acc])
end.

Expand All @@ -189,13 +186,13 @@ assert_integrity(Schema, [{Name, Validator} | Rest], Conf, Acc) ->
assert_integrity_failure(Schema, Rest, Conf, Name, Reason);
Exception:Reason:St ->
Error =
{error,
?VALIDATION_ERRS(#{
reason => integrity_validation_crash,
validation_name => Name,
exception => {Exception, Reason},
stacktrace => St
})},
{error, #{
kind => validation_error,
reason => integrity_validation_crash,
validation_name => Name,
exception => {Exception, Reason},
stacktrace => St
}},
assert_integrity(Schema, Rest, Conf, [Error | Acc])
end.

Expand All @@ -205,12 +202,12 @@ assert_integrity_failure(Schema, Rest, Conf, Name, Reason) ->
Rest,
Conf,
[
{error,
?VALIDATION_ERRS(#{
reason => integrity_validation_failure,
validation_name => Name,
result => Reason
})}
{error, #{
kind => validation_error,
reason => integrity_validation_failure,
validation_name => Name,
result => Reason
}}
]
).

Expand Down Expand Up @@ -653,11 +650,17 @@ map_field(Type, Schema, Value0, Opts) ->
%% primitive type
Value = unbox(Opts, Value0),
PlainValue = ensure_plain(Value),
ConvertedValue = eval_builtin_converter(PlainValue, Type, Opts),
Validators = get_validators(Schema, Type, Opts),
ValidationResult = validate(Opts, Schema, ConvertedValue, Validators),
Value1 = boxit(Opts, ConvertedValue, Value0),
{ValidationResult, ensure_obfuscate_sensitive(Opts, Schema, Value1)}.
try
ConvertedValue = eval_builtin_converter(PlainValue, Type, Opts),
Validators = get_validators(Schema, Type, Opts),
ValidationResult = validate(Opts, Schema, ConvertedValue, Validators),
Value1 = boxit(Opts, ConvertedValue, Value0),
{ValidationResult, ensure_obfuscate_sensitive(Opts, Schema, Value1)}
catch
{hocon_schema_builtin, Error} ->
ValidationErrors = validation_errs(Opts, Error, obfuscate(Schema, PlainValue)),
{ValidationErrors, ensure_obfuscate_sensitive(Opts, Schema, Value0)}
end.

eval_builtin_converter(PlainValue, Type, Opts) ->
case is_make_serializable(Opts) of
Expand Down Expand Up @@ -1181,14 +1184,14 @@ validation_errs(Opts, Reason, Value) ->
end,
validation_errs(Opts, Err).

validation_errs(Opts, Context) ->
ContextWithPath = ensure_path(Opts, Context),
[{error, ?VALIDATION_ERRS(ContextWithPath)}].
validation_errs(Opts, ContextIn) ->
Context = ContextIn#{kind => validation_error},
[{error, ensure_path(Opts, Context)}].

ensure_path(_Opts, #{path := _} = Context) -> Context;
ensure_path(Opts, Context) -> Context#{path => path(Opts)}.

-spec plain_put(opts(), [binary()], term(), hocon:confing()) -> hocon:config().
-spec plain_put(opts(), [binary()], term(), hocon:config()) -> hocon:config().
plain_put(_Opts, [], Value, _Old) ->
Value;
plain_put(Opts, [Name | Path], Value, Conf0) ->
Expand Down
61 changes: 33 additions & 28 deletions test/hocon_schema_builtin_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
%% limitations under the License.
%%--------------------------------------------------------------------
-module(hocon_schema_builtin_tests).
-feature(maybe_expr, enable).

-include_lib("typerefl/include/types.hrl").
-include_lib("eunit/include/eunit.hrl").
Expand Down Expand Up @@ -42,32 +43,35 @@ builtin_check_test() ->
?assertEqual(#{<<"listener">> => #{<<"bind">> => 65535}}, check_plain(Conf2)),
BadConf1 = "listener.bind = 65536",
?assertThrow(
#{
exception := "port_number_too_large",
field := <<"bind">>,
path := "listener",
reason := failed_to_check_field
},
{?MODULE, [
#{
kind := validation_error,
reason := "port_number_too_large",
path := "listener.bind"
}
]},
check_plain(BadConf1)
),
BadConf2 = "listener.bind = -1",
?assertThrow(
#{
exception := "port_number_must_be_positive",
field := <<"bind">>,
path := "listener",
reason := failed_to_check_field
},
{?MODULE, [
#{
kind := validation_error,
reason := "port_number_must_be_positive",
path := "listener.bind"
}
]},
check_plain(BadConf2)
),
BadConf3 = "listener.bind = 1883d",
?assertThrow(
#{
exception := "bad_port_number",
field := <<"bind">>,
path := "listener",
reason := failed_to_check_field
},
{?MODULE, [
#{
kind := validation_error,
reason := "bad_port_number",
path := "listener.bind"
}
]},
check_plain(BadConf3)
),
ok.
Expand All @@ -80,13 +84,14 @@ to_ip_port(Str) ->
case split_ip_port(Str) of
{"", Port} ->
%% this is a local address
{ok, parse_port(Port)};
parse_port(Port);
{MaybeIp, Port} ->
PortVal = parse_port(Port),
case inet:parse_address(MaybeIp) of
{ok, IpTuple} ->
{ok, {IpTuple, PortVal}};
_ ->
maybe
{ok, PortVal} ?= parse_port(Port),
{ok, IpTuple} ?= inet:parse_address(MaybeIp),
{ok, {IpTuple, PortVal}}
else
{error, _} ->
{error, bad_ip_port}
end;
_ ->
Expand Down Expand Up @@ -115,8 +120,8 @@ split_ip_port(Str0) ->

parse_port(Port) ->
case string:to_integer(string:strip(Port)) of
{P, ""} when P < 0 -> throw("port_number_must_be_positive");
{P, ""} when P > 65535 -> throw("port_number_too_large");
{P, ""} -> P;
_ -> throw("bad_port_number")
{P, ""} when P < 0 -> {error, "port_number_must_be_positive"};
{P, ""} when P > 65535 -> {error, "port_number_too_large"};
{P, ""} -> {ok, P};
_ -> {error, "bad_port_number"}
end.
Loading