Packages

Generate typed Gleam modules from a SQL schema file

Current section

Files

Jump to
glsql src glsql@config.erl
Raw

src/glsql@config.erl

-module(glsql@config).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glsql/config.gleam").
-export([default/0, parse/1, load/1]).
-export_type([table_naming/0, config/0]).
-type table_naming() :: {table_naming,
gleam@option:option(binary()),
gleam@option:option(binary())}.
-type config() :: {config,
binary(),
binary(),
binary(),
gleam@dict:dict(binary(), glsql@mapping:type_mapping()),
gleam@dict:dict(binary(), binary()),
gleam@dict:dict(binary(), table_naming())}.
-file("src/glsql/config.gleam", 31).
-spec default() -> config().
default() ->
{config,
<<"priv/schema.sql"/utf8>>,
<<"src/db"/utf8>>,
<<"pog"/utf8>>,
maps:new(),
maps:new(),
maps:new()}.
-file("src/glsql/config.gleam", 171).
-spec parse_table_names(gleam@dict:dict(binary(), tom:toml())) -> gleam@dict:dict(binary(), table_naming()).
parse_table_names(Parsed) ->
case tom:get_table(Parsed, [<<"tables"/utf8>>]) of
{error, _} ->
maps:new();
{ok, T} ->
_pipe = T,
_pipe@1 = maps:to_list(_pipe),
_pipe@2 = gleam@list:map(
_pipe@1,
fun(Pair) ->
{Name, _} = Pair,
Module = case tom:get_string(
Parsed,
[<<"tables"/utf8>>, Name, <<"module"/utf8>>]
) of
{ok, V} ->
{some, V};
{error, _} ->
none
end,
Type_name = case tom:get_string(
Parsed,
[<<"tables"/utf8>>, Name, <<"type"/utf8>>]
) of
{ok, V@1} ->
{some, V@1};
{error, _} ->
none
end,
{Name, {table_naming, Module, Type_name}}
end
),
maps:from_list(_pipe@2)
end.
-file("src/glsql/config.gleam", 154).
-spec parse_renames(gleam@dict:dict(binary(), tom:toml())) -> gleam@dict:dict(binary(), binary()).
parse_renames(Parsed) ->
case tom:get_table(Parsed, [<<"rename"/utf8>>]) of
{error, _} ->
maps:new();
{ok, T} ->
_pipe = T,
_pipe@1 = maps:to_list(_pipe),
_pipe@2 = gleam@list:filter_map(
_pipe@1,
fun(Pair) ->
{K, _} = Pair,
case tom:get_string(Parsed, [<<"rename"/utf8>>, K]) of
{ok, V} ->
{ok, {K, V}};
{error, _} ->
{error, nil}
end
end
),
maps:from_list(_pipe@2)
end.
-file("src/glsql/config.gleam", 139).
-spec type_field(gleam@dict:dict(binary(), tom:toml()), binary(), binary()) -> {ok,
binary()} |
{error, glsql@error:error()}.
type_field(Parsed, Name, Field) ->
case tom:get_string(Parsed, [<<"types"/utf8>>, Name, Field]) of
{ok, V} ->
{ok, V};
{error, _} ->
{error,
{config_error,
<<<<<<<<"[types."/utf8, Name/binary>>/binary,
"] must set `"/utf8>>/binary,
Field/binary>>/binary,
"`"/utf8>>,
none}}
end.
-file("src/glsql/config.gleam", 69).
-spec check_keys(list(binary()), list(binary())) -> {ok, nil} |
{error, glsql@error:error()}.
check_keys(Found, Known) ->
case gleam@list:find(Found, fun(K) -> not gleam@list:contains(Known, K) end) of
{error, nil} ->
{ok, nil};
{ok, Bad} ->
{error,
{config_error,
<<<<"`"/utf8, Bad/binary>>/binary,
"` is not a glsql.toml key"/utf8>>,
glsql@suggest:closest(Bad, Known)}}
end.
-file("src/glsql/config.gleam", 129).
-spec check_type_keys(gleam@dict:dict(binary(), tom:toml()), binary()) -> {ok,
nil} |
{error, glsql@error:error()}.
check_type_keys(Parsed, Name) ->
case tom:get_table(Parsed, [<<"types"/utf8>>, Name]) of
{error, _} ->
{ok, nil};
{ok, T} ->
check_keys(
maps:keys(T),
[<<"gleam_type"/utf8>>,
<<"decoder"/utf8>>,
<<"encoder"/utf8>>,
<<"imports"/utf8>>]
)
end.
-file("src/glsql/config.gleam", 101).
-spec parse_types(gleam@dict:dict(binary(), tom:toml())) -> {ok,
gleam@dict:dict(binary(), glsql@mapping:type_mapping())} |
{error, glsql@error:error()}.
parse_types(Parsed) ->
case tom:get_table(Parsed, [<<"types"/utf8>>]) of
{error, _} ->
{ok, maps:new()};
{ok, Types} ->
_pipe = Types,
_pipe@1 = maps:to_list(_pipe),
_pipe@2 = gleam@list:try_map(
_pipe@1,
fun(Pair) ->
{Name, _} = Pair,
gleam@result:'try'(
check_type_keys(Parsed, Name),
fun(_) ->
gleam@result:'try'(
type_field(Parsed, Name, <<"gleam_type"/utf8>>),
fun(Gleam_type) ->
gleam@result:'try'(
type_field(
Parsed,
Name,
<<"decoder"/utf8>>
),
fun(Decoder) ->
gleam@result:'try'(
type_field(
Parsed,
Name,
<<"encoder"/utf8>>
),
fun(Encoder) ->
{ok,
{string:lowercase(Name),
{type_mapping,
Gleam_type,
Decoder,
Encoder,
[]}}}
end
)
end
)
end
)
end
)
end
),
gleam@result:map(_pipe@2, fun maps:from_list/1)
end.
-file("src/glsql/config.gleam", 90).
-spec optional_string(gleam@dict:dict(binary(), tom:toml()), binary(), binary()) -> binary().
optional_string(Parsed, Key, Fallback) ->
case tom:get_string(Parsed, [Key]) of
{ok, V} ->
V;
{error, _} ->
Fallback
end.
-file("src/glsql/config.gleam", 80).
-spec require_string(gleam@dict:dict(binary(), tom:toml()), binary()) -> {ok,
binary()} |
{error, glsql@error:error()}.
require_string(Parsed, Key) ->
case tom:get_string(Parsed, [Key]) of
{ok, V} ->
{ok, V};
{error, _} ->
{error,
{config_error,
<<<<"glsql.toml must set `"/utf8, Key/binary>>/binary,
"`"/utf8>>,
none}}
end.
-file("src/glsql/config.gleam", 42).
-spec parse(binary()) -> {ok, config()} | {error, glsql@error:error()}.
parse(Src) ->
gleam@result:'try'(
begin
_pipe = tom:parse(Src),
gleam@result:map_error(
_pipe,
fun(_) ->
{config_error, <<"This file is not valid TOML"/utf8>>, none}
end
)
end,
fun(Parsed) ->
gleam@result:'try'(
check_keys(
maps:keys(Parsed),
[<<"schema"/utf8>>,
<<"out_dir"/utf8>>,
<<"driver"/utf8>>,
<<"types"/utf8>>,
<<"rename"/utf8>>,
<<"tables"/utf8>>]
),
fun(_) ->
gleam@result:'try'(
require_string(Parsed, <<"schema"/utf8>>),
fun(Schema) ->
Out_dir = optional_string(
Parsed,
<<"out_dir"/utf8>>,
<<"src/db"/utf8>>
),
Driver = optional_string(
Parsed,
<<"driver"/utf8>>,
<<"pog"/utf8>>
),
gleam@result:'try'(
parse_types(Parsed),
fun(Types) ->
Renames = parse_renames(Parsed),
Table_names = parse_table_names(Parsed),
{ok,
{config,
Schema,
Out_dir,
Driver,
Types,
Renames,
Table_names}}
end
)
end
)
end
)
end
).
-file("src/glsql/config.gleam", 195).
-spec load(binary()) -> {ok, config()} | {error, glsql@error:error()}.
load(Path) ->
case simplifile:read(Path) of
{ok, Src} ->
parse(Src);
{error, _} ->
{ok, default()}
end.