Current section

Files

Jump to
openfeature src openfeature@providers@in_memory.erl
Raw

src/openfeature@providers@in_memory.erl

-module(openfeature@providers@in_memory).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new_variants/1, provider/1]).
-export_type([flag/0]).
-type flag() :: {flag,
binary(),
gleam@dict:dict(binary(), gleam@dynamic:dynamic_()),
gleam@option:option(fun((openfeature@evaluation_context:evaluation_context()) -> openfeature@evaluation:resolution_details(gleam@dynamic:dynamic_())))}.
-spec new_variants(list({binary(), any()})) -> gleam@dict:dict(binary(), gleam@dynamic:dynamic_()).
new_variants(Variants) ->
_pipe = Variants,
_pipe@1 = gleam@list:map(
_pipe,
fun(Key_and_value) ->
{erlang:element(1, Key_and_value),
gleam@dynamic:from(erlang:element(2, Key_and_value))}
end
),
maps:from_list(_pipe@1).
-spec get_metadata() -> openfeature@provider:metadata().
get_metadata() ->
{metadata, <<"In-Memory Provider"/utf8>>}.
-spec evaluate_context_evaluator(
GBX,
fun((openfeature@evaluation_context:evaluation_context()) -> openfeature@evaluation:resolution_details(gleam@dynamic:dynamic_())),
openfeature@evaluation_context:evaluation_context(),
fun((gleam@dynamic:dynamic_()) -> {ok, GBX} |
{error, list(gleam@dynamic:decode_error())})
) -> openfeature@evaluation:resolution_details(GBX).
evaluate_context_evaluator(Default_value, Evaluator, Context, Decoder) ->
Resolution_details = Evaluator(Context),
_pipe = Decoder(erlang:element(2, Resolution_details)),
_pipe@1 = gleam@result:map(_pipe, fun(Val) -> case Resolution_details of
{resolution_success, _, Reason} ->
{resolution_success, Val, Reason};
{resolution_error, _, Reason@1, Code, Message} ->
{resolution_error, Val, Reason@1, Code, Message}
end end),
gleam@result:unwrap(
_pipe@1,
{resolution_error,
Default_value,
error,
type_mismatch,
<<"type mismatch"/utf8>>}
).
-spec inner_resolve(
binary(),
gleam@dict:dict(binary(), flag()),
GBU,
openfeature@evaluation_context:evaluation_context(),
fun((gleam@dynamic:dynamic_()) -> {ok, GBU} |
{error, list(gleam@dynamic:decode_error())})
) -> {ok, openfeature@evaluation:resolution_details(GBU)} | {error, nil}.
inner_resolve(Flag, Flags, Default_value, Evaluation_context, Decoder) ->
gleam@result:map(
gleam@dict:get(Flags, Flag),
fun(Found_flag) -> case erlang:element(4, Found_flag) of
{some, Context_evaluator} ->
evaluate_context_evaluator(
Default_value,
Context_evaluator,
Evaluation_context,
Decoder
);
none ->
case gleam@dict:get(
erlang:element(3, Found_flag),
erlang:element(2, Found_flag)
) of
{ok, Val} ->
case begin
_pipe = gleam@dynamic:from(Val),
Decoder(_pipe)
end of
{ok, Resolved_val} ->
{resolution_success, Resolved_val, static};
{error, _} ->
{resolution_error,
Default_value,
error,
type_mismatch,
<<"type mismatch"/utf8>>}
end;
{error, _} ->
{resolution_error,
Default_value,
error,
general,
<<<<"default variant "/utf8,
(erlang:element(2, Found_flag))/binary>>/binary,
" not found in variants"/utf8>>}
end
end end
).
-spec resolve_evaluation(
binary(),
gleam@dict:dict(binary(), flag()),
GBP,
openfeature@evaluation_context:evaluation_context(),
fun((gleam@dynamic:dynamic_()) -> {ok, GBP} |
{error, list(gleam@dynamic:decode_error())})
) -> openfeature@evaluation:resolution_details(GBP).
resolve_evaluation(Flag, Flags, Default_value, Evaluation_context, Decoder) ->
_pipe = inner_resolve(
Flag,
Flags,
Default_value,
Evaluation_context,
Decoder
),
gleam@result:unwrap(
_pipe,
{resolution_error,
Default_value,
error,
general,
<<<<"flag "/utf8, Flag/binary>>/binary, " not found"/utf8>>}
).
-spec resolve_dynamic_evaluation(
binary(),
gleam@dict:dict(binary(), any()),
gleam@dynamic:dynamic_()
) -> openfeature@evaluation:resolution_details(gleam@dynamic:dynamic_()).
resolve_dynamic_evaluation(Flag, Flags, Default_value) ->
case gleam@dict:get(Flags, Flag) of
{ok, Val} ->
{resolution_success, gleam@dynamic:from(Val), static};
{error, _} ->
{resolution_error,
Default_value,
error,
flag_not_found,
<<"not found"/utf8>>}
end.
-spec provider(gleam@dict:dict(binary(), flag())) -> openfeature@provider:feature_provider().
provider(From) ->
{feature_provider,
fun(Context) ->
gleam@io:debug(Context),
{ok, nil}
end,
fun() -> nil end,
fun get_metadata/0,
fun(Flag, Default_value, Evaluation_context) ->
resolve_evaluation(
Flag,
From,
Default_value,
Evaluation_context,
fun gleam@dynamic:bool/1
)
end,
fun(Flag@1, Default_value@1, Evaluation_context@1) ->
resolve_evaluation(
Flag@1,
From,
Default_value@1,
Evaluation_context@1,
fun gleam@dynamic:string/1
)
end,
fun(Flag@2, Default_value@2, Evaluation_context@2) ->
resolve_evaluation(
Flag@2,
From,
Default_value@2,
Evaluation_context@2,
fun gleam@dynamic:int/1
)
end,
fun(Flag@3, Default_value@3, Evaluation_context@3) ->
resolve_evaluation(
Flag@3,
From,
Default_value@3,
Evaluation_context@3,
fun gleam@dynamic:float/1
)
end,
fun(Flag@4, Default_value@4, _) ->
resolve_dynamic_evaluation(Flag@4, From, Default_value@4)
end}.