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

Naming the types, instead of the generic term() or any() #55

Merged
merged 1 commit into from
Jun 4, 2014
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
2 changes: 1 addition & 1 deletion examples/blog/src/blog_post.erl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ sumo_schema() ->
%% We don't have the extends module attribute in R16, so this was moved out from
%% the old blog_post_repo in the example, we should allocate some time to create
%% a proper parse transform for this.
% -spec total_posts(sumo:schema_name(), term() ) -> {ok, {raw, non_neg_integer()}, term()} | {ok, error, term()}.
% -spec total_posts(sumo:schema_name(), State ) -> {ok, {raw, non_neg_integer()}, State} | {ok, error, State}.
% count(DocName, State) ->
% Sql = "SELECT COUNT(1) FROM `" ++ atom_to_list(DocName) ++ "`",
% Result = sumo_repo_mysql:execute(Sql, State),
Expand Down
33 changes: 19 additions & 14 deletions src/sumo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@

-export_type([schema/0, field/0]).

%% @doc the user representation of a doc (i.e. not the proplists)
-type user_doc() :: term().

-export_type([user_doc/0]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Code starts here.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand All @@ -77,15 +82,15 @@ create_schema() ->
ok.

%% @doc Returns 1 doc that matches the given Conditions.
-spec find_one(sumo:schema_name(), conditions()) -> doc() | notfound.
-spec find_one(schema_name(), conditions()) -> user_doc() | notfound.
find_one(DocName, Conditions) ->
case find_by(DocName, Conditions, 1, 0) of
[] -> notfound;
List -> hd(List)
end.

%% @doc Returns the doc identified by Id.
-spec find(sumo:schema_name(), term()) -> doc() | notfound.
-spec find(schema_name(), field_value()) -> user_doc() | notfound.
find(DocName, Id) ->
IdFieldName = sumo_internal:id_field_name(DocName),
find_one(DocName, [{IdFieldName, Id}]).
Expand Down Expand Up @@ -114,8 +119,8 @@ find_all(DocName, OrderField, Limit, Offset) ->
%% @doc Returns Limit number of docs that match Conditions, starting at
%% offset Offset.
-spec find_by(
sumo:schema_name(), conditions(), non_neg_integer(), non_neg_integer()
) -> [doc()].
schema_name(), conditions(), non_neg_integer(), non_neg_integer()
) -> [user_doc()].
find_by(DocName, Conditions, Limit, Offset) ->
Repo = sumo_internal:get_repo(DocName),
case sumo_repo:find_by(Repo, DocName, Conditions, Limit, Offset) of
Expand All @@ -127,7 +132,7 @@ find_by(DocName, Conditions, Limit, Offset) ->
end.

%% @doc Returns *all* docs that match Conditions.
-spec find_by(sumo:schema_name(), conditions()) -> [doc()].
-spec find_by(schema_name(), conditions()) -> [user_doc()].
find_by(DocName, Conditions) ->
Repo = sumo_internal:get_repo(DocName),
case sumo_repo:find_by(Repo, DocName, Conditions) of
Expand All @@ -136,7 +141,7 @@ find_by(DocName, Conditions) ->
end.

%% @doc Creates or updates the given Doc.
-spec persist(sumo:schema_name(), term()) -> term().
-spec persist(schema_name(), UserDoc) -> UserDoc.
persist(DocName, State) ->
IdField = sumo_internal:id_field_name(DocName),
PropList = DocName:sumo_sleep(State),
Expand All @@ -154,7 +159,7 @@ persist(DocName, State) ->
end.

%% @doc Deletes all docs of type DocName.
-spec delete_all(sumo:schema_name()) -> non_neg_integer().
-spec delete_all(schema_name()) -> non_neg_integer().
delete_all(DocName) ->
Repo = sumo_internal:get_repo(DocName),
case sumo_repo:delete_all(Repo, DocName) of
Expand All @@ -168,7 +173,7 @@ delete_all(DocName) ->
end.

%% @doc Deletes the doc identified by Id.
-spec delete(sumo:schema_name(), term()) -> boolean().
-spec delete(schema_name(), user_doc()) -> boolean().
delete(DocName, Id) ->
IdField = sumo_internal:id_field_name(DocName),
case delete_by(DocName, [{IdField, Id}]) of
Expand All @@ -177,7 +182,7 @@ delete(DocName, Id) ->
end.

