Current section

Files

Jump to
caffeine_lang src caffeine_lang@helpers.erl
Raw

src/caffeine_lang@helpers.erl

-module(caffeine_lang@helpers).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/caffeine_lang/helpers.gleam").
-export([map_reference_to_referrer_over_collection/4, extract_path_prefix/1, index_value_tuples/1, extract_value/3, extract_depends_on/1, extract_window_in_days/1, extract_indicators/1, extract_tags/1, build_system_tag_pairs/6]).
-export_type([value_tuple/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type value_tuple() :: {value_tuple,
binary(),
caffeine_lang@types:accepted_types(),
caffeine_lang@value:value()}.
-file("src/caffeine_lang/helpers.gleam", 22).
?DOC(false).
-spec map_reference_to_referrer_over_collection(
list(HEO),
list(HEQ),
fun((HEO) -> binary()),
fun((HEQ) -> binary())
) -> list({HEQ, HEO}).
map_reference_to_referrer_over_collection(
References,
Referrers,
Reference_name,
Referrer_reference
) ->
Reference_map = begin
_pipe = References,
_pipe@1 = gleam@list:map(
_pipe,
fun(Ref) -> {Reference_name(Ref), Ref} end
),
maps:from_list(_pipe@1)
end,
_pipe@2 = Referrers,
gleam@list:filter_map(
_pipe@2,
fun(Referrer) ->
_pipe@3 = gleam_stdlib:map_get(
Reference_map,
Referrer_reference(Referrer)
),
gleam@result:map(
_pipe@3,
fun(Reference) -> {Referrer, Reference} end
)
end
).
-file("src/caffeine_lang/helpers.gleam", 43).
?DOC(false).
-spec extract_path_prefix(binary()) -> {binary(), binary(), binary()}.
extract_path_prefix(Path) ->
case begin
_pipe = Path,
_pipe@1 = gleam@string:split(_pipe, <<"/"/utf8>>),
_pipe@2 = lists:reverse(_pipe@1),
_pipe@3 = gleam@list:take(_pipe@2, 3),
_pipe@4 = lists:reverse(_pipe@3),
gleam@list:map(
_pipe@4,
fun(Segment) ->
case gleam_stdlib:string_ends_with(
Segment,
<<".caffeine"/utf8>>
) of
true ->
gleam@string:drop_end(Segment, 9);
false ->
case gleam_stdlib:string_ends_with(
Segment,
<<".json"/utf8>>
) of
true ->
gleam@string:drop_end(Segment, 5);
false ->
Segment
end
end
end
)
end of
[Org, Team, Service] ->
{Org, Team, Service};
_ ->
{<<"unknown"/utf8>>, <<"unknown"/utf8>>, <<"unknown"/utf8>>}
end.
-file("src/caffeine_lang/helpers.gleam", 79).
?DOC(false).
-spec index_value_tuples(list(value_tuple())) -> gleam@dict:dict(binary(), value_tuple()).
index_value_tuples(Values) ->
_pipe = Values,
_pipe@1 = gleam@list:map(_pipe, fun(Vt) -> {erlang:element(2, Vt), Vt} end),
maps:from_list(_pipe@1).
-file("src/caffeine_lang/helpers.gleam", 89).
?DOC(false).
-spec extract_value(
gleam@dict:dict(binary(), value_tuple()),
binary(),
fun((caffeine_lang@value:value()) -> {ok, HEY} | {error, nil})
) -> {ok, HEY} | {error, nil}.
extract_value(Index, Label, Extractor) ->
_pipe = Index,
_pipe@1 = gleam_stdlib:map_get(_pipe, Label),
gleam@result:'try'(_pipe@1, fun(Vt) -> Extractor(erlang:element(4, Vt)) end).
-file("src/caffeine_lang/helpers.gleam", 110).
?DOC(" Shared implementation for extracting relations from a Result(ValueTuple, Nil).\n").
-spec extract_relations_from_value_tuple({ok, value_tuple()} | {error, nil}) -> gleam@dict:dict(caffeine_lang@linker@dependency:dependency_relation_type(), list(binary())).
extract_relations_from_value_tuple(Vt_result) ->
_pipe = Vt_result,
_pipe@8 = gleam@result:'try'(_pipe, fun(Vt) -> case erlang:element(4, Vt) of
{dict_value, D} ->
_pipe@1 = D,
_pipe@2 = maps:to_list(_pipe@1),
_pipe@5 = gleam@list:try_map(
_pipe@2,
fun(Pair) -> case erlang:element(2, Pair) of
{list_value, Items} ->
_pipe@3 = Items,
_pipe@4 = gleam@list:try_map(
_pipe@3,
fun caffeine_lang@value:extract_string/1
),
gleam@result:map(
_pipe@4,
fun(Strings) ->
{erlang:element(1, Pair), Strings}
end
);
_ ->
{error, nil}
end end
),
gleam@result:map(_pipe@5, fun(Pairs) -> _pipe@6 = Pairs,
_pipe@7 = gleam@list:filter_map(
_pipe@6,
fun(Pair@1) ->
case caffeine_lang@linker@dependency:parse_relation_type(
erlang:element(1, Pair@1)
) of
{ok, Rt} ->
{ok,
{Rt, erlang:element(2, Pair@1)}};
{error, nil} ->
{error, nil}
end
end
),
maps:from_list(_pipe@7) end);
_ ->
{error, nil}
end end),
gleam@result:unwrap(_pipe@8, maps:new()).
-file("src/caffeine_lang/helpers.gleam", 101).
?DOC(false).
-spec extract_depends_on(gleam@dict:dict(binary(), value_tuple())) -> gleam@dict:dict(caffeine_lang@linker@dependency:dependency_relation_type(), list(binary())).
extract_depends_on(Index) ->
_pipe = Index,
_pipe@1 = gleam_stdlib:map_get(_pipe, <<"depends_on"/utf8>>),
extract_relations_from_value_tuple(_pipe@1).
-file("src/caffeine_lang/helpers.gleam", 147).
?DOC(false).
-spec extract_window_in_days(gleam@dict:dict(binary(), value_tuple())) -> integer().
extract_window_in_days(Index) ->
_pipe = extract_value(
Index,
<<"window_in_days"/utf8>>,
fun caffeine_lang@value:extract_int/1
),
gleam@result:unwrap(_pipe, 30).
-file("src/caffeine_lang/helpers.gleam", 154).
?DOC(false).
-spec extract_indicators(gleam@dict:dict(binary(), value_tuple())) -> gleam@dict:dict(binary(), binary()).
extract_indicators(Index) ->
_pipe = Index,
_pipe@1 = gleam_stdlib:map_get(_pipe, <<"indicators"/utf8>>),
_pipe@2 = gleam@result:'try'(
_pipe@1,
fun(Vt) ->
caffeine_lang@value:extract_string_dict(erlang:element(4, Vt))
end
),
gleam@result:unwrap(_pipe@2, maps:new()).
-file("src/caffeine_lang/helpers.gleam", 165).
?DOC(false).
-spec extract_tags(gleam@dict:dict(binary(), value_tuple())) -> list({binary(),
binary()}).
extract_tags(Index) ->
_pipe = Index,
_pipe@1 = gleam_stdlib:map_get(_pipe, <<"tags"/utf8>>),
_pipe@2 = gleam@result:'try'(
_pipe@1,
fun(Vt) -> case erlang:element(4, Vt) of
nil_value ->
{ok, maps:new()};
{dict_value, _} ->
caffeine_lang@value:extract_string_dict(
erlang:element(4, Vt)
);
_ ->
{error, nil}
end end
),
_pipe@3 = gleam@result:unwrap(_pipe@2, maps:new()),
_pipe@4 = maps:to_list(_pipe@3),
gleam@list:sort(
_pipe@4,
fun(A, B) ->
gleam@string:compare(erlang:element(1, A), erlang:element(1, B))
end
).
-file("src/caffeine_lang/helpers.gleam", 185).
?DOC(false).
-spec build_system_tag_pairs(
caffeine_lang@identifiers:org_name(),
caffeine_lang@identifiers:team_name(),
caffeine_lang@identifiers:service_name(),
caffeine_lang@identifiers:measurement_name(),
caffeine_lang@identifiers:expectation_label(),
gleam@dict:dict(binary(), list(binary()))
) -> list({binary(), binary()}).
build_system_tag_pairs(
Org_name,
Team_name,
Service_name,
Measurement_name,
Friendly_label,
Misc
) ->
_pipe = [{<<"managed_by"/utf8>>, <<"caffeine"/utf8>>},
{<<"caffeine_version"/utf8>>, <<"6.2.2"/utf8>>},
{<<"org"/utf8>>, erlang:element(2, Org_name)},
{<<"team"/utf8>>, erlang:element(2, Team_name)},
{<<"service"/utf8>>, erlang:element(2, Service_name)},
{<<"measurement"/utf8>>, erlang:element(2, Measurement_name)},
{<<"expectation"/utf8>>, erlang:element(2, Friendly_label)}],
lists:append(
_pipe,
begin
_pipe@1 = Misc,
_pipe@2 = maps:to_list(_pipe@1),
_pipe@3 = gleam@list:sort(
_pipe@2,
fun(A, B) ->
gleam@string:compare(
erlang:element(1, A),
erlang:element(1, B)
)
end
),
gleam@list:flat_map(
_pipe@3,
fun(Pair) ->
{Key, Values} = Pair,
_pipe@4 = Values,
_pipe@5 = gleam@list:sort(
_pipe@4,
fun gleam@string:compare/2
),
gleam@list:map(_pipe@5, fun(Value) -> {Key, Value} end)
end
)
end
).