Current section
Files
Jump to
Current section
Files
src/simplejson@internal@pointer.erl
-module(simplejson@internal@pointer).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/simplejson/internal/pointer.gleam").
-export([jsonpath/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/simplejson/internal/pointer.gleam", 11).
?DOC(false).
-spec jsonpath(simplejson@jsonvalue:json_value(), binary()) -> {ok,
simplejson@jsonvalue:json_value()} |
{error, simplejson@jsonvalue:json_path_error()}.
jsonpath(Json, Jsonpath) ->
gleam@list:fold_until(
gleam@string:split(Jsonpath, <<"."/utf8>>),
{ok, Json},
fun(Current_json_result, Path_segment) ->
Current_json@1 = case Current_json_result of
{ok, Current_json} -> Current_json;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"simplejson/internal/pointer"/utf8>>,
function => <<"jsonpath"/utf8>>,
line => 19,
value => _assert_fail,
start => 410,
'end' => 459,
pattern_start => 421,
pattern_end => 437})
end,
case Path_segment of
<<""/utf8>> ->
{continue, {ok, Current_json@1}};
<<"#"/utf8, Array_index/binary>> ->
case gleam_stdlib:parse_int(Array_index) of
{ok, Index} ->
case Current_json@1 of
{json_array, Json_list} ->
case gleam_stdlib:map_get(Json_list, Index) of
{ok, Found_json} ->
{continue, {ok, Found_json}};
{error, _} ->
{stop, {error, path_not_found}}
end;
_ ->
{stop, {error, path_not_found}}
end;
{error, _} ->
{stop, {error, invalid_json_path}}
end;
_ ->
case Current_json@1 of
{json_object, Found_dict} ->
case gleam_stdlib:map_get(Found_dict, Path_segment) of
{ok, Json_found_at_path} ->
{continue, {ok, Json_found_at_path}};
{error, _} ->
{stop, {error, path_not_found}}
end;
_ ->
{stop, {error, path_not_found}}
end
end
end
).