Current section

Files

Jump to
caffeine_lang src caffeine_lang@linker@expectations.erl
Raw

src/caffeine_lang@linker@expectations.erl

-module(caffeine_lang@linker@expectations).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/caffeine_lang/linker/expectations.gleam").
-export([validate_expectations/3]).
-export_type([expectation/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 expectation() :: {expectation,
binary(),
binary(),
gleam@dict:dict(binary(), caffeine_lang@value:value())}.
-file("src/caffeine_lang/linker/expectations.gleam", 76).
?DOC(
" Validates that every expectation's blueprint_ref matches an existing blueprint.\n"
" Includes Levenshtein-based \"did you mean?\" suggestions for unknown refs.\n"
).
-spec validate_blueprint_refs(
list(expectation()),
list(caffeine_lang@linker@blueprints:blueprint())
) -> {ok, nil} | {error, caffeine_lang@errors:compilation_error()}.
validate_blueprint_refs(Expectations, Blueprints) ->
Blueprint_names = gleam@list:map(
Blueprints,
fun(B) -> erlang:element(2, B) end
),
Missing = begin
_pipe = Expectations,
_pipe@1 = gleam@list:filter(
_pipe,
fun(E) ->
not gleam@list:contains(Blueprint_names, erlang:element(3, E))
end
),
gleam@list:map(_pipe@1, fun(E@1) -> erlang:element(3, E@1) end)
end,
case Missing of
[] ->
{ok, nil};
[Single_ref] ->
Suggestion = caffeine_lang@string_distance:closest_match(
Single_ref,
Blueprint_names
),
{error,
{linker_parse_error,
<<"Unknown blueprint reference: "/utf8, Single_ref/binary>>,
begin
_record = caffeine_lang@errors:empty_context(),
{error_context,
erlang:element(2, _record),
erlang:element(3, _record),
erlang:element(4, _record),
erlang:element(5, _record),
Suggestion}
end}};
_ ->
{error,
{linker_parse_error,
<<"Unknown blueprint reference(s): "/utf8,
(gleam@string:join(Missing, <<", "/utf8>>))/binary>>,
caffeine_lang@errors:empty_context()}}
end.
-file("src/caffeine_lang/linker/expectations.gleam", 107).
-spec check_input_overshadowing(
list({expectation(), caffeine_lang@linker@blueprints:blueprint()}),
binary()
) -> {ok, nil} | {error, caffeine_lang@errors:compilation_error()}.
check_input_overshadowing(Expectations_blueprint_collection, Path_prefix) ->
caffeine_lang@linker@validations:validate_no_overshadowing(
Expectations_blueprint_collection,
fun(Expectation) -> erlang:element(4, Expectation) end,
fun(Blueprint) -> erlang:element(5, Blueprint) end,
fun(Expectation@1) ->
<<<<<<"expectation '"/utf8, Path_prefix/binary>>/binary,
(erlang:element(2, Expectation@1))/binary>>/binary,
"' - overshadowing inputs from blueprint: "/utf8>>
end
).
-file("src/caffeine_lang/linker/expectations.gleam", 23).
?DOC(false).
-spec validate_expectations(
list(expectation()),
list(caffeine_lang@linker@blueprints:blueprint()),
binary()
) -> {ok, list({expectation(), caffeine_lang@linker@blueprints:blueprint()})} |
{error, caffeine_lang@errors:compilation_error()}.
validate_expectations(Expectations, Blueprints, Source_path) ->
gleam@result:'try'(
validate_blueprint_refs(Expectations, Blueprints),
fun(_) ->
Expectations_blueprint_collection = caffeine_lang@helpers:map_reference_to_referrer_over_collection(
Blueprints,
Expectations,
fun(B) -> erlang:element(2, B) end,
fun(E) -> erlang:element(3, E) end
),
{Org, Team, Service} = caffeine_lang@helpers:extract_path_prefix(
Source_path
),
Path_prefix = <<<<<<<<<<Org/binary, "."/utf8>>/binary, Team/binary>>/binary,
"."/utf8>>/binary,
Service/binary>>/binary,
"."/utf8>>,
gleam@result:'try'(
check_input_overshadowing(
Expectations_blueprint_collection,
Path_prefix
),
fun(_) ->
gleam@result:'try'(
caffeine_lang@linker@validations:validate_inputs_for_collection(
Expectations_blueprint_collection,
fun(Expectation) ->
erlang:element(4, Expectation)
end,
fun(Blueprint) ->
Blueprint_input_keys = begin
_pipe = erlang:element(5, Blueprint),
maps:keys(_pipe)
end,
_pipe@1 = erlang:element(4, Blueprint),
gleam@dict:filter(
_pipe@1,
fun(Key, _) ->
not gleam@list:contains(
Blueprint_input_keys,
Key
)
end
)
end,
fun(Expectation@1) ->
<<<<<<"expectation '"/utf8, Path_prefix/binary>>/binary,
(erlang:element(2, Expectation@1))/binary>>/binary,
"'"/utf8>>
end,
false
),
fun(_) ->
gleam@result:'try'(
caffeine_lang@linker@validations:validate_relevant_uniqueness(
Expectations,
fun(E@1) -> erlang:element(2, E@1) end,
<<"expectation names"/utf8>>
),
fun(_) ->
{ok, Expectations_blueprint_collection}
end
)
end
)
end
)
end
).