Packages

Gleam library for working with arbitrary JSON structures

Current section

Files

Jump to
jelly src jelly.erl
Raw

src/jelly.erl

-module(jelly).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([parse_dict/1, dynamic_to_json/1, parse/1, path/2]).
-export_type([decode_error/0, json_type/0, path_key/0]).
-type decode_error() :: unexpected_end_of_input |
{unexpected_byte, binary()} |
{unexpected_sequence, binary()}.
-type json_type() :: unknown |
null |
{bool, boolean()} |
{float, float()} |
{int, integer()} |
{string, binary()} |
{array, list(json_type())} |
{object, gleam@dict:dict(binary(), json_type())}.
-type path_key() :: {indexer, integer()} | {property, binary()}.
-file("/home/runner/work/jelly/jelly/src/jelly.gleam", 50).
-spec parse_dict(binary()) -> {ok,
gleam@dict:dict(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_())} |
{error, decode_error()}.
parse_dict(Json) ->
jelly_json:decode(Json).
-file("/home/runner/work/jelly/jelly/src/jelly.gleam", 56).
-spec dynamic_to_json(gleam@dynamic:dynamic_()) -> json_type().
dynamic_to_json(Value) ->
case begin
_pipe = Value,
(gleam@dynamic:dict(
fun gleam@dynamic:string/1,
fun gleam@dynamic:dynamic/1
))(_pipe)
end of
{ok, Dict_value} ->
_pipe@1 = Dict_value,
_pipe@2 = gleam@dict:map_values(
_pipe@1,
fun(_, Value@1) -> dynamic_to_json(Value@1) end
),
{object, _pipe@2};
{error, _} ->
case gleam@dynamic:string(Value) of
{ok, String_value} ->
{string, String_value};
{error, _} ->
case gleam@dynamic:bool(Value) of
{ok, Bool_value} ->
{bool, Bool_value};
{error, _} ->
case gleam@dynamic:float(Value) of
{ok, Float_value} ->
{float, Float_value};
{error, _} ->
case gleam@dynamic:int(Value) of
{ok, Int_value} ->
{int, Int_value};
{error, _} ->
case gleam@dynamic:shallow_list(
Value
) of
{ok, List_value} ->
_pipe@3 = List_value,
_pipe@4 = gleam@list:map(
_pipe@3,
fun dynamic_to_json/1
),
{array, _pipe@4};
{error, _} ->
case gleam_erlang_ffi:atom_from_dynamic(
Value
) of
{ok, A} ->
case erlang:atom_to_binary(
A
) of
<<"null"/utf8>> ->
null;
_ ->
unknown
end;
{error, _} ->
unknown
end
end
end
end
end
end
end.
-file("/home/runner/work/jelly/jelly/src/jelly.gleam", 36).
-spec parse(binary()) -> {ok, json_type()} | {error, decode_error()}.
parse(Json) ->
case jelly_json:decode(Json) of
{ok, Dict} ->
_pipe = Dict,
_pipe@1 = gleam_stdlib:identity(_pipe),
_pipe@2 = dynamic_to_json(_pipe@1),
{ok, _pipe@2};
{error, Error} ->
{error, Error}
end.
-file("/home/runner/work/jelly/jelly/src/jelly.gleam", 145).
-spec parse_indexer(binary()) -> {ok, list(path_key())} | {error, nil}.
parse_indexer(Val) ->
_assert_subject = gleam@regexp:from_string(
<<"^(?<property>[A-z]+)\\[(?<index>[0-9]+)\\]$"/utf8>>
),
{ok, Regex} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"jelly"/utf8>>,
function => <<"parse_indexer"/utf8>>,
line => 146})
end,
case gleam@regexp:scan(Regex, Val) of
[Match] ->
case erlang:element(3, Match) of
[{some, Property}, {some, Index}] ->
case gleam_stdlib:parse_int(Index) of
{ok, Index@1} ->
{ok, [{property, Property}, {indexer, Index@1}]};
{error, _} ->
{error, nil}
end;
_ ->
{error, nil}
end;
_ ->
{error, nil}
end.
-file("/home/runner/work/jelly/jelly/src/jelly.gleam", 134).
-spec parse_path_keys(binary()) -> list(path_key()).
parse_path_keys(Path) ->
_pipe = gleam@string:split(Path, <<"."/utf8>>),
_pipe@1 = gleam@list:map(
_pipe,
fun(Path_key) -> case parse_indexer(Path_key) of
{ok, Keys} ->
Keys;
{error, _} ->
[{property, Path_key}]
end end
),
gleam@list:flatten(_pipe@1).
-file("/home/runner/work/jelly/jelly/src/jelly.gleam", 168).
-spec list_get(list(GGF), integer()) -> {ok, GGF} | {error, nil}.
list_get(List, Index) ->
case Index of
0 ->
gleam@list:first(List);
_ ->
case gleam@list:rest(List) of
{ok, Rest} ->
list_get(Rest, Index - 1);
{error, _} ->
{error, nil}
end
end.
-file("/home/runner/work/jelly/jelly/src/jelly.gleam", 109).
-spec do_path(json_type(), list(path_key())) -> {ok, json_type()} | {error, nil}.
do_path(Json, Keys) ->
case Keys of
[] ->
{ok, Json};
[Key | Remaining_keys] ->
case {Json, Key} of
{{object, X}, {property, Property}} ->
case gleam_stdlib:map_get(X, Property) of
{ok, Value} ->
do_path(Value, Remaining_keys);
{error, _} ->
{error, nil}
end;
{{array, X@1}, {indexer, Index}} ->
_pipe = list_get(X@1, Index),
gleam@result:'try'(
_pipe,
fun(_capture) -> do_path(_capture, Remaining_keys) end
);
{_, _} ->
{error, nil}
end
end.
-file("/home/runner/work/jelly/jelly/src/jelly.gleam", 104).
-spec path(json_type(), binary()) -> {ok, json_type()} | {error, nil}.
path(Json, Path) ->
Keys = parse_path_keys(Path),
do_path(Json, Keys).