-module(oadr3@utils).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oadr3/utils.gleam").
-export([set_method/2, append_path/2, set_query/2, set_body/3, fields_decoder/0, any_decoder/0, json_to_bits/1, fields_to_json/1, any_to_json/1, dict/2, fields_to_dynamic/1, any_to_dynamic/1, merge/1, decode_additional/3, object/1]).
-export_type([any_/0, never/0]).
-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(
" This module is copied from `oas_generator` utils directory\n"
" adding `merge` function for the Erlang target.\n"
).
-type any_() :: {object, gleam@dict:dict(binary(), any_())} |
{array, list(any_())} |
{boolean, boolean()} |
{integer, integer()} |
{number, float()} |
{string, binary()} |
null.
-type never() :: {never, never()}.
-file("src/oadr3/utils.gleam", 14).
-spec set_method(gleam@http@request:request(HDQ), gleam@http:method()) -> gleam@http@request:request(HDQ).
set_method(Request, Method) ->
gleam@http@request:set_method(Request, Method).
-file("src/oadr3/utils.gleam", 18).
-spec append_path(gleam@http@request:request(HDR), binary()) -> gleam@http@request:request(HDR).
append_path(Request, Path) ->
gleam@http@request:set_path(
Request,
<<(erlang:element(8, Request))/binary, Path/binary>>
).
-file("src/oadr3/utils.gleam", 22).
-spec set_query(
gleam@http@request:request(HEH),
list({binary(), gleam@option:option(binary())})
) -> gleam@http@request:request(HEH).
set_query(Request, Query) ->
Query@1 = gleam@list:filter_map(
Query,
fun(Q) ->
{K, V} = Q,
case V of
{some, V@1} ->
{ok, {K, V@1}};
none ->
{error, nil}
end
end
),
case Query@1 of
[] ->
Request;
_ ->
gleam@http@request:set_query(Request, Query@1)
end.
-file("src/oadr3/utils.gleam", 37).
-spec set_body(gleam@http@request:request(any()), binary(), HCQ) -> gleam@http@request:request(HCQ).
set_body(Request, Mime, Content) ->
_pipe = Request,
_pipe@1 = gleam@http@request:prepend_header(
_pipe,
<<"content-type"/utf8>>,
Mime
),
gleam@http@request:set_body(_pipe@1, Content).
-file("src/oadr3/utils.gleam", 72).
-spec fields_decoder() -> gleam@dynamic@decode:decoder(gleam@dict:dict(binary(), any_())).
fields_decoder() ->
gleam@dynamic@decode:dict(
{decoder, fun gleam@dynamic@decode:decode_string/1},
any_decoder()
).
-file("src/oadr3/utils.gleam", 56).
-spec any_decoder() -> gleam@dynamic@decode:decoder(any_()).
any_decoder() ->
gleam@dynamic@decode:recursive(
fun() ->
gleam@dynamic@decode:one_of(
gleam@dynamic@decode:map(
fields_decoder(),
fun(Field@0) -> {object, Field@0} end
),
[begin
_pipe = gleam@dynamic@decode:list(any_decoder()),
gleam@dynamic@decode:map(
_pipe,
fun(Field@0) -> {array, Field@0} end
)
end,
begin
_pipe@1 = {decoder,
fun gleam@dynamic@decode:decode_bool/1},
gleam@dynamic@decode:map(
_pipe@1,
fun(Field@0) -> {boolean, Field@0} end
)
end,
begin
_pipe@2 = {decoder,
fun gleam@dynamic@decode:decode_int/1},
gleam@dynamic@decode:map(
_pipe@2,
fun(Field@0) -> {integer, Field@0} end
)
end,
begin
_pipe@3 = {decoder,
fun gleam@dynamic@decode:decode_float/1},
gleam@dynamic@decode:map(
_pipe@3,
fun(Field@0) -> {number, Field@0} end
)
end,
gleam@dynamic@decode:map(
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Decoded) -> case Decoded of
{some, Str} ->
{string, Str};
none ->
null
end end
)]
)
end
).
-file("src/oadr3/utils.gleam", 76).
-spec json_to_bits(gleam@json:json()) -> bitstring().
json_to_bits(Json) ->
_pipe = Json,
_pipe@1 = gleam@json:to_string(_pipe),
gleam_stdlib:identity(_pipe@1).
-file("src/oadr3/utils.gleam", 94).
-spec fields_to_json(gleam@dict:dict(binary(), any_())) -> gleam@json:json().
fields_to_json(Fields) ->
gleam@json:dict(Fields, fun(X) -> X end, fun any_to_json/1).
-file("src/oadr3/utils.gleam", 82).
-spec any_to_json(any_()) -> gleam@json:json().
any_to_json(Any) ->
case Any of
{object, Fields} ->
fields_to_json(Fields);
{array, List} ->
gleam@json:array(List, fun any_to_json/1);
{boolean, Bool} ->
gleam@json:bool(Bool);
{integer, Int} ->
gleam@json:int(Int);
{number, Float} ->
gleam@json:float(Float);
{string, String} ->
gleam@json:string(String);
null ->
gleam@json:null()
end.
-file("src/oadr3/utils.gleam", 125).
-spec dict(gleam@dict:dict(binary(), HFQ), fun((HFQ) -> gleam@json:json())) -> gleam@json:json().
dict(Dict, Values) ->
gleam@json:dict(Dict, fun(X) -> X end, Values).
-file("src/oadr3/utils.gleam", 110).
-spec fields_to_dynamic(gleam@dict:dict(binary(), any_())) -> gleam@dynamic:dynamic_().
fields_to_dynamic(Fields) ->
gleam@dynamic:properties(
begin
_pipe = Fields,
_pipe@1 = maps:to_list(_pipe),
gleam@list:map(
_pipe@1,
fun(Entry) ->
{Key, Value} = Entry,
{gleam_stdlib:identity(Key), any_to_dynamic(Value)}
end
)
end
).
-file("src/oadr3/utils.gleam", 98).
-spec any_to_dynamic(any_()) -> gleam@dynamic:dynamic_().
any_to_dynamic(Any) ->
case Any of
{object, Fields} ->
fields_to_dynamic(Fields);
{array, Items} ->
gleam_stdlib:identity(gleam@list:map(Items, fun any_to_dynamic/1));
{boolean, Bool} ->
gleam_stdlib:identity(Bool);
{integer, Int} ->
gleam_stdlib:identity(Int);
{number, Float} ->
gleam_stdlib:identity(Float);
{string, String} ->
gleam_stdlib:identity(String);
null ->
gleam@dynamic:nil()
end.
-file("src/oadr3/utils.gleam", 133).
-spec merge(list(gleam@json:json())) -> gleam@json:json().
merge(Items) ->
oadr3_utils_ffi:merge(Items).
-file("src/oadr3/utils.gleam", 137).
-spec decode_additional(
any(),
any(),
fun((gleam@dict:dict(any(), any())) -> gleam@dynamic@decode:decoder(HGE))
) -> gleam@dynamic@decode:decoder(HGE).
decode_additional(_, _, Next) ->
gleam@dynamic@decode:then(
gleam@dynamic@decode:success(maps:new()),
fun(Additional) -> Next(Additional) end
).
-file("src/oadr3/utils.gleam", 145).
-spec object(list({binary(), gleam@json:json()})) -> gleam@json:json().
object(Entries) ->
_pipe = gleam@list:filter(
Entries,
fun(Entry) ->
{_, V} = Entry,
V /= gleam@json:null()
end
),
gleam@json:object(_pipe).