-module(sqlode@naming). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/sqlode/naming.gleam"). -export([new/0, to_pascal_case/2, normalize_identifier/1, singularize/1, table_type_name/3, to_snake_case/2]). -export_type([naming_context/0]). -type naming_context() :: {naming_context, gleam@regexp:regexp(), gleam@regexp:regexp(), gleam@regexp:regexp()}. -file("src/sqlode/naming.gleam", 14). -spec new() -> naming_context(). new() -> Word_separator@1 = case gleam@regexp:from_string(<<"[_\\-\\s./]+"/utf8>>) of {ok, Word_separator} -> Word_separator; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"sqlode/naming"/utf8>>, function => <<"new"/utf8>>, line => 16, value => _assert_fail, start => 350, 'end' => 416, pattern_start => 361, pattern_end => 379}) end, Camel_case@1 = case gleam@regexp:from_string( <<"([A-Z]+(?=[A-Z][a-z])|[A-Z]?[a-z]+[0-9]*|[A-Z]+[0-9]*|[0-9]+)"/utf8>> ) of {ok, Camel_case} -> Camel_case; _assert_fail@1 -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"sqlode/naming"/utf8>>, function => <<"new"/utf8>>, line => 40, value => _assert_fail@1, start => 1754, 'end' => 1882, pattern_start => 1765, pattern_end => 1779}) end, Underscore_before_caps@1 = case gleam@regexp:from_string( <<"([a-z0-9])([A-Z])"/utf8>> ) of {ok, Underscore_before_caps} -> Underscore_before_caps; _assert_fail@2 -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"sqlode/naming"/utf8>>, function => <<"new"/utf8>>, line => 45, value => _assert_fail@2, start => 1964, 'end' => 2047, pattern_start => 1975, pattern_end => 2001}) end, {naming_context, Word_separator@1, Camel_case@1, Underscore_before_caps@1}. -file("src/sqlode/naming.gleam", 68). -spec capitalize(binary()) -> binary(). capitalize(Input) -> case gleam_stdlib:string_pop_grapheme(Input) of {ok, {First, Rest}} -> <<(string:uppercase(First))/binary, Rest/binary>>; {error, _} -> Input end. -file("src/sqlode/naming.gleam", 83). -spec split_camel_case(binary(), naming_context()) -> list(binary()). split_camel_case(Input, Ctx) -> Matches = gleam@regexp:scan(erlang:element(3, Ctx), Input), case Matches of [] -> [Input]; _ -> gleam@list:map( Matches, fun(Match) -> {match, Content, _} = Match, Content end ) end. -file("src/sqlode/naming.gleam", 75). -spec split_words(binary(), naming_context()) -> list(binary()). split_words(Input, Ctx) -> Parts = gleam@regexp:split(erlang:element(2, Ctx), Input), _pipe = Parts, _pipe@1 = gleam@list:flat_map( _pipe, fun(_capture) -> split_camel_case(_capture, Ctx) end ), gleam@list:filter(_pipe@1, fun(Part) -> Part /= <<""/utf8>> end). -file("src/sqlode/naming.gleam", 50). -spec to_pascal_case(naming_context(), binary()) -> binary(). to_pascal_case(Ctx, Input) -> _pipe = Input, _pipe@1 = split_words(_pipe, Ctx), _pipe@2 = gleam@list:map(_pipe@1, fun capitalize/1), gleam@string:join(_pipe@2, <<""/utf8>>). -file("src/sqlode/naming.gleam", 96). -spec insert_underscores_before_caps(binary(), naming_context()) -> binary(). insert_underscores_before_caps(Input, Ctx) -> gleam_regexp_ffi:replace(erlang:element(4, Ctx), Input, <<"\\1_\\2"/utf8>>). -file("src/sqlode/naming.gleam", 109). -spec strip_identifier_quotes(binary()) -> binary(). strip_identifier_quotes(Identifier) -> Length = string:length(Identifier), case Length >= 2 of false -> Identifier; true -> First = gleam@string:slice(Identifier, 0, 1), Last = gleam@string:slice(Identifier, Length - 1, 1), Is_quoted = (((First =:= <<"\""/utf8>>) andalso (Last =:= <<"\""/utf8>>)) orelse ((First =:= <<"`"/utf8>>) andalso (Last =:= <<"`"/utf8>>))) orelse ((First =:= <<"["/utf8>>) andalso (Last =:= <<"]"/utf8>>)), case Is_quoted of true -> gleam@string:slice(Identifier, 1, Length - 2); false -> Identifier end end. -file("src/sqlode/naming.gleam", 131). -spec last_dot_segment(binary()) -> binary(). last_dot_segment(Identifier) -> case gleam_stdlib:contains_string(Identifier, <<"."/utf8>>) of true -> _pipe = Identifier, _pipe@1 = gleam@string:split(_pipe, <<"."/utf8>>), _pipe@2 = gleam@list:last(_pipe@1), gleam@result:unwrap(_pipe@2, Identifier); false -> Identifier end. -file("src/sqlode/naming.gleam", 100). -spec normalize_identifier(binary()) -> binary(). normalize_identifier(Identifier) -> _pipe = Identifier, _pipe@1 = gleam@string:trim(_pipe), _pipe@2 = strip_identifier_quotes(_pipe@1), _pipe@3 = last_dot_segment(_pipe@2), _pipe@4 = strip_identifier_quotes(_pipe@3), string:lowercase(_pipe@4). -file("src/sqlode/naming.gleam", 238). -spec apply_case(binary(), binary()) -> binary(). apply_case(Original, Replacement) -> case string:uppercase(Original) =:= Original of true -> string:uppercase(Replacement); false -> case begin _pipe = gleam@string:first(Original), _pipe@1 = gleam@result:map( _pipe, fun(C) -> string:uppercase(C) =:= C end ), gleam@result:unwrap(_pipe@1, false) end of true -> capitalize(Replacement); false -> Replacement end end. -file("src/sqlode/naming.gleam", 253). -spec apply_suffix_case(binary(), binary()) -> binary(). apply_suffix_case(Word, Suffix) -> Last_char = begin _pipe = gleam@string:slice(Word, string:length(Word) - 1, 1), string:lowercase(_pipe) end, case Last_char =:= string:uppercase(Last_char) of true -> string:uppercase(Suffix); false -> Suffix end. -file("src/sqlode/naming.gleam", 214). -spec apply_first_suffix_rule( binary(), binary(), list({binary(), integer(), binary()}) ) -> binary(). apply_first_suffix_rule(Word, Lower, Rules) -> case Rules of [] -> Word; [{Suffix, Drop, Replacement} | Rest] -> case gleam_stdlib:string_ends_with(Lower, Suffix) of true -> case Drop of 0 -> Word; _ -> <<(gleam@string:drop_end(Word, Drop))/binary, (case Replacement of <<""/utf8>> -> <<""/utf8>>; R -> apply_suffix_case(Word, R) end)/binary>> end; false -> apply_first_suffix_rule(Word, Lower, Rest) end end. -file("src/sqlode/naming.gleam", 199). -spec singularize_by_suffix(binary(), binary()) -> binary(). singularize_by_suffix(Word, Lower) -> Suffix_rules = [{<<"ies"/utf8>>, 3, <<"y"/utf8>>}, {<<"ves"/utf8>>, 3, <<"f"/utf8>>}, {<<"sses"/utf8>>, 2, <<""/utf8>>}, {<<"shes"/utf8>>, 2, <<""/utf8>>}, {<<"ches"/utf8>>, 2, <<""/utf8>>}, {<<"xes"/utf8>>, 2, <<""/utf8>>}, {<<"zes"/utf8>>, 2, <<""/utf8>>}, {<<"ss"/utf8>>, 0, <<""/utf8>>}, {<<"s"/utf8>>, 1, <<""/utf8>>}], apply_first_suffix_rule(Word, Lower, Suffix_rules). -file("src/sqlode/naming.gleam", 192). -spec singularize_regular(binary(), binary()) -> binary(). singularize_regular(Word, Lower) -> case string:length(Word) =< 2 of true -> Word; false -> singularize_by_suffix(Word, Lower) end. -file("src/sqlode/naming.gleam", 153). -spec singularize(binary()) -> binary(). singularize(Word) -> Lower = string:lowercase(Word), case Lower of <<"people"/utf8>> -> apply_case(Word, <<"person"/utf8>>); <<"children"/utf8>> -> apply_case(Word, <<"child"/utf8>>); <<"men"/utf8>> -> apply_case(Word, <<"man"/utf8>>); <<"women"/utf8>> -> apply_case(Word, <<"woman"/utf8>>); <<"mice"/utf8>> -> apply_case(Word, <<"mouse"/utf8>>); <<"geese"/utf8>> -> apply_case(Word, <<"goose"/utf8>>); <<"teeth"/utf8>> -> apply_case(Word, <<"tooth"/utf8>>); <<"feet"/utf8>> -> apply_case(Word, <<"foot"/utf8>>); <<"data"/utf8>> -> apply_case(Word, <<"datum"/utf8>>); <<"media"/utf8>> -> apply_case(Word, <<"medium"/utf8>>); <<"criteria"/utf8>> -> apply_case(Word, <<"criterion"/utf8>>); <<"news"/utf8>> -> Word; <<"series"/utf8>> -> Word; <<"species"/utf8>> -> Word; <<"status"/utf8>> -> Word; <<"bus"/utf8>> -> Word; <<"alias"/utf8>> -> Word; <<"address"/utf8>> -> Word; <<"campus"/utf8>> -> Word; <<"bonus"/utf8>> -> Word; <<"process"/utf8>> -> Word; <<"analysis"/utf8>> -> Word; <<"basis"/utf8>> -> Word; <<"crisis"/utf8>> -> Word; <<"diagnosis"/utf8>> -> Word; <<"thesis"/utf8>> -> Word; <<"virus"/utf8>> -> Word; <<"focus"/utf8>> -> Word; <<"consensus"/utf8>> -> Word; <<"corpus"/utf8>> -> Word; _ -> singularize_regular(Word, Lower) end. -file("src/sqlode/naming.gleam", 142). -spec table_type_name(naming_context(), binary(), boolean()) -> binary(). table_type_name(Naming_ctx, Table_name, Emit_exact_table_names) -> case Emit_exact_table_names of true -> to_pascal_case(Naming_ctx, Table_name); false -> to_pascal_case(Naming_ctx, singularize(Table_name)) end. -file("src/sqlode/naming.gleam", 263). -spec escape_keyword(binary()) -> binary(). escape_keyword(Name) -> case Name of <<"as"/utf8>> -> <>; <<"assert"/utf8>> -> <>; <<"auto"/utf8>> -> <>; <<"case"/utf8>> -> <>; <<"const"/utf8>> -> <>; <<"external"/utf8>> -> <>; <<"fn"/utf8>> -> <>; <<"if"/utf8>> -> <>; <<"import"/utf8>> -> <>; <<"let"/utf8>> -> <>; <<"opaque"/utf8>> -> <>; <<"panic"/utf8>> -> <>; <<"pub"/utf8>> -> <>; <<"test"/utf8>> -> <>; <<"todo"/utf8>> -> <>; <<"type"/utf8>> -> <>; <<"use"/utf8>> -> <>; _ -> Name end. -file("src/sqlode/naming.gleam", 57). -spec to_snake_case(naming_context(), binary()) -> binary(). to_snake_case(Ctx, Input) -> Result = begin _pipe = Input, _pipe@1 = insert_underscores_before_caps(_pipe, Ctx), _pipe@2 = split_words(_pipe@1, Ctx), _pipe@3 = gleam@list:map(_pipe@2, fun string:lowercase/1), gleam@string:join(_pipe@3, <<"_"/utf8>>) end, escape_keyword(Result).