src/datadog_query@lint.erl

-module(datadog_query@lint).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/datadog_query/lint.gleam").
-export([at_prefixed_attrs/1]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

-file("src/datadog_query/lint.gleam", 30).
-spec token_to_at_attr(binary()) -> {ok, binary()} | {error, nil}.
token_to_at_attr(Token) ->
    Stripped = case gleam_stdlib:string_starts_with(Token, <<"!"/utf8>>) of
        true ->
            gleam@string:drop_start(Token, 1);

        false ->
            Token
    end,
    case gleam_stdlib:string_starts_with(Stripped, <<"@"/utf8>>) of
        false ->
            {error, nil};

        true ->
            Attr = case gleam@string:split_once(Stripped, <<":"/utf8>>) of
                {ok, {Before_colon, _}} ->
                    Before_colon;

                {error, _} ->
                    Stripped
            end,
            case Attr of
                <<"@"/utf8>> ->
                    {error, nil};

                _ ->
                    {ok, Attr}
            end
    end.

-file("src/datadog_query/lint.gleam", 18).
?DOC(
    " Scans a Datadog query string for `@`-prefixed attribute names used in\n"
    " filter position (e.g. `@type:foo` or `@type IN (...)`).\n"
    "\n"
    " Datadog metric filters strip the leading `@` from log facets when log-based\n"
    " metrics are generated — the metric tag for log facet `@type` is just `type`.\n"
    " A query that filters on `@type` against a log-based metric is rejected by\n"
    " the SLO API with `400: numerator query is invalid`.\n"
    "\n"
    " Returns a deduplicated list of `@`-prefixed attribute names found.\n"
).
-spec at_prefixed_attrs(binary()) -> list(binary()).
at_prefixed_attrs(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, <<"("/utf8>>, <<" "/utf8>>),
    _pipe@4 = gleam@string:replace(_pipe@3, <<")"/utf8>>, <<" "/utf8>>),
    _pipe@5 = gleam@string:replace(_pipe@4, <<","/utf8>>, <<" "/utf8>>),
    _pipe@6 = gleam@string:split(_pipe@5, <<" "/utf8>>),
    _pipe@7 = gleam@list:filter_map(_pipe@6, fun token_to_at_attr/1),
    gleam@list:unique(_pipe@7).