Current section

Files

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

src/caffeine_lang@parser@expectations.erl

-module(caffeine_lang@parser@expectations).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/caffeine_lang/parser/expectations.gleam").
-export([expectations_from_json/2, parse_from_json_string/2, parse_from_json_file/2]).
-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(), gleam@dynamic:dynamic_())}.
-file("src/caffeine_lang/parser/expectations.gleam", 53).
?DOC(false).
-spec expectations_from_json(
binary(),
list(caffeine_lang@parser@blueprints:blueprint())
) -> {ok, list(expectation())} | {error, gleam@json:decode_error()}.
expectations_from_json(Json_string, Blueprints) ->
Expectation_decoded = begin
gleam@dynamic@decode:field(
<<"name"/utf8>>,
caffeine_lang@common@decoders:non_empty_string_decoder(),
fun(Name) ->
gleam@dynamic@decode:field(
<<"blueprint_ref"/utf8>>,
caffeine_lang@common@decoders:named_reference_decoder(
Blueprints,
fun(B) -> erlang:element(2, B) end
),
fun(Blueprint_ref) ->
gleam@dynamic@decode:field(
<<"inputs"/utf8>>,
gleam@dynamic@decode:dict(
{decoder,
fun gleam@dynamic@decode:decode_string/1},
{decoder,
fun gleam@dynamic@decode:decode_dynamic/1}
),
fun(Inputs) ->
gleam@dynamic@decode:success(
{expectation, Name, Blueprint_ref, Inputs}
)
end
)
end
)
end
)
end,
Expectations_decoded = begin
gleam@dynamic@decode:field(
<<"expectations"/utf8>>,
gleam@dynamic@decode:list(Expectation_decoded),
fun(Expectations) -> gleam@dynamic@decode:success(Expectations) end
)
end,
gleam@json:parse(Json_string, Expectations_decoded).
-file("src/caffeine_lang/parser/expectations.gleam", 124).
-spec check_input_overshadowing(
list({expectation(), caffeine_lang@parser@blueprints:blueprint()})
) -> {ok, boolean()} | {error, caffeine_lang@common@errors:compilation_error()}.
check_input_overshadowing(Expectations_blueprint_collection) ->
Overshadow_errors = begin
_pipe = Expectations_blueprint_collection,
_pipe@1 = gleam@list:filter_map(
_pipe,
fun(Pair) ->
{Expectation, Blueprint} = Pair,
case caffeine_lang@common@validations:check_collection_key_overshadowing(
erlang:element(4, Expectation),
erlang:element(5, Blueprint),
<<<<"Expectation '"/utf8,
(erlang:element(2, Expectation))/binary>>/binary,
"' overshadowing inputs from blueprint: "/utf8>>
) of
{ok, _} ->
{error, nil};
{error, Msg} ->
{ok, Msg}
end
end
),
gleam@string:join(_pipe@1, <<", "/utf8>>)
end,
case Overshadow_errors of
<<""/utf8>> ->
{ok, true};
_ ->
{error, {parser_duplicate_error, Overshadow_errors}}
end.
-file("src/caffeine_lang/parser/expectations.gleam", 83).
?DOC(
" Validate expectations and return paired with their blueprints.\n"
" TODO: This provides massive duplication and is an area of low hanging fruit for optimization.\n"
).
-spec validate_expectations(
list(expectation()),
list(caffeine_lang@parser@blueprints:blueprint())
) -> {ok, list({expectation(), caffeine_lang@parser@blueprints:blueprint()})} |
{error, caffeine_lang@common@errors:compilation_error()}.
validate_expectations(Expectations, Blueprints) ->
Expectations_blueprint_collection = caffeine_lang@common@helpers:map_reference_to_referrer_over_collection(
Blueprints,
Expectations,
fun(B) -> erlang:element(2, B) end,
fun(E) -> erlang:element(3, E) end
),
gleam@result:'try'(
check_input_overshadowing(Expectations_blueprint_collection),
fun(_) ->
gleam@result:'try'(
caffeine_lang@common@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,
false
),
fun(_) ->
gleam@result:'try'(
caffeine_lang@common@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
).
-file("src/caffeine_lang/parser/expectations.gleam", 36).
?DOC(false).
-spec parse_from_json_string(
binary(),
list(caffeine_lang@parser@blueprints:blueprint())
) -> {ok, list({expectation(), caffeine_lang@parser@blueprints:blueprint()})} |
{error, caffeine_lang@common@errors:compilation_error()}.
parse_from_json_string(Json_string, Blueprints) ->
gleam@result:'try'(case expectations_from_json(Json_string, Blueprints) of
{ok, Expectations} ->
{ok, Expectations};
{error, Err} ->
{error,
caffeine_lang@common@errors:format_json_decode_error(Err)}
end, fun(Expectations@1) ->
validate_expectations(Expectations@1, Blueprints)
end).
-file("src/caffeine_lang/parser/expectations.gleam", 25).
?DOC(false).
-spec parse_from_json_file(
binary(),
list(caffeine_lang@parser@blueprints:blueprint())
) -> {ok, list({expectation(), caffeine_lang@parser@blueprints:blueprint()})} |
{error, caffeine_lang@common@errors:compilation_error()}.
parse_from_json_file(File_path, Blueprints) ->
gleam@result:'try'(
caffeine_lang@common@helpers:json_from_file(File_path),
fun(Json_string) -> parse_from_json_string(Json_string, Blueprints) end
).