From d1ad80d04133e9ac471887add1488f219a022952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Menou?= Date: Thu, 19 Dec 2024 22:13:34 +0100 Subject: [PATCH] Code dans le bon module --- apps/transport/lib/registry/engine.ex | 12 ++++++------ apps/transport/lib/registry/extractor.ex | 18 ++---------------- .../{extractor_test.exs => result_test.exs} | 8 ++++---- 3 files changed, 12 insertions(+), 26 deletions(-) rename apps/transport/test/registry/{extractor_test.exs => result_test.exs} (75%) diff --git a/apps/transport/lib/registry/engine.ex b/apps/transport/lib/registry/engine.ex index 0ebbeec0fb..106dca2fe7 100644 --- a/apps/transport/lib/registry/engine.ex +++ b/apps/transport/lib/registry/engine.ex @@ -3,9 +3,9 @@ defmodule Transport.Registry.Engine do Stream eligible resources and run extractors to produce a raw registry at the end. """ - alias Transport.Registry.Extractor alias Transport.Registry.GTFS alias Transport.Registry.Model.Stop + alias Transport.Registry.Result import Ecto.Query @@ -19,13 +19,13 @@ defmodule Transport.Registry.Engine do create_empty_csv_with_headers(output_file) enumerate_gtfs_resources(limit, formats) - |> Extractor.map_result(&prepare_extractor/1) + |> Result.map_result(&prepare_extractor/1) |> Task.async_stream(&download/1, max_concurrency: 10, timeout: 120_000) # one for Task.async_stream - |> Extractor.cat_results() + |> Result.cat_results() # one for download/1 - |> Extractor.cat_results() - |> Extractor.map_result(&extract_from_archive/1) + |> Result.cat_results() + |> Result.map_result(&extract_from_archive/1) |> dump_to_csv(output_file) end @@ -84,7 +84,7 @@ defmodule Transport.Registry.Engine do end end - @spec extract_from_archive({module(), Path.t()}) :: Extractor.result([Stop.t()]) + @spec extract_from_archive({module(), Path.t()}) :: Result.t([Stop.t()]) def extract_from_archive({extractor, file}) do Logger.debug("extract_from_archive #{extractor} #{file}") extractor.extract_from_archive(file) diff --git a/apps/transport/lib/registry/extractor.ex b/apps/transport/lib/registry/extractor.ex index 19ab45308b..0f5db7d9ec 100644 --- a/apps/transport/lib/registry/extractor.ex +++ b/apps/transport/lib/registry/extractor.ex @@ -6,21 +6,7 @@ defmodule Transport.Registry.Extractor do require Logger alias Transport.Registry.Model.Stop + alias Transport.Registry.Result - @type result(positive) :: {:ok, positive} | {:error, binary()} - - @callback extract_from_archive(path :: Path.t()) :: result([Stop.t()]) - - @spec cat_results(Stream.t(result(term()))) :: Stream.t(term()) - def cat_results(enumerable), do: Stream.flat_map(enumerable, &keep_ok/1) - - defp keep_ok({:ok, result}), do: [result] - defp keep_ok(_), do: [] - - @spec map_result(Stream.t(term()), (term() -> result(term()))) :: Stream.t(term()) - def map_result(enumerable, mapper) do - enumerable - |> Stream.map(mapper) - |> cat_results() - end + @callback extract_from_archive(path :: Path.t()) :: Result.t([Stop.t()]) end diff --git a/apps/transport/test/registry/extractor_test.exs b/apps/transport/test/registry/result_test.exs similarity index 75% rename from apps/transport/test/registry/extractor_test.exs rename to apps/transport/test/registry/result_test.exs index da60497735..d48a8d587b 100644 --- a/apps/transport/test/registry/extractor_test.exs +++ b/apps/transport/test/registry/result_test.exs @@ -1,8 +1,8 @@ -defmodule Transport.Registry.ExtractorTest do +defmodule Transport.Registry.ResultTest do use ExUnit.Case, async: false require Integer - alias Transport.Registry.Extractor + alias Transport.Registry.Result test "cat_results" do assert [] == cat_results([]) @@ -16,11 +16,11 @@ defmodule Transport.Registry.ExtractorTest do end defp cat_results(enumerable) do - enumerable |> Extractor.cat_results() |> Enum.to_list() + enumerable |> Result.cat_results() |> Enum.to_list() end defp map_result(enumerable, mapper) do - enumerable |> Extractor.map_result(mapper) |> Enum.to_list() + enumerable |> Result.map_result(mapper) |> Enum.to_list() end defp even_is_forbidden(i) when Integer.is_odd(i), do: {:ok, i}