diff --git a/src/trails.erl b/src/trails.erl index 75b633c..fec7ec6 100644 --- a/src/trails.erl +++ b/src/trails.erl @@ -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. @@ -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. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -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. diff --git a/test/trails_SUITE.erl b/test/trails_SUITE.erl index 4071628..eb18cbc 100644 --- a/test/trails_SUITE.erl +++ b/test/trails_SUITE.erl @@ -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, [], #{}), @@ -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"), @@ -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.