Skip to content

Commit

Permalink
[R21] Fix complilation, specs, add specs to generated repo code
Browse files Browse the repository at this point in the history
Set build version to R21
  • Loading branch information
maximvl committed Jul 3, 2019
1 parent 1bbe07a commit e66e156
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 17 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
]}
]}.

{erl_first_files, ["src/xdb_transform.erl"]}.

%% == Cover ==

{cover_enabled, true}.
Expand Down
1 change: 1 addition & 0 deletions src/adapters/xdb_mnesia_adapter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/test/xdb_ct.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/test/xdb_repo_basic_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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, []}
Expand Down
13 changes: 5 additions & 8 deletions src/xdb_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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}).
27 changes: 23 additions & 4 deletions src/xdb_transform.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand All @@ -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 ++ ").";
Expand All @@ -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]) ->
Expand All @@ -310,14 +321,22 @@ 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
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]);
Expand Down
2 changes: 2 additions & 0 deletions test/xdb_lib_SUITE.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-module(xdb_lib_SUITE).

-dialyzer({nowarn_function, t_raise/1}).

%% Common Test
-export([
all/0
Expand Down

0 comments on commit e66e156

Please sign in to comment.