Current section

Files

Jump to
jsonlogic src jsonlogic@internal@util.erl
Raw

src/jsonlogic@internal@util.erl

-module(jsonlogic@internal@util).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/jsonlogic/internal/util.gleam").
-export([comparison_reduce/2, chain_reduce/2, chain_reduce_result/2, modulo/2, divide/2, float_to_dynamic/1, flatten/1, normalize_dynamic/1, loose_equals/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(false).
-file("src/jsonlogic/internal/util.gleam", 10).
?DOC(false).
-spec comparison_reduce(list(float()), fun((float(), float()) -> boolean())) -> {ok,
boolean()} |
{error, jsonlogic@error:evaluation_error()}.
comparison_reduce(Values, Operator) ->
case Values of
[First | Rest] ->
_pipe = gleam@list:fold_until(
Rest,
{First, true},
fun(Previous, Current) ->
case Operator(erlang:element(1, Previous), Current) of
true ->
{continue, {Current, true}};
false ->
{stop, {Current, false}}
end
end
),
(fun(Res) -> {ok, erlang:element(2, Res)} end)(_pipe);
_ ->
{error, invalid_arguments_error}
end.
-file("src/jsonlogic/internal/util.gleam", 28).
?DOC(false).
-spec chain_reduce(list(float()), fun((float(), float()) -> float())) -> {ok,
float()} |
{error, jsonlogic@error:evaluation_error()}.
chain_reduce(Values, Operator) ->
case Values of
[First | Rest] ->
{ok, gleam@list:fold(Rest, First, Operator)};
_ ->
{error, invalid_arguments_error}
end.
-file("src/jsonlogic/internal/util.gleam", 38).
?DOC(false).
-spec chain_reduce_result(
list(float()),
fun((float(), float()) -> {ok, float()} |
{error, jsonlogic@error:evaluation_error()})
) -> {ok, float()} | {error, jsonlogic@error:evaluation_error()}.
chain_reduce_result(Values, Operator) ->
case Values of
[First | Rest] ->
gleam@list:fold_until(
Rest,
{ok, First},
fun(Previous, Current) ->
Previous@2 = case Previous of
{ok, Previous@1} -> Previous@1;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/util"/utf8>>,
function => <<"chain_reduce_result"/utf8>>,
line => 45,
value => _assert_fail,
start => 1193,
'end' => 1227,
pattern_start => 1204,
pattern_end => 1216})
end,
case Operator(Previous@2, Current) of
{ok, Result} ->
{continue, {ok, Result}};
{error, Error} ->
{stop, {error, Error}}
end
end
);
_ ->
{error, invalid_arguments_error}
end.
-file("src/jsonlogic/internal/util.gleam", 55).
?DOC(false).
-spec modulo(float(), float()) -> {ok, float()} |
{error, jsonlogic@error:evaluation_error()}.
modulo(A, B) ->
_pipe = gleam@float:modulo(
gleam@float:absolute_value(A),
gleam@float:absolute_value(B)
),
_pipe@1 = gleam@result:map(_pipe, fun(Value) -> case {A < +0.0, B < +0.0} of
{true, false} ->
gleam@float:negate(Value);
{_, _} ->
Value
end end),
gleam@result:try_recover(_pipe@1, fun(_) -> {error, na_n_error} end).
-file("src/jsonlogic/internal/util.gleam", 66).
?DOC(false).
-spec divide(float(), float()) -> {ok, float()} |
{error, jsonlogic@error:evaluation_error()}.
divide(A, B) ->
_pipe = gleam@float:divide(A, B),
gleam@result:try_recover(_pipe, fun(_) -> {error, na_n_error} end).
-file("src/jsonlogic/internal/util.gleam", 81).
?DOC(false).
-spec is_zero(float()) -> boolean().
is_zero(Value) ->
case {Value =:= +0.0, Value =:= -0.0} of
{true, _} ->
true;
{_, true} ->
true;
{_, _} ->
false
end.
-file("src/jsonlogic/internal/util.gleam", 71).
?DOC(false).
-spec float_to_dynamic(float()) -> gleam@dynamic:dynamic_().
float_to_dynamic(Value) ->
Truncated = erlang:trunc(Value),
Is_zero = is_zero(Value),
case Value =:= erlang:float(Truncated) of
true ->
gleam_stdlib:identity(Truncated);
false when Is_zero ->
gleam_stdlib:identity(0);
false ->
gleam_stdlib:identity(Value)
end.
-file("src/jsonlogic/internal/util.gleam", 88).
?DOC(false).
-spec flatten(list(gleam@dynamic:dynamic_())) -> list(gleam@dynamic:dynamic_()).
flatten(Values) ->
gleam@list:flat_map(
Values,
fun(Value) ->
Decoded@1 = case gleam@dynamic@decode:run(
Value,
gleam@dynamic@decode:one_of(
gleam@dynamic@decode:list(
{decoder, fun gleam@dynamic@decode:decode_dynamic/1}
),
[gleam@dynamic@decode:map(
{decoder, fun gleam@dynamic@decode:decode_dynamic/1},
fun gleam@list:wrap/1
)]
)
) of
{ok, Decoded} -> Decoded;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/util"/utf8>>,
function => <<"flatten"/utf8>>,
line => 90,
value => _assert_fail,
start => 2455,
'end' => 2626,
pattern_start => 2466,
pattern_end => 2477})
end,
Decoded@1
end
).
-file("src/jsonlogic/internal/util.gleam", 100).
?DOC(false).
-spec normalize_dynamic(gleam@dynamic:dynamic_()) -> gleam@dynamic:dynamic_().
normalize_dynamic(Value) ->
case gleam_stdlib:classify_dynamic(Value) of
<<"Nil"/utf8>> ->
gleam@dynamic:nil();
_ ->
Value
end.
-file("src/jsonlogic/internal/util.gleam", 107).
?DOC(false).
-spec loose_equals(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok,
boolean()} |
{error, jsonlogic@error:evaluation_error()}.
loose_equals(First, Second) ->
case {gleam_stdlib:classify_dynamic(First),
gleam_stdlib:classify_dynamic(Second)} of
{X, Y} when X =:= Y ->
{ok, First =:= Second};
{<<"Bool"/utf8>>, _} ->
First@2 = case gleam@dynamic@decode:run(
First,
{decoder, fun gleam@dynamic@decode:decode_bool/1}
) of
{ok, First@1} -> First@1;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/util"/utf8>>,
function => <<"loose_equals"/utf8>>,
line => 115,
value => _assert_fail,
start => 3036,
'end' => 3089,
pattern_start => 3047,
pattern_end => 3056})
end,
_pipe = jsonlogic@internal@decoding:bool_to_float(First@2),
_pipe@1 = gleam_stdlib:identity(_pipe),
loose_equals(_pipe@1, Second);
{_, <<"Bool"/utf8>>} ->
Second@2 = case gleam@dynamic@decode:run(
Second,
{decoder, fun gleam@dynamic@decode:decode_bool/1}
) of
{ok, Second@1} -> Second@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/util"/utf8>>,
function => <<"loose_equals"/utf8>>,
line => 121,
value => _assert_fail@1,
start => 3210,
'end' => 3265,
pattern_start => 3221,
pattern_end => 3231})
end,
_pipe@2 = jsonlogic@internal@decoding:bool_to_float(Second@2),
_pipe@3 = gleam_stdlib:identity(_pipe@2),
loose_equals(_pipe@3, First);
{<<"Int"/utf8>>, <<"String"/utf8>>} ->
gleam@result:'try'(
jsonlogic@internal@decoding:dynamic_to_float(First),
fun(First@3) ->
gleam@result:map(
jsonlogic@internal@decoding:dynamic_to_float(Second),
fun(Second@3) ->
gleam_stdlib:identity(First@3) =:= gleam_stdlib:identity(
Second@3
)
end
)
end
);
{<<"Float"/utf8>>, <<"String"/utf8>>} ->
gleam@result:'try'(
jsonlogic@internal@decoding:dynamic_to_float(First),
fun(First@3) ->
gleam@result:map(
jsonlogic@internal@decoding:dynamic_to_float(Second),
fun(Second@3) ->
gleam_stdlib:identity(First@3) =:= gleam_stdlib:identity(
Second@3
)
end
)
end
);
{<<"String"/utf8>>, <<"Float"/utf8>>} ->
gleam@result:'try'(
jsonlogic@internal@decoding:dynamic_to_float(First),
fun(First@3) ->
gleam@result:map(
jsonlogic@internal@decoding:dynamic_to_float(Second),
fun(Second@3) ->
gleam_stdlib:identity(First@3) =:= gleam_stdlib:identity(
Second@3
)
end
)
end
);
{<<"String"/utf8>>, <<"Int"/utf8>>} ->
gleam@result:'try'(
jsonlogic@internal@decoding:dynamic_to_float(First),
fun(First@3) ->
gleam@result:map(
jsonlogic@internal@decoding:dynamic_to_float(Second),
fun(Second@3) ->
gleam_stdlib:identity(First@3) =:= gleam_stdlib:identity(
Second@3
)
end
)
end
);
{<<"Float"/utf8>>, <<"Int"/utf8>>} ->
gleam@result:'try'(
jsonlogic@internal@decoding:dynamic_to_float(First),
fun(First@3) ->
gleam@result:map(
jsonlogic@internal@decoding:dynamic_to_float(Second),
fun(Second@3) ->
gleam_stdlib:identity(First@3) =:= gleam_stdlib:identity(
Second@3
)
end
)
end
);
{<<"Int"/utf8>>, <<"Float"/utf8>>} ->
gleam@result:'try'(
jsonlogic@internal@decoding:dynamic_to_float(First),
fun(First@3) ->
gleam@result:map(
jsonlogic@internal@decoding:dynamic_to_float(Second),
fun(Second@3) ->
gleam_stdlib:identity(First@3) =:= gleam_stdlib:identity(
Second@3
)
end
)
end
);
{<<"Nil"/utf8>>, _} ->
{ok, gleam@dynamic:nil() =:= Second};
{_, <<"Nil"/utf8>>} ->
{ok, gleam@dynamic:nil() =:= First};
{_, _} ->
{ok, false}
end.