Current section
Files
Jump to
Current section
Files
src/jsonlogic@internal@decoding.erl
-module(jsonlogic@internal@decoding).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/jsonlogic/internal/decoding.gleam").
-export([decode_operator/1, dynamic_to_bool/1, dynamic_to_string/1, dynamic_to_array/1, dynamic_to_int/1, dynamic_to_float/1, coerce_types/2, decode_data/3, decode_rule/1, decode_rule_string/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.
?MODULEDOC(false).
-file("src/jsonlogic/internal/decoding.gleam", 94).
?DOC(false).
-spec decode_operator(binary()) -> {ok, jsonlogic@internal@operator:operator()} |
{error, jsonlogic@error:evaluation_error()}.
decode_operator(Operator) ->
case Operator of
<<"var"/utf8>> ->
{ok, variable};
<<"missing"/utf8>> ->
{ok, missing};
<<"missing_some"/utf8>> ->
{ok, missing_some};
<<"=="/utf8>> ->
{ok, abstract_equals};
<<"!="/utf8>> ->
{ok, abstract_not_equals};
<<"==="/utf8>> ->
{ok, strict_equals};
<<"!=="/utf8>> ->
{ok, strict_not_equals};
<<">"/utf8>> ->
{ok, greater_than};
<<"<"/utf8>> ->
{ok, less_than};
<<">="/utf8>> ->
{ok, greater_than_or_equal};
<<"<="/utf8>> ->
{ok, less_than_or_equal};
<<"!"/utf8>> ->
{ok, negate};
<<"!!"/utf8>> ->
{ok, double_negate};
<<"or"/utf8>> ->
{ok, 'or'};
<<"and"/utf8>> ->
{ok, 'and'};
<<"?:"/utf8>> ->
{ok, conditional};
<<"in"/utf8>> ->
{ok, in};
<<"cat"/utf8>> ->
{ok, concatenate};
<<"%"/utf8>> ->
{ok, modulo};
<<"max"/utf8>> ->
{ok, max};
<<"min"/utf8>> ->
{ok, min};
<<"+"/utf8>> ->
{ok, plus};
<<"*"/utf8>> ->
{ok, multiply};
<<"-"/utf8>> ->
{ok, minus};
<<"/"/utf8>> ->
{ok, divide};
<<"substr"/utf8>> ->
{ok, substring};
<<"merge"/utf8>> ->
{ok, merge};
<<"if"/utf8>> ->
{ok, 'if'};
<<"filter"/utf8>> ->
{ok, filter};
<<"map"/utf8>> ->
{ok, map};
<<"reduce"/utf8>> ->
{ok, reduce};
<<"all"/utf8>> ->
{ok, all};
<<"none"/utf8>> ->
{ok, none};
<<"some"/utf8>> ->
{ok, some};
_ ->
{error, {unknown_operator_error, Operator}}
end.
-file("src/jsonlogic/internal/decoding.gleam", 204).
?DOC(false).
-spec dynamic_to_bool(gleam@dynamic:dynamic_()) -> {ok,
{boolean(), gleam@dynamic:dynamic_()}} |
{error, jsonlogic@error:evaluation_error()}.
dynamic_to_bool(Input) ->
case gleam_stdlib:classify_dynamic(Input) of
<<"Bool"/utf8>> ->
Decoded@1 = case gleam@dynamic@decode:run(
Input,
{decoder, fun gleam@dynamic@decode:decode_bool/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/decoding"/utf8>>,
function => <<"dynamic_to_bool"/utf8>>,
line => 209,
value => _assert_fail,
start => 6029,
'end' => 6084,
pattern_start => 6040,
pattern_end => 6051})
end,
{ok, {Decoded@1, Input}};
<<"Int"/utf8>> ->
Decoded@3 = case gleam@dynamic@decode:run(
Input,
{decoder, fun gleam@dynamic@decode:decode_int/1}
) of
{ok, Decoded@2} -> Decoded@2;
_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/decoding"/utf8>>,
function => <<"dynamic_to_bool"/utf8>>,
line => 213,
value => _assert_fail@1,
start => 6140,
'end' => 6194,
pattern_start => 6151,
pattern_end => 6162})
end,
{ok, {Decoded@3 /= 0, Input}};
<<"Float"/utf8>> ->
Decoded@5 = case gleam@dynamic@decode:run(
Input,
{decoder, fun gleam@dynamic@decode:decode_float/1}
) of
{ok, Decoded@4} -> Decoded@4;
_assert_fail@2 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"dynamic_to_bool"/utf8>>,
line => 217,
value => _assert_fail@2,
start => 6257,
'end' => 6313,
pattern_start => 6268,
pattern_end => 6279})
end,
{ok, {Decoded@5 /= +0.0, Input}};
<<"String"/utf8>> ->
Decoded@7 = case gleam@dynamic@decode:run(
Input,
{decoder, fun gleam@dynamic@decode:decode_string/1}
) of
{ok, Decoded@6} -> Decoded@6;
_assert_fail@3 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"dynamic_to_bool"/utf8>>,
line => 221,
value => _assert_fail@3,
start => 6379,
'end' => 6436,
pattern_start => 6390,
pattern_end => 6401})
end,
{ok, {Decoded@7 /= <<""/utf8>>, Input}};
<<"List"/utf8>> ->
Decoded@9 = case gleam@dynamic@decode:run(
Input,
gleam@dynamic@decode:list(
{decoder, fun gleam@dynamic@decode:decode_dynamic/1}
)
) of
{ok, Decoded@8} -> Decoded@8;
_assert_fail@4 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"dynamic_to_bool"/utf8>>,
line => 225,
value => _assert_fail@4,
start => 6499,
'end' => 6570,
pattern_start => 6510,
pattern_end => 6521})
end,
{ok, {Decoded@9 /= [], Input}};
<<"Nil"/utf8>> ->
{ok, {false, Input}};
T ->
erlang:error(#{gleam_error => panic,
message => (<<"Cannot convert type: "/utf8, T/binary>>),
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"dynamic_to_bool"/utf8>>,
line => 230})
end.
-file("src/jsonlogic/internal/decoding.gleam", 234).
?DOC(false).
-spec dynamic_to_string(gleam@dynamic:dynamic_()) -> {ok, binary()} |
{error, jsonlogic@error:evaluation_error()}.
dynamic_to_string(Input) ->
_pipe@4 = gleam@dynamic@decode:run(
Input,
gleam@dynamic@decode:one_of(
{decoder, fun gleam@dynamic@decode:decode_string/1},
[begin
_pipe = {decoder, fun gleam@dynamic@decode:decode_float/1},
gleam@dynamic@decode:map(
_pipe,
fun gleam_stdlib:float_to_string/1
)
end,
begin
_pipe@1 = {decoder, fun gleam@dynamic@decode:decode_int/1},
gleam@dynamic@decode:map(
_pipe@1,
fun erlang:integer_to_binary/1
)
end,
begin
_pipe@2 = {decoder, fun gleam@dynamic@decode:decode_bool/1},
_pipe@3 = gleam@dynamic@decode:map(
_pipe@2,
fun gleam@bool:to_string/1
),
gleam@dynamic@decode:map(_pipe@3, fun string:lowercase/1)
end]
)
),
gleam@result:map_error(_pipe@4, fun(Field@0) -> {decode_error, Field@0} end).
-file("src/jsonlogic/internal/decoding.gleam", 248).
?DOC(false).
-spec dynamic_to_array(gleam@dynamic:dynamic_()) -> {ok,
list(gleam@dynamic:dynamic_())} |
{error, jsonlogic@error:evaluation_error()}.
dynamic_to_array(Input) ->
_pipe@2 = gleam@dynamic@decode:run(
Input,
gleam@dynamic@decode:one_of(
begin
_pipe = gleam@dynamic@decode:optional(
gleam@dynamic@decode:list(
{decoder, fun gleam@dynamic@decode:decode_dynamic/1}
)
),
gleam@dynamic@decode:map(_pipe, fun(Value) -> case Value of
none ->
[];
{some, Value@1} ->
Value@1
end end)
end,
[begin
_pipe@1 = {decoder,
fun gleam@dynamic@decode:decode_dynamic/1},
gleam@dynamic@decode:map(_pipe@1, fun gleam@list:wrap/1)
end]
)
),
gleam@result:map_error(_pipe@2, fun(Field@0) -> {decode_error, Field@0} end).
-file("src/jsonlogic/internal/decoding.gleam", 297).
?DOC(false).
-spec bool_to_float(boolean()) -> float().
bool_to_float(Value) ->
case Value of
true ->
1.0;
false ->
+0.0
end.
-file("src/jsonlogic/internal/decoding.gleam", 136).
?DOC(false).
-spec dynamic_to_int(gleam@dynamic:dynamic_()) -> {ok, integer()} |
{error, jsonlogic@error:evaluation_error()}.
dynamic_to_int(Input) ->
case gleam_stdlib:classify_dynamic(Input) of
<<"String"/utf8>> ->
Decoded@1 = case gleam@dynamic@decode:run(
Input,
{decoder, fun gleam@dynamic@decode:decode_string/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/decoding"/utf8>>,
function => <<"dynamic_to_int"/utf8>>,
line => 141,
value => _assert_fail,
start => 4193,
'end' => 4250,
pattern_start => 4204,
pattern_end => 4215})
end,
case Decoded@1 of
<<""/utf8>> ->
{ok, 0};
_ ->
_pipe = gleam_stdlib:parse_float(Decoded@1),
_pipe@1 = gleam@result:map(_pipe, fun erlang:trunc/1),
_pipe@2 = gleam@result:try_recover(
_pipe@1,
fun(_) -> gleam_stdlib:parse_int(Decoded@1) end
),
gleam@result:map_error(_pipe@2, fun(_) -> na_n_error end)
end;
<<"Int"/utf8>> ->
Decoded@3 = case gleam@dynamic@decode:run(
Input,
{decoder, fun gleam@dynamic@decode:decode_int/1}
) of
{ok, Decoded@2} -> Decoded@2;
_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/decoding"/utf8>>,
function => <<"dynamic_to_int"/utf8>>,
line => 152,
value => _assert_fail@1,
start => 4529,
'end' => 4583,
pattern_start => 4540,
pattern_end => 4551})
end,
{ok, Decoded@3};
<<"Float"/utf8>> ->
Decoded@5 = case gleam@dynamic@decode:run(
Input,
{decoder, fun gleam@dynamic@decode:decode_float/1}
) of
{ok, Decoded@4} -> Decoded@4;
_assert_fail@2 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"dynamic_to_int"/utf8>>,
line => 156,
value => _assert_fail@2,
start => 4631,
'end' => 4687,
pattern_start => 4642,
pattern_end => 4653})
end,
{ok, erlang:trunc(Decoded@5)};
<<"Bool"/utf8>> ->
Decoded@7 = case gleam@dynamic@decode:run(
Input,
{decoder, fun gleam@dynamic@decode:decode_bool/1}
) of
{ok, Decoded@6} -> Decoded@6;
_assert_fail@3 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"dynamic_to_int"/utf8>>,
line => 160,
value => _assert_fail@3,
start => 4750,
'end' => 4805,
pattern_start => 4761,
pattern_end => 4772})
end,
_pipe@3 = bool_to_float(Decoded@7),
_pipe@4 = erlang:trunc(_pipe@3),
{ok, _pipe@4};
T ->
erlang:error(#{gleam_error => panic,
message => (<<"Cannot convert type: "/utf8, T/binary>>),
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"dynamic_to_int"/utf8>>,
line => 165})
end.
-file("src/jsonlogic/internal/decoding.gleam", 169).
?DOC(false).
-spec dynamic_to_float(gleam@dynamic:dynamic_()) -> {ok, float()} |
{error, jsonlogic@error:evaluation_error()}.
dynamic_to_float(Input) ->
case gleam_stdlib:classify_dynamic(Input) of
<<"String"/utf8>> ->
Decoded@1 = case gleam@dynamic@decode:run(
Input,
{decoder, fun gleam@dynamic@decode:decode_string/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/decoding"/utf8>>,
function => <<"dynamic_to_float"/utf8>>,
line => 174,
value => _assert_fail,
start => 5087,
'end' => 5144,
pattern_start => 5098,
pattern_end => 5109})
end,
case Decoded@1 of
<<""/utf8>> ->
{ok, +0.0};
_ ->
_pipe = gleam_stdlib:parse_int(Decoded@1),
_pipe@1 = gleam@result:map(_pipe, fun erlang:float/1),
_pipe@2 = gleam@result:try_recover(
_pipe@1,
fun(_) -> gleam_stdlib:parse_float(Decoded@1) end
),
gleam@result:map_error(_pipe@2, fun(_) -> na_n_error end)
end;
<<"Float"/utf8>> ->
Decoded@3 = case gleam@dynamic@decode:run(
Input,
{decoder, fun gleam@dynamic@decode:decode_float/1}
) of
{ok, Decoded@2} -> Decoded@2;
_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/decoding"/utf8>>,
function => <<"dynamic_to_float"/utf8>>,
line => 185,
value => _assert_fail@1,
start => 5425,
'end' => 5481,
pattern_start => 5436,
pattern_end => 5447})
end,
{ok, Decoded@3};
<<"Int"/utf8>> ->
Decoded@5 = case gleam@dynamic@decode:run(
Input,
{decoder, fun gleam@dynamic@decode:decode_int/1}
) of
{ok, Decoded@4} -> Decoded@4;
_assert_fail@2 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"dynamic_to_float"/utf8>>,
line => 189,
value => _assert_fail@2,
start => 5527,
'end' => 5581,
pattern_start => 5538,
pattern_end => 5549})
end,
{ok, erlang:float(Decoded@5)};
<<"Bool"/utf8>> ->
Decoded@7 = case gleam@dynamic@decode:run(
Input,
{decoder, fun gleam@dynamic@decode:decode_bool/1}
) of
{ok, Decoded@6} -> Decoded@6;
_assert_fail@3 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"dynamic_to_float"/utf8>>,
line => 193,
value => _assert_fail@3,
start => 5642,
'end' => 5697,
pattern_start => 5653,
pattern_end => 5664})
end,
_pipe@3 = bool_to_float(Decoded@7),
{ok, _pipe@3};
<<"Nil"/utf8>> ->
{ok, +0.0};
<<"List"/utf8>> ->
{error, na_n_error};
T ->
erlang:error(#{gleam_error => panic,
message => (<<"Cannot convert type: "/utf8, T/binary>>),
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"dynamic_to_float"/utf8>>,
line => 200})
end.
-file("src/jsonlogic/internal/decoding.gleam", 269).
?DOC(false).
-spec coerce_types(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok,
{gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()}} |
{error, jsonlogic@error:evaluation_error()}.
coerce_types(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/decoding"/utf8>>,
function => <<"coerce_types"/utf8>>,
line => 277,
value => _assert_fail,
start => 7857,
'end' => 7910,
pattern_start => 7868,
pattern_end => 7877})
end,
_pipe = bool_to_float(First@2),
_pipe@1 = gleam_stdlib:identity(_pipe),
coerce_types(_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/decoding"/utf8>>,
function => <<"coerce_types"/utf8>>,
line => 283,
value => _assert_fail@1,
start => 8022,
'end' => 8077,
pattern_start => 8033,
pattern_end => 8043})
end,
_pipe@2 = bool_to_float(Second@2),
_pipe@3 = gleam_stdlib:identity(_pipe@2),
coerce_types(_pipe@3, First);
{<<"Int"/utf8>>, <<"String"/utf8>>} ->
gleam@result:'try'(
dynamic_to_float(First),
fun(First@3) ->
gleam@result:map(
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'(
dynamic_to_float(First),
fun(First@3) ->
gleam@result:map(
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'(
dynamic_to_float(First),
fun(First@3) ->
gleam@result:map(
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'(
dynamic_to_float(First),
fun(First@3) ->
gleam@result:map(
dynamic_to_float(Second),
fun(Second@3) ->
{gleam_stdlib:identity(First@3),
gleam_stdlib:identity(Second@3)}
end
)
end
);
{A, B} ->
erlang:error(#{gleam_error => panic,
message => (<<<<<<"Unsupported types: "/utf8, A/binary>>/binary,
" and "/utf8>>/binary,
B/binary>>),
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"coerce_types"/utf8>>,
line => 293})
end.
-file("src/jsonlogic/internal/decoding.gleam", 319).
?DOC(false).
-spec do_decode_data(
gleam@dynamic:dynamic_(),
gleam@dynamic:dynamic_(),
gleam@option:option(gleam@dynamic:dynamic_())
) -> {ok, gleam@dynamic:dynamic_()} |
{error, jsonlogic@error:evaluation_error()}.
do_decode_data(Key, Data, Default) ->
gleam@result:map(
dynamic_to_string(Key),
fun(Key@1) ->
Keys = gleam@string:split(Key@1, <<"."/utf8>>),
_pipe = gleam@dynamic@decode:run(
Data,
gleam@dynamic@decode:at(
Keys,
{decoder, fun gleam@dynamic@decode:decode_dynamic/1}
)
),
_pipe@3 = gleam@result:try_recover(
_pipe,
fun(E) ->
Indices = begin
_pipe@1 = gleam@list:try_map(
Keys,
fun gleam_stdlib:parse_int/1
),
gleam@result:replace_error(_pipe@1, E)
end,
gleam@result:'try'(
Indices,
fun(Indices@1) ->
_pipe@2 = gleam@dynamic@decode:run(
Data,
gleam@dynamic@decode:at(
Indices@1,
{decoder,
fun gleam@dynamic@decode:decode_dynamic/1}
)
),
gleam@result:replace_error(_pipe@2, E)
end
)
end
),
gleam@result:lazy_unwrap(_pipe@3, fun() -> case Default of
{some, Default@1} ->
Default@1;
none ->
gleam@dynamic:nil()
end end)
end
).
-file("src/jsonlogic/internal/decoding.gleam", 304).
?DOC(false).
-spec decode_data(
gleam@dynamic:dynamic_(),
gleam@dynamic:dynamic_(),
gleam@option:option(gleam@dynamic:dynamic_())
) -> {ok, gleam@dynamic:dynamic_()} |
{error, jsonlogic@error:evaluation_error()}.
decode_data(Key, Data, Default) ->
case {Key =:= gleam@dynamic:nil(),
Key =:= gleam_stdlib:identity(<<""/utf8>>),
Key =:= gleam_stdlib:identity([])} of
{true, _, _} ->
{ok, Data};
{_, true, _} ->
{ok, Data};
{_, _, true} ->
{ok, Data};
{_, _, _} ->
do_decode_data(Key, Data, Default)
end.
-file("src/jsonlogic/internal/decoding.gleam", 64).
?DOC(false).
-spec decode_literal(gleam@dynamic:dynamic_()) -> {ok,
jsonlogic@internal@rule:json_literal()} |
{error, jsonlogic@error:evaluation_error()}.
decode_literal(Literal) ->
case gleam_stdlib:classify_dynamic(Literal) of
<<"Bool"/utf8>> ->
Decoded@1 = case gleam@dynamic@decode:run(
Literal,
{decoder, fun gleam@dynamic@decode:decode_bool/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/decoding"/utf8>>,
function => <<"decode_literal"/utf8>>,
line => 69,
value => _assert_fail,
start => 1938,
'end' => 1995,
pattern_start => 1949,
pattern_end => 1960})
end,
{ok, {bool_literal, Decoded@1}};
<<"String"/utf8>> ->
Decoded@3 = case gleam@dynamic@decode:run(
Literal,
{decoder, fun gleam@dynamic@decode:decode_string/1}
) of
{ok, Decoded@2} -> Decoded@2;
_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/decoding"/utf8>>,
function => <<"decode_literal"/utf8>>,
line => 73,
value => _assert_fail@1,
start => 2062,
'end' => 2121,
pattern_start => 2073,
pattern_end => 2084})
end,
{ok, {string_literal, Decoded@3}};
<<"Int"/utf8>> ->
Decoded@5 = case gleam@dynamic@decode:run(
Literal,
{decoder, fun gleam@dynamic@decode:decode_int/1}
) of
{ok, Decoded@4} -> Decoded@4;
_assert_fail@2 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"decode_literal"/utf8>>,
line => 77,
value => _assert_fail@2,
start => 2187,
'end' => 2243,
pattern_start => 2198,
pattern_end => 2209})
end,
{ok, {int_literal, Decoded@5}};
<<"Float"/utf8>> ->
Decoded@7 = case gleam@dynamic@decode:run(
Literal,
{decoder, fun gleam@dynamic@decode:decode_float/1}
) of
{ok, Decoded@6} -> Decoded@6;
_assert_fail@3 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"decode_literal"/utf8>>,
line => 81,
value => _assert_fail@3,
start => 2308,
'end' => 2366,
pattern_start => 2319,
pattern_end => 2330})
end,
{ok, {float_literal, Decoded@7}};
<<"Nil"/utf8>> ->
{ok, nil_literal};
<<"List"/utf8>> ->
Decoded@9 = case gleam@dynamic@decode:run(
Literal,
gleam@dynamic@decode:list(
{decoder, fun gleam@dynamic@decode:decode_dynamic/1}
)
) of
{ok, Decoded@8} -> Decoded@8;
_assert_fail@4 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"decode_literal"/utf8>>,
line => 86,
value => _assert_fail@4,
start => 2465,
'end' => 2538,
pattern_start => 2476,
pattern_end => 2487})
end,
_pipe = gleam@list:try_map(Decoded@9, fun decode_rule/1),
gleam@result:map(
_pipe,
fun(Field@0) -> {array_literal, Field@0} end
);
T ->
erlang:error(#{gleam_error => panic,
message => (<<"Unsupported literal type: "/utf8, T/binary>>),
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"decode_literal"/utf8>>,
line => 90})
end.
-file("src/jsonlogic/internal/decoding.gleam", 24).
?DOC(false).
-spec decode_rule(gleam@dynamic:dynamic_()) -> {ok,
jsonlogic@internal@rule:rule()} |
{error, jsonlogic@error:evaluation_error()}.
decode_rule(Rule) ->
case gleam_stdlib:classify_dynamic(Rule) of
<<"Bool"/utf8>> ->
_pipe = decode_literal(Rule),
gleam@result:map(_pipe, fun(Field@0) -> {literal, Field@0} end);
<<"String"/utf8>> ->
_pipe = decode_literal(Rule),
gleam@result:map(_pipe, fun(Field@0) -> {literal, Field@0} end);
<<"Int"/utf8>> ->
_pipe = decode_literal(Rule),
gleam@result:map(_pipe, fun(Field@0) -> {literal, Field@0} end);
<<"Float"/utf8>> ->
_pipe = decode_literal(Rule),
gleam@result:map(_pipe, fun(Field@0) -> {literal, Field@0} end);
<<"Nil"/utf8>> ->
_pipe = decode_literal(Rule),
gleam@result:map(_pipe, fun(Field@0) -> {literal, Field@0} end);
<<"List"/utf8>> ->
_pipe = decode_literal(Rule),
gleam@result:map(_pipe, fun(Field@0) -> {literal, Field@0} end);
<<"Dict"/utf8>> ->
Decoding_result = begin
_pipe@1 = gleam@dynamic@decode:run(
Rule,
gleam@dynamic@decode:dict(
{decoder, fun gleam@dynamic@decode:decode_string/1},
{decoder, fun gleam@dynamic@decode:decode_dynamic/1}
)
),
_pipe@2 = gleam@result:map(_pipe@1, fun maps:to_list/1),
gleam@result:map_error(
_pipe@2,
fun(Field@0) -> {decode_error, Field@0} end
)
end,
gleam@result:'try'(
Decoding_result,
fun(Decoded_entries) ->
Raw_rule = begin
_pipe@3 = gleam@list:first(Decoded_entries),
_pipe@4 = gleam@result:map_error(
_pipe@3,
fun(_) ->
{invalid_rule_error,
<<"no operator in rule"/utf8>>}
end
),
gleam@result:'try'(
_pipe@4,
fun(Rule@1) ->
_pipe@5 = decode_rule(erlang:element(2, Rule@1)),
gleam@result:map(
_pipe@5,
fun(Values) -> case Values of
{literal,
{array_literal, Inner_values}} ->
{erlang:element(1, Rule@1),
Inner_values};
Other_rule ->
{erlang:element(1, Rule@1),
[Other_rule]}
end end
)
end
)
end,
gleam@result:'try'(
Raw_rule,
fun(_use0) ->
{Operator, Values@1} = _use0,
gleam@result:map(
decode_operator(Operator),
fun(Typed_operator) ->
{operation, Typed_operator, Values@1}
end
)
end
)
end
);
Unrecognized ->
erlang:error(#{gleam_error => panic,
message => (<<"Unrecognized rule: "/utf8,
Unrecognized/binary>>),
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/decoding"/utf8>>,
function => <<"decode_rule"/utf8>>,
line => 60})
end.
-file("src/jsonlogic/internal/decoding.gleam", 16).
?DOC(false).
-spec decode_rule_string(binary()) -> {ok, jsonlogic@internal@rule:rule()} |
{error, jsonlogic@error:evaluation_error()}.
decode_rule_string(Rule) ->
_pipe = gleam@json:parse(
Rule,
{decoder, fun gleam@dynamic@decode:decode_dynamic/1}
),
_pipe@1 = gleam@result:map_error(
_pipe,
fun(Field@0) -> {json_decode_error, Field@0} end
),
gleam@result:'try'(_pipe@1, fun decode_rule/1).