Packages
caffeine_lang
5.4.3
6.3.1
6.3.0
6.2.2
6.2.1
6.2.0
6.1.2
6.1.1
6.1.0
6.0.0
5.6.0
5.5.0
5.4.4
5.4.3
5.4.2
5.4.1
5.4.0
5.3.0
5.2.0
5.1.1
5.1.0
5.0.12
5.0.11
5.0.10
5.0.8
5.0.7
5.0.6
5.0.5
5.0.4
5.0.1
5.0.0
4.10.0
4.9.0
4.8.3
4.8.2
4.8.1
4.8.0
4.7.9
4.7.8
4.7.7
4.7.6
4.7.5
4.6.7
4.6.6
4.6.5
4.6.4
4.6.3
4.6.2
4.6.0
4.5.1
4.5.0
4.4.4
4.4.3
4.4.1
4.4.0
4.3.7
4.3.6
3.0.6
3.0.5
3.0.4
3.0.3
3.0.2
3.0.1
3.0.0
2.0.5
2.0.4
2.0.3
2.0.2
2.0.1
2.0.0
1.0.2
1.0.1
0.1.0
0.0.24
0.0.23
0.0.22
0.0.21
0.0.20
0.0.19
0.0.18
0.0.17
0.0.16
0.0.15
0.0.14
0.0.13
0.0.12
0.0.11
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.2
0.0.1
A compiler for generating reliability artifacts from service expectation definitions.
Current section
Files
Jump to
Current section
Files
src/caffeine_lang@codegen@datadog_template.erl
-module(caffeine_lang@codegen@datadog_template).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/caffeine_lang/codegen/datadog_template.gleam").
-export([cleanup_empty_template_artifacts/1, resolve_list_value/2, resolve_string_value/2, resolve_template/2, parse_template_type/1, parse_template_variable/1, parse_and_resolve_query_template/3]).
-export_type([template_variable/0, datadog_template_type/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 template_variable() :: {template_variable,
binary(),
binary(),
datadog_template_type()}.
-type datadog_template_type() :: raw | default | 'not'.
-file("src/caffeine_lang/codegen/datadog_template.gleam", 153).
-spec do_cleanup(binary()) -> binary().
do_cleanup(Query) ->
_pipe = Query,
_pipe@1 = gleam@string:replace(_pipe, <<", ,"/utf8>>, <<","/utf8>>),
_pipe@2 = gleam@string:replace(_pipe@1, <<",,"/utf8>>, <<","/utf8>>),
_pipe@3 = gleam@string:replace(
_pipe@2,
<<" AND AND "/utf8>>,
<<" AND "/utf8>>
),
_pipe@4 = gleam@string:replace(_pipe@3, <<", }"/utf8>>, <<"}"/utf8>>),
_pipe@5 = gleam@string:replace(_pipe@4, <<",}"/utf8>>, <<"}"/utf8>>),
_pipe@6 = gleam@string:replace(_pipe@5, <<"{, "/utf8>>, <<"{"/utf8>>),
_pipe@7 = gleam@string:replace(_pipe@6, <<"{,"/utf8>>, <<"{"/utf8>>),
_pipe@8 = gleam@string:replace(_pipe@7, <<", )"/utf8>>, <<")"/utf8>>),
_pipe@9 = gleam@string:replace(_pipe@8, <<",)"/utf8>>, <<")"/utf8>>),
_pipe@10 = gleam@string:replace(_pipe@9, <<"(, "/utf8>>, <<"("/utf8>>),
_pipe@11 = gleam@string:replace(_pipe@10, <<"(,"/utf8>>, <<"("/utf8>>),
_pipe@12 = gleam@string:replace(_pipe@11, <<" AND }"/utf8>>, <<"}"/utf8>>),
_pipe@13 = gleam@string:replace(_pipe@12, <<"{ AND "/utf8>>, <<"{"/utf8>>),
_pipe@14 = gleam@string:replace(_pipe@13, <<"{ }"/utf8>>, <<"{}"/utf8>>),
gleam@string:replace(_pipe@14, <<"( )"/utf8>>, <<"()"/utf8>>).
-file("src/caffeine_lang/codegen/datadog_template.gleam", 145).
?DOC(false).
-spec cleanup_empty_template_artifacts(binary()) -> binary().
cleanup_empty_template_artifacts(Query) ->
Cleaned = do_cleanup(Query),
case Cleaned =:= Query of
true ->
Cleaned;
false ->
cleanup_empty_template_artifacts(Cleaned)
end.
-file("src/caffeine_lang/codegen/datadog_template.gleam", 302).
?DOC(false).
-spec resolve_list_value(template_variable(), list(binary())) -> binary().
resolve_list_value(Template, Values) ->
case {erlang:element(4, Template), Values} of
{_, []} ->
<<""/utf8>>;
{raw, Values@1} ->
_pipe = Values@1,
gleam@string:join(_pipe, <<", "/utf8>>);
{default, Values@2} ->
datadog_query@filter:tag_in(erlang:element(3, Template), Values@2);
{'not', Values@3} ->
datadog_query@filter:tag_not_in(
erlang:element(3, Template),
Values@3
)
end.
-file("src/caffeine_lang/codegen/datadog_template.gleam", 286).
?DOC(false).
-spec resolve_string_value(template_variable(), binary()) -> binary().
resolve_string_value(Template, Value) ->
case erlang:element(4, Template) of
raw ->
Value;
default ->
datadog_query@filter:tag(erlang:element(3, Template), Value);
'not' ->
datadog_query@filter:negated_tag(erlang:element(3, Template), Value)
end.
-file("src/caffeine_lang/codegen/datadog_template.gleam", 249).
?DOC(false).
-spec resolve_template(template_variable(), caffeine_lang@helpers:value_tuple()) -> {ok,
binary()} |
{error, caffeine_lang@errors:compilation_error()}.
resolve_template(Template, Value_tuple) ->
gleam@result:'try'(
case erlang:element(2, Template) =:= erlang:element(2, Value_tuple) of
true ->
{ok, nil};
_ ->
{error,
caffeine_lang@errors:semantic_analysis_template_resolution_error(
<<<<<<<<"Mismatch between template input name ("/utf8,
(erlang:element(2, Template))/binary>>/binary,
") and input value label ("/utf8>>/binary,
(erlang:element(2, Value_tuple))/binary>>/binary,
")."/utf8>>
)}
end,
fun(_) ->
case caffeine_lang@types:resolve_to_string(
erlang:element(3, Value_tuple),
erlang:element(4, Value_tuple),
fun(_capture) -> resolve_string_value(Template, _capture) end,
fun(_capture@1) -> resolve_list_value(Template, _capture@1) end
) of
{ok, Resolved} ->
{ok, Resolved};
{error, Msg} ->
{error,
caffeine_lang@errors:semantic_analysis_template_resolution_error(
Msg
)}
end
end
).
-file("src/caffeine_lang/codegen/datadog_template.gleam", 234).
?DOC(false).
-spec parse_template_type(binary()) -> {ok, datadog_template_type()} |
{error, caffeine_lang@errors:compilation_error()}.
parse_template_type(Type_string) ->
case Type_string of
<<"not"/utf8>> ->
{ok, 'not'};
_ ->
{error,
caffeine_lang@errors:semantic_analysis_template_parse_error(
<<"Unknown template type: "/utf8, Type_string/binary>>
)}
end.
-file("src/caffeine_lang/codegen/datadog_template.gleam", 315).
?DOC(" Helper to parse Datadog format template variables (with ->).\n").
-spec parse_datadog_template_variable(binary(), binary()) -> {ok,
template_variable()} |
{error, caffeine_lang@errors:compilation_error()}.
parse_datadog_template_variable(Input_name, Rest) ->
case gleam@string:split_once(Rest, <<":"/utf8>>) of
{error, _} ->
{ok,
{template_variable,
gleam@string:trim(Rest),
Input_name,
default}};
{ok, {Datadog_attr, Type_string}} ->
case parse_template_type(gleam@string:trim(Type_string)) of
{error, E} ->
{error, E};
{ok, Template_type} ->
{ok,
{template_variable,
gleam@string:trim(Datadog_attr),
Input_name,
Template_type}}
end
end.
-file("src/caffeine_lang/codegen/datadog_template.gleam", 190).
?DOC(false).
-spec parse_template_variable(binary()) -> {ok, template_variable()} |
{error, caffeine_lang@errors:compilation_error()}.
parse_template_variable(Variable) ->
case gleam@string:split_once(Variable, <<"->"/utf8>>) of
{error, _} ->
Trimmed = gleam@string:trim(Variable),
case Trimmed of
<<""/utf8>> ->
{error,
caffeine_lang@errors:semantic_analysis_template_parse_error(
<<"Empty template variable name: "/utf8,
Variable/binary>>
)};
_ ->
{ok, {template_variable, Trimmed, <<""/utf8>>, raw}}
end;
{ok, {Input_name, Rest}} ->
Trimmed_input = gleam@string:trim(Input_name),
case {Trimmed_input, Rest} of
{<<""/utf8>>, _} ->
{error,
caffeine_lang@errors:semantic_analysis_template_parse_error(
<<"Empty input name in template: "/utf8,
Variable/binary>>
)};
{_, <<""/utf8>>} ->
{error,
caffeine_lang@errors:semantic_analysis_template_parse_error(
<<"Empty label name in template: "/utf8,
Variable/binary>>
)};
{_, _} ->
parse_datadog_template_variable(Trimmed_input, Rest)
end
end.
-file("src/caffeine_lang/codegen/datadog_template.gleam", 93).
?DOC(" Internal recursive implementation of template resolution.\n").
-spec do_parse_and_resolve_query_template(
binary(),
gleam@dict:dict(binary(), caffeine_lang@helpers:value_tuple())
) -> {ok, binary()} | {error, caffeine_lang@errors:compilation_error()}.
do_parse_and_resolve_query_template(Query, Index) ->
case gleam@string:split_once(Query, <<"$$"/utf8>>) of
{error, _} ->
{ok, Query};
{ok, {Before, Rest}} ->
case gleam@string:split_once(Rest, <<"$$"/utf8>>) of
{error, _} ->
{error,
caffeine_lang@errors:semantic_analysis_template_parse_error(
<<"Unexpected incomplete `$$` for substring: "/utf8,
Query/binary>>
)};
{ok, {Inside, Rest@1}} ->
gleam@result:'try'(
do_parse_and_resolve_query_template(Rest@1, Index),
fun(Rest_of_items) ->
gleam@result:'try'(
parse_template_variable(Inside),
fun(Template) ->
gleam@result:'try'(
begin
_pipe = Index,
_pipe@1 = gleam_stdlib:map_get(
_pipe,
erlang:element(2, Template)
),
gleam@result:replace_error(
_pipe@1,
caffeine_lang@errors:semantic_analysis_template_parse_error(
<<"Missing input for template: "/utf8,
(erlang:element(
2,
Template
))/binary>>
)
)
end,
fun(Value_tuple) ->
gleam@result:'try'(
resolve_template(
Template,
Value_tuple
),
fun(Resolved_template) ->
{ok,
<<<<Before/binary,
Resolved_template/binary>>/binary,
Rest_of_items/binary>>}
end
)
end
)
end
)
end
)
end
end.
-file("src/caffeine_lang/codegen/datadog_template.gleam", 79).
?DOC(false).
-spec parse_and_resolve_query_template(
binary(),
list(caffeine_lang@helpers:value_tuple()),
binary()
) -> {ok, binary()} | {error, caffeine_lang@errors:compilation_error()}.
parse_and_resolve_query_template(Query, Value_tuples, Identifier) ->
Index = caffeine_lang@helpers:index_value_tuples(Value_tuples),
gleam@result:'try'(
begin
_pipe = do_parse_and_resolve_query_template(Query, Index),
gleam@result:map_error(
_pipe,
fun(Err) ->
caffeine_lang@errors:prefix_error(Err, Identifier)
end
)
end,
fun(Resolved) -> {ok, cleanup_empty_template_artifacts(Resolved)} end
).