%% @doc Deletes the doc identified by Conditions.
-spec delete_by(sumo:schema_name(), term()) -> non_neg_integer().
-spec delete_by(schema_name(), conditions()) -> non_neg_integer().
delete_by(DocName, Conditions) ->
Repo = sumo_internal:get_repo(DocName),
case sumo_repo:delete_by(Repo, DocName, Conditions) of
Expand All @@ -187,13 +192,13 @@ delete_by(DocName, Conditions) ->
end.

%% @doc Creates the schema for the docs of type DocName.
-spec create_schema(sumo:schema_name()) -> ok.
-spec create_schema(schema_name()) -> ok.
create_schema(DocName) ->
create_schema(DocName, sumo_internal:get_repo(DocName)).

%% @doc Creates the schema for the docs of type DocName using the given
%% repository.
-spec create_schema(sumo:schema_name(), atom()) -> ok.
-spec create_schema(schema_name(), atom()) -> ok.
create_schema(DocName, Repo) ->
case sumo_repo:create_schema(Repo, sumo_internal:get_schema(DocName)) of
ok ->
Expand All @@ -203,12 +208,12 @@ create_schema(DocName, Repo) ->
end.

%% @doc Calls the given custom function of a repo.
-spec call(sumo:schema_name(), atom()) -> term().
-spec call(schema_name(), atom()) -> term().
call(DocName, Function) ->
call(DocName, Function, []).

%% @doc Calls the given custom function of a repo with the given args.
-spec call(sumo:schema_name(), atom(), [term()]) -> term().
-spec call(schema_name(), atom(), [term()]) -> term().
call(DocName, Function, Args) ->
Repo = sumo_internal:get_repo(DocName),
case sumo_repo:call(Repo, DocName, Function, Args) of
Expand All @@ -225,7 +230,7 @@ docs_wakeup(DocName, Docs) ->
)).

%% @doc Returns a new schema.
-spec new_schema(sumo:schema_name(), [field()]) -> schema().
-spec new_schema(schema_name(), [field()]) -> schema().
new_schema(Name, Fields) ->
sumo_internal:new_schema(Name, Fields).

Expand Down
4 changes: 2 additions & 2 deletions src/sumo_doc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@

%% @doc Returns all behavior callbacks.
-callback sumo_schema() -> sumo:schema().
-callback sumo_wakeup(sumo:doc()) -> term().
-callback sumo_sleep(term()) -> sumo:doc().
-callback sumo_wakeup(sumo:doc()) -> sumo:user_doc().
-callback sumo_sleep(sumo:user_doc()) -> sumo:doc().
4 changes: 2 additions & 2 deletions src/sumo_internal.erl
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ doc_fields(Doc) ->
Doc#sumo_doc.fields.

%% @doc Wakes up the document
-spec wakeup(doc()) -> term().
-spec wakeup(doc()) -> sumo:user_doc().
wakeup(Doc) ->
wakeup(doc_name(Doc), Doc).

%% @doc Wakes up the document
-spec wakeup(module(), doc()) -> term().
-spec wakeup(module(), doc()) -> sumo:user_doc().
wakeup(DocName, Doc) ->
DocName:sumo_wakeup(Doc#sumo_doc.fields).

Expand Down
6 changes: 4 additions & 2 deletions src/sumo_repo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
-callback init(term()) -> {ok, term()}.
-callback persist(sumo_internal:doc(), State) ->
result(sumo_internal:doc(), State).
-callback delete(sumo:schema_name(), term(), State) ->
-callback delete(sumo:schema_name(), sumo:field_value(), State) ->
result(affected_rows(), State).
-callback delete_by(sumo:schema_name(), sumo:conditions(), State) ->
result(affected_rows(), State).
Expand Down Expand Up @@ -105,7 +105,9 @@ persist(Name, Doc) ->
wpool:call(Name, {persist, Doc}).

%% @doc Deletes the doc identified by id in the given repository name.
-spec delete(atom(), sumo:schema_name(), term()) -> ok | {error, term()}.
-spec delete(
atom(), sumo:schema_name(), sumo:field_value()
) -> ok | {error, term()}.
delete(Name, DocName, Id) ->
wpool:call(Name, {delete, DocName, Id}).

Expand Down