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

Add sort by id implementation in trails.erl #32

Merged
merged 2 commits into from
Jul 3, 2015
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
35 changes: 28 additions & 7 deletions src/trails.erl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ store(Trails) ->
all() ->
case application:get_application(trails) of
{ok, trails} ->
all(ets:select(trails, [{{'$1', '$2'}, [], ['$$']}]), []);
Trails = all(ets:select(trails, [{{'$1', '$2'}, [], ['$$']}]), []),
SortIdFun =
fun(A, B) -> maps:get(trails_id, A) < maps:get(trails_id, B) end,
SortedStoredTrails = lists:sort(SortIdFun, Trails),
lists:map(fun remove_id/1, SortedStoredTrails);
_ ->
throw({not_started, trails})
end.
Expand All @@ -142,13 +146,15 @@ retrieve(Path) ->
case application:get_application(trails) of
{ok, trails} ->
case ets:lookup(trails, Path) of
[{_, Val}] -> Val;
[{_, Val}] -> remove_id(Val);
[] -> notfound
end;
_ ->
throw({not_started, trails})
end.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Private API.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand All @@ -175,12 +181,27 @@ all([[_, Trail] | T], Acc) ->
%% @private
-spec normalize_store_input([route_path()]) -> trails().
normalize_store_input(RoutesPaths) ->
[normalize(Path) || Path <- RoutesPaths].
normalize_id(normalize_paths(RoutesPaths)).

-spec normalize_id([route_path()]) -> trails().
normalize_id(Trails) ->
Length = length(Trails),
AddIdFun = fun(Trail, Id) -> Trail#{ trails_id => Id} end,
lists:zipwith(AddIdFun, Trails, lists:seq(1, Length)).

%% @private
-spec normalize_paths([route_path()]) -> trails().
normalize_paths(RoutesPaths) ->
[normalize_path(Path) || Path <- RoutesPaths].

%% @private
-spec remove_id(trail()) -> trail().
remove_id(Trail) -> maps:remove(trails_id, Trail).

%% @private
-spec normalize(route_path() | trail()) -> trail().
normalize({PathMatch, ModuleHandler, Options}) ->
-spec normalize_path(route_path() | trail()) -> trail().
normalize_path({PathMatch, ModuleHandler, Options}) ->
trail(PathMatch, ModuleHandler, Options);
normalize({PathMatch, Constraints, ModuleHandler, Options}) ->
normalize_path({PathMatch, Constraints, ModuleHandler, Options}) ->
trail(PathMatch, ModuleHandler, Options, #{}, Constraints);
normalize(Trail) when is_map(Trail) -> Trail.
normalize_path(Trail) when is_map(Trail) -> Trail.
19 changes: 16 additions & 3 deletions test/trails_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ basic_trails_routes(_Config) ->

-spec trails_store(config()) -> {atom(), string()}.
trails_store(_Config) ->
Trails = [
{"/resource/[:id]", trails_test2_handler, []},
TrailsRaw = [
{"/resource/[:id]", trails_test_handler, []},
{"/api/:id/resource", [], trails_test2_handler, [arg0]},
trails:trail("/assets/[...]", cowboy_static, {dir, "www/assets"}),
trails:trail("/such/path", http_basic_route, [], #{}),
Expand All @@ -277,9 +277,11 @@ trails_store(_Config) ->
],
{not_started, trails} = (catch trails:all()),
{not_started, trails} = (catch trails:retrieve("/")),
ok = trails:store(Trails),
ok = trails:store(TrailsRaw),
Trails = normalize_paths(TrailsRaw),
Length = length(Trails),
Length = length(trails:all()),
Trails = trails:all(),
#{path_match := "/assets/[...]"} = trails:retrieve("/assets/[...]"),
#{path_match := "/such/path"} = trails:retrieve("/such/path"),
#{path_match := "/very"} = trails:retrieve("/very"),
Expand All @@ -288,3 +290,14 @@ trails_store(_Config) ->
#{path_match := "/api/:id/resource"} = trails:retrieve("/api/:id/resource"),
notfound = trails:retrieve("/other"),
{comment, ""}.

%% @private
normalize_paths(RoutesPaths) ->
[normalize_path(Path) || Path <- RoutesPaths].

%% @private
normalize_path({PathMatch, ModuleHandler, Options}) ->
trails:trail(PathMatch, ModuleHandler, Options);
normalize_path({PathMatch, Constraints, ModuleHandler, Options}) ->
trails:trail(PathMatch, ModuleHandler, Options, #{}, Constraints);
normalize_path(Trail) when is_map(Trail) -> Trail.