Current section

Files

Jump to
sqlode src sqlode@sql_paths.erl
Raw

src/sqlode@sql_paths.erl

-module(sqlode@sql_paths).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/sqlode/sql_paths.gleam").
-export([expand/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Shared `.sql` path expansion used by `generate` and `verify`.\n"
"\n"
" A configured `schema` or `queries` entry may be either a direct\n"
" path to a `.sql` file or a directory of `.sql` files. This\n"
" module applies the expansion rules both commands rely on so the\n"
" CLI presents a single, consistent surface: directories expand\n"
" to their `.sql` children in ascii-sorted order, empty\n"
" directories fail, and errors carry the originating path so\n"
" callers can render diagnostics.\n"
).
-file("src/sqlode/sql_paths.gleam", 21).
?DOC(
" Expand directory entries in `paths` into their `.sql` children.\n"
" File entries pass through unchanged. `error_fn` maps an\n"
" offending path and detail string into the caller's error type\n"
" so each command can attach its own wrapping variant while the\n"
" underlying diagnostic text stays consistent.\n"
).
-spec expand(list(binary()), fun((binary(), binary()) -> QRF)) -> {ok,
list(binary())} |
{error, QRF}.
expand(Paths, Error_fn) ->
_pipe = Paths,
_pipe@3 = gleam@list:try_map(
_pipe,
fun(Path) -> case simplifile_erl:is_directory(Path) of
{ok, true} ->
case simplifile:get_files(Path) of
{ok, Files} ->
Sql_files = begin
_pipe@1 = Files,
_pipe@2 = gleam@list:filter(
_pipe@1,
fun(F) ->
gleam_stdlib:string_ends_with(
F,
<<".sql"/utf8>>
)
end
),
gleam@list:sort(
_pipe@2,
fun gleam@string:compare/2
)
end,
case Sql_files of
[] ->
{error,
Error_fn(
Path,
<<"Directory contains no .sql files"/utf8>>
)};
_ ->
{ok, Sql_files}
end;
{error, Error} ->
{error,
Error_fn(
Path,
<<"Failed to read directory: "/utf8,
(simplifile:describe_error(Error))/binary>>
)}
end;
{ok, false} ->
{ok, [Path]};
{error, Error@1} ->
{error,
Error_fn(
Path,
<<"Failed to access path: "/utf8,
(simplifile:describe_error(Error@1))/binary>>
)}
end end
),
gleam@result:map(_pipe@3, fun lists:append/1).