From e66e156fa16e5ca7015ea21164a45a44796ed954 Mon Sep 17 00:00:00 2001 From: Maxim Velesyuk Date: Wed, 3 Jul 2019 14:14:00 +0100 Subject: [PATCH] [R21] Fix complilation, specs, add specs to generated repo code Set build version to R21 --- .travis.yml | 5 +++-- rebar.config | 2 ++ src/adapters/xdb_mnesia_adapter.erl | 1 + src/test/xdb_ct.erl | 2 +- src/test/xdb_repo_basic_test.erl | 9 +++++++-- src/xdb_lib.erl | 13 +++++-------- src/xdb_transform.erl | 27 +++++++++++++++++++++++---- test/xdb_lib_SUITE.erl | 2 ++ 8 files changed, 44 insertions(+), 17 deletions(-) diff --git a/.travis.yml b/.travis.yml index 52bdb67..5c96bd4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: erlang otp_release: - - 20.2 + - 21.1 script: - - rebar3 do dialyzer, ct + - rebar3 version + - rebar3 do dialyzer, ct --readable=false diff --git a/rebar.config b/rebar.config index 356da66..5205a39 100644 --- a/rebar.config +++ b/rebar.config @@ -62,6 +62,8 @@ ]} ]}. +{erl_first_files, ["src/xdb_transform.erl"]}. + %% == Cover == {cover_enabled, true}. diff --git a/src/adapters/xdb_mnesia_adapter.erl b/src/adapters/xdb_mnesia_adapter.erl index fbebe84..d2127f2 100644 --- a/src/adapters/xdb_mnesia_adapter.erl +++ b/src/adapters/xdb_mnesia_adapter.erl @@ -89,6 +89,7 @@ in_transaction(_Repo) -> mnesia:is_transaction(). %% @hidden +-spec rollback(atom(), any()) -> no_return(). rollback(Repo, Value) -> case in_transaction(Repo) of true -> error(Value); diff --git a/src/test/xdb_ct.erl b/src/test/xdb_ct.erl index f80059f..5f1d9c9 100644 --- a/src/test/xdb_ct.erl +++ b/src/test/xdb_ct.erl @@ -14,7 +14,7 @@ %%% API %%%=================================================================== --spec assert_error(fun(), term()) -> any(). +-spec assert_error(fun(() -> no_return()), term()) -> any(). assert_error(Fun, Error) -> try Fun() catch diff --git a/src/test/xdb_repo_basic_test.erl b/src/test/xdb_repo_basic_test.erl index 8e5fd97..7709579 100644 --- a/src/test/xdb_repo_basic_test.erl +++ b/src/test/xdb_repo_basic_test.erl @@ -45,7 +45,12 @@ init_per_testcase(_, Config) -> Repo = xdb_lib:keyfetch(repo, Config), {ok, _} = Repo:start_link(), - {_, _} = Repo:delete_all(person), + %% On the first run this table does not exist and this will throw an error + try + Repo:delete_all(person) + catch _:_ -> + ok + end, Config. -spec end_per_testcase(atom(), xdb_ct:config()) -> xdb_ct:config(). @@ -211,7 +216,7 @@ t_update(Config) -> ok = seed(Config), Person = Repo:get(person, 1), - {ok, CS} = + {ok, _CS} = xdb_ct:pipe(Person, [ {fun person:changeset/2, [#{first_name => <<"Joe2">>}]}, {fun Repo:update/1, []} diff --git a/src/xdb_lib.erl b/src/xdb_lib.erl index 1deaaed..8412733 100644 --- a/src/xdb_lib.erl +++ b/src/xdb_lib.erl @@ -144,22 +144,19 @@ reduce_while(Fun, AccIn, List) when is_function(Fun, 2) -> catch throw:{halt, AccOut} -> AccOut; - Kind:Reason -> - erlang:raise(Kind, Reason, erlang:get_stacktrace()) + _:Reason -> + erlang:error(Reason) end. -spec raise(any()) -> no_return(). raise(Reason) -> - {_, Trace} = erlang:process_info(self(), current_stacktrace), - erlang:raise(error, Reason, Trace). + erlang:error(Reason). -spec raise(atom(), any()) -> no_return(). raise(Error, Reason) when is_atom(Error) -> - {_, Trace} = erlang:process_info(self(), current_stacktrace), - erlang:raise(error, {Error, Reason}, Trace). + erlang:error({Error, Reason}). -spec raise(atom(), string(), [any()]) -> no_return(). raise(Error, Text, Args) when is_atom(Error) -> Reason = stringify(Text, Args), - {_, Trace} = erlang:process_info(self(), current_stacktrace), - erlang:raise(error, {Error, Reason}, Trace). + erlang:error({Error, Reason}). diff --git a/src/xdb_transform.erl b/src/xdb_transform.erl index 5d7ea5e..50250b1 100644 --- a/src/xdb_transform.erl +++ b/src/xdb_transform.erl @@ -259,7 +259,10 @@ repo_sup_spec(Repo) -> repo_fun_template(Mod, Fun, Arity, Repo, Adapter) -> Args = splicing_args(Arity), Body = build_repo_fun(Repo, Adapter, Mod, atom_to_list(Fun), Args), - {Fun, Arity, Body}. + case build_repo_fun_spec(Repo, Fun, Args) of + [] -> {Fun, Arity, Body}; + Spec -> {Fun, Arity, Body, Spec} + end. %% @private build_repo_fun(Repo, Adapter, Adapter, Fun, Args) -> @@ -269,6 +272,14 @@ build_repo_fun(Repo, Adapter, Mod, Fun, Args) -> Body = build_repo_fun_body(Fun, "~p, ~p", Args), ?Q(text(Body, [Mod, Repo, Adapter])). +%%@private +build_repo_fun_spec(_, rollback, _) -> + ?Q("-spec rollback(any()) -> no_return()."); + +build_repo_fun_spec(_, _, _) -> + %% TODO generate specs for all funs? + []. + %% @private build_repo_fun_body(Fun, Prefix, "") -> Fun ++ "() -> ~p:" ++ Fun ++ "(" ++ Prefix ++ ")."; @@ -292,12 +303,12 @@ maybe_transaction_funs(Specs, Repo, Adapter) -> maybe_add_funs(Result) -> case erlang:get(funs) of undefined -> Result; - ExtFuns -> lists:droplast(Result) ++ ExtFuns ++ [lists:last(Result)] + ExtFuns -> lists:droplast(Result) ++ lists:reverse(ExtFuns) ++ [lists:last(Result)] end. %% @private build_export() -> - build_export(erlang:erase(exports)). + build_export(lists:reverse(erlang:erase(exports))). %% @private build_export([{FirstFun, FirstArity} | Exports]) -> @@ -310,7 +321,9 @@ build_export(_) -> %% @private add_funs(FunSpecs) -> lists:foreach(fun({Name, Arity, Body}) -> - add_fun(Name, Arity, Body) + add_fun(Name, Arity, Body); + ({Name, Arity, Body, Spec}) -> + add_fun(Name, Arity, Body, Spec) end, FunSpecs). %% @private @@ -318,6 +331,12 @@ add_fun(Name, Arity, Body) -> _ = do_put(exports, {Name, Arity}, erlang:get(exports)), do_put(funs, Body, erlang:get(funs)). +%% @private +add_fun(Name, Arity, Body, Spec) -> + _ = do_put(exports, {Name, Arity}, erlang:get(exports)), + do_put(funs, Spec, erlang:get(funs)), + do_put(funs, Body, erlang:get(funs)). + %% @private do_put(Key, Value, undefined) -> erlang:put(Key, [Value]); diff --git a/test/xdb_lib_SUITE.erl b/test/xdb_lib_SUITE.erl index b80df01..c4f5bb4 100644 --- a/test/xdb_lib_SUITE.erl +++ b/test/xdb_lib_SUITE.erl @@ -1,5 +1,7 @@ -module(xdb_lib_SUITE). +-dialyzer({nowarn_function, t_raise/1}). + %% Common Test -export([ all/0