Current section
Files
Jump to
Current section
Files
src/glome@core@serde.erl
-module(glome@core@serde).
-compile(no_auto_import).
-export([decode_to_dynamic/1, string_field/2, get_field_by_path/2, get_field_as_string/1]).
-spec decode_to_dynamic(binary()) -> {ok, gleam@dynamic:dynamic()} |
{error, glome@core@error:glome_error()}.
decode_to_dynamic(Json) ->
_pipe = Json,
_pipe@1 = gleam@json:decode(_pipe, fun gleam@dynamic:dynamic/1),
_pipe@2 = gleam@result:map_error(
_pipe@1,
fun glome@core@error:json_decode_to_dynamic_decode_error/1
),
glome@core@error:map_decode_errors(_pipe@2).
-spec string_field(binary(), binary()) -> {ok, binary()} |
{error, glome@core@error:glome_error()}.
string_field(Data, Field_name) ->
_pipe = gleam@json:decode(
Data,
gleam@dynamic:field(Field_name, fun gleam@dynamic:string/1)
),
_pipe@1 = gleam@result:map_error(
_pipe,
fun glome@core@error:json_decode_to_dynamic_decode_error/1
),
glome@core@error:map_decode_errors(_pipe@1).
-spec get_field_by_path(gleam@dynamic:dynamic(), binary()) -> {ok,
gleam@dynamic:dynamic()} |
{error, glome@core@error:glome_error()}.
get_field_by_path(Data, Path) ->
case gleam@string:split(Path, <<"."/utf8>>) of
[X] ->
_pipe = Data,
_pipe@1 = (gleam@dynamic:field(X, fun gleam@dynamic:dynamic/1))(
_pipe
),
glome@core@error:map_decode_errors(_pipe@1);
[X@1 | Xs] ->
_pipe@2 = Data,
_pipe@3 = (gleam@dynamic:field(X@1, fun gleam@dynamic:dynamic/1))(
_pipe@2
),
_pipe@4 = glome@core@error:map_decode_errors(_pipe@3),
gleam@result:then(
_pipe@4,
fun(_capture) ->
get_field_by_path(
_capture,
gleam@string:join(Xs, <<"."/utf8>>)
)
end
)
end.
-spec get_field_as_string(gleam@dynamic:dynamic()) -> {ok, binary()} |
{error, glome@core@error:glome_error()}.
get_field_as_string(Value) ->
_pipe = gleam@dynamic:string(Value),
glome@core@error:map_decode_errors(_pipe).