Packages
oaspec
0.6.3
0.68.0
0.67.0
0.66.0
0.65.0
0.64.0
0.63.0
0.62.0
0.61.0
0.60.0
0.59.0
0.58.1
0.58.0
0.57.0
0.56.0
0.55.0
0.54.0
0.53.0
0.52.0
0.51.0
0.50.0
0.49.0
0.48.0
0.47.0
0.46.0
0.45.0
0.44.0
0.43.0
0.42.0
0.41.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.0
0.34.0
0.33.0
0.32.0
0.31.0
0.30.0
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.0
0.23.0
0.22.0
0.21.0
0.20.0
0.19.0
0.18.0
0.17.0
0.16.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.0
0.7.0
0.6.3
0.6.1
0.6.0
0.5.0
0.4.0
0.3.0
0.1.3
Generate Gleam code from OpenAPI 3.x specifications
Current section
Files
Jump to
Current section
Files
src/oaspec@codegen@ir_build.erl
-module(oaspec@codegen@ir_build).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/codegen/ir_build.gleam").
-export([build_types_module/1]).
-export_type([merged_all_of/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 merged_all_of() :: {merged_all_of,
gleam@dict:dict(binary(), oaspec@openapi@schema:schema_ref()),
list(binary()),
gleam@option:option(oaspec@openapi@schema:schema_ref()),
boolean()}.
-file("src/oaspec/codegen/ir_build.gleam", 486).
-spec schema_ref_to_type(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> binary().
schema_ref_to_type(Ref, _) ->
case Ref of
{inline, S} ->
oaspec@codegen@schema_dispatch:schema_type(S);
{reference, _, Name} ->
oaspec@util@naming:schema_to_type_name(Name)
end.
-file("src/oaspec/codegen/ir_build.gleam", 471).
-spec schema_ref_to_type_with_inline_enum(
oaspec@openapi@schema:schema_ref(),
binary(),
binary(),
oaspec@codegen@context:context()
) -> binary().
schema_ref_to_type_with_inline_enum(Ref, Parent_name, Prop_name, Ctx) ->
case Ref of
{inline, {string_schema, _, _, Enum_values, _, _, _}} when Enum_values =/= [] ->
<<(oaspec@util@naming:schema_to_type_name(Parent_name))/binary,
(oaspec@util@naming:schema_to_type_name(Prop_name))/binary>>;
_ ->
schema_ref_to_type(Ref, Ctx)
end.
-file("src/oaspec/codegen/ir_build.gleam", 493).
-spec schema_ref_is_nullable(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> boolean().
schema_ref_is_nullable(Ref, Ctx) ->
case Ref of
{inline, S} ->
oaspec@openapi@schema:is_nullable(S);
{reference, _, _} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Ref,
erlang:element(2, Ctx)
) of
{ok, S@1} ->
oaspec@openapi@schema:is_nullable(S@1);
{error, _} ->
false
end
end.
-file("src/oaspec/codegen/ir_build.gleam", 504).
-spec schema_has_optional_fields(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> boolean().
schema_has_optional_fields(Schema_ref, Ctx) ->
case Schema_ref of
{inline, {object_schema, _, Properties, Required, _, _, _, _}} ->
_pipe = maps:to_list(Properties),
gleam@list:any(
_pipe,
fun(Entry) ->
{Prop_name, Prop_ref} = Entry,
not gleam@list:contains(Required, Prop_name) orelse schema_ref_is_nullable(
Prop_ref,
Ctx
)
end
);
{inline, {all_of_schema, _, Schemas}} ->
gleam@list:any(
Schemas,
fun(S) -> schema_has_optional_fields(S, Ctx) end
);
{reference, _, _} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref,
erlang:element(2, Ctx)
) of
{ok, Schema_obj} ->
schema_has_optional_fields({inline, Schema_obj}, Ctx);
{error, _} ->
false
end;
_ ->
false
end.
-file("src/oaspec/codegen/ir_build.gleam", 525).
-spec schema_has_additional_properties(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> boolean().
schema_has_additional_properties(Schema_ref, Ctx) ->
case Schema_ref of
{inline, {object_schema, _, _, _, {some, _}, _, _, _}} ->
true;
{inline, {object_schema, _, _, _, _, true, _, _}} ->
true;
{inline, {all_of_schema, _, Schemas}} ->
gleam@list:any(
Schemas,
fun(S) -> schema_has_additional_properties(S, Ctx) end
);
{reference, _, _} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref,
erlang:element(2, Ctx)
) of
{ok, Schema_obj} ->
schema_has_additional_properties({inline, Schema_obj}, Ctx);
{error, _} ->
false
end;
_ ->
false
end.
-file("src/oaspec/codegen/ir_build.gleam", 541).
-spec schema_has_untyped_additional_properties(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> boolean().
schema_has_untyped_additional_properties(Schema_ref, Ctx) ->
case Schema_ref of
{inline, {object_schema, _, _, _, _, true, _, _}} ->
true;
{inline, {all_of_schema, _, Schemas}} ->
gleam@list:any(
Schemas,
fun(S) -> schema_has_untyped_additional_properties(S, Ctx) end
);
{reference, _, _} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref,
erlang:element(2, Ctx)
) of
{ok, Schema_obj} ->
schema_has_untyped_additional_properties(
{inline, Schema_obj},
Ctx
);
{error, _} ->
false
end;
_ ->
false
end.
-file("src/oaspec/codegen/ir_build.gleam", 66).
-spec compute_imports(
list({binary(), oaspec@openapi@schema:schema_ref()}),
oaspec@codegen@context:context()
) -> list(binary()).
compute_imports(Schemas, Ctx) ->
Needs_option = gleam@list:any(
Schemas,
fun(Entry) ->
{_, Schema_ref} = Entry,
case Schema_ref of
{inline, {any_of_schema, _, _, _}} ->
true;
_ ->
schema_has_optional_fields(Schema_ref, Ctx)
end
end
),
Needs_dict = gleam@list:any(
Schemas,
fun(Entry@1) ->
{_, Schema_ref@1} = Entry@1,
schema_has_additional_properties(Schema_ref@1, Ctx)
end
),
Needs_dynamic = gleam@list:any(
Schemas,
fun(Entry@2) ->
{_, Schema_ref@2} = Entry@2,
schema_has_untyped_additional_properties(Schema_ref@2, Ctx)
end
),
case {Needs_option, Needs_dict, Needs_dynamic} of
{true, true, true} ->
[<<"gleam/dict.{type Dict}"/utf8>>,
<<"gleam/dynamic.{type Dynamic}"/utf8>>,
<<"gleam/option.{type Option}"/utf8>>];
{true, true, false} ->
[<<"gleam/dict.{type Dict}"/utf8>>,
<<"gleam/option.{type Option}"/utf8>>];
{true, false, _} ->
[<<"gleam/option.{type Option}"/utf8>>];
{false, true, true} ->
[<<"gleam/dict.{type Dict}"/utf8>>,
<<"gleam/dynamic.{type Dynamic}"/utf8>>];
{false, true, false} ->
[<<"gleam/dict.{type Dict}"/utf8>>];
{false, false, _} ->
[]
end.
-file("src/oaspec/codegen/ir_build.gleam", 571).
-spec merge_allof_schemas(
list(oaspec@openapi@schema:schema_ref()),
oaspec@codegen@context:context()
) -> merged_all_of().
merge_allof_schemas(Schemas, Ctx) ->
gleam@list:index_fold(
Schemas,
{merged_all_of, maps:new(), [], none, false},
fun(Acc, S_ref, Idx) ->
Resolved = case S_ref of
{inline, Obj} ->
{ok, Obj};
{reference, _, _} ->
oaspec@openapi@resolver:resolve_schema_ref(
S_ref,
erlang:element(2, Ctx)
)
end,
case Resolved of
{ok,
{object_schema,
_,
Properties,
Required,
Additional_properties,
Additional_properties_untyped,
_,
_}} ->
Merged_ap = case {erlang:element(4, Acc),
Additional_properties} of
{none, Ap} ->
Ap;
{Existing, _} ->
Existing
end,
Merged_ap_untyped = erlang:element(5, Acc) orelse Additional_properties_untyped,
{merged_all_of,
maps:merge(erlang:element(2, Acc), Properties),
lists:append(erlang:element(3, Acc), Required),
Merged_ap,
Merged_ap_untyped};
{ok, Schema_obj} ->
Field_name = case Idx of
0 ->
<<"value"/utf8>>;
N ->
<<"value_"/utf8,
(erlang:integer_to_binary(N))/binary>>
end,
{merged_all_of,
gleam@dict:insert(
erlang:element(2, Acc),
Field_name,
{inline, Schema_obj}
),
[Field_name | erlang:element(3, Acc)],
erlang:element(4, Acc),
erlang:element(5, Acc)};
_ ->
Acc
end
end
).
-file("src/oaspec/codegen/ir_build.gleam", 630).
-spec collect_operations(oaspec@codegen@context:context()) -> list({binary(),
oaspec@openapi@spec:operation(),
binary(),
oaspec@openapi@spec:http_method()}).
collect_operations(Ctx) ->
Paths = gleam@list:sort(
maps:to_list(erlang:element(4, erlang:element(2, Ctx))),
fun(A, B) ->
gleam@string:compare(erlang:element(1, A), erlang:element(1, B))
end
),
gleam@list:flat_map(
Paths,
fun(Entry) ->
{Path, Path_item} = Entry,
Ops = [{erlang:element(4, Path_item), get},
{erlang:element(5, Path_item), post},
{erlang:element(6, Path_item), put},
{erlang:element(7, Path_item), delete},
{erlang:element(8, Path_item), patch},
{erlang:element(9, Path_item), head},
{erlang:element(10, Path_item), options},
{erlang:element(11, Path_item), trace}],
gleam@list:filter_map(
Ops,
fun(Op_entry) ->
{Maybe_op, Method} = Op_entry,
case Maybe_op of
{some, Operation} ->
Op_param_keys = gleam@list:map(
erlang:element(6, Operation),
fun(P) ->
{erlang:element(2, P), erlang:element(3, P)}
end
),
Inherited_params = gleam@list:filter(
erlang:element(12, Path_item),
fun(P@1) ->
not gleam@list:contains(
Op_param_keys,
{erlang:element(2, P@1),
erlang:element(3, P@1)}
)
end
),
Merged_params = lists:append(
Inherited_params,
erlang:element(6, Operation)
),
Effective_security = case erlang:element(
10,
Operation
) of
{some, Sec} ->
Sec;
none ->
erlang:element(7, erlang:element(2, Ctx))
end,
Operation@1 = {operation,
erlang:element(2, Operation),
erlang:element(3, Operation),
erlang:element(4, Operation),
erlang:element(5, Operation),
Merged_params,
erlang:element(7, Operation),
erlang:element(8, Operation),
erlang:element(9, Operation),
{some, Effective_security},
erlang:element(11, Operation),
erlang:element(12, Operation),
erlang:element(13, Operation)},
Op_id = case erlang:element(2, Operation@1) of
{some, Id} ->
Id;
none ->
<<<<(oaspec@openapi@spec:method_to_lower(
Method
))/binary,
"_"/utf8>>/binary,
(begin
_pipe = gleam@string:replace(
Path,
<<"/"/utf8>>,
<<"_"/utf8>>
),
_pipe@1 = gleam@string:replace(
_pipe,
<<"{"/utf8>>,
<<""/utf8>>
),
gleam@string:replace(
_pipe@1,
<<"}"/utf8>>,
<<""/utf8>>
)
end)/binary>>
end,
{ok, {Op_id, Operation@1, Path, Method}};
none ->
{error, nil}
end
end
)
end
).
-file("src/oaspec/codegen/ir_build.gleam", 767).
-spec schema_ref_is_read_only(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> boolean().
schema_ref_is_read_only(Ref, Ctx) ->
case Ref of
{inline, S} ->
erlang:element(6, oaspec@openapi@schema:get_metadata(S));
{reference, _, _} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Ref,
erlang:element(2, Ctx)
) of
{ok, S@1} ->
erlang:element(6, oaspec@openapi@schema:get_metadata(S@1));
{error, _} ->
false
end
end.
-file("src/oaspec/codegen/ir_build.gleam", 728).
-spec filter_read_only_properties(
oaspec@openapi@schema:schema_object(),
oaspec@codegen@context:context()
) -> oaspec@openapi@schema:schema_object().
filter_read_only_properties(Schema_obj, Ctx) ->
case Schema_obj of
{object_schema,
Metadata,
Properties,
Required,
Additional_properties,
Additional_properties_untyped,
Min_properties,
Max_properties} ->
Filtered_props = gleam@dict:filter(
Properties,
fun(_, Prop_ref) ->
not schema_ref_is_read_only(Prop_ref, Ctx)
end
),
Filtered_required = gleam@list:filter(
Required,
fun(Name) -> case gleam_stdlib:map_get(Filtered_props, Name) of
{ok, _} ->
true;
{error, _} ->
false
end end
),
{object_schema,
Metadata,
Filtered_props,
Filtered_required,
Additional_properties,
Additional_properties_untyped,
Min_properties,
Max_properties};
_ ->
Schema_obj
end.
-file("src/oaspec/codegen/ir_build.gleam", 778).
-spec schema_ref_is_write_only(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> boolean().
schema_ref_is_write_only(Ref, Ctx) ->
case Ref of
{inline, S} ->
erlang:element(7, oaspec@openapi@schema:get_metadata(S));
{reference, _, _} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Ref,
erlang:element(2, Ctx)
) of
{ok, S@1} ->
erlang:element(7, oaspec@openapi@schema:get_metadata(S@1));
{error, _} ->
false
end
end.
-file("src/oaspec/codegen/ir_build.gleam", 689).
-spec filter_write_only_properties(
oaspec@openapi@schema:schema_object(),
oaspec@codegen@context:context()
) -> oaspec@openapi@schema:schema_object().
filter_write_only_properties(Schema_obj, Ctx) ->
case Schema_obj of
{object_schema,
Metadata,
Properties,
Required,
Additional_properties,
Additional_properties_untyped,
Min_properties,
Max_properties} ->
Filtered_props = gleam@dict:filter(
Properties,
fun(_, Prop_ref) ->
not schema_ref_is_write_only(Prop_ref, Ctx)
end
),
Filtered_required = gleam@list:filter(
Required,
fun(Name) -> case gleam_stdlib:map_get(Filtered_props, Name) of
{ok, _} ->
true;
{error, _} ->
false
end end
),
{object_schema,
Metadata,
Filtered_props,
Filtered_required,
Additional_properties,
Additional_properties_untyped,
Min_properties,
Max_properties};
_ ->
Schema_obj
end.
-file("src/oaspec/codegen/ir_build.gleam", 789).
-spec list_at_or(list(binary()), integer(), binary()) -> binary().
list_at_or(Lst, Idx, Default) ->
case {Lst, Idx} of
{[], _} ->
Default;
{[Head | _], 0} ->
Head;
{[_ | Rest], N} ->
list_at_or(Rest, N - 1, Default)
end.
-file("src/oaspec/codegen/ir_build.gleam", 142).
-spec inline_enums_from_properties(
binary(),
gleam@dict:dict(binary(), oaspec@openapi@schema:schema_ref()),
oaspec@codegen@context:context()
) -> list(oaspec@codegen@ir:declaration()).
inline_enums_from_properties(Parent_name, Properties, _) ->
Entries = maps:to_list(Properties),
gleam@list:filter_map(
Entries,
fun(Entry) ->
{Prop_name, Prop_ref} = Entry,
case Prop_ref of
{inline, {string_schema, Metadata, _, Enum_values, _, _, _}} when Enum_values =/= [] ->
Type_name = <<(oaspec@util@naming:schema_to_type_name(
Parent_name
))/binary,
(oaspec@util@naming:schema_to_type_name(Prop_name))/binary>>,
Deduped_variants = oaspec@openapi@dedup:dedup_enum_variants(
Enum_values
),
Variants = gleam@list:index_map(
Enum_values,
fun(Value, Idx) ->
Variant_suffix = list_at_or(
Deduped_variants,
Idx,
oaspec@util@naming:to_pascal_case(Value)
),
<<(oaspec@util@naming:schema_to_type_name(Type_name))/binary,
Variant_suffix/binary>>
end
),
{ok,
{declaration,
erlang:element(2, Metadata),
{enum_type, Type_name, Variants}}};
_ ->
{error, nil}
end
end
).
-file("src/oaspec/codegen/ir_build.gleam", 115).
-spec inline_enums_for_schema(
binary(),
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> list(oaspec@codegen@ir:declaration()).
inline_enums_for_schema(Parent_name, Schema_ref, Ctx) ->
case Schema_ref of
{inline, {object_schema, _, Properties, _, _, _, _, _}} ->
inline_enums_from_properties(Parent_name, Properties, Ctx);
{inline, {all_of_schema, _, Schemas}} ->
Merged_props = gleam@list:fold(
Schemas,
maps:new(),
fun(Acc, S_ref) -> case S_ref of
{inline,
{object_schema, _, Properties@1, _, _, _, _, _}} ->
maps:merge(Acc, Properties@1);
{reference, _, _} ->
case oaspec@openapi@resolver:resolve_schema_ref(
S_ref,
erlang:element(2, Ctx)
) of
{ok,
{object_schema,
_,
Properties@2,
_,
_,
_,
_,
_}} ->
maps:merge(Acc, Properties@2);
_ ->
Acc
end;
_ ->
Acc
end end
),
inline_enums_from_properties(Parent_name, Merged_props, Ctx);
_ ->
[]
end.
-file("src/oaspec/codegen/ir_build.gleam", 196).
-spec schema_type_decls(
binary(),
binary(),
oaspec@openapi@schema:schema_object(),
oaspec@codegen@context:context()
) -> list(oaspec@codegen@ir:declaration()).
schema_type_decls(Type_name, Raw_name, Schema, Ctx) ->
case Schema of
{object_schema,
Metadata,
Properties,
Required,
Additional_properties,
Additional_properties_untyped,
_,
_} ->
Props = maps:to_list(Properties),
Deduped_names = oaspec@openapi@dedup:dedup_property_names(
gleam@list:map(Props, fun(E) -> erlang:element(1, E) end)
),
Fields = gleam@list:index_map(
Props,
fun(Entry, Idx) ->
{Prop_name, Prop_ref} = Entry,
Field_name = list_at_or(
Deduped_names,
Idx,
oaspec@util@naming:to_snake_case(Prop_name)
),
Field_type = schema_ref_to_type_with_inline_enum(
Prop_ref,
Raw_name,
Prop_name,
Ctx
),
Is_required = gleam@list:contains(Required, Prop_name),
Is_already_optional = schema_ref_is_nullable(Prop_ref, Ctx),
Final_type = case {Is_required, Is_already_optional} of
{true, _} ->
Field_type;
{false, true} ->
Field_type;
{false, false} ->
<<<<"Option("/utf8, Field_type/binary>>/binary,
")"/utf8>>
end,
{field, Field_name, Final_type}
end
),
Fields@1 = case {Additional_properties,
Additional_properties_untyped} of
{{some, Ap_ref}, _} ->
Inner_type = schema_ref_to_type(Ap_ref, Ctx),
lists:append(
Fields,
[{field,
<<"additional_properties"/utf8>>,
<<<<"Dict(String, "/utf8, Inner_type/binary>>/binary,
")"/utf8>>}]
);
{none, true} ->
lists:append(
Fields,
[{field,
<<"additional_properties"/utf8>>,
<<"Dict(String, Dynamic)"/utf8>>}]
);
{none, false} ->
Fields
end,
[{declaration,
erlang:element(2, Metadata),
{record_type, Type_name, Fields@1}}];
{string_schema, Metadata@1, _, Enum_values, _, _, _} when Enum_values =/= [] ->
Deduped_variants = oaspec@openapi@dedup:dedup_enum_variants(
Enum_values
),
Variants = gleam@list:index_map(
Enum_values,
fun(Value, Idx@1) ->
Variant_suffix = list_at_or(
Deduped_variants,
Idx@1,
oaspec@util@naming:to_pascal_case(Value)
),
<<(oaspec@util@naming:schema_to_type_name(Type_name))/binary,
Variant_suffix/binary>>
end
),
[{declaration,
erlang:element(2, Metadata@1),
{enum_type, Type_name, Variants}}];
{one_of_schema, Metadata@2, Schemas, _} ->
Variants@1 = gleam@list:map(
Schemas,
fun(S_ref) ->
Variant_type = schema_ref_to_type(S_ref, Ctx),
Variant_name = <<Type_name/binary, Variant_type/binary>>,
{variant_with_type, Variant_name, Variant_type}
end
),
[{declaration,
erlang:element(2, Metadata@2),
{union_type, Type_name, Variants@1}}];
{any_of_schema, Metadata@3, Schemas@1, _} ->
Fields@2 = gleam@list:map(
Schemas@1,
fun(S_ref@1) ->
Variant_type@1 = schema_ref_to_type(S_ref@1, Ctx),
Field_name@1 = oaspec@util@naming:to_snake_case(
Variant_type@1
),
{field,
Field_name@1,
<<<<"Option("/utf8, Variant_type@1/binary>>/binary,
")"/utf8>>}
end
),
[{declaration,
erlang:element(2, Metadata@3),
{record_type, Type_name, Fields@2}}];
{all_of_schema, Metadata@4, Schemas@2} ->
Merged = merge_allof_schemas(Schemas@2, Ctx),
Merged_schema = {object_schema,
Metadata@4,
erlang:element(2, Merged),
erlang:element(3, Merged),
erlang:element(4, Merged),
erlang:element(5, Merged),
none,
none},
schema_type_decls(Type_name, Raw_name, Merged_schema, Ctx);
_ ->
Gleam_type = oaspec@codegen@schema_dispatch:schema_type(Schema),
[{declaration, none, {type_alias, Type_name, Gleam_type}}]
end.
-file("src/oaspec/codegen/ir_build.gleam", 176).
-spec type_def_decls(
binary(),
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> list(oaspec@codegen@ir:declaration()).
type_def_decls(Name, Schema_ref, Ctx) ->
Type_name = oaspec@util@naming:schema_to_type_name(Name),
case Schema_ref of
{inline, Schema} ->
schema_type_decls(Type_name, Name, Schema, Ctx);
{reference, _, Ref_name} ->
Resolved_type = oaspec@util@naming:schema_to_type_name(Ref_name),
[{declaration, none, {type_alias, Type_name, Resolved_type}}]
end.
-file("src/oaspec/codegen/ir_build.gleam", 411).
-spec anonymous_type_for_schema(
binary(),
binary(),
oaspec@openapi@schema:schema_object(),
oaspec@codegen@context:context()
) -> list(oaspec@codegen@ir:declaration()).
anonymous_type_for_schema(Op_id, Suffix, Schema_obj, Ctx) ->
Type_name = <<(oaspec@util@naming:schema_to_type_name(Op_id))/binary,
Suffix/binary>>,
Raw_name = <<<<Op_id/binary, "_"/utf8>>/binary, Suffix/binary>>,
case Schema_obj of
{object_schema, _, _, _, _, _, _, _} ->
schema_type_decls(Type_name, Raw_name, Schema_obj, Ctx);
{one_of_schema, _, Schemas, _} ->
All_refs = gleam@list:all(Schemas, fun(S) -> case S of
{reference, _, _} ->
true;
_ ->
false
end end),
case All_refs of
true ->
Variants = gleam@list:map(
Schemas,
fun(S_ref) ->
Variant_type = schema_ref_to_type(S_ref, Ctx),
Variant_name = <<Type_name/binary,
Variant_type/binary>>,
{variant_with_type, Variant_name, Variant_type}
end
),
[{declaration, none, {union_type, Type_name, Variants}}];
false ->
[]
end;
{any_of_schema, _, _, _} ->
schema_type_decls(Type_name, Raw_name, Schema_obj, Ctx);
{all_of_schema, Metadata, Schemas@1} ->
Merged = merge_allof_schemas(Schemas@1, Ctx),
Merged_schema = {object_schema,
Metadata,
erlang:element(2, Merged),
erlang:element(3, Merged),
erlang:element(4, Merged),
erlang:element(5, Merged),
none,
none},
schema_type_decls(Type_name, Raw_name, Merged_schema, Ctx);
_ ->
[]
end.
-file("src/oaspec/codegen/ir_build.gleam", 354).
-spec anonymous_response_type_decls(
binary(),
oaspec@openapi@spec:operation(),
oaspec@codegen@context:context()
) -> list(oaspec@codegen@ir:declaration()).
anonymous_response_type_decls(Op_id, Operation, Ctx) ->
Responses = maps:to_list(erlang:element(8, Operation)),
gleam@list:flat_map(
Responses,
fun(Entry) ->
{Status_code, Response} = Entry,
Content_entries = maps:to_list(erlang:element(3, Response)),
case Content_entries of
[{_, Media_type} | _] ->
case erlang:element(2, Media_type) of
{some, {inline, Schema_obj}} ->
Filtered_schema = filter_write_only_properties(
Schema_obj,
Ctx
),
anonymous_type_for_schema(
Op_id,
<<"Response"/utf8,
(oaspec@util@http:status_code_suffix(
Status_code
))/binary>>,
Filtered_schema,
Ctx
);
_ ->
[]
end;
_ ->
[]
end
end
).
-file("src/oaspec/codegen/ir_build.gleam", 382).
-spec anonymous_request_body_type_decls(
binary(),
oaspec@openapi@spec:operation(),
oaspec@codegen@context:context()
) -> list(oaspec@codegen@ir:declaration()).
anonymous_request_body_type_decls(Op_id, Operation, Ctx) ->
case erlang:element(7, Operation) of
{some, Rb} ->
Content_entries = maps:to_list(erlang:element(3, Rb)),
case Content_entries of
[{_, Media_type} | _] ->
case erlang:element(2, Media_type) of
{some, {inline, Schema_obj}} ->
Filtered_schema = filter_read_only_properties(
Schema_obj,
Ctx
),
anonymous_type_for_schema(
Op_id,
<<"RequestBody"/utf8>>,
Filtered_schema,
Ctx
);
_ ->
[]
end;
_ ->
[]
end;
none ->
[]
end.
-file("src/oaspec/codegen/ir_build.gleam", 344).
-spec anonymous_type_decls(oaspec@codegen@context:context()) -> list(oaspec@codegen@ir:declaration()).
anonymous_type_decls(Ctx) ->
Operations = collect_operations(Ctx),
gleam@list:flat_map(
Operations,
fun(Op) ->
{Op_id, Operation, _, _} = Op,
Response_decls = anonymous_response_type_decls(
Op_id,
Operation,
Ctx
),
Request_decls = anonymous_request_body_type_decls(
Op_id,
Operation,
Ctx
),
lists:append(Response_decls, Request_decls)
end
).
-file("src/oaspec/codegen/ir_build.gleam", 27).
?DOC(" Build an IR Module for the types.gleam file from component schemas.\n").
-spec build_types_module(oaspec@codegen@context:context()) -> oaspec@codegen@ir:module_().
build_types_module(Ctx) ->
Schemas = case erlang:element(5, erlang:element(2, Ctx)) of
{some, Components} ->
gleam@list:sort(
maps:to_list(erlang:element(2, Components)),
fun(A, B) ->
gleam@string:compare(
erlang:element(1, A),
erlang:element(1, B)
)
end
);
none ->
[]
end,
Imports = compute_imports(Schemas, Ctx),
Inline_enum_decls = gleam@list:flat_map(
Schemas,
fun(Entry) ->
{Name, Schema_ref} = Entry,
inline_enums_for_schema(Name, Schema_ref, Ctx)
end
),
Main_decls = gleam@list:flat_map(
Schemas,
fun(Entry@1) ->
{Name@1, Schema_ref@1} = Entry@1,
type_def_decls(Name@1, Schema_ref@1, Ctx)
end
),
Anon_decls = anonymous_type_decls(Ctx),
{module,
<<""/utf8>>,
Imports,
lists:append([Inline_enum_decls, Main_decls, Anon_decls])}.