Current section

Files

Jump to
oaspec src oaspec@codegen@server.erl
Raw

src/oaspec@codegen@server.erl

-module(oaspec@codegen@server).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/codegen/server.gleam").
-export([generate/1]).
-export_type([deep_object_property/0, body_field_kind/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.
-type deep_object_property() :: {deep_object_property,
binary(),
binary(),
oaspec@openapi@schema:schema_ref(),
boolean()}.
-type body_field_kind() :: body_field_unknown |
body_field_string |
body_field_int |
body_field_float |
body_field_bool |
body_field_string_array |
body_field_int_array |
body_field_float_array |
body_field_bool_array.
-file("src/oaspec/codegen/server.gleam", 66).
?DOC(" Generate a single handler stub.\n").
-spec generate_handler(
gleam@string_tree:string_tree(),
binary(),
oaspec@openapi@spec:operation(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_handler(Sb, Op_id, Operation, _) ->
Fn_name = oaspec@util@naming:operation_to_function_name(Op_id),
Request_type = <<(oaspec@util@naming:schema_to_type_name(Op_id))/binary,
"Request"/utf8>>,
Response_type = <<(oaspec@util@naming:schema_to_type_name(Op_id))/binary,
"Response"/utf8>>,
Sb@1 = case erlang:element(3, Operation) of
{some, Summary} ->
_pipe = Sb,
oaspec@util@string_extra:doc_comment(_pipe, Summary);
_ ->
Sb
end,
Sb@2 = case erlang:element(4, Operation) of
{some, Desc} ->
_pipe@1 = Sb@1,
oaspec@util@string_extra:doc_comment(_pipe@1, Desc);
_ ->
Sb@1
end,
Has_params = not gleam@list:is_empty(erlang:element(6, Operation)) orelse gleam@option:is_some(
erlang:element(7, Operation)
),
Sb@3 = case Has_params of
true ->
_pipe@2 = Sb@2,
oaspec@util@string_extra:line(
_pipe@2,
<<<<<<<<<<<<"pub fn "/utf8, Fn_name/binary>>/binary,
"(req: request_types."/utf8>>/binary,
Request_type/binary>>/binary,
") -> response_types."/utf8>>/binary,
Response_type/binary>>/binary,
" {"/utf8>>
);
false ->
_pipe@3 = Sb@2,
oaspec@util@string_extra:line(
_pipe@3,
<<<<<<<<"pub fn "/utf8, Fn_name/binary>>/binary,
"() -> response_types."/utf8>>/binary,
Response_type/binary>>/binary,
" {"/utf8>>
)
end,
Sb@4 = case Has_params of
true ->
_pipe@4 = Sb@3,
oaspec@util@string_extra:indent(_pipe@4, 1, <<"let _ = req"/utf8>>);
false ->
Sb@3
end,
_pipe@5 = Sb@4,
_pipe@6 = oaspec@util@string_extra:indent(
_pipe@5,
1,
<<"// TODO: Implement "/utf8, Fn_name/binary>>
),
_pipe@7 = oaspec@util@string_extra:indent(_pipe@6, 1, <<"todo"/utf8>>),
_pipe@8 = oaspec@util@string_extra:line(_pipe@7, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@8).
-file("src/oaspec/codegen/server.gleam", 153).
?DOC(" Extract a short suffix from a URL expression for function naming.\n").
-spec url_expression_to_suffix(binary()) -> binary().
url_expression_to_suffix(Url_expression) ->
Parts = gleam@string:split(Url_expression, <<"/"/utf8>>),
case gleam@list:last(Parts) of
{ok, Last} ->
_pipe = Last,
_pipe@1 = gleam@string:replace(_pipe, <<"{"/utf8>>, <<""/utf8>>),
_pipe@2 = gleam@string:replace(_pipe@1, <<"}"/utf8>>, <<""/utf8>>),
_pipe@3 = gleam@string:replace(_pipe@2, <<"$"/utf8>>, <<""/utf8>>),
gleam@string:replace(_pipe@3, <<"#"/utf8>>, <<""/utf8>>);
{error, _} ->
<<"handler"/utf8>>
end.
-file("src/oaspec/codegen/server.gleam", 121).
?DOC(" Generate callback handler stubs for an operation's callbacks.\n").
-spec generate_callback_handlers(
gleam@string_tree:string_tree(),
binary(),
oaspec@openapi@spec:operation(oaspec@openapi@spec:spec_stage())
) -> gleam@string_tree:string_tree().
generate_callback_handlers(Sb, Op_id, Operation) ->
Callbacks = maps:to_list(erlang:element(11, Operation)),
gleam@list:fold(
Callbacks,
Sb,
fun(Sb@1, Entry) ->
{Callback_name, Callback} = Entry,
Callback_entries = maps:to_list(erlang:element(2, Callback)),
gleam@list:fold(
Callback_entries,
Sb@1,
fun(Sb@2, Cb_entry) ->
{Url_expression, _} = Cb_entry,
Fn_name = <<<<<<<<(oaspec@util@naming:operation_to_function_name(
Op_id
))/binary,
"_callback_"/utf8>>/binary,
(oaspec@util@naming:to_snake_case(Callback_name))/binary>>/binary,
"_"/utf8>>/binary,
(oaspec@util@naming:to_snake_case(
url_expression_to_suffix(Url_expression)
))/binary>>,
_pipe = Sb@2,
_pipe@1 = oaspec@util@string_extra:doc_comment(
_pipe,
<<<<<<"Callback handler stub for "/utf8,
Callback_name/binary>>/binary,
" on "/utf8>>/binary,
Op_id/binary>>
),
_pipe@2 = oaspec@util@string_extra:doc_comment(
_pipe@1,
<<"URL: "/utf8, Url_expression/binary>>
),
_pipe@3 = oaspec@util@string_extra:line(
_pipe@2,
<<<<"pub fn "/utf8, Fn_name/binary>>/binary,
"() -> String {"/utf8>>
),
_pipe@4 = oaspec@util@string_extra:indent(
_pipe@3,
1,
<<"// TODO: Implement callback "/utf8,
Callback_name/binary>>
),
_pipe@5 = oaspec@util@string_extra:indent(
_pipe@4,
1,
<<"todo"/utf8>>
),
_pipe@6 = oaspec@util@string_extra:line(
_pipe@5,
<<"}"/utf8>>
),
oaspec@util@string_extra:blank_line(_pipe@6)
end
)
end
).
-file("src/oaspec/codegen/server.gleam", 39).
?DOC(" Generate handler stubs for all operations.\n").
-spec generate_handlers(oaspec@codegen@context:context()) -> binary().
generate_handlers(Ctx) ->
Sb = begin
_pipe = oaspec@util@string_extra:file_header(<<"0.8.0"/utf8>>),
oaspec@util@string_extra:imports(
_pipe,
[<<(erlang:element(5, erlang:element(3, Ctx)))/binary,
"/request_types"/utf8>>,
<<(erlang:element(5, erlang:element(3, Ctx)))/binary,
"/response_types"/utf8>>]
)
end,
Operations = oaspec@codegen@types:collect_operations(Ctx),
Sb@2 = gleam@list:fold(
Operations,
Sb,
fun(Sb@1, Op) ->
{Op_id, Operation, _, _} = Op,
generate_handler(Sb@1, Op_id, Operation, Ctx)
end
),
Sb@4 = gleam@list:fold(
Operations,
Sb@2,
fun(Sb@3, Op@1) ->
{Op_id@1, Operation@1, _, _} = Op@1,
generate_callback_handlers(Sb@3, Op_id@1, Operation@1)
end
),
oaspec@util@string_extra:to_string(Sb@4).
-file("src/oaspec/codegen/server.gleam", 729).
-spec route_arg_name(binary(), boolean()) -> binary().
route_arg_name(Name, Used) ->
case Used of
true ->
Name;
false ->
<<"_"/utf8, Name/binary>>
end.
-file("src/oaspec/codegen/server.gleam", 736).
-spec generate_cookie_lookup(gleam@string_tree:string_tree()) -> gleam@string_tree:string_tree().
generate_cookie_lookup(Sb) ->
_pipe = Sb,
_pipe@1 = oaspec@util@string_extra:doc_comment(
_pipe,
<<"Extract a cookie value from the Cookie header."/utf8>>
),
_pipe@2 = oaspec@util@string_extra:line(
_pipe@1,
<<"fn cookie_lookup(headers: Dict(String, String), key: String) -> Result(String, Nil) {"/utf8>>
),
_pipe@3 = oaspec@util@string_extra:indent(
_pipe@2,
1,
<<"case dict.get(headers, \"cookie\") {"/utf8>>
),
_pipe@4 = oaspec@util@string_extra:indent(_pipe@3, 2, <<"Ok(raw) ->"/utf8>>),
_pipe@5 = oaspec@util@string_extra:indent(
_pipe@4,
3,
<<"list.find_map(string.split(raw, \";\"), fn(part) {"/utf8>>
),
_pipe@6 = oaspec@util@string_extra:indent(
_pipe@5,
4,
<<"let trimmed = string.trim(part)"/utf8>>
),
_pipe@7 = oaspec@util@string_extra:indent(
_pipe@6,
4,
<<"case string.split_once(trimmed, on: \"=\") {"/utf8>>
),
_pipe@8 = oaspec@util@string_extra:indent(
_pipe@7,
5,
<<"Ok(#(cookie_key, cookie_value)) ->"/utf8>>
),
_pipe@9 = oaspec@util@string_extra:indent(
_pipe@8,
6,
<<"case string.trim(cookie_key) == key {"/utf8>>
),
_pipe@10 = oaspec@util@string_extra:indent(
_pipe@9,
7,
<<"True -> uri.percent_decode(string.trim(cookie_value))"/utf8>>
),
_pipe@11 = oaspec@util@string_extra:indent(
_pipe@10,
7,
<<"False -> Error(Nil)"/utf8>>
),
_pipe@12 = oaspec@util@string_extra:indent(_pipe@11, 6, <<"}"/utf8>>),
_pipe@13 = oaspec@util@string_extra:indent(
_pipe@12,
5,
<<"Error(_) -> Error(Nil)"/utf8>>
),
_pipe@14 = oaspec@util@string_extra:indent(_pipe@13, 4, <<"}"/utf8>>),
_pipe@15 = oaspec@util@string_extra:indent(_pipe@14, 3, <<"})"/utf8>>),
_pipe@16 = oaspec@util@string_extra:indent(
_pipe@15,
2,
<<"Error(_) -> Error(Nil)"/utf8>>
),
_pipe@17 = oaspec@util@string_extra:indent(_pipe@16, 1, <<"}"/utf8>>),
_pipe@18 = oaspec@util@string_extra:line(_pipe@17, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@18).
-file("src/oaspec/codegen/server.gleam", 1141).
-spec body_field_kind_needs_int(body_field_kind()) -> boolean().
body_field_kind_needs_int(Kind) ->
case Kind of
body_field_int ->
true;
body_field_int_array ->
true;
_ ->
false
end.
-file("src/oaspec/codegen/server.gleam", 1148).
-spec body_field_kind_needs_float(body_field_kind()) -> boolean().
body_field_kind_needs_float(Kind) ->
case Kind of
body_field_float ->
true;
body_field_float_array ->
true;
_ ->
false
end.
-file("src/oaspec/codegen/server.gleam", 1155).
-spec is_deep_object_param(
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> boolean().
is_deep_object_param(Param, Ctx) ->
case {erlang:element(3, Param),
erlang:element(7, Param),
erlang:element(6, Param)} of
{in_query,
{some, deep_object_style},
{some, {reference, _, _} = Schema_ref}} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref,
erlang:element(2, Ctx)
) of
{ok, {object_schema, _, _, _, _, _, _, _}} ->
true;
_ ->
false
end;
{in_query,
{some, deep_object_style},
{some, {inline, {object_schema, _, _, _, _, _, _, _}}}} ->
true;
{_, _, _} ->
false
end.
-file("src/oaspec/codegen/server.gleam", 1168).
-spec deep_object_properties(
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> list(deep_object_property()).
deep_object_properties(Param, Ctx) ->
Details = case erlang:element(6, Param) of
{some, {reference, _, _} = Schema_ref} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref,
erlang:element(2, Ctx)
) of
{ok, {object_schema, _, Properties, Required, _, _, _, _}} ->
{Properties, Required};
_ ->
{maps:new(), []}
end;
{some,
{inline, {object_schema, _, Properties@1, Required@1, _, _, _, _}}} ->
{Properties@1, Required@1};
_ ->
{maps:new(), []}
end,
{Properties@2, Required_fields} = Details,
_pipe = maps:to_list(Properties@2),
gleam@list:map(
_pipe,
fun(Entry) ->
{Prop_name, Prop_ref} = Entry,
{deep_object_property,
Prop_name,
oaspec@util@naming:to_snake_case(Prop_name),
Prop_ref,
gleam@list:contains(Required_fields, Prop_name)}
end
).
-file("src/oaspec/codegen/server.gleam", 1197).
-spec deep_object_type_name(
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage()),
binary()
) -> binary().
deep_object_type_name(Param, Op_id) ->
case erlang:element(6, Param) of
{some, {reference, _, Name}} ->
<<"types."/utf8,
(oaspec@util@naming:schema_to_type_name(Name))/binary>>;
_ ->
<<<<<<"types."/utf8,
(oaspec@util@naming:schema_to_type_name(Op_id))/binary>>/binary,
"Param"/utf8>>/binary,
(oaspec@util@naming:to_pascal_case(erlang:element(2, Param)))/binary>>
end.
-file("src/oaspec/codegen/server.gleam", 1270).
-spec deep_object_param_has_optional_fields(
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> boolean().
deep_object_param_has_optional_fields(Param, Ctx) ->
case is_deep_object_param(Param, Ctx) of
true ->
gleam@list:any(
deep_object_properties(Param, Ctx),
fun(Prop) -> not erlang:element(5, Prop) end
);
false ->
false
end.
-file("src/oaspec/codegen/server.gleam", 1320).
-spec request_body_uses_form_urlencoded(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage())
) -> boolean().
request_body_uses_form_urlencoded(Rb) ->
gleam@dict:has_key(
erlang:element(3, Rb),
<<"application/x-www-form-urlencoded"/utf8>>
).
-file("src/oaspec/codegen/server.gleam", 1324).
-spec request_body_uses_multipart(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage())
) -> boolean().
request_body_uses_multipart(Rb) ->
gleam@dict:has_key(erlang:element(3, Rb), <<"multipart/form-data"/utf8>>).
-file("src/oaspec/codegen/server.gleam", 1328).
-spec operation_uses_form_urlencoded_body(
oaspec@openapi@spec:operation(oaspec@openapi@spec:spec_stage())
) -> boolean().
operation_uses_form_urlencoded_body(Operation) ->
case erlang:element(7, Operation) of
{some, {value, Rb}} ->
request_body_uses_form_urlencoded(Rb);
_ ->
false
end.
-file("src/oaspec/codegen/server.gleam", 1337).
-spec operation_uses_multipart_body(
oaspec@openapi@spec:operation(oaspec@openapi@spec:spec_stage())
) -> boolean().
operation_uses_multipart_body(Operation) ->
case erlang:element(7, Operation) of
{some, {value, Rb}} ->
request_body_uses_multipart(Rb);
_ ->
false
end.
-file("src/oaspec/codegen/server.gleam", 1344).
-spec object_properties_from_schema_ref(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> list(deep_object_property()).
object_properties_from_schema_ref(Schema_ref, Ctx) ->
Details = case Schema_ref of
{reference, _, _} = Schema_ref@1 ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref@1,
erlang:element(2, Ctx)
) of
{ok, {object_schema, _, Properties, Required, _, _, _, _}} ->
{Properties, Required};
_ ->
{maps:new(), []}
end;
{inline, {object_schema, _, Properties@1, Required@1, _, _, _, _}} ->
{Properties@1, Required@1};
_ ->
{maps:new(), []}
end,
{Properties@2, Required_fields} = Details,
_pipe = maps:to_list(Properties@2),
gleam@list:map(
_pipe,
fun(Entry) ->
{Prop_name, Prop_ref} = Entry,
{deep_object_property,
Prop_name,
oaspec@util@naming:to_snake_case(Prop_name),
Prop_ref,
gleam@list:contains(Required_fields, Prop_name)}
end
).
-file("src/oaspec/codegen/server.gleam", 1370).
-spec form_urlencoded_body_properties(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> list(deep_object_property()).
form_urlencoded_body_properties(Rb, Ctx) ->
case gleam_stdlib:map_get(
erlang:element(3, Rb),
<<"application/x-www-form-urlencoded"/utf8>>
) of
{ok, Media_type} ->
case erlang:element(2, Media_type) of
{some, Schema_ref} ->
object_properties_from_schema_ref(Schema_ref, Ctx);
none ->
[]
end;
{error, _} ->
[]
end.
-file("src/oaspec/codegen/server.gleam", 1385).
-spec multipart_body_properties(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> list(deep_object_property()).
multipart_body_properties(Rb, Ctx) ->
case gleam_stdlib:map_get(
erlang:element(3, Rb),
<<"multipart/form-data"/utf8>>
) of
{ok, Media_type} ->
case erlang:element(2, Media_type) of
{some, Schema_ref} ->
object_properties_from_schema_ref(Schema_ref, Ctx);
none ->
[]
end;
{error, _} ->
[]
end.
-file("src/oaspec/codegen/server.gleam", 1399).
-spec schema_ref_resolves_to_object(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> boolean().
schema_ref_resolves_to_object(Schema_ref, Ctx) ->
case Schema_ref of
{inline, {object_schema, _, _, _, _, _, _, _}} ->
true;
{reference, _, _} = Schema_ref@1 ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref@1,
erlang:element(2, Ctx)
) of
{ok, {object_schema, _, _, _, _, _, _, _}} ->
true;
_ ->
false
end;
_ ->
false
end.
-file("src/oaspec/codegen/server.gleam", 1411).
-spec form_urlencoded_schema_ref_type_name(oaspec@openapi@schema:schema_ref()) -> binary().
form_urlencoded_schema_ref_type_name(Schema_ref) ->
case Schema_ref of
{reference, _, Name} ->
<<"types."/utf8,
(oaspec@util@naming:schema_to_type_name(Name))/binary>>;
_ ->
<<"String"/utf8>>
end.
-file("src/oaspec/codegen/server.gleam", 1418).
-spec form_urlencoded_body_type_name(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
binary()
) -> binary().
form_urlencoded_body_type_name(Rb, Op_id) ->
case gleam_stdlib:map_get(
erlang:element(3, Rb),
<<"application/x-www-form-urlencoded"/utf8>>
) of
{ok, Media_type} ->
case erlang:element(2, Media_type) of
{some, {reference, _, Name}} ->
<<"types."/utf8,
(oaspec@util@naming:schema_to_type_name(Name))/binary>>;
{some, {inline, {object_schema, _, _, _, _, _, _, _}}} ->
<<<<"types."/utf8,
(oaspec@util@naming:schema_to_type_name(Op_id))/binary>>/binary,
"Request"/utf8>>;
_ ->
<<"String"/utf8>>
end;
{error, _} ->
<<"String"/utf8>>
end.
-file("src/oaspec/codegen/server.gleam", 1435).
-spec multipart_body_type_name(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
binary()
) -> binary().
multipart_body_type_name(Rb, Op_id) ->
case gleam_stdlib:map_get(
erlang:element(3, Rb),
<<"multipart/form-data"/utf8>>
) of
{ok, Media_type} ->
case erlang:element(2, Media_type) of
{some, {reference, _, Name}} ->
<<"types."/utf8,
(oaspec@util@naming:schema_to_type_name(Name))/binary>>;
{some, {inline, {object_schema, _, _, _, _, _, _, _}}} ->
<<<<"types."/utf8,
(oaspec@util@naming:schema_to_type_name(Op_id))/binary>>/binary,
"Request"/utf8>>;
_ ->
<<"String"/utf8>>
end;
{error, _} ->
<<"String"/utf8>>
end.
-file("src/oaspec/codegen/server.gleam", 1452).
-spec form_urlencoded_key(binary(), binary()) -> binary().
form_urlencoded_key(Prefix, Name) ->
case Prefix of
<<""/utf8>> ->
Name;
_ ->
<<<<<<Prefix/binary, "["/utf8>>/binary, Name/binary>>/binary,
"]"/utf8>>
end.
-file("src/oaspec/codegen/server.gleam", 1598).
-spec form_urlencoded_body_has_nested_object(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> boolean().
form_urlencoded_body_has_nested_object(Rb, Ctx) ->
gleam@list:any(
form_urlencoded_body_properties(Rb, Ctx),
fun(Prop) ->
schema_ref_resolves_to_object(erlang:element(4, Prop), Ctx)
end
).
-file("src/oaspec/codegen/server.gleam", 1635).
-spec multipart_body_has_optional_fields(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> boolean().
multipart_body_has_optional_fields(Rb, Ctx) ->
gleam@list:any(
multipart_body_properties(Rb, Ctx),
fun(Prop) -> not erlang:element(5, Prop) end
).
-file("src/oaspec/codegen/server.gleam", 1666).
-spec form_urlencoded_properties_have_optional_fields(
list(deep_object_property()),
oaspec@codegen@context:context(),
boolean()
) -> boolean().
form_urlencoded_properties_have_optional_fields(
Props,
Ctx,
Allow_nested_objects
) ->
gleam@list:any(
Props,
fun(Prop) ->
not erlang:element(5, Prop) orelse case Allow_nested_objects andalso schema_ref_resolves_to_object(
erlang:element(4, Prop),
Ctx
) of
true ->
form_urlencoded_properties_have_optional_fields(
object_properties_from_schema_ref(
erlang:element(4, Prop),
Ctx
),
Ctx,
false
);
false ->
false
end
end
).
-file("src/oaspec/codegen/server.gleam", 1554).
-spec form_urlencoded_body_has_optional_fields(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> boolean().
form_urlencoded_body_has_optional_fields(Rb, Ctx) ->
form_urlencoded_properties_have_optional_fields(
form_urlencoded_body_properties(Rb, Ctx),
Ctx,
true
).
-file("src/oaspec/codegen/server.gleam", 1760).
-spec query_schema_needs_string(
gleam@option:option(oaspec@openapi@schema:schema_ref())
) -> boolean().
query_schema_needs_string(Schema_ref) ->
case Schema_ref of
{some, {inline, {array_schema, _, _, _, _, _}}} ->
true;
{some, {inline, {boolean_schema, _}}} ->
true;
_ ->
false
end.
-file("src/oaspec/codegen/server.gleam", 1281).
-spec deep_object_param_needs_string(
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> boolean().
deep_object_param_needs_string(Param, Ctx) ->
case is_deep_object_param(Param, Ctx) of
true ->
gleam@list:any(
deep_object_properties(Param, Ctx),
fun(Prop) ->
query_schema_needs_string({some, erlang:element(4, Prop)})
end
);
false ->
false
end.
-file("src/oaspec/codegen/server.gleam", 1688).
-spec form_urlencoded_properties_need_string(
list(deep_object_property()),
oaspec@codegen@context:context(),
boolean()
) -> boolean().
form_urlencoded_properties_need_string(Props, Ctx, Allow_nested_objects) ->
gleam@list:any(
Props,
fun(Prop) ->
query_schema_needs_string({some, erlang:element(4, Prop)}) orelse case Allow_nested_objects
andalso schema_ref_resolves_to_object(erlang:element(4, Prop), Ctx) of
true ->
form_urlencoded_properties_need_string(
object_properties_from_schema_ref(
erlang:element(4, Prop),
Ctx
),
Ctx,
false
);
false ->
false
end
end
).
-file("src/oaspec/codegen/server.gleam", 1565).
-spec form_urlencoded_body_needs_string(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> boolean().
form_urlencoded_body_needs_string(Rb, Ctx) ->
form_urlencoded_properties_need_string(
form_urlencoded_body_properties(Rb, Ctx),
Ctx,
true
).
-file("src/oaspec/codegen/server.gleam", 1768).
-spec query_schema_needs_int(
gleam@option:option(oaspec@openapi@schema:schema_ref())
) -> boolean().
query_schema_needs_int(Schema_ref) ->
case Schema_ref of
{some, {inline, {integer_schema, _, _, _, _, _, _, _}}} ->
true;
{some,
{inline,
{array_schema,
_,
{inline, {integer_schema, _, _, _, _, _, _, _}},
_,
_,
_}}} ->
true;
_ ->
false
end.
-file("src/oaspec/codegen/server.gleam", 1294).
-spec deep_object_param_needs_int(
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> boolean().
deep_object_param_needs_int(Param, Ctx) ->
case is_deep_object_param(Param, Ctx) of
true ->
gleam@list:any(
deep_object_properties(Param, Ctx),
fun(Prop) ->
query_schema_needs_int({some, erlang:element(4, Prop)})
end
);
false ->
false
end.
-file("src/oaspec/codegen/server.gleam", 1777).
-spec query_schema_needs_float(
gleam@option:option(oaspec@openapi@schema:schema_ref())
) -> boolean().
query_schema_needs_float(Schema_ref) ->
case Schema_ref of
{some, {inline, {number_schema, _, _, _, _, _, _, _}}} ->
true;
{some,
{inline,
{array_schema,
_,
{inline, {number_schema, _, _, _, _, _, _, _}},
_,
_,
_}}} ->
true;
_ ->
false
end.
-file("src/oaspec/codegen/server.gleam", 1307).
-spec deep_object_param_needs_float(
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> boolean().
deep_object_param_needs_float(Param, Ctx) ->
case is_deep_object_param(Param, Ctx) of
true ->
gleam@list:any(
deep_object_properties(Param, Ctx),
fun(Prop) ->
query_schema_needs_float({some, erlang:element(4, Prop)})
end
);
false ->
false
end.
-file("src/oaspec/codegen/server.gleam", 2287).
?DOC(" Get the encode function name for a schema reference.\n").
-spec get_encode_function(
gleam@option:option(oaspec@openapi@schema:schema_ref()),
oaspec@codegen@context:context()
) -> binary().
get_encode_function(Schema_ref, _) ->
case Schema_ref of
{some, {reference, _, Name}} ->
<<<<"encode.encode_"/utf8,
(oaspec@util@naming:to_snake_case(Name))/binary>>/binary,
"_json"/utf8>>;
{some, {inline, {array_schema, _, Items, _, _, _}}} ->
case Items of
{reference, _, Name@1} ->
<<<<"fn(items) { json.array(items, encode.encode_"/utf8,
(oaspec@util@naming:to_snake_case(Name@1))/binary>>/binary,
"_json) }"/utf8>>;
_ ->
<<"json.string"/utf8>>
end;
{some, {inline, {string_schema, _, _, _, _, _, _}}} ->
<<"json.string"/utf8>>;
{some, {inline, {integer_schema, _, _, _, _, _, _, _}}} ->
<<"json.int"/utf8>>;
{some, {inline, {number_schema, _, _, _, _, _, _, _}}} ->
<<"json.float"/utf8>>;
{some, {inline, {boolean_schema, _}}} ->
<<"json.bool"/utf8>>;
_ ->
<<"json.string"/utf8>>
end.
-file("src/oaspec/codegen/server.gleam", 2135).
?DOC(" Generate code to convert a handler response to ServerResponse.\n").
-spec generate_response_conversion(
gleam@string_tree:string_tree(),
binary(),
oaspec@openapi@spec:operation(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_response_conversion(Sb, Response_type_name, Operation, Ctx) ->
Responses = oaspec@util@http:sort_response_entries(
maps:to_list(erlang:element(8, Operation))
),
case gleam@list:is_empty(Responses) of
true ->
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
3,
<<"ServerResponse(status: 200, body: \"\", headers: [])"/utf8>>
);
false ->
Sb@1 = begin
_pipe@1 = Sb,
oaspec@util@string_extra:indent(
_pipe@1,
3,
<<"case response {"/utf8>>
)
end,
Sb@3 = gleam@list:fold(
Responses,
Sb@1,
fun(Sb@2, Entry) ->
{Status_code, Ref_or} = Entry,
case Ref_or of
{value, Response} ->
Variant_name = <<Response_type_name/binary,
(oaspec@util@http:status_code_suffix(
Status_code
))/binary>>,
Status_int = oaspec@util@http:status_code_to_int(
Status_code
),
Content_entries = maps:to_list(
erlang:element(3, Response)
),
case Content_entries of
[] ->
_pipe@2 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@2,
4,
<<<<<<<<"response_types."/utf8,
Variant_name/binary>>/binary,
" -> ServerResponse(status: "/utf8>>/binary,
Status_int/binary>>/binary,
", body: \"\", headers: [])"/utf8>>
);
[{Media_type_name, Media_type}] ->
case Media_type_name of
<<"application/json"/utf8>> ->
case erlang:element(2, Media_type) of
{some, _} ->
Encode_fn = get_encode_function(
erlang:element(
2,
Media_type
),
Ctx
),
_pipe@3 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@3,
4,
<<<<<<<<<<<<"response_types."/utf8,
Variant_name/binary>>/binary,
"(data) -> ServerResponse(status: "/utf8>>/binary,
Status_int/binary>>/binary,
", body: json.to_string("/utf8>>/binary,
Encode_fn/binary>>/binary,
"(data)), headers: [#(\"content-type\", \"application/json\")])"/utf8>>
);
none ->
_pipe@4 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@4,
4,
<<<<<<<<"response_types."/utf8,
Variant_name/binary>>/binary,
" -> ServerResponse(status: "/utf8>>/binary,
Status_int/binary>>/binary,
", body: \"\", headers: [])"/utf8>>
)
end;
<<"text/plain"/utf8>> ->
case erlang:element(2, Media_type) of
{some, _} ->
_pipe@5 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@5,
4,
<<<<<<<<<<<<"response_types."/utf8,
Variant_name/binary>>/binary,
"(data) -> ServerResponse(status: "/utf8>>/binary,
Status_int/binary>>/binary,
", body: data, headers: [#(\"content-type\", \""/utf8>>/binary,
Media_type_name/binary>>/binary,
"\")])"/utf8>>
);
none ->
_pipe@6 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@6,
4,
<<<<<<<<"response_types."/utf8,
Variant_name/binary>>/binary,
" -> ServerResponse(status: "/utf8>>/binary,
Status_int/binary>>/binary,
", body: \"\", headers: [])"/utf8>>
)
end;
<<"application/xml"/utf8>> ->
case erlang:element(2, Media_type) of
{some, _} ->
_pipe@5 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@5,
4,
<<<<<<<<<<<<"response_types."/utf8,
Variant_name/binary>>/binary,
"(data) -> ServerResponse(status: "/utf8>>/binary,
Status_int/binary>>/binary,
", body: data, headers: [#(\"content-type\", \""/utf8>>/binary,
Media_type_name/binary>>/binary,
"\")])"/utf8>>
);
none ->
_pipe@6 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@6,
4,
<<<<<<<<"response_types."/utf8,
Variant_name/binary>>/binary,
" -> ServerResponse(status: "/utf8>>/binary,
Status_int/binary>>/binary,
", body: \"\", headers: [])"/utf8>>
)
end;
<<"text/xml"/utf8>> ->
case erlang:element(2, Media_type) of
{some, _} ->
_pipe@5 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@5,
4,
<<<<<<<<<<<<"response_types."/utf8,
Variant_name/binary>>/binary,
"(data) -> ServerResponse(status: "/utf8>>/binary,
Status_int/binary>>/binary,
", body: data, headers: [#(\"content-type\", \""/utf8>>/binary,
Media_type_name/binary>>/binary,
"\")])"/utf8>>
);
none ->
_pipe@6 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@6,
4,
<<<<<<<<"response_types."/utf8,
Variant_name/binary>>/binary,
" -> ServerResponse(status: "/utf8>>/binary,
Status_int/binary>>/binary,
", body: \"\", headers: [])"/utf8>>
)
end;
<<"application/octet-stream"/utf8>> ->
case erlang:element(2, Media_type) of
{some, _} ->
_pipe@5 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@5,
4,
<<<<<<<<<<<<"response_types."/utf8,
Variant_name/binary>>/binary,
"(data) -> ServerResponse(status: "/utf8>>/binary,
Status_int/binary>>/binary,
", body: data, headers: [#(\"content-type\", \""/utf8>>/binary,
Media_type_name/binary>>/binary,
"\")])"/utf8>>
);
none ->
_pipe@6 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@6,
4,
<<<<<<<<"response_types."/utf8,
Variant_name/binary>>/binary,
" -> ServerResponse(status: "/utf8>>/binary,
Status_int/binary>>/binary,
", body: \"\", headers: [])"/utf8>>
)
end;
_ ->
case erlang:element(2, Media_type) of
{some, _} ->
Encode_fn@1 = get_encode_function(
erlang:element(
2,
Media_type
),
Ctx
),
_pipe@7 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@7,
4,
<<<<<<<<<<<<<<<<"response_types."/utf8,
Variant_name/binary>>/binary,
"(data) -> ServerResponse(status: "/utf8>>/binary,
Status_int/binary>>/binary,
", body: json.to_string("/utf8>>/binary,
Encode_fn@1/binary>>/binary,
"(data)), headers: [#(\"content-type\", \""/utf8>>/binary,
Media_type_name/binary>>/binary,
"\")])"/utf8>>
);
none ->
_pipe@8 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@8,
4,
<<<<<<<<"response_types."/utf8,
Variant_name/binary>>/binary,
" -> ServerResponse(status: "/utf8>>/binary,
Status_int/binary>>/binary,
", body: \"\", headers: [])"/utf8>>
)
end
end;
[{First_media_type, _}, _ | _] ->
_pipe@9 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@9,
4,
<<<<<<<<<<<<"response_types."/utf8,
Variant_name/binary>>/binary,
"(data) -> ServerResponse(status: "/utf8>>/binary,
Status_int/binary>>/binary,
", body: data, headers: [#(\"content-type\", \""/utf8>>/binary,
First_media_type/binary>>/binary,
"\")])"/utf8>>
)
end;
_ ->
Sb@2
end
end
),
_pipe@10 = Sb@3,
oaspec@util@string_extra:indent(_pipe@10, 3, <<"}"/utf8>>)
end.
-file("src/oaspec/codegen/server.gleam", 2334).
?DOC(" Check if a path segment is a parameter.\n").
-spec is_path_param(binary()) -> boolean().
is_path_param(Segment) ->
case Segment of
<<"{"/utf8, _/binary>> ->
true;
_ ->
false
end.
-file("src/oaspec/codegen/server.gleam", 2342).
?DOC(" Extract parameter name from {name}.\n").
-spec extract_param_name(binary()) -> binary().
extract_param_name(Segment) ->
_pipe = Segment,
_pipe@1 = gleam@string:replace(_pipe, <<"{"/utf8>>, <<""/utf8>>),
gleam@string:replace(_pipe@1, <<"}"/utf8>>, <<""/utf8>>).
-file("src/oaspec/codegen/server.gleam", 2313).
?DOC(" Convert an OpenAPI path to a Gleam pattern match expression.\n").
-spec path_to_pattern(binary()) -> binary().
path_to_pattern(Path) ->
Segments = begin
_pipe = Path,
_pipe@1 = gleam@string:split(_pipe, <<"/"/utf8>>),
gleam@list:filter(_pipe@1, fun(S) -> S /= <<""/utf8>> end)
end,
Patterns = gleam@list:map(Segments, fun(Seg) -> case is_path_param(Seg) of
true ->
Param_name = extract_param_name(Seg),
oaspec@util@naming:to_snake_case(Param_name);
false ->
<<<<"\""/utf8, Seg/binary>>/binary, "\""/utf8>>
end end),
<<<<"["/utf8,
(oaspec@util@string_extra:join_with(Patterns, <<", "/utf8>>))/binary>>/binary,
"]"/utf8>>.
-file("src/oaspec/codegen/server.gleam", 892).
?DOC(" Generate parse expression for a path parameter (already bound as String).\n").
-spec param_parse_expr(
binary(),
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage())
) -> binary().
param_parse_expr(Var_name, Param) ->
case erlang:element(6, Param) of
{some, {inline, {integer_schema, _, _, _, _, _, _, _}}} ->
<<<<"{ let assert Ok(v) = int.parse("/utf8, Var_name/binary>>/binary,
") v }"/utf8>>;
{some, {inline, {number_schema, _, _, _, _, _, _, _}}} ->
<<<<"{ let assert Ok(v) = float.parse("/utf8, Var_name/binary>>/binary,
") v }"/utf8>>;
{some, {inline, {boolean_schema, _}}} ->
<<<<<<<<"{ let v = "/utf8, Var_name/binary>>/binary, " "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" }"/utf8>>;
_ ->
Var_name
end.
-file("src/oaspec/codegen/server.gleam", 916).
-spec query_required_expr_with_schema(
binary(),
gleam@option:option(oaspec@openapi@schema:schema_ref()),
gleam@option:option(boolean())
) -> binary().
query_required_expr_with_schema(Key, Schema_ref, Explode) ->
Base = <<<<"{ let assert Ok([v, ..]) = dict.get(query, \""/utf8,
Key/binary>>/binary,
"\") v }"/utf8>>,
case Schema_ref of
{some,
{inline,
{array_schema,
_,
{inline, {string_schema, _, _, _, _, _, _}},
_,
_,
_}}} ->
case Explode of
{some, false} ->
<<<<"{ let assert Ok([v, ..]) = dict.get(query, \""/utf8,
Key/binary>>/binary,
"\") list.map(string.split(v, \",\"), fn(item) { string.trim(item) }) }"/utf8>>;
_ ->
<<<<"{ let assert Ok(vs) = dict.get(query, \""/utf8,
Key/binary>>/binary,
"\") list.map(vs, fn(item) { string.trim(item) }) }"/utf8>>
end;
{some,
{inline,
{array_schema,
_,
{inline, {integer_schema, _, _, _, _, _, _, _}},
_,
_,
_}}} ->
case Explode of
{some, false} ->
<<<<"{ let assert Ok([v, ..]) = dict.get(query, \""/utf8,
Key/binary>>/binary,
"\") list.map(string.split(v, \",\"), fn(item) { let trimmed = string.trim(item) let assert Ok(n) = int.parse(trimmed) n }) }"/utf8>>;
_ ->
<<<<"{ let assert Ok(vs) = dict.get(query, \""/utf8,
Key/binary>>/binary,
"\") list.map(vs, fn(item) { let trimmed = string.trim(item) let assert Ok(n) = int.parse(trimmed) n }) }"/utf8>>
end;
{some,
{inline,
{array_schema,
_,
{inline, {number_schema, _, _, _, _, _, _, _}},
_,
_,
_}}} ->
case Explode of
{some, false} ->
<<<<"{ let assert Ok([v, ..]) = dict.get(query, \""/utf8,
Key/binary>>/binary,
"\") list.map(string.split(v, \",\"), fn(item) { let trimmed = string.trim(item) let assert Ok(n) = float.parse(trimmed) n }) }"/utf8>>;
_ ->
<<<<"{ let assert Ok(vs) = dict.get(query, \""/utf8,
Key/binary>>/binary,
"\") list.map(vs, fn(item) { let trimmed = string.trim(item) let assert Ok(n) = float.parse(trimmed) n }) }"/utf8>>
end;
{some,
{inline, {array_schema, _, {inline, {boolean_schema, _}}, _, _, _}}} ->
case Explode of
{some, false} ->
<<<<<<<<"{ let assert Ok([v, ..]) = dict.get(query, \""/utf8,
Key/binary>>/binary,
"\") list.map(string.split(v, \",\"), fn(item) { let v = string.trim(item) "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" }) }"/utf8>>;
_ ->
<<<<<<<<"{ let assert Ok(vs) = dict.get(query, \""/utf8,
Key/binary>>/binary,
"\") list.map(vs, fn(item) { let v = string.trim(item) "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" }) }"/utf8>>
end;
{some, {inline, {integer_schema, _, _, _, _, _, _, _}}} ->
<<<<"{ let assert Ok([v, ..]) = dict.get(query, \""/utf8,
Key/binary>>/binary,
"\") let assert Ok(n) = int.parse(v) n }"/utf8>>;
{some, {inline, {number_schema, _, _, _, _, _, _, _}}} ->
<<<<"{ let assert Ok([v, ..]) = dict.get(query, \""/utf8,
Key/binary>>/binary,
"\") let assert Ok(n) = float.parse(v) n }"/utf8>>;
{some, {inline, {boolean_schema, _}}} ->
<<<<<<<<"{ let assert Ok([v, ..]) = dict.get(query, \""/utf8,
Key/binary>>/binary,
"\") "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" }"/utf8>>;
_ ->
Base
end.
-file("src/oaspec/codegen/server.gleam", 912).
?DOC(" Generate expression for a required query parameter.\n").
-spec query_required_expr(
binary(),
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage())
) -> binary().
query_required_expr(Key, Param) ->
query_required_expr_with_schema(
Key,
erlang:element(6, Param),
erlang:element(8, Param)
).
-file("src/oaspec/codegen/server.gleam", 994).
-spec query_optional_expr_with_schema(
binary(),
gleam@option:option(oaspec@openapi@schema:schema_ref()),
gleam@option:option(boolean())
) -> binary().
query_optional_expr_with_schema(Key, Schema_ref, Explode) ->
case Schema_ref of
{some,
{inline,
{array_schema,
_,
{inline, {string_schema, _, _, _, _, _, _}},
_,
_,
_}}} ->
case Explode of
{some, false} ->
<<<<"case dict.get(query, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> Some(list.map(string.split(v, \",\"), fn(item) { string.trim(item) })) _ -> None }"/utf8>>;
_ ->
<<<<"case dict.get(query, \""/utf8, Key/binary>>/binary,
"\") { Ok(vs) -> Some(list.map(vs, fn(item) { string.trim(item) })) _ -> None }"/utf8>>
end;
{some,
{inline,
{array_schema,
_,
{inline, {integer_schema, _, _, _, _, _, _, _}},
_,
_,
_}}} ->
case Explode of
{some, false} ->
<<<<"case dict.get(query, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> Some(list.map(string.split(v, \",\"), fn(item) { let trimmed = string.trim(item) let assert Ok(n) = int.parse(trimmed) n })) _ -> None }"/utf8>>;
_ ->
<<<<"case dict.get(query, \""/utf8, Key/binary>>/binary,
"\") { Ok(vs) -> Some(list.map(vs, fn(item) { let trimmed = string.trim(item) let assert Ok(n) = int.parse(trimmed) n })) _ -> None }"/utf8>>
end;
{some,
{inline,
{array_schema,
_,
{inline, {number_schema, _, _, _, _, _, _, _}},
_,
_,
_}}} ->
case Explode of
{some, false} ->
<<<<"case dict.get(query, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> Some(list.map(string.split(v, \",\"), fn(item) { let trimmed = string.trim(item) let assert Ok(n) = float.parse(trimmed) n })) _ -> None }"/utf8>>;
_ ->
<<<<"case dict.get(query, \""/utf8, Key/binary>>/binary,
"\") { Ok(vs) -> Some(list.map(vs, fn(item) { let trimmed = string.trim(item) let assert Ok(n) = float.parse(trimmed) n })) _ -> None }"/utf8>>
end;
{some,
{inline, {array_schema, _, {inline, {boolean_schema, _}}, _, _, _}}} ->
case Explode of
{some, false} ->
<<<<<<<<"case dict.get(query, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> Some(list.map(string.split(v, \",\"), fn(item) { let v = string.trim(item) "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" })) _ -> None }"/utf8>>;
_ ->
<<<<<<<<"case dict.get(query, \""/utf8, Key/binary>>/binary,
"\") { Ok(vs) -> Some(list.map(vs, fn(item) { let v = string.trim(item) "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" })) _ -> None }"/utf8>>
end;
{some, {inline, {integer_schema, _, _, _, _, _, _, _}}} ->
<<<<"case dict.get(query, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> { case int.parse(v) { Ok(n) -> Some(n) _ -> None } } _ -> None }"/utf8>>;
{some, {inline, {number_schema, _, _, _, _, _, _, _}}} ->
<<<<"case dict.get(query, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> { case float.parse(v) { Ok(n) -> Some(n) _ -> None } } _ -> None }"/utf8>>;
{some, {inline, {boolean_schema, _}}} ->
<<<<<<<<"case dict.get(query, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> Some("/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
") _ -> None }"/utf8>>;
_ ->
<<<<"case dict.get(query, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> Some(v) _ -> None }"/utf8>>
end.
-file("src/oaspec/codegen/server.gleam", 990).
?DOC(" Generate expression for an optional query parameter.\n").
-spec query_optional_expr(
binary(),
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage())
) -> binary().
query_optional_expr(Key, Param) ->
query_optional_expr_with_schema(
Key,
erlang:element(6, Param),
erlang:element(8, Param)
).
-file("src/oaspec/codegen/server.gleam", 1240).
-spec deep_object_constructor_expr(
binary(),
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage()),
binary(),
oaspec@codegen@context:context()
) -> binary().
deep_object_constructor_expr(Key, Param, Op_id, Ctx) ->
Fields = begin
_pipe = deep_object_properties(Param, Ctx),
_pipe@1 = gleam@list:map(
_pipe,
fun(Prop) ->
Prop_key = <<<<<<Key/binary, "["/utf8>>/binary,
(erlang:element(2, Prop))/binary>>/binary,
"]"/utf8>>,
Value_expr = case erlang:element(5, Prop) of
true ->
query_required_expr_with_schema(
Prop_key,
{some, erlang:element(4, Prop)},
{some, true}
);
false ->
query_optional_expr_with_schema(
Prop_key,
{some, erlang:element(4, Prop)},
{some, true}
)
end,
<<<<(erlang:element(3, Prop))/binary, ": "/utf8>>/binary,
Value_expr/binary>>
end
),
gleam@string:join(_pipe@1, <<", "/utf8>>)
end,
<<<<<<(deep_object_type_name(Param, Op_id))/binary, "("/utf8>>/binary,
Fields/binary>>/binary,
")"/utf8>>.
-file("src/oaspec/codegen/server.gleam", 1211).
-spec deep_object_required_expr(
binary(),
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage()),
binary(),
oaspec@codegen@context:context()
) -> binary().
deep_object_required_expr(Key, Param, Op_id, Ctx) ->
deep_object_constructor_expr(Key, Param, Op_id, Ctx).
-file("src/oaspec/codegen/server.gleam", 1220).
-spec deep_object_optional_expr(
binary(),
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage()),
binary(),
oaspec@codegen@context:context()
) -> binary().
deep_object_optional_expr(Key, Param, Op_id, Ctx) ->
Props = deep_object_properties(Param, Ctx),
Prop_names = begin
_pipe = Props,
_pipe@1 = gleam@list:map(
_pipe,
fun(Prop) ->
<<<<"\""/utf8, (erlang:element(2, Prop))/binary>>/binary,
"\""/utf8>>
end
),
gleam@string:join(_pipe@1, <<", "/utf8>>)
end,
<<<<<<<<<<<<"case deep_object_present(query, \""/utf8, Key/binary>>/binary,
"\", ["/utf8>>/binary,
Prop_names/binary>>/binary,
"]) { True -> Some("/utf8>>/binary,
(deep_object_constructor_expr(Key, Param, Op_id, Ctx))/binary>>/binary,
") False -> None }"/utf8>>.
-file("src/oaspec/codegen/server.gleam", 1963).
?DOC(" Generate expression for a required header parameter.\n").
-spec header_required_expr(
binary(),
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage())
) -> binary().
header_required_expr(Key, Param) ->
case erlang:element(6, Param) of
{some,
{inline,
{array_schema,
_,
{inline, {string_schema, _, _, _, _, _, _}},
_,
_,
_}}} ->
<<<<"{ let assert Ok(v) = dict.get(headers, \""/utf8, Key/binary>>/binary,
"\") list.map(string.split(v, \",\"), fn(item) { string.trim(item) }) }"/utf8>>;
{some,
{inline,
{array_schema,
_,
{inline, {integer_schema, _, _, _, _, _, _, _}},
_,
_,
_}}} ->
<<<<"{ let assert Ok(v) = dict.get(headers, \""/utf8, Key/binary>>/binary,
"\") list.map(string.split(v, \",\"), fn(item) { let trimmed = string.trim(item) let assert Ok(n) = int.parse(trimmed) n }) }"/utf8>>;
{some,
{inline,
{array_schema,
_,
{inline, {number_schema, _, _, _, _, _, _, _}},
_,
_,
_}}} ->
<<<<"{ let assert Ok(v) = dict.get(headers, \""/utf8, Key/binary>>/binary,
"\") list.map(string.split(v, \",\"), fn(item) { let trimmed = string.trim(item) let assert Ok(n) = float.parse(trimmed) n }) }"/utf8>>;
{some,
{inline, {array_schema, _, {inline, {boolean_schema, _}}, _, _, _}}} ->
<<<<<<<<"{ let assert Ok(v) = dict.get(headers, \""/utf8,
Key/binary>>/binary,
"\") list.map(string.split(v, \",\"), fn(item) { let v = string.trim(item) "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" }) }"/utf8>>;
{some, {inline, {integer_schema, _, _, _, _, _, _, _}}} ->
<<<<"{ let assert Ok(v) = dict.get(headers, \""/utf8, Key/binary>>/binary,
"\") let assert Ok(n) = int.parse(v) n }"/utf8>>;
{some, {inline, {number_schema, _, _, _, _, _, _, _}}} ->
<<<<"{ let assert Ok(v) = dict.get(headers, \""/utf8, Key/binary>>/binary,
"\") let assert Ok(n) = float.parse(v) n }"/utf8>>;
{some, {inline, {boolean_schema, _}}} ->
<<<<<<<<"{ let assert Ok(v) = dict.get(headers, \""/utf8,
Key/binary>>/binary,
"\") "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" }"/utf8>>;
_ ->
<<<<"{ let assert Ok(v) = dict.get(headers, \""/utf8, Key/binary>>/binary,
"\") v }"/utf8>>
end.
-file("src/oaspec/codegen/server.gleam", 2002).
?DOC(" Generate expression for an optional header parameter.\n").
-spec header_optional_expr(
binary(),
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage())
) -> binary().
header_optional_expr(Key, Param) ->
case erlang:element(6, Param) of
{some,
{inline,
{array_schema,
_,
{inline, {string_schema, _, _, _, _, _, _}},
_,
_,
_}}} ->
<<<<"case dict.get(headers, \""/utf8, Key/binary>>/binary,
"\") { Ok(v) -> Some(list.map(string.split(v, \",\"), fn(item) { string.trim(item) })) _ -> None }"/utf8>>;
{some,
{inline,
{array_schema,
_,
{inline, {integer_schema, _, _, _, _, _, _, _}},
_,
_,
_}}} ->
<<<<"case dict.get(headers, \""/utf8, Key/binary>>/binary,
"\") { Ok(v) -> Some(list.map(string.split(v, \",\"), fn(item) { let trimmed = string.trim(item) let assert Ok(n) = int.parse(trimmed) n })) _ -> None }"/utf8>>;
{some,
{inline,
{array_schema,
_,
{inline, {number_schema, _, _, _, _, _, _, _}},
_,
_,
_}}} ->
<<<<"case dict.get(headers, \""/utf8, Key/binary>>/binary,
"\") { Ok(v) -> Some(list.map(string.split(v, \",\"), fn(item) { let trimmed = string.trim(item) let assert Ok(n) = float.parse(trimmed) n })) _ -> None }"/utf8>>;
{some,
{inline, {array_schema, _, {inline, {boolean_schema, _}}, _, _, _}}} ->
<<<<<<<<"case dict.get(headers, \""/utf8, Key/binary>>/binary,
"\") { Ok(v) -> Some(list.map(string.split(v, \",\"), fn(item) { let v = string.trim(item) "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" })) _ -> None }"/utf8>>;
{some, {inline, {integer_schema, _, _, _, _, _, _, _}}} ->
<<<<"case dict.get(headers, \""/utf8, Key/binary>>/binary,
"\") { Ok(v) -> { case int.parse(v) { Ok(n) -> Some(n) _ -> None } } _ -> None }"/utf8>>;
{some, {inline, {number_schema, _, _, _, _, _, _, _}}} ->
<<<<"case dict.get(headers, \""/utf8, Key/binary>>/binary,
"\") { Ok(v) -> { case float.parse(v) { Ok(n) -> Some(n) _ -> None } } _ -> None }"/utf8>>;
{some, {inline, {boolean_schema, _}}} ->
<<<<<<<<"case dict.get(headers, \""/utf8, Key/binary>>/binary,
"\") { Ok(v) -> Some("/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
") _ -> None }"/utf8>>;
_ ->
<<<<"case dict.get(headers, \""/utf8, Key/binary>>/binary,
"\") { Ok(v) -> Some(v) _ -> None }"/utf8>>
end.
-file("src/oaspec/codegen/server.gleam", 2042).
?DOC(" Generate expression for a required cookie parameter.\n").
-spec cookie_required_expr(
binary(),
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage())
) -> binary().
cookie_required_expr(Key, Param) ->
case erlang:element(6, Param) of
{some, {inline, {integer_schema, _, _, _, _, _, _, _}}} ->
<<<<"{ let assert Ok(v) = cookie_lookup(headers, \""/utf8,
Key/binary>>/binary,
"\") let assert Ok(n) = int.parse(v) n }"/utf8>>;
{some, {inline, {number_schema, _, _, _, _, _, _, _}}} ->
<<<<"{ let assert Ok(v) = cookie_lookup(headers, \""/utf8,
Key/binary>>/binary,
"\") let assert Ok(n) = float.parse(v) n }"/utf8>>;
{some, {inline, {boolean_schema, _}}} ->
<<<<<<<<"{ let assert Ok(v) = cookie_lookup(headers, \""/utf8,
Key/binary>>/binary,
"\") "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" }"/utf8>>;
_ ->
<<<<"{ let assert Ok(v) = cookie_lookup(headers, \""/utf8,
Key/binary>>/binary,
"\") v }"/utf8>>
end.
-file("src/oaspec/codegen/server.gleam", 2063).
?DOC(" Generate expression for an optional cookie parameter.\n").
-spec cookie_optional_expr(
binary(),
oaspec@openapi@spec:parameter(oaspec@openapi@spec:spec_stage())
) -> binary().
cookie_optional_expr(Key, Param) ->
case erlang:element(6, Param) of
{some, {inline, {integer_schema, _, _, _, _, _, _, _}}} ->
<<<<"case cookie_lookup(headers, \""/utf8, Key/binary>>/binary,
"\") { Ok(v) -> { case int.parse(v) { Ok(n) -> Some(n) _ -> None } } Error(_) -> None }"/utf8>>;
{some, {inline, {number_schema, _, _, _, _, _, _, _}}} ->
<<<<"case cookie_lookup(headers, \""/utf8, Key/binary>>/binary,
"\") { Ok(v) -> { case float.parse(v) { Ok(n) -> Some(n) _ -> None } } Error(_) -> None }"/utf8>>;
{some, {inline, {boolean_schema, _}}} ->
<<<<<<<<"case cookie_lookup(headers, \""/utf8, Key/binary>>/binary,
"\") { Ok(v) -> Some("/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
") Error(_) -> None }"/utf8>>;
_ ->
<<<<"case cookie_lookup(headers, \""/utf8, Key/binary>>/binary,
"\") { Ok(v) -> Some(v) Error(_) -> None }"/utf8>>
end.
-file("src/oaspec/codegen/server.gleam", 1499).
-spec form_urlencoded_object_required_expr(
binary(),
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context(),
integer()
) -> binary().
form_urlencoded_object_required_expr(Prefix, Schema_ref, Ctx, Nesting_depth) ->
form_urlencoded_object_constructor_expr(
form_urlencoded_schema_ref_type_name(Schema_ref),
Prefix,
object_properties_from_schema_ref(Schema_ref, Ctx),
Ctx,
Nesting_depth
).
-file("src/oaspec/codegen/server.gleam", 1459).
-spec form_urlencoded_object_constructor_expr(
binary(),
binary(),
list(deep_object_property()),
oaspec@codegen@context:context(),
integer()
) -> binary().
form_urlencoded_object_constructor_expr(
Type_name,
Prefix,
Properties,
Ctx,
Nesting_depth
) ->
Fields = begin
_pipe = Properties,
_pipe@1 = gleam@list:map(
_pipe,
fun(Prop) ->
Key = form_urlencoded_key(Prefix, erlang:element(2, Prop)),
Value_expr = case {(Nesting_depth < 5) andalso schema_ref_resolves_to_object(
erlang:element(4, Prop),
Ctx
),
erlang:element(5, Prop)} of
{true, true} ->
form_urlencoded_object_required_expr(
Key,
erlang:element(4, Prop),
Ctx,
Nesting_depth + 1
);
{true, false} ->
form_urlencoded_object_optional_expr(
Key,
erlang:element(4, Prop),
Ctx,
Nesting_depth + 1
);
{false, true} ->
form_body_required_expr_with_schema(
Key,
{some, erlang:element(4, Prop)},
Ctx
);
{false, false} ->
form_body_optional_expr_with_schema(
Key,
{some, erlang:element(4, Prop)},
Ctx
)
end,
<<<<(erlang:element(3, Prop))/binary, ": "/utf8>>/binary,
Value_expr/binary>>
end
),
gleam@string:join(_pipe@1, <<", "/utf8>>)
end,
<<<<<<Type_name/binary, "("/utf8>>/binary, Fields/binary>>/binary,
")"/utf8>>.
-file("src/oaspec/codegen/server.gleam", 1514).
-spec form_urlencoded_object_optional_expr(
binary(),
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context(),
integer()
) -> binary().
form_urlencoded_object_optional_expr(Prefix, Schema_ref, Ctx, Nesting_depth) ->
Props = object_properties_from_schema_ref(Schema_ref, Ctx),
Prop_names = begin
_pipe = Props,
_pipe@1 = gleam@list:map(
_pipe,
fun(Prop) ->
<<<<"\""/utf8, (erlang:element(2, Prop))/binary>>/binary,
"\""/utf8>>
end
),
gleam@string:join(_pipe@1, <<", "/utf8>>)
end,
<<<<<<<<<<<<"case form_object_present(form_body, \""/utf8, Prefix/binary>>/binary,
"\", ["/utf8>>/binary,
Prop_names/binary>>/binary,
"]) { True -> Some("/utf8>>/binary,
(form_urlencoded_object_constructor_expr(
form_urlencoded_schema_ref_type_name(Schema_ref),
Prefix,
Props,
Ctx,
Nesting_depth
))/binary>>/binary,
") False -> None }"/utf8>>.
-file("src/oaspec/codegen/server.gleam", 1540).
-spec form_urlencoded_body_constructor_expr(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
binary(),
oaspec@codegen@context:context()
) -> binary().
form_urlencoded_body_constructor_expr(Rb, Op_id, Ctx) ->
form_urlencoded_object_constructor_expr(
form_urlencoded_body_type_name(Rb, Op_id),
<<""/utf8>>,
form_urlencoded_body_properties(Rb, Ctx),
Ctx,
0
).
-file("src/oaspec/codegen/server.gleam", 1123).
-spec body_field_kind_from_object(
oaspec@openapi@schema:schema_object(),
oaspec@codegen@context:context()
) -> body_field_kind().
body_field_kind_from_object(Schema_obj, Ctx) ->
case Schema_obj of
{string_schema, _, _, _, _, _, _} ->
body_field_string;
{integer_schema, _, _, _, _, _, _, _} ->
body_field_int;
{number_schema, _, _, _, _, _, _, _} ->
body_field_float;
{boolean_schema, _} ->
body_field_bool;
{array_schema, _, Items, _, _, _} ->
case body_field_kind(Items, Ctx) of
body_field_string ->
body_field_string_array;
body_field_int ->
body_field_int_array;
body_field_float ->
body_field_float_array;
body_field_bool ->
body_field_bool_array;
_ ->
body_field_unknown
end;
_ ->
body_field_unknown
end.
-file("src/oaspec/codegen/server.gleam", 1100).
-spec body_field_kind(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> body_field_kind().
body_field_kind(Schema_ref, Ctx) ->
case Schema_ref of
{inline, {string_schema, _, _, _, _, _, _}} ->
body_field_string;
{inline, {integer_schema, _, _, _, _, _, _, _}} ->
body_field_int;
{inline, {number_schema, _, _, _, _, _, _, _}} ->
body_field_float;
{inline, {boolean_schema, _}} ->
body_field_bool;
{inline, {array_schema, _, Items, _, _, _}} ->
case body_field_kind(Items, Ctx) of
body_field_string ->
body_field_string_array;
body_field_int ->
body_field_int_array;
body_field_float ->
body_field_float_array;
body_field_bool ->
body_field_bool_array;
_ ->
body_field_unknown
end;
{reference, _, _} = Schema_ref@1 ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref@1,
erlang:element(2, Ctx)
) of
{ok, Schema_obj} ->
body_field_kind_from_object(Schema_obj, Ctx);
{error, _} ->
body_field_unknown
end;
_ ->
body_field_unknown
end.
-file("src/oaspec/codegen/server.gleam", 1090).
-spec schema_ref_body_field_kind(
gleam@option:option(oaspec@openapi@schema:schema_ref()),
oaspec@codegen@context:context()
) -> body_field_kind().
schema_ref_body_field_kind(Schema_ref, Ctx) ->
case Schema_ref of
{some, Schema_ref@1} ->
body_field_kind(Schema_ref@1, Ctx);
none ->
body_field_unknown
end.
-file("src/oaspec/codegen/server.gleam", 1642).
-spec multipart_body_needs_int(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> boolean().
multipart_body_needs_int(Rb, Ctx) ->
gleam@list:any(
multipart_body_properties(Rb, Ctx),
fun(Prop) ->
body_field_kind_needs_int(
schema_ref_body_field_kind({some, erlang:element(4, Prop)}, Ctx)
)
end
).
-file("src/oaspec/codegen/server.gleam", 1654).
-spec multipart_body_needs_float(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> boolean().
multipart_body_needs_float(Rb, Ctx) ->
gleam@list:any(
multipart_body_properties(Rb, Ctx),
fun(Prop) ->
body_field_kind_needs_float(
schema_ref_body_field_kind({some, erlang:element(4, Prop)}, Ctx)
)
end
).
-file("src/oaspec/codegen/server.gleam", 1710).
-spec form_urlencoded_properties_need_int(
list(deep_object_property()),
oaspec@codegen@context:context(),
boolean()
) -> boolean().
form_urlencoded_properties_need_int(Props, Ctx, Allow_nested_objects) ->
gleam@list:any(
Props,
fun(Prop) ->
body_field_kind_needs_int(
schema_ref_body_field_kind({some, erlang:element(4, Prop)}, Ctx)
)
orelse case Allow_nested_objects andalso schema_ref_resolves_to_object(
erlang:element(4, Prop),
Ctx
) of
true ->
form_urlencoded_properties_need_int(
object_properties_from_schema_ref(
erlang:element(4, Prop),
Ctx
),
Ctx,
false
);
false ->
false
end
end
).
-file("src/oaspec/codegen/server.gleam", 1576).
-spec form_urlencoded_body_needs_int(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> boolean().
form_urlencoded_body_needs_int(Rb, Ctx) ->
form_urlencoded_properties_need_int(
form_urlencoded_body_properties(Rb, Ctx),
Ctx,
true
).
-file("src/oaspec/codegen/server.gleam", 1735).
-spec form_urlencoded_properties_need_float(
list(deep_object_property()),
oaspec@codegen@context:context(),
boolean()
) -> boolean().
form_urlencoded_properties_need_float(Props, Ctx, Allow_nested_objects) ->
gleam@list:any(
Props,
fun(Prop) ->
body_field_kind_needs_float(
schema_ref_body_field_kind({some, erlang:element(4, Prop)}, Ctx)
)
orelse case Allow_nested_objects andalso schema_ref_resolves_to_object(
erlang:element(4, Prop),
Ctx
) of
true ->
form_urlencoded_properties_need_float(
object_properties_from_schema_ref(
erlang:element(4, Prop),
Ctx
),
Ctx,
false
);
false ->
false
end
end
).
-file("src/oaspec/codegen/server.gleam", 1587).
-spec form_urlencoded_body_needs_float(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
oaspec@codegen@context:context()
) -> boolean().
form_urlencoded_body_needs_float(Rb, Ctx) ->
form_urlencoded_properties_need_float(
form_urlencoded_body_properties(Rb, Ctx),
Ctx,
true
).
-file("src/oaspec/codegen/server.gleam", 1786).
-spec form_body_required_expr_with_schema(
binary(),
gleam@option:option(oaspec@openapi@schema:schema_ref()),
oaspec@codegen@context:context()
) -> binary().
form_body_required_expr_with_schema(Key, Schema_ref, Ctx) ->
Base = <<<<"{ let assert Ok([v, ..]) = dict.get(form_body, \""/utf8,
Key/binary>>/binary,
"\") v }"/utf8>>,
case schema_ref_body_field_kind(Schema_ref, Ctx) of
body_field_string_array ->
<<<<"{ let assert Ok(vs) = dict.get(form_body, \""/utf8,
Key/binary>>/binary,
"\") list.map(vs, fn(item) { string.trim(item) }) }"/utf8>>;
body_field_int_array ->
<<<<"{ let assert Ok(vs) = dict.get(form_body, \""/utf8,
Key/binary>>/binary,
"\") list.map(vs, fn(item) { let trimmed = string.trim(item) let assert Ok(n) = int.parse(trimmed) n }) }"/utf8>>;
body_field_float_array ->
<<<<"{ let assert Ok(vs) = dict.get(form_body, \""/utf8,
Key/binary>>/binary,
"\") list.map(vs, fn(item) { let trimmed = string.trim(item) let assert Ok(n) = float.parse(trimmed) n }) }"/utf8>>;
body_field_bool_array ->
<<<<<<<<"{ let assert Ok(vs) = dict.get(form_body, \""/utf8,
Key/binary>>/binary,
"\") list.map(vs, fn(item) { let v = string.trim(item) "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" }) }"/utf8>>;
body_field_int ->
<<<<"{ let assert Ok([v, ..]) = dict.get(form_body, \""/utf8,
Key/binary>>/binary,
"\") let assert Ok(n) = int.parse(v) n }"/utf8>>;
body_field_float ->
<<<<"{ let assert Ok([v, ..]) = dict.get(form_body, \""/utf8,
Key/binary>>/binary,
"\") let assert Ok(n) = float.parse(v) n }"/utf8>>;
body_field_bool ->
<<<<<<<<"{ let assert Ok([v, ..]) = dict.get(form_body, \""/utf8,
Key/binary>>/binary,
"\") "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" }"/utf8>>;
_ ->
Base
end.
-file("src/oaspec/codegen/server.gleam", 1830).
-spec form_body_optional_expr_with_schema(
binary(),
gleam@option:option(oaspec@openapi@schema:schema_ref()),
oaspec@codegen@context:context()
) -> binary().
form_body_optional_expr_with_schema(Key, Schema_ref, Ctx) ->
case schema_ref_body_field_kind(Schema_ref, Ctx) of
body_field_string_array ->
<<<<"case dict.get(form_body, \""/utf8, Key/binary>>/binary,
"\") { Ok(vs) -> Some(list.map(vs, fn(item) { string.trim(item) })) _ -> None }"/utf8>>;
body_field_int_array ->
<<<<"case dict.get(form_body, \""/utf8, Key/binary>>/binary,
"\") { Ok(vs) -> Some(list.map(vs, fn(item) { let trimmed = string.trim(item) let assert Ok(n) = int.parse(trimmed) n })) _ -> None }"/utf8>>;
body_field_float_array ->
<<<<"case dict.get(form_body, \""/utf8, Key/binary>>/binary,
"\") { Ok(vs) -> Some(list.map(vs, fn(item) { let trimmed = string.trim(item) let assert Ok(n) = float.parse(trimmed) n })) _ -> None }"/utf8>>;
body_field_bool_array ->
<<<<<<<<"case dict.get(form_body, \""/utf8, Key/binary>>/binary,
"\") { Ok(vs) -> Some(list.map(vs, fn(item) { let v = string.trim(item) "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" })) _ -> None }"/utf8>>;
body_field_int ->
<<<<"case dict.get(form_body, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> { case int.parse(v) { Ok(n) -> Some(n) _ -> None } } _ -> None }"/utf8>>;
body_field_float ->
<<<<"case dict.get(form_body, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> { case float.parse(v) { Ok(n) -> Some(n) _ -> None } } _ -> None }"/utf8>>;
body_field_bool ->
<<<<<<<<"case dict.get(form_body, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> Some("/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
") _ -> None }"/utf8>>;
_ ->
<<<<"case dict.get(form_body, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> Some(v) _ -> None }"/utf8>>
end.
-file("src/oaspec/codegen/server.gleam", 1875).
-spec multipart_body_required_expr_with_schema(
binary(),
gleam@option:option(oaspec@openapi@schema:schema_ref()),
oaspec@codegen@context:context()
) -> binary().
multipart_body_required_expr_with_schema(Key, Schema_ref, Ctx) ->
Base = <<<<"{ let assert Ok([v, ..]) = dict.get(multipart_body, \""/utf8,
Key/binary>>/binary,
"\") v }"/utf8>>,
case schema_ref_body_field_kind(Schema_ref, Ctx) of
body_field_int ->
<<<<"{ let assert Ok([v, ..]) = dict.get(multipart_body, \""/utf8,
Key/binary>>/binary,
"\") let assert Ok(n) = int.parse(v) n }"/utf8>>;
body_field_float ->
<<<<"{ let assert Ok([v, ..]) = dict.get(multipart_body, \""/utf8,
Key/binary>>/binary,
"\") let assert Ok(n) = float.parse(v) n }"/utf8>>;
body_field_bool ->
<<<<<<<<"{ let assert Ok([v, ..]) = dict.get(multipart_body, \""/utf8,
Key/binary>>/binary,
"\") "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" }"/utf8>>;
body_field_string_array ->
<<<<"{ let assert Ok(vs) = dict.get(multipart_body, \""/utf8,
Key/binary>>/binary,
"\") vs }"/utf8>>;
body_field_int_array ->
<<<<"{ let assert Ok(vs) = dict.get(multipart_body, \""/utf8,
Key/binary>>/binary,
"\") list.map(vs, fn(item) { let assert Ok(n) = int.parse(item) n }) }"/utf8>>;
body_field_float_array ->
<<<<"{ let assert Ok(vs) = dict.get(multipart_body, \""/utf8,
Key/binary>>/binary,
"\") list.map(vs, fn(item) { let assert Ok(n) = float.parse(item) n }) }"/utf8>>;
body_field_bool_array ->
<<<<<<<<"{ let assert Ok(vs) = dict.get(multipart_body, \""/utf8,
Key/binary>>/binary,
"\") list.map(vs, fn(item) { let v = item "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" }) }"/utf8>>;
_ ->
Base
end.
-file("src/oaspec/codegen/server.gleam", 1917).
-spec multipart_body_optional_expr_with_schema(
binary(),
gleam@option:option(oaspec@openapi@schema:schema_ref()),
oaspec@codegen@context:context()
) -> binary().
multipart_body_optional_expr_with_schema(Key, Schema_ref, Ctx) ->
case schema_ref_body_field_kind(Schema_ref, Ctx) of
body_field_int ->
<<<<"case dict.get(multipart_body, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> { case int.parse(v) { Ok(n) -> Some(n) _ -> None } } _ -> None }"/utf8>>;
body_field_float ->
<<<<"case dict.get(multipart_body, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> { case float.parse(v) { Ok(n) -> Some(n) _ -> None } } _ -> None }"/utf8>>;
body_field_bool ->
<<<<<<<<"case dict.get(multipart_body, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> Some("/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
") _ -> None }"/utf8>>;
body_field_string_array ->
<<<<"case dict.get(multipart_body, \""/utf8, Key/binary>>/binary,
"\") { Ok(vs) -> Some(vs) _ -> None }"/utf8>>;
body_field_int_array ->
<<<<"case dict.get(multipart_body, \""/utf8, Key/binary>>/binary,
"\") { Ok(vs) -> Some(list.map(vs, fn(item) { let assert Ok(n) = int.parse(item) n })) _ -> None }"/utf8>>;
body_field_float_array ->
<<<<"case dict.get(multipart_body, \""/utf8, Key/binary>>/binary,
"\") { Ok(vs) -> Some(list.map(vs, fn(item) { let assert Ok(n) = float.parse(item) n })) _ -> None }"/utf8>>;
body_field_bool_array ->
<<<<<<<<"case dict.get(multipart_body, \""/utf8, Key/binary>>/binary,
"\") { Ok(vs) -> Some(list.map(vs, fn(item) { let v = item "/utf8>>/binary,
"case string.lowercase(v) { \"true\" -> True _ -> False }"/utf8>>/binary,
" })) _ -> None }"/utf8>>;
_ ->
<<<<"case dict.get(multipart_body, \""/utf8, Key/binary>>/binary,
"\") { Ok([v, ..]) -> Some(v) _ -> None }"/utf8>>
end.
-file("src/oaspec/codegen/server.gleam", 1607).
-spec multipart_body_constructor_expr(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
binary(),
oaspec@codegen@context:context()
) -> binary().
multipart_body_constructor_expr(Rb, Op_id, Ctx) ->
Fields = begin
_pipe = multipart_body_properties(Rb, Ctx),
_pipe@1 = gleam@list:map(
_pipe,
fun(Prop) ->
Value_expr = case erlang:element(5, Prop) of
true ->
multipart_body_required_expr_with_schema(
erlang:element(2, Prop),
{some, erlang:element(4, Prop)},
Ctx
);
false ->
multipart_body_optional_expr_with_schema(
erlang:element(2, Prop),
{some, erlang:element(4, Prop)},
Ctx
)
end,
<<<<(erlang:element(3, Prop))/binary, ": "/utf8>>/binary,
Value_expr/binary>>
end
),
gleam@string:join(_pipe@1, <<", "/utf8>>)
end,
<<<<<<(multipart_body_type_name(Rb, Op_id))/binary, "("/utf8>>/binary,
Fields/binary>>/binary,
")"/utf8>>.
-file("src/oaspec/codegen/server.gleam", 2087).
?DOC(" Generate the body decode expression for a request body.\n").
-spec generate_body_decode_expr(
oaspec@openapi@spec:request_body(oaspec@openapi@spec:spec_stage()),
binary(),
oaspec@codegen@context:context()
) -> binary().
generate_body_decode_expr(Rb, Op_id, Ctx) ->
Content_entries = maps:to_list(erlang:element(3, Rb)),
case Content_entries of
[{<<"application/json"/utf8>>, Media_type}] ->
Decode_fn = case erlang:element(2, Media_type) of
{some, {reference, _, Name}} ->
<<<<"decode.decode_"/utf8,
(oaspec@util@naming:to_snake_case(Name))/binary>>/binary,
"(body)"/utf8>>;
_ ->
<<<<"decode.decode_"/utf8,
(oaspec@util@naming:to_snake_case(Op_id))/binary>>/binary,
"_request_body(body)"/utf8>>
end,
case erlang:element(4, Rb) of
true ->
<<<<"{ let assert Ok(decoded) = "/utf8, Decode_fn/binary>>/binary,
" decoded }"/utf8>>;
false ->
<<<<"case body { \"\" -> None _ -> { case "/utf8,
Decode_fn/binary>>/binary,
" { Ok(decoded) -> Some(decoded) _ -> None } } }"/utf8>>
end;
[{<<"application/x-www-form-urlencoded"/utf8>>, _}] ->
Body_expr = form_urlencoded_body_constructor_expr(Rb, Op_id, Ctx),
case erlang:element(4, Rb) of
true ->
Body_expr;
false ->
<<<<"case body { \"\" -> None _ -> Some("/utf8,
Body_expr/binary>>/binary,
") }"/utf8>>
end;
[{<<"multipart/form-data"/utf8>>, _}] ->
Body_expr@1 = multipart_body_constructor_expr(Rb, Op_id, Ctx),
case erlang:element(4, Rb) of
true ->
Body_expr@1;
false ->
<<<<"case body { \"\" -> None _ -> Some("/utf8,
Body_expr@1/binary>>/binary,
") }"/utf8>>
end;
_ ->
case erlang:element(4, Rb) of
true ->
<<"body"/utf8>>;
false ->
<<"case body { \"\" -> None _ -> Some(body) }"/utf8>>
end
end.
-file("src/oaspec/codegen/server.gleam", 801).
?DOC(" Generate code to construct a typed request from raw inputs.\n").
-spec generate_request_construction(
gleam@string_tree:string_tree(),
binary(),
binary(),
oaspec@openapi@spec:operation(oaspec@openapi@spec:spec_stage()),
binary(),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_request_construction(Sb, Request_type_name, Op_id, Operation, _, Ctx) ->
Sb@1 = case erlang:element(7, Operation) of
{some, {value, Rb}} ->
case request_body_uses_form_urlencoded(Rb) of
true ->
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
3,
<<"let form_body = parse_form_body(body)"/utf8>>
);
false ->
case request_body_uses_multipart(Rb) of
true ->
_pipe@1 = Sb,
oaspec@util@string_extra:indent(
_pipe@1,
3,
<<"let multipart_body = parse_multipart_body(body, headers)"/utf8>>
);
false ->
Sb
end
end;
_ ->
Sb
end,
Sb@2 = begin
_pipe@2 = Sb@1,
oaspec@util@string_extra:indent(
_pipe@2,
3,
<<<<"let request = request_types."/utf8, Request_type_name/binary>>/binary,
"("/utf8>>
)
end,
Sb@4 = gleam@list:index_fold(
gleam@list:filter_map(erlang:element(6, Operation), fun(R) -> case R of
{value, P} ->
{ok, P};
_ ->
{error, nil}
end end),
Sb@2,
fun(Sb@3, Param, _) ->
Field_name = oaspec@util@naming:to_snake_case(
erlang:element(2, Param)
),
Trailing = <<","/utf8>>,
Value_expr = case erlang:element(3, Param) of
in_path ->
Var_name = oaspec@util@naming:to_snake_case(
erlang:element(2, Param)
),
param_parse_expr(Var_name, Param);
in_query ->
Key = erlang:element(2, Param),
case {is_deep_object_param(Param, Ctx),
erlang:element(5, Param)} of
{true, true} ->
deep_object_required_expr(Key, Param, Op_id, Ctx);
{true, false} ->
deep_object_optional_expr(Key, Param, Op_id, Ctx);
{false, true} ->
query_required_expr(Key, Param);
{false, false} ->
query_optional_expr(Key, Param)
end;
in_header ->
Key@1 = string:lowercase(erlang:element(2, Param)),
case erlang:element(5, Param) of
true ->
header_required_expr(Key@1, Param);
false ->
header_optional_expr(Key@1, Param)
end;
in_cookie ->
Key@2 = erlang:element(2, Param),
case erlang:element(5, Param) of
true ->
cookie_required_expr(Key@2, Param);
false ->
cookie_optional_expr(Key@2, Param)
end
end,
_pipe@3 = Sb@3,
oaspec@util@string_extra:indent(
_pipe@3,
4,
<<<<<<Field_name/binary, ": "/utf8>>/binary, Value_expr/binary>>/binary,
Trailing/binary>>
)
end
),
Sb@5 = case erlang:element(7, Operation) of
{some, {value, Rb@1}} ->
Body_expr = generate_body_decode_expr(Rb@1, Op_id, Ctx),
_pipe@4 = Sb@4,
oaspec@util@string_extra:indent(
_pipe@4,
4,
<<<<"body: "/utf8, Body_expr/binary>>/binary, ","/utf8>>
);
_ ->
Sb@4
end,
_pipe@5 = Sb@5,
oaspec@util@string_extra:indent(_pipe@5, 3, <<")"/utf8>>).
-file("src/oaspec/codegen/server.gleam", 762).
?DOC(" Generate the body of a single route case branch.\n").
-spec generate_route_body(
gleam@string_tree:string_tree(),
binary(),
binary(),
oaspec@openapi@spec:operation(oaspec@openapi@spec:spec_stage()),
binary(),
boolean(),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_route_body(Sb, Op_id, Fn_name, Operation, Path, Has_params, Ctx) ->
Type_name = oaspec@util@naming:schema_to_type_name(Op_id),
Response_type_name = <<Type_name/binary, "Response"/utf8>>,
case Has_params of
false ->
_pipe = Sb,
_pipe@1 = oaspec@util@string_extra:indent(
_pipe,
3,
<<<<"let response = handlers."/utf8, Fn_name/binary>>/binary,
"()"/utf8>>
),
generate_response_conversion(
_pipe@1,
Response_type_name,
Operation,
Ctx
);
true ->
Request_type_name = <<Type_name/binary, "Request"/utf8>>,
Sb@1 = generate_request_construction(
Sb,
Request_type_name,
Op_id,
Operation,
Path,
Ctx
),
_pipe@2 = Sb@1,
_pipe@3 = oaspec@util@string_extra:indent(
_pipe@2,
3,
<<<<"let response = handlers."/utf8, Fn_name/binary>>/binary,
"(request)"/utf8>>
),
generate_response_conversion(
_pipe@3,
Response_type_name,
Operation,
Ctx
)
end.
-file("src/oaspec/codegen/server.gleam", 168).
?DOC(" Generate a router module that dispatches requests.\n").
-spec generate_router(oaspec@codegen@context:context()) -> binary().
generate_router(Ctx) ->
Operations = oaspec@codegen@types:collect_operations(Ctx),
Has_deep_object = gleam@list:any(
Operations,
fun(Op) ->
{_, Operation, _, _} = Op,
gleam@list:any(
erlang:element(6, Operation),
fun(Ref_p) -> case Ref_p of
{value, P} ->
is_deep_object_param(P, Ctx);
_ ->
false
end end
)
end
),
Has_form_urlencoded_body = gleam@list:any(
Operations,
fun(Op@1) ->
{_, Operation@1, _, _} = Op@1,
operation_uses_form_urlencoded_body(Operation@1)
end
),
Has_multipart_body = gleam@list:any(
Operations,
fun(Op@2) ->
{_, Operation@2, _, _} = Op@2,
operation_uses_multipart_body(Operation@2)
end
),
Has_nested_form_urlencoded_body = gleam@list:any(
Operations,
fun(Op@3) ->
{_, Operation@3, _, _} = Op@3,
case erlang:element(7, Operation@3) of
{some, {value, Rb}} ->
form_urlencoded_body_has_nested_object(Rb, Ctx);
_ ->
false
end
end
),
Needs_dict = gleam@list:any(
Operations,
fun(Op@4) ->
{_, Operation@4, _, _} = Op@4,
Has_query = gleam@list:any(
erlang:element(6, Operation@4),
fun(Ref_p@1) -> case Ref_p@1 of
{value, P@1} ->
erlang:element(3, P@1) =:= in_query;
_ ->
false
end end
),
Has_header = gleam@list:any(
erlang:element(6, Operation@4),
fun(Ref_p@2) -> case Ref_p@2 of
{value, P@2} ->
erlang:element(3, P@2) =:= in_header;
_ ->
false
end end
),
Has_query orelse Has_header
end
),
Needs_int = gleam@list:any(
Operations,
fun(Op@5) ->
{_, Operation@5, _, _} = Op@5,
(gleam@list:any(
erlang:element(6, Operation@5),
fun(Ref_p@3) -> case Ref_p@3 of
{value, P@3} ->
query_schema_needs_int(erlang:element(6, P@3))
orelse deep_object_param_needs_int(P@3, Ctx);
_ ->
false
end end
)
orelse case erlang:element(7, Operation@5) of
{some, {value, Rb@1}} ->
form_urlencoded_body_needs_int(Rb@1, Ctx);
_ ->
false
end)
orelse case erlang:element(7, Operation@5) of
{some, {value, Rb@2}} ->
multipart_body_needs_int(Rb@2, Ctx);
_ ->
false
end
end
),
Needs_float = gleam@list:any(
Operations,
fun(Op@6) ->
{_, Operation@6, _, _} = Op@6,
(gleam@list:any(
erlang:element(6, Operation@6),
fun(Ref_p@4) -> case Ref_p@4 of
{value, P@4} ->
query_schema_needs_float(erlang:element(6, P@4))
orelse deep_object_param_needs_float(P@4, Ctx);
_ ->
false
end end
)
orelse case erlang:element(7, Operation@6) of
{some, {value, Rb@3}} ->
form_urlencoded_body_needs_float(Rb@3, Ctx);
_ ->
false
end)
orelse case erlang:element(7, Operation@6) of
{some, {value, Rb@4}} ->
multipart_body_needs_float(Rb@4, Ctx);
_ ->
false
end
end
),
Needs_string = (Has_form_urlencoded_body orelse Has_multipart_body) orelse gleam@list:any(
Operations,
fun(Op@7) ->
{_, Operation@7, _, _} = Op@7,
gleam@list:any(
erlang:element(6, Operation@7),
fun(Ref_p@5) -> case Ref_p@5 of
{value, P@5} ->
case erlang:element(3, P@5) of
in_cookie ->
true;
in_query ->
query_schema_needs_string(
erlang:element(6, P@5)
)
orelse deep_object_param_needs_string(
P@5,
Ctx
);
in_header ->
query_schema_needs_string(
erlang:element(6, P@5)
)
orelse deep_object_param_needs_string(
P@5,
Ctx
);
in_path ->
query_schema_needs_string(
erlang:element(6, P@5)
)
end;
_ ->
false
end end
)
orelse case erlang:element(7, Operation@7) of
{some, {value, Rb@5}} ->
form_urlencoded_body_needs_string(Rb@5, Ctx);
_ ->
false
end
end
),
Needs_cookie_lookup = gleam@list:any(
Operations,
fun(Op@8) ->
{_, Operation@8, _, _} = Op@8,
gleam@list:any(
erlang:element(6, Operation@8),
fun(Ref_p@6) -> case Ref_p@6 of
{value, P@6} ->
erlang:element(3, P@6) =:= in_cookie;
_ ->
false
end end
)
end
),
Needs_list_import = (((Needs_cookie_lookup orelse Has_deep_object) orelse Has_form_urlencoded_body)
orelse Has_multipart_body)
orelse gleam@list:any(
Operations,
fun(Op@9) ->
{_, Operation@9, _, _} = Op@9,
gleam@list:any(
erlang:element(6, Operation@9),
fun(Ref_p@7) -> case Ref_p@7 of
{value, P@7} ->
case {erlang:element(3, P@7),
erlang:element(6, P@7)} of
{in_query,
{some,
{inline, {array_schema, _, _, _, _, _}}}} ->
true;
{in_header,
{some,
{inline, {array_schema, _, _, _, _, _}}}} ->
true;
{_, _} ->
false
end;
_ ->
false
end end
)
end
),
Needs_uri_import = Needs_cookie_lookup orelse Has_form_urlencoded_body,
Needs_option = gleam@list:any(
Operations,
fun(Op@10) ->
{_, Operation@10, _, _} = Op@10,
Has_optional_params = gleam@list:any(
erlang:element(6, Operation@10),
fun(Ref_p@8) -> case Ref_p@8 of
{value, P@8} ->
not erlang:element(5, P@8);
_ ->
false
end end
),
Has_optional_deep_object_fields = gleam@list:any(
erlang:element(6, Operation@10),
fun(Ref_p@9) -> case Ref_p@9 of
{value, P@9} ->
deep_object_param_has_optional_fields(P@9, Ctx);
_ ->
false
end end
),
Has_optional_form_urlencoded_fields = case erlang:element(
7,
Operation@10
) of
{some, {value, Rb@6}} ->
form_urlencoded_body_has_optional_fields(Rb@6, Ctx);
_ ->
false
end,
Has_optional_multipart_fields = case erlang:element(7, Operation@10) of
{some, {value, Rb@7}} ->
multipart_body_has_optional_fields(Rb@7, Ctx);
_ ->
false
end,
Has_optional_body = case erlang:element(7, Operation@10) of
{some, {value, Rb@8}} ->
not erlang:element(4, Rb@8);
_ ->
false
end,
(((Has_optional_params orelse Has_optional_deep_object_fields)
orelse Has_optional_form_urlencoded_fields)
orelse Has_optional_multipart_fields)
orelse Has_optional_body
end
),
Needs_json = gleam@list:any(
Operations,
fun(Op@11) ->
{_, Operation@11, _, _} = Op@11,
Responses = maps:to_list(erlang:element(8, Operation@11)),
gleam@list:any(
Responses,
fun(Entry) ->
{_, Ref_or} = Entry,
case Ref_or of
{value, Response} ->
Content_entries = maps:to_list(
erlang:element(3, Response)
),
case Content_entries of
[{Media_type_name, Media_type}] ->
case Media_type_name of
<<"application/json"/utf8>> ->
case erlang:element(2, Media_type) of
{some, _} ->
true;
none ->
false
end;
_ ->
false
end;
_ ->
false
end;
_ ->
false
end
end
)
end
),
Needs_decode = Needs_json andalso gleam@list:any(
Operations,
fun(Op@12) ->
{_, Operation@12, _, _} = Op@12,
gleam@option:is_some(erlang:element(7, Operation@12))
end
),
Needs_encode = Needs_json,
Uses_query = gleam@list:any(
Operations,
fun(Op@13) ->
{_, Operation@13, _, _} = Op@13,
gleam@list:any(
erlang:element(6, Operation@13),
fun(Ref_p@10) -> case Ref_p@10 of
{value, P@10} ->
erlang:element(3, P@10) =:= in_query;
_ ->
false
end end
)
end
),
Uses_headers = Has_multipart_body orelse gleam@list:any(
Operations,
fun(Op@14) ->
{_, Operation@14, _, _} = Op@14,
gleam@list:any(
erlang:element(6, Operation@14),
fun(Ref_p@11) -> case Ref_p@11 of
{value, P@11} ->
(erlang:element(3, P@11) =:= in_header) orelse (erlang:element(
3,
P@11
)
=:= in_cookie);
_ ->
false
end end
)
end
),
Uses_body = gleam@list:any(
Operations,
fun(Op@15) ->
{_, Operation@15, _, _} = Op@15,
gleam@option:is_some(erlang:element(7, Operation@15))
end
),
Has_params_ops = gleam@list:any(
Operations,
fun(Op@16) ->
{_, Operation@16, _, _} = Op@16,
not gleam@list:is_empty(erlang:element(6, Operation@16)) orelse gleam@option:is_some(
erlang:element(7, Operation@16)
)
end
),
_ = Needs_dict,
Std_imports = [<<"gleam/dict.{type Dict}"/utf8>>],
Std_imports@1 = case Needs_list_import of
true ->
lists:append(Std_imports, [<<"gleam/list"/utf8>>]);
false ->
Std_imports
end,
Std_imports@2 = case Needs_uri_import of
true ->
lists:append(Std_imports@1, [<<"gleam/uri"/utf8>>]);
false ->
Std_imports@1
end,
Std_imports@3 = case Needs_int of
true ->
lists:append(Std_imports@2, [<<"gleam/int"/utf8>>]);
false ->
Std_imports@2
end,
Std_imports@4 = case Needs_float of
true ->
lists:append(Std_imports@3, [<<"gleam/float"/utf8>>]);
false ->
Std_imports@3
end,
Std_imports@5 = case Needs_option of
true ->
lists:append(Std_imports@4, [<<"gleam/option.{None, Some}"/utf8>>]);
false ->
Std_imports@4
end,
Std_imports@6 = case Needs_json of
true ->
lists:append(Std_imports@5, [<<"gleam/json"/utf8>>]);
false ->
Std_imports@5
end,
Std_imports@7 = case Needs_string of
true ->
lists:append(Std_imports@6, [<<"gleam/string"/utf8>>]);
false ->
Std_imports@6
end,
Pkg_imports = [<<(erlang:element(5, erlang:element(3, Ctx)))/binary,
"/handlers"/utf8>>],
Pkg_imports@1 = case (Has_deep_object orelse Has_form_urlencoded_body)
orelse Has_multipart_body of
true ->
lists:append(
Pkg_imports,
[<<(erlang:element(5, erlang:element(3, Ctx)))/binary,
"/types"/utf8>>]
);
false ->
Pkg_imports
end,
Pkg_imports@2 = case Needs_decode of
true ->
lists:append(
Pkg_imports@1,
[<<(erlang:element(5, erlang:element(3, Ctx)))/binary,
"/decode"/utf8>>]
);
false ->
Pkg_imports@1
end,
Pkg_imports@3 = case Needs_encode of
true ->
lists:append(
Pkg_imports@2,
[<<(erlang:element(5, erlang:element(3, Ctx)))/binary,
"/encode"/utf8>>]
);
false ->
Pkg_imports@2
end,
Pkg_imports@4 = case Has_params_ops of
true ->
lists:append(
Pkg_imports@3,
[<<(erlang:element(5, erlang:element(3, Ctx)))/binary,
"/request_types"/utf8>>,
<<(erlang:element(5, erlang:element(3, Ctx)))/binary,
"/response_types"/utf8>>]
);
false ->
lists:append(
Pkg_imports@3,
[<<(erlang:element(5, erlang:element(3, Ctx)))/binary,
"/response_types"/utf8>>]
)
end,
All_imports = lists:append(Std_imports@7, Pkg_imports@4),
Sb = begin
_pipe = oaspec@util@string_extra:file_header(<<"0.8.0"/utf8>>),
oaspec@util@string_extra:imports(_pipe, All_imports)
end,
Sb@1 = begin
_pipe@1 = Sb,
_pipe@2 = oaspec@util@string_extra:doc_comment(
_pipe@1,
<<"A server response with status code, body, and headers."/utf8>>
),
_pipe@3 = oaspec@util@string_extra:line(
_pipe@2,
<<"pub type ServerResponse {"/utf8>>
),
_pipe@4 = oaspec@util@string_extra:indent(
_pipe@3,
1,
<<"ServerResponse(status: Int, body: String, headers: List(#(String, String)))"/utf8>>
),
_pipe@5 = oaspec@util@string_extra:line(_pipe@4, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@5)
end,
Sb@2 = case Has_deep_object of
true ->
_pipe@6 = Sb@1,
_pipe@7 = oaspec@util@string_extra:line(
_pipe@6,
<<"fn deep_object_present(query: Dict(String, List(String)), prefix: String, props: List(String)) -> Bool {"/utf8>>
),
_pipe@8 = oaspec@util@string_extra:indent(
_pipe@7,
1,
<<"list.any(props, fn(prop) { dict.has_key(query, prefix <> \"[\" <> prop <> \"]\") })"/utf8>>
),
_pipe@9 = oaspec@util@string_extra:line(_pipe@8, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@9);
false ->
Sb@1
end,
Sb@3 = case Has_form_urlencoded_body of
true ->
_pipe@10 = Sb@2,
_pipe@11 = oaspec@util@string_extra:line(
_pipe@10,
<<"fn form_url_decode(value: String) -> String {"/utf8>>
),
_pipe@12 = oaspec@util@string_extra:indent(
_pipe@11,
1,
<<"let value = string.replace(value, \"+\", \" \")"/utf8>>
),
_pipe@13 = oaspec@util@string_extra:indent(
_pipe@12,
1,
<<"case uri.percent_decode(value) { Ok(decoded) -> decoded Error(_) -> value }"/utf8>>
),
_pipe@14 = oaspec@util@string_extra:line(_pipe@13, <<"}"/utf8>>),
_pipe@15 = oaspec@util@string_extra:blank_line(_pipe@14),
_pipe@16 = oaspec@util@string_extra:line(
_pipe@15,
<<"fn parse_form_body(body: String) -> Dict(String, List(String)) {"/utf8>>
),
_pipe@17 = oaspec@util@string_extra:indent(
_pipe@16,
1,
<<"let parts = case body { \"\" -> [] _ -> string.split(body, \"&\") }"/utf8>>
),
_pipe@18 = oaspec@util@string_extra:indent(
_pipe@17,
1,
<<"list.fold(parts, dict.new(), fn(acc, part) {"/utf8>>
),
_pipe@19 = oaspec@util@string_extra:indent(
_pipe@18,
2,
<<"case part {"/utf8>>
),
_pipe@20 = oaspec@util@string_extra:indent(
_pipe@19,
3,
<<"\"\" -> acc"/utf8>>
),
_pipe@21 = oaspec@util@string_extra:indent(
_pipe@20,
3,
<<"_ ->"/utf8>>
),
_pipe@22 = oaspec@util@string_extra:indent(
_pipe@21,
4,
<<"case string.split_once(part, on: \"=\") {"/utf8>>
),
_pipe@23 = oaspec@util@string_extra:indent(
_pipe@22,
5,
<<"Ok(#(raw_key, raw_value)) -> {"/utf8>>
),
_pipe@24 = oaspec@util@string_extra:indent(
_pipe@23,
6,
<<"let key = form_url_decode(raw_key)"/utf8>>
),
_pipe@25 = oaspec@util@string_extra:indent(
_pipe@24,
6,
<<"let value = form_url_decode(raw_value)"/utf8>>
),
_pipe@26 = oaspec@util@string_extra:indent(
_pipe@25,
6,
<<"case dict.get(acc, key) {"/utf8>>
),
_pipe@27 = oaspec@util@string_extra:indent(
_pipe@26,
7,
<<"Ok(existing) -> dict.insert(acc, key, list.append(existing, [value]))"/utf8>>
),
_pipe@28 = oaspec@util@string_extra:indent(
_pipe@27,
7,
<<"Error(_) -> dict.insert(acc, key, [value])"/utf8>>
),
_pipe@29 = oaspec@util@string_extra:indent(
_pipe@28,
6,
<<"}"/utf8>>
),
_pipe@30 = oaspec@util@string_extra:indent(
_pipe@29,
5,
<<"}"/utf8>>
),
_pipe@31 = oaspec@util@string_extra:indent(
_pipe@30,
5,
<<"Error(_) -> {"/utf8>>
),
_pipe@32 = oaspec@util@string_extra:indent(
_pipe@31,
6,
<<"let key = form_url_decode(part)"/utf8>>
),
_pipe@33 = oaspec@util@string_extra:indent(
_pipe@32,
6,
<<"case dict.get(acc, key) {"/utf8>>
),
_pipe@34 = oaspec@util@string_extra:indent(
_pipe@33,
7,
<<"Ok(existing) -> dict.insert(acc, key, list.append(existing, [\"\"]))"/utf8>>
),
_pipe@35 = oaspec@util@string_extra:indent(
_pipe@34,
7,
<<"Error(_) -> dict.insert(acc, key, [\"\"])"/utf8>>
),
_pipe@36 = oaspec@util@string_extra:indent(
_pipe@35,
6,
<<"}"/utf8>>
),
_pipe@37 = oaspec@util@string_extra:indent(
_pipe@36,
5,
<<"}"/utf8>>
),
_pipe@38 = oaspec@util@string_extra:indent(
_pipe@37,
4,
<<"}"/utf8>>
),
_pipe@39 = oaspec@util@string_extra:indent(
_pipe@38,
2,
<<"}"/utf8>>
),
_pipe@40 = oaspec@util@string_extra:indent(
_pipe@39,
1,
<<"})"/utf8>>
),
_pipe@41 = oaspec@util@string_extra:line(_pipe@40, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@41);
false ->
Sb@2
end,
Sb@4 = case Has_multipart_body of
true ->
_pipe@42 = Sb@3,
_pipe@43 = oaspec@util@string_extra:line(
_pipe@42,
<<"fn multipart_boundary(headers: Dict(String, String)) -> Result(String, Nil) {"/utf8>>
),
_pipe@44 = oaspec@util@string_extra:indent(
_pipe@43,
1,
<<"case dict.get(headers, \"content-type\") {"/utf8>>
),
_pipe@45 = oaspec@util@string_extra:indent(
_pipe@44,
2,
<<"Ok(content_type) ->"/utf8>>
),
_pipe@46 = oaspec@util@string_extra:indent(
_pipe@45,
3,
<<"list.find_map(string.split(content_type, \";\"), fn(part) {"/utf8>>
),
_pipe@47 = oaspec@util@string_extra:indent(
_pipe@46,
4,
<<"let trimmed = string.trim(part)"/utf8>>
),
_pipe@48 = oaspec@util@string_extra:indent(
_pipe@47,
4,
<<"case string.starts_with(trimmed, \"boundary=\") {"/utf8>>
),
_pipe@49 = oaspec@util@string_extra:indent(
_pipe@48,
5,
<<"True -> Ok(string.replace(trimmed, \"boundary=\", \"\"))"/utf8>>
),
_pipe@50 = oaspec@util@string_extra:indent(
_pipe@49,
5,
<<"False -> Error(Nil)"/utf8>>
),
_pipe@51 = oaspec@util@string_extra:indent(
_pipe@50,
4,
<<"}"/utf8>>
),
_pipe@52 = oaspec@util@string_extra:indent(
_pipe@51,
3,
<<"})"/utf8>>
),
_pipe@53 = oaspec@util@string_extra:indent(
_pipe@52,
2,
<<"Error(_) -> Error(Nil)"/utf8>>
),
_pipe@54 = oaspec@util@string_extra:indent(
_pipe@53,
1,
<<"}"/utf8>>
),
_pipe@55 = oaspec@util@string_extra:line(_pipe@54, <<"}"/utf8>>),
_pipe@56 = oaspec@util@string_extra:blank_line(_pipe@55),
_pipe@57 = oaspec@util@string_extra:line(
_pipe@56,
<<"fn multipart_name(raw_headers: String) -> Result(String, Nil) {"/utf8>>
),
_pipe@58 = oaspec@util@string_extra:indent(
_pipe@57,
1,
<<"list.find_map(string.split(raw_headers, \"\\r\\n\"), fn(line) {"/utf8>>
),
_pipe@59 = oaspec@util@string_extra:indent(
_pipe@58,
2,
<<"case string.contains(line, \"name=\") {"/utf8>>
),
_pipe@60 = oaspec@util@string_extra:indent(
_pipe@59,
3,
<<"True ->"/utf8>>
),
_pipe@61 = oaspec@util@string_extra:indent(
_pipe@60,
4,
<<"list.find_map(string.split(line, \";\"), fn(part) {"/utf8>>
),
_pipe@62 = oaspec@util@string_extra:indent(
_pipe@61,
5,
<<"let trimmed = string.trim(part)"/utf8>>
),
_pipe@63 = oaspec@util@string_extra:indent(
_pipe@62,
5,
<<"case string.starts_with(trimmed, \"name=\") {"/utf8>>
),
_pipe@64 = oaspec@util@string_extra:indent(
_pipe@63,
6,
<<"True -> Ok(string.replace(string.replace(trimmed, \"name=\", \"\"), \"\\\"\", \"\"))"/utf8>>
),
_pipe@65 = oaspec@util@string_extra:indent(
_pipe@64,
6,
<<"False -> Error(Nil)"/utf8>>
),
_pipe@66 = oaspec@util@string_extra:indent(
_pipe@65,
5,
<<"}"/utf8>>
),
_pipe@67 = oaspec@util@string_extra:indent(
_pipe@66,
4,
<<"})"/utf8>>
),
_pipe@68 = oaspec@util@string_extra:indent(
_pipe@67,
3,
<<"False -> Error(Nil)"/utf8>>
),
_pipe@69 = oaspec@util@string_extra:indent(
_pipe@68,
2,
<<"}"/utf8>>
),
_pipe@70 = oaspec@util@string_extra:indent(
_pipe@69,
1,
<<"})"/utf8>>
),
_pipe@71 = oaspec@util@string_extra:line(_pipe@70, <<"}"/utf8>>),
_pipe@72 = oaspec@util@string_extra:blank_line(_pipe@71),
_pipe@73 = oaspec@util@string_extra:line(
_pipe@72,
<<"fn parse_multipart_body(body: String, headers: Dict(String, String)) -> Dict(String, List(String)) {"/utf8>>
),
_pipe@74 = oaspec@util@string_extra:indent(
_pipe@73,
1,
<<"case multipart_boundary(headers) {"/utf8>>
),
_pipe@75 = oaspec@util@string_extra:indent(
_pipe@74,
2,
<<"Ok(boundary) -> {"/utf8>>
),
_pipe@76 = oaspec@util@string_extra:indent(
_pipe@75,
3,
<<"let delimiter = \"--\" <> boundary"/utf8>>
),
_pipe@77 = oaspec@util@string_extra:indent(
_pipe@76,
3,
<<"let parts = string.split(body, delimiter)"/utf8>>
),
_pipe@78 = oaspec@util@string_extra:indent(
_pipe@77,
3,
<<"list.fold(parts, dict.new(), fn(acc, part) {"/utf8>>
),
_pipe@79 = oaspec@util@string_extra:indent(
_pipe@78,
4,
<<"let normalized_part = part |> string.remove_prefix(\"\\r\\n\") |> string.remove_suffix(\"\\r\\n\")"/utf8>>
),
_pipe@80 = oaspec@util@string_extra:indent(
_pipe@79,
4,
<<"case normalized_part == \"\" || normalized_part == \"--\" {"/utf8>>
),
_pipe@81 = oaspec@util@string_extra:indent(
_pipe@80,
5,
<<"True -> acc"/utf8>>
),
_pipe@82 = oaspec@util@string_extra:indent(
_pipe@81,
5,
<<"False ->"/utf8>>
),
_pipe@83 = oaspec@util@string_extra:indent(
_pipe@82,
6,
<<"case string.split_once(normalized_part, on: \"\\r\\n\\r\\n\") {"/utf8>>
),
_pipe@84 = oaspec@util@string_extra:indent(
_pipe@83,
7,
<<"Ok(#(raw_part_headers, raw_value)) ->"/utf8>>
),
_pipe@85 = oaspec@util@string_extra:indent(
_pipe@84,
8,
<<"case multipart_name(raw_part_headers) {"/utf8>>
),
_pipe@86 = oaspec@util@string_extra:indent(
_pipe@85,
9,
<<"Ok(name) -> {"/utf8>>
),
_pipe@87 = oaspec@util@string_extra:indent(
_pipe@86,
10,
<<"let value = raw_value"/utf8>>
),
_pipe@88 = oaspec@util@string_extra:indent(
_pipe@87,
10,
<<"case dict.get(acc, name) {"/utf8>>
),
_pipe@89 = oaspec@util@string_extra:indent(
_pipe@88,
11,
<<"Ok(existing) -> dict.insert(acc, name, list.append(existing, [value]))"/utf8>>
),
_pipe@90 = oaspec@util@string_extra:indent(
_pipe@89,
11,
<<"Error(_) -> dict.insert(acc, name, [value])"/utf8>>
),
_pipe@91 = oaspec@util@string_extra:indent(
_pipe@90,
10,
<<"}"/utf8>>
),
_pipe@92 = oaspec@util@string_extra:indent(
_pipe@91,
9,
<<"}"/utf8>>
),
_pipe@93 = oaspec@util@string_extra:indent(
_pipe@92,
9,
<<"Error(_) -> acc"/utf8>>
),
_pipe@94 = oaspec@util@string_extra:indent(
_pipe@93,
8,
<<"}"/utf8>>
),
_pipe@95 = oaspec@util@string_extra:indent(
_pipe@94,
7,
<<"Error(_) -> acc"/utf8>>
),
_pipe@96 = oaspec@util@string_extra:indent(
_pipe@95,
6,
<<"}"/utf8>>
),
_pipe@97 = oaspec@util@string_extra:indent(
_pipe@96,
4,
<<"}"/utf8>>
),
_pipe@98 = oaspec@util@string_extra:indent(
_pipe@97,
3,
<<"})"/utf8>>
),
_pipe@99 = oaspec@util@string_extra:indent(
_pipe@98,
2,
<<"}"/utf8>>
),
_pipe@100 = oaspec@util@string_extra:indent(
_pipe@99,
2,
<<"Error(_) -> dict.new()"/utf8>>
),
_pipe@101 = oaspec@util@string_extra:indent(
_pipe@100,
1,
<<"}"/utf8>>
),
_pipe@102 = oaspec@util@string_extra:line(_pipe@101, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@102);
false ->
Sb@3
end,
Sb@5 = case Has_nested_form_urlencoded_body of
true ->
_pipe@103 = Sb@4,
_pipe@104 = oaspec@util@string_extra:line(
_pipe@103,
<<"fn form_object_present(form_body: Dict(String, List(String)), prefix: String, props: List(String)) -> Bool {"/utf8>>
),
_pipe@105 = oaspec@util@string_extra:indent(
_pipe@104,
1,
<<"list.any(props, fn(prop) { dict.has_key(form_body, prefix <> \"[\" <> prop <> \"]\") })"/utf8>>
),
_pipe@106 = oaspec@util@string_extra:line(_pipe@105, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@106);
false ->
Sb@4
end,
Sb@6 = begin
_pipe@107 = Sb@5,
_pipe@108 = oaspec@util@string_extra:doc_comment(
_pipe@107,
<<"Route an incoming request to the appropriate handler."/utf8>>
),
_pipe@109 = oaspec@util@string_extra:line(
_pipe@108,
<<<<<<<<<<<<"pub fn route(method: String, path: List(String), "/utf8,
(route_arg_name(
<<"query"/utf8>>,
Uses_query
))/binary>>/binary,
": Dict(String, List(String)), "/utf8>>/binary,
(route_arg_name(<<"headers"/utf8>>, Uses_headers))/binary>>/binary,
": Dict(String, String), "/utf8>>/binary,
(route_arg_name(<<"body"/utf8>>, Uses_body))/binary>>/binary,
": String) -> ServerResponse {"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@109,
1,
<<"case method, path {"/utf8>>
)
end,
Sb@8 = gleam@list:fold(
Operations,
Sb@6,
fun(Sb@7, Op@17) ->
{Op_id, Operation@17, Path, Method} = Op@17,
Fn_name = oaspec@util@naming:operation_to_function_name(Op_id),
Method_str = oaspec@openapi@spec:method_to_string(Method),
Path_pattern = path_to_pattern(Path),
Has_params = not gleam@list:is_empty(
erlang:element(6, Operation@17)
)
orelse gleam@option:is_some(erlang:element(7, Operation@17)),
_pipe@110 = Sb@7,
_pipe@111 = oaspec@util@string_extra:indent(
_pipe@110,
2,
<<<<<<<<"\""/utf8, Method_str/binary>>/binary, "\", "/utf8>>/binary,
Path_pattern/binary>>/binary,
" -> {"/utf8>>
),
_pipe@112 = generate_route_body(
_pipe@111,
Op_id,
Fn_name,
Operation@17,
Path,
Has_params,
Ctx
),
oaspec@util@string_extra:indent(_pipe@112, 2, <<"}"/utf8>>)
end
),
Sb@9 = begin
_pipe@113 = Sb@8,
_pipe@114 = oaspec@util@string_extra:indent(
_pipe@113,
2,
<<"_, _ -> ServerResponse(status: 404, body: \"Not Found\", headers: [])"/utf8>>
),
_pipe@115 = oaspec@util@string_extra:indent(_pipe@114, 1, <<"}"/utf8>>),
_pipe@116 = oaspec@util@string_extra:line(_pipe@115, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@116)
end,
Sb@10 = case Needs_cookie_lookup of
true ->
generate_cookie_lookup(Sb@9);
false ->
Sb@9
end,
oaspec@util@string_extra:to_string(Sb@10).
-file("src/oaspec/codegen/server.gleam", 20).
?DOC(" Generate server stub files.\n").
-spec generate(oaspec@codegen@context:context()) -> list(oaspec@codegen@context:generated_file()).
generate(Ctx) ->
Handlers_content = generate_handlers(Ctx),
Router_content = generate_router(Ctx),
[{generated_file,
<<"handlers.gleam"/utf8>>,
Handlers_content,
server_target},
{generated_file, <<"router.gleam"/utf8>>, Router_content, server_target}].