Current section
Files
Jump to
Current section
Files
src/convert@json.erl
-module(convert@json).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/convert/json.gleam").
-export([encode_value/1, json_encode/2, decoder/1, json_decode/1, json_decode_bits/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.
-file("src/convert/json.gleam", 51).
?DOC(
" Encode a GlitrValue into its corresponding JSON representation. \n"
" This is not meant to be used directly ! \n"
" It is better to use converters.\n"
).
-spec encode_value(convert:glitr_value()) -> gleam@json:json().
encode_value(Val) ->
case Val of
{string_value, V} ->
gleam@json:string(V);
{bool_value, V@1} ->
gleam@json:bool(V@1);
{float_value, V@2} ->
gleam@json:float(V@2);
{int_value, V@3} ->
gleam@json:int(V@3);
{bit_array_value, V@4} ->
gleam@json:string(gleam@bit_array:base64_url_encode(V@4, true));
{list_value, Vals} ->
gleam@json:array(Vals, fun encode_value/1);
{dict_value, V@5} ->
gleam@json:array(
begin
_pipe = V@5,
maps:to_list(_pipe)
end,
fun(Keyval) ->
gleam@json:array(
[erlang:element(1, Keyval), erlang:element(2, Keyval)],
fun encode_value/1
)
end
);
{object_value, V@6} ->
gleam@json:object(
gleam@list:map(
V@6,
fun(F) ->
{erlang:element(1, F),
encode_value(erlang:element(2, F))}
end
)
);
{optional_value, V@7} ->
gleam@json:nullable(V@7, fun encode_value/1);
{result_value, V@8} ->
case V@8 of
{ok, Res} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"ok"/utf8>>)},
{<<"value"/utf8>>, encode_value(Res)}]
);
{error, Err} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"error"/utf8>>)},
{<<"value"/utf8>>, encode_value(Err)}]
)
end;
{enum_value, Variant, V@9} ->
gleam@json:object(
[{<<"variant"/utf8>>, gleam@json:string(Variant)},
{<<"value"/utf8>>, encode_value(V@9)}]
);
_ ->
gleam@json:null()
end.
-file("src/convert/json.gleam", 12).
?DOC(
" Encode a value into the corresponding Json using the converter. \n"
" If the converter isn't valid, a NullValue is returned.\n"
).
-spec json_encode(EPS, convert:converter(EPS)) -> gleam@json:json().
json_encode(Value, Converter) ->
_pipe = Value,
_pipe@1 = (convert:encode(Converter))(_pipe),
encode_value(_pipe@1).
-file("src/convert/json.gleam", 151).
-spec object_decoder(
gleam@dynamic@decode:decoder(list({binary(), convert:glitr_value()})),
list({binary(), convert:glitr_type()})
) -> gleam@dynamic@decode:decoder(convert:glitr_value()).
object_decoder(Fields_decoder, Fields) ->
case Fields of
[] ->
_pipe = Fields_decoder,
gleam@dynamic@decode:map(_pipe, fun(Fields@1) -> _pipe@1 = Fields@1,
_pipe@2 = lists:reverse(_pipe@1),
{object_value, _pipe@2} end);
[{Name, Of} | Rest] ->
_pipe@3 = Fields_decoder,
_pipe@4 = gleam@dynamic@decode:then(
_pipe@3,
fun(Fields@2) ->
gleam@dynamic@decode:field(
Name,
decoder(Of),
fun(Field_val) ->
gleam@dynamic@decode:success(
[{Name, Field_val} | Fields@2]
)
end
)
end
),
object_decoder(_pipe@4, Rest)
end.
-file("src/convert/json.gleam", 91).
?DOC(
" Create a Gleam decoder for the provided GlitrType.\n"
" This is not meant to be used directly !\n"
" It is better to use converters.\n"
).
-spec decoder(convert:glitr_type()) -> gleam@dynamic@decode:decoder(convert:glitr_value()).
decoder(Of) ->
case Of of
string ->
_pipe = {decoder, fun gleam@dynamic@decode:decode_string/1},
gleam@dynamic@decode:map(
_pipe,
fun(Field@0) -> {string_value, Field@0} end
);
bool ->
_pipe@1 = {decoder, fun gleam@dynamic@decode:decode_bool/1},
gleam@dynamic@decode:map(
_pipe@1,
fun(Field@0) -> {bool_value, Field@0} end
);
float ->
_pipe@2 = {decoder, fun gleam@dynamic@decode:decode_float/1},
gleam@dynamic@decode:map(
_pipe@2,
fun(Field@0) -> {float_value, Field@0} end
);
int ->
_pipe@3 = {decoder, fun gleam@dynamic@decode:decode_int/1},
gleam@dynamic@decode:map(
_pipe@3,
fun(Field@0) -> {int_value, Field@0} end
);
bit_array ->
_pipe@4 = {decoder, fun gleam@dynamic@decode:decode_string/1},
gleam@dynamic@decode:then(
_pipe@4,
fun(Base64_str) ->
Base64_data = begin
_pipe@5 = Base64_str,
gleam@bit_array:base64_url_decode(_pipe@5)
end,
case Base64_data of
{ok, Bits} ->
gleam@dynamic@decode:success(
{bit_array_value, Bits}
);
{error, _} ->
gleam@dynamic@decode:failure(
null_value,
<<"Expected a base64 encoded BitArray"/utf8>>
)
end
end
);
dynamic ->
_pipe@6 = {decoder, fun gleam@dynamic@decode:decode_dynamic/1},
gleam@dynamic@decode:map(
_pipe@6,
fun(Field@0) -> {dynamic_value, Field@0} end
);
{list, El} ->
_pipe@7 = gleam@dynamic@decode:list(decoder(El)),
gleam@dynamic@decode:map(
_pipe@7,
fun(Field@0) -> {list_value, Field@0} end
);
{dict, K, V} ->
_pipe@8 = gleam@dynamic@decode:dict(decoder(K), decoder(V)),
gleam@dynamic@decode:map(
_pipe@8,
fun(Field@0) -> {dict_value, Field@0} end
);
{optional, Of@1} ->
_pipe@9 = gleam@dynamic@decode:optional(decoder(Of@1)),
gleam@dynamic@decode:map(
_pipe@9,
fun(Field@0) -> {optional_value, Field@0} end
);
{object, Fields} ->
object_decoder(gleam@dynamic@decode:success([]), Fields);
{result, Res, Err} ->
_pipe@10 = gleam@dynamic@decode:at(
[<<"type"/utf8>>],
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
gleam@dynamic@decode:then(
_pipe@10,
fun(Type_val) -> case Type_val of
<<"ok"/utf8>> ->
_pipe@11 = gleam@dynamic@decode:at(
[<<"value"/utf8>>],
decoder(Res)
),
_pipe@12 = gleam@dynamic@decode:map(
_pipe@11,
fun(Field@0) -> {ok, Field@0} end
),
gleam@dynamic@decode:map(
_pipe@12,
fun(Field@0) -> {result_value, Field@0} end
);
<<"error"/utf8>> ->
_pipe@13 = gleam@dynamic@decode:at(
[<<"value"/utf8>>],
decoder(Err)
),
_pipe@14 = gleam@dynamic@decode:map(
_pipe@13,
fun(Field@0) -> {error, Field@0} end
),
gleam@dynamic@decode:map(
_pipe@14,
fun(Field@0) -> {result_value, Field@0} end
);
_ ->
gleam@dynamic@decode:failure(
null_value,
<<"Type must be 'ok' or 'error'"/utf8>>
)
end end
);
{enum, Variants} ->
_pipe@15 = gleam@dynamic@decode:at(
[<<"variant"/utf8>>],
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
gleam@dynamic@decode:then(
_pipe@15,
fun(Variant_name) ->
case gleam@list:key_find(Variants, Variant_name) of
{ok, Variant_def} ->
_pipe@16 = gleam@dynamic@decode:at(
[<<"value"/utf8>>],
decoder(Variant_def)
),
gleam@dynamic@decode:map(
_pipe@16,
fun(Value) ->
{enum_value, Variant_name, Value}
end
);
{error, _} ->
gleam@dynamic@decode:failure(
null_value,
<<"Variant must be one of: "/utf8,
(begin
_pipe@17 = Variants,
_pipe@18 = gleam@list:map(
_pipe@17,
fun(V@1) ->
erlang:element(1, V@1)
end
),
gleam@string:join(
_pipe@18,
<<"/"/utf8>>
)
end)/binary>>
)
end
end
);
null ->
gleam@dynamic@decode:success(null_value)
end.
-file("src/convert/json.gleam", 17).
?DOC(" Decode a Json string using the provided converter.\n").
-spec json_decode(convert:converter(EPU)) -> fun((binary()) -> {ok, EPU} |
{error, gleam@json:decode_error()}).
json_decode(Converter) ->
Decoder = decoder(convert:type_def(Converter)),
fun(Value) ->
gleam@result:'try'(
begin
_pipe = Value,
gleam@json:parse(_pipe, Decoder)
end,
fun(Glitr_value) -> _pipe@1 = Glitr_value,
_pipe@2 = (convert:decode(Converter))(_pipe@1),
gleam@result:map_error(
_pipe@2,
fun(Field@0) -> {unable_to_decode, Field@0} end
) end
)
end.
-file("src/convert/json.gleam", 33).
?DOC(" Decode a Json bit array using the provided converter.\n").
-spec json_decode_bits(convert:converter(EPY)) -> fun((bitstring()) -> {ok, EPY} |
{error, gleam@json:decode_error()}).
json_decode_bits(Converter) ->
Decoder = decoder(convert:type_def(Converter)),
fun(Value) ->
gleam@result:'try'(
begin
_pipe = Value,
gleam@json:parse_bits(_pipe, Decoder)
end,
fun(Glitr_value) -> _pipe@1 = Glitr_value,
_pipe@2 = (convert:decode(Converter))(_pipe@1),
gleam@result:map_error(
_pipe@2,
fun(Field@0) -> {unable_to_decode, Field@0} end
) end
)
end.