Packages

A fun, friendly, and type-safe ORM for Gleam! Compose type-safe SQL queries, generate decoders and types from schemas, and with a fun CLI!

Current section

Files

Jump to
funsies src funsies@decoder.erl
Raw

src/funsies@decoder.erl

-module(funsies@decoder).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([generate_row_type/1, generate_decoder_code/1]).
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/decoder.gleam", 142).
-spec simple_hash(list(funsies@schema:column())) -> binary().
simple_hash(Columns) ->
<<""/utf8>>.
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/decoder.gleam", 11).
-spec generate_row_type(funsies@schema:table()) -> binary().
generate_row_type(Table) ->
Columns = erlang:element(3, Table),
Fields = begin
_pipe = Columns,
_pipe@1 = gleam@list:map(_pipe, fun(Column) -> case Column of
{string_column, Name, _} ->
<<Name/binary, ": String"/utf8>>;
{int_column, Name@1} ->
<<Name@1/binary, ": Int"/utf8>>;
{bool_column, Name@2} ->
<<Name@2/binary, ": Bool"/utf8>>;
{serial_column, Name@3} ->
<<Name@3/binary, ": Int"/utf8>>;
{foreign_key_column, Name@4, _, _, Ref_type} ->
<<<<Name@4/binary, ": "/utf8>>/binary,
(gleam@string:capitalise(Ref_type))/binary>>;
_ ->
<<""/utf8>>
end end),
gleam@string:join(_pipe@1, <<", "/utf8>>)
end,
Capitalized_name = gleam@string:capitalise(erlang:element(2, Table)),
Unique_id = simple_hash(Columns),
Path = <<<<"./src/funs/"/utf8, (erlang:element(2, Table))/binary>>/binary,
".gleam"/utf8>>,
Type_name = <<<<Capitalized_name/binary, "Row"/utf8>>/binary,
Unique_id/binary>>,
Type_definition = <<<<<<<<<<<<"pub type "/utf8, Type_name/binary>>/binary,
" { "/utf8>>/binary,
Type_name/binary>>/binary,
"("/utf8>>/binary,
Fields/binary>>/binary,
") }\n"/utf8>>,
simplifile_erl:create_directory(<<"./src/funs/"/utf8>>),
Existing_content = case simplifile:read(Path) of
{ok, Content} ->
Content;
{error, _} ->
<<""/utf8>>
end,
case gleam_stdlib:contains_string(Existing_content, Type_name) of
true ->
gleam@io:debug(
<<"Type definition already exists, skipping generation."/utf8>>
);
false ->
simplifile:append(Path, Type_definition),
gleam@io:debug(<<"Generating..."/utf8>>)
end.
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/decoder.gleam", 55).
-spec generate_decoder_code(funsies@schema:table()) -> binary().
generate_decoder_code(Table) ->
Columns = erlang:element(3, Table),
Capitalized_name = gleam@string:capitalise(erlang:element(2, Table)),
Unique_id = simple_hash(Columns),
Decoder_name = <<<<(erlang:element(2, Table))/binary, "_decoder_"/utf8>>/binary,
(gleam@string:lowercase(Unique_id))/binary>>,
Path = <<<<"./src/funs/"/utf8, (erlang:element(2, Table))/binary>>/binary,
"_decoder.gleam"/utf8>>,
Existing_content = case simplifile:read(Path) of
{ok, Content} ->
Content;
{error, _} ->
<<""/utf8>>
end,
Import_schema = <<"import funs/"/utf8, (erlang:element(2, Table))/binary>>,
Import_decode = <<"import decode"/utf8>>,
Imports = <<(case gleam_stdlib:contains_string(
Existing_content,
Import_schema
) of
true ->
<<""/utf8>>;
false ->
<<Import_schema/binary, "\n"/utf8>>
end)/binary,
(case gleam_stdlib:contains_string(Existing_content, Import_decode) of
true ->
<<""/utf8>>;
false ->
<<Import_decode/binary, "\n"/utf8>>
end)/binary>>,
Parameters = begin
_pipe = Columns,
_pipe@1 = gleam@list:map(
_pipe,
fun(Column) ->
<<<<"use "/utf8, (erlang:element(2, Column))/binary>>/binary,
" <- decode.parameter"/utf8>>
end
),
gleam@string:join(_pipe@1, <<"\n"/utf8>>)
end,
Fields = begin
_pipe@2 = Columns,
_pipe@3 = gleam@list:index_map(
_pipe@2,
fun(Column@1, Index) -> case Column@1 of
{string_column, Name, _} ->
<<<<" |> decode.field("/utf8,
(gleam@int:to_string(Index))/binary>>/binary,
", decode.string)"/utf8>>;
{int_column, Name@1} ->
<<<<" |> decode.field("/utf8,
(gleam@int:to_string(Index))/binary>>/binary,
", decode.int)"/utf8>>;
{bool_column, Name@2} ->
<<<<" |> decode.field("/utf8,
(gleam@int:to_string(Index))/binary>>/binary,
", decode.bool)"/utf8>>;
{serial_column, Name@3} ->
<<<<" |> decode.field("/utf8,
(gleam@int:to_string(Index))/binary>>/binary,
", decode.int)"/utf8>>;
{foreign_key_column, Name@4, _, _, Ref_type} ->
<<<<<<<<" |> decode.field("/utf8,
(gleam@int:to_string(Index))/binary>>/binary,
", decode."/utf8>>/binary,
(gleam@string:lowercase(Ref_type))/binary>>/binary,
")"/utf8>>;
_ ->
<<<<" |> decode.field("/utf8,
(gleam@int:to_string(Index))/binary>>/binary,
", decode.string)"/utf8>>
end end
),
gleam@string:join(_pipe@3, <<"\n"/utf8>>)
end,
Decoder_code = <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Imports/binary,
"pub fn "/utf8>>/binary,
Decoder_name/binary>>/binary,
"() {\n"/utf8>>/binary,
" decode.into({\n"/utf8>>/binary,
Parameters/binary>>/binary,
"\n "/utf8>>/binary,
(erlang:element(2, Table))/binary>>/binary,
"."/utf8>>/binary,
Capitalized_name/binary>>/binary,
"Row"/utf8>>/binary,
Unique_id/binary>>/binary,
"("/utf8>>/binary,
(begin
_pipe@4 = Columns,
_pipe@5 = gleam@list:map(
_pipe@4,
fun(Column@2) ->
<<<<(erlang:element(2, Column@2))/binary,
": "/utf8>>/binary,
(erlang:element(2, Column@2))/binary>>
end
),
gleam@string:join(_pipe@5, <<", "/utf8>>)
end)/binary>>/binary,
")\n })\n"/utf8>>/binary,
Fields/binary>>/binary,
"\n}\n"/utf8>>,
case gleam_stdlib:contains_string(Existing_content, Decoder_name) of
true ->
gleam@io:debug(
<<"Decoder function already exists, skipping generation."/utf8>>
);
false ->
simplifile_erl:create_directory(<<"./src/funs/"/utf8>>),
simplifile:append(Path, Decoder_code),
gleam@io:debug(<<"Generating..."/utf8>>)
end.