Current section
Files
Jump to
Current section
Files
src/glean@schema.erl
-module(glean@schema).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glean/schema.gleam").
-export([object/2, string/0, string_enum/1, int/0, int_range/2, float/0, bool/0, array/1, nullable/1, one_of/1, describe/2, string_with_length/2, to_json_strict/1, to_json/1, to_json_string/1]).
-export_type([schema/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.
-opaque schema() :: {object_schema,
gleam@dict:dict(binary(), schema()),
list(binary()),
gleam@option:option(binary())} |
{string_schema,
gleam@option:option(binary()),
gleam@option:option(list(binary())),
gleam@option:option(integer()),
gleam@option:option(integer())} |
{int_schema,
gleam@option:option(binary()),
gleam@option:option(integer()),
gleam@option:option(integer())} |
{float_schema, gleam@option:option(binary())} |
{bool_schema, gleam@option:option(binary())} |
{array_schema, schema(), gleam@option:option(binary())} |
{nullable_schema, schema()} |
{one_of_schema, list(schema()), gleam@option:option(binary())}.
-file("src/glean/schema.gleam", 35).
-spec object(list({binary(), schema()}), list(binary())) -> schema().
object(Properties, Required) ->
case not gleam@list:is_empty(Properties) of
true -> nil;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glean/schema"/utf8>>,
function => <<"object"/utf8>>,
line => 39,
value => _assert_fail,
start => 1050,
'end' => 1094,
pattern_start => 1061,
pattern_end => 1065})
end,
Prop_dict = maps:from_list(Properties),
Prop_names = gleam@list:map(Properties, fun(P) -> erlang:element(1, P) end),
gleam@list:each(
Required,
fun(R) -> _assert_subject = gleam@list:contains(Prop_names, R),
case _assert_subject of
true -> _assert_subject;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glean/schema"/utf8>>,
function => <<"object"/utf8>>,
line => 44,
value => _assert_fail@1,
start => 1281,
'end' => 1327,
pattern_start => 1292,
pattern_end => 1296})
end end
),
{object_schema, Prop_dict, Required, none}.
-file("src/glean/schema.gleam", 49).
-spec string() -> schema().
string() ->
{string_schema, none, none, none, none}.
-file("src/glean/schema.gleam", 58).
-spec string_enum(list(binary())) -> schema().
string_enum(Values) ->
case not gleam@list:is_empty(Values) of
true -> nil;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glean/schema"/utf8>>,
function => <<"string_enum"/utf8>>,
line => 59,
value => _assert_fail,
start => 1616,
'end' => 1656,
pattern_start => 1627,
pattern_end => 1631})
end,
{string_schema, none, {some, Values}, none, none}.
-file("src/glean/schema.gleam", 68).
-spec int() -> schema().
int() ->
{int_schema, none, none, none}.
-file("src/glean/schema.gleam", 72).
-spec int_range(integer(), integer()) -> schema().
int_range(Minimum, Maximum) ->
case Minimum =< Maximum of
true -> nil;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glean/schema"/utf8>>,
function => <<"int_range"/utf8>>,
line => 73,
value => _assert_fail,
start => 1942,
'end' => 1978,
pattern_start => 1953,
pattern_end => 1957})
end,
{int_schema, none, {some, Minimum}, {some, Maximum}}.
-file("src/glean/schema.gleam", 77).
-spec float() -> schema().
float() ->
{float_schema, none}.
-file("src/glean/schema.gleam", 81).
-spec bool() -> schema().
bool() ->
{bool_schema, none}.
-file("src/glean/schema.gleam", 85).
-spec array(schema()) -> schema().
array(Items) ->
{array_schema, Items, none}.
-file("src/glean/schema.gleam", 89).
-spec nullable(schema()) -> schema().
nullable(Schema) ->
{nullable_schema, Schema}.
-file("src/glean/schema.gleam", 93).
-spec one_of(list(schema())) -> schema().
one_of(Variants) ->
case erlang:length(Variants) >= 2 of
true -> nil;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glean/schema"/utf8>>,
function => <<"one_of"/utf8>>,
line => 94,
value => _assert_fail,
start => 2418,
'end' => 2462,
pattern_start => 2429,
pattern_end => 2433})
end,
{one_of_schema, Variants, none}.
-file("src/glean/schema.gleam", 98).
-spec describe(schema(), binary()) -> schema().
describe(Schema, Description) ->
case Schema of
{object_schema, P, R, _} ->
{object_schema, P, R, {some, Description}};
{string_schema, _, E, Mn, Mx} ->
{string_schema, {some, Description}, E, Mn, Mx};
{int_schema, _, Mn@1, Mx@1} ->
{int_schema, {some, Description}, Mn@1, Mx@1};
{float_schema, _} ->
{float_schema, {some, Description}};
{bool_schema, _} ->
{bool_schema, {some, Description}};
{array_schema, I, _} ->
{array_schema, I, {some, Description}};
{nullable_schema, Inner} ->
{nullable_schema, describe(Inner, Description)};
{one_of_schema, V, _} ->
{one_of_schema, V, {some, Description}}
end.
-file("src/glean/schema.gleam", 112).
-spec string_with_length(
gleam@option:option(integer()),
gleam@option:option(integer())
) -> schema().
string_with_length(Min_length, Max_length) ->
{string_schema, none, none, Min_length, Max_length}.
-file("src/glean/schema.gleam", 169).
-spec with_description(
list({binary(), gleam@json:json()}),
gleam@option:option(binary())
) -> list({binary(), gleam@json:json()}).
with_description(Fields, Description) ->
case Description of
none ->
Fields;
{some, D} ->
[{<<"description"/utf8>>, gleam@json:string(D)} | Fields]
end.
-file("src/glean/schema.gleam", 208).
-spec string_to_json(
gleam@option:option(binary()),
gleam@option:option(list(binary())),
gleam@option:option(integer()),
gleam@option:option(integer())
) -> gleam@json:json().
string_to_json(Description, Enum_values, Min_length, Max_length) ->
Fields = [{<<"type"/utf8>>, gleam@json:string(<<"string"/utf8>>)}],
Fields@1 = case Enum_values of
none ->
Fields;
{some, Values} ->
lists:append(
Fields,
[{<<"enum"/utf8>>,
gleam@json:array(Values, fun gleam@json:string/1)}]
)
end,
Fields@2 = case Min_length of
none ->
Fields@1;
{some, Ml} ->
lists:append(Fields@1, [{<<"minLength"/utf8>>, gleam@json:int(Ml)}])
end,
Fields@3 = case Max_length of
none ->
Fields@2;
{some, Ml@1} ->
lists:append(
Fields@2,
[{<<"maxLength"/utf8>>, gleam@json:int(Ml@1)}]
)
end,
_pipe = Fields@3,
_pipe@1 = with_description(_pipe, Description),
gleam@json:object(_pipe@1).
-file("src/glean/schema.gleam", 239).
-spec int_to_json(
gleam@option:option(binary()),
gleam@option:option(integer()),
gleam@option:option(integer())
) -> gleam@json:json().
int_to_json(Description, Minimum, Maximum) ->
Fields = [{<<"type"/utf8>>, gleam@json:string(<<"integer"/utf8>>)}],
Fields@1 = case Minimum of
none ->
Fields;
{some, M} ->
lists:append(Fields, [{<<"minimum"/utf8>>, gleam@json:int(M)}])
end,
Fields@2 = case Maximum of
none ->
Fields@1;
{some, M@1} ->
lists:append(Fields@1, [{<<"maximum"/utf8>>, gleam@json:int(M@1)}])
end,
_pipe = Fields@2,
_pipe@1 = with_description(_pipe, Description),
gleam@json:object(_pipe@1).
-file("src/glean/schema.gleam", 261).
-spec float_to_json(gleam@option:option(binary())) -> gleam@json:json().
float_to_json(Description) ->
_pipe = [{<<"type"/utf8>>, gleam@json:string(<<"number"/utf8>>)}],
_pipe@1 = with_description(_pipe, Description),
gleam@json:object(_pipe@1).
-file("src/glean/schema.gleam", 267).
-spec bool_to_json(gleam@option:option(binary())) -> gleam@json:json().
bool_to_json(Description) ->
_pipe = [{<<"type"/utf8>>, gleam@json:string(<<"boolean"/utf8>>)}],
_pipe@1 = with_description(_pipe, Description),
gleam@json:object(_pipe@1).
-file("src/glean/schema.gleam", 295).
-spec type_null() -> gleam@json:json().
type_null() ->
gleam@json:object([{<<"type"/utf8>>, gleam@json:string(<<"null"/utf8>>)}]).
-file("src/glean/schema.gleam", 301).
-spec object_to_json_strict(
gleam@dict:dict(binary(), schema()),
list(binary()),
gleam@option:option(binary())
) -> gleam@json:json().
object_to_json_strict(Properties, Required, Description) ->
Props = begin
_pipe = Properties,
_pipe@1 = maps:to_list(_pipe),
_pipe@2 = gleam@list:map(
_pipe@1,
fun(Entry) ->
{erlang:element(1, Entry),
to_json_strict(erlang:element(2, Entry))}
end
),
gleam@json:object(_pipe@2)
end,
Fields = [{<<"type"/utf8>>, gleam@json:string(<<"object"/utf8>>)},
{<<"properties"/utf8>>, Props}],
Fields@1 = case Required of
[] ->
Fields;
_ ->
lists:append(
Fields,
[{<<"required"/utf8>>,
gleam@json:array(Required, fun gleam@json:string/1)}]
)
end,
Fields@2 = lists:append(
Fields@1,
[{<<"additionalProperties"/utf8>>, gleam@json:bool(false)}]
),
_pipe@3 = Fields@2,
_pipe@4 = with_description(_pipe@3, Description),
gleam@json:object(_pipe@4).
-file("src/glean/schema.gleam", 150).
?DOC(
" Convert to JSON with `additionalProperties: false` on all object schemas.\n"
" Required by OpenAI strict structured outputs.\n"
).
-spec to_json_strict(schema()) -> gleam@json:json().
to_json_strict(Schema) ->
case Schema of
{object_schema, Properties, Required, Description} ->
object_to_json_strict(Properties, Required, Description);
{string_schema, Description@1, Enum_values, Min_length, Max_length} ->
string_to_json(Description@1, Enum_values, Min_length, Max_length);
{int_schema, Description@2, Minimum, Maximum} ->
int_to_json(Description@2, Minimum, Maximum);
{float_schema, Description@3} ->
float_to_json(Description@3);
{bool_schema, Description@4} ->
bool_to_json(Description@4);
{array_schema, Items, Description@5} ->
array_to_json_strict(Items, Description@5);
{nullable_schema, Inner} ->
nullable_to_json_strict(Inner);
{one_of_schema, Variants, Description@6} ->
one_of_to_json_strict(Variants, Description@6)
end.
-file("src/glean/schema.gleam", 333).
-spec array_to_json_strict(schema(), gleam@option:option(binary())) -> gleam@json:json().
array_to_json_strict(Items, Description) ->
_pipe = [{<<"type"/utf8>>, gleam@json:string(<<"array"/utf8>>)},
{<<"items"/utf8>>, to_json_strict(Items)}],
_pipe@1 = with_description(_pipe, Description),
gleam@json:object(_pipe@1).
-file("src/glean/schema.gleam", 339).
-spec nullable_to_json_strict(schema()) -> gleam@json:json().
nullable_to_json_strict(Inner) ->
gleam@json:object(
[{<<"oneOf"/utf8>>,
gleam@json:preprocessed_array(
[to_json_strict(Inner), type_null()]
)}]
).
-file("src/glean/schema.gleam", 348).
-spec one_of_to_json_strict(list(schema()), gleam@option:option(binary())) -> gleam@json:json().
one_of_to_json_strict(Variants, Description) ->
Variant_jsons = gleam@list:map(Variants, fun to_json_strict/1),
_pipe = [{<<"oneOf"/utf8>>, gleam@json:preprocessed_array(Variant_jsons)}],
_pipe@1 = with_description(_pipe, Description),
gleam@json:object(_pipe@1).
-file("src/glean/schema.gleam", 179).
-spec object_to_json(
gleam@dict:dict(binary(), schema()),
list(binary()),
gleam@option:option(binary())
) -> gleam@json:json().
object_to_json(Properties, Required, Description) ->
Props = begin
_pipe = Properties,
_pipe@1 = maps:to_list(_pipe),
_pipe@2 = gleam@list:map(
_pipe@1,
fun(Entry) ->
{erlang:element(1, Entry), to_json(erlang:element(2, Entry))}
end
),
gleam@json:object(_pipe@2)
end,
Fields = [{<<"type"/utf8>>, gleam@json:string(<<"object"/utf8>>)},
{<<"properties"/utf8>>, Props}],
Fields@1 = case Required of
[] ->
Fields;
_ ->
lists:append(
Fields,
[{<<"required"/utf8>>,
gleam@json:array(Required, fun gleam@json:string/1)}]
)
end,
_pipe@3 = Fields@1,
_pipe@4 = with_description(_pipe@3, Description),
gleam@json:object(_pipe@4).
-file("src/glean/schema.gleam", 126).
-spec to_json(schema()) -> gleam@json:json().
to_json(Schema) ->
case Schema of
{object_schema, Properties, Required, Description} ->
object_to_json(Properties, Required, Description);
{string_schema, Description@1, Enum_values, Min_length, Max_length} ->
string_to_json(Description@1, Enum_values, Min_length, Max_length);
{int_schema, Description@2, Minimum, Maximum} ->
int_to_json(Description@2, Minimum, Maximum);
{float_schema, Description@3} ->
float_to_json(Description@3);
{bool_schema, Description@4} ->
bool_to_json(Description@4);
{array_schema, Items, Description@5} ->
array_to_json(Items, Description@5);
{nullable_schema, Inner} ->
nullable_to_json(Inner);
{one_of_schema, Variants, Description@6} ->
one_of_to_json(Variants, Description@6)
end.
-file("src/glean/schema.gleam", 142).
-spec to_json_string(schema()) -> binary().
to_json_string(Schema) ->
_pipe = Schema,
_pipe@1 = to_json(_pipe),
gleam@json:to_string(_pipe@1).
-file("src/glean/schema.gleam", 273).
-spec array_to_json(schema(), gleam@option:option(binary())) -> gleam@json:json().
array_to_json(Items, Description) ->
_pipe = [{<<"type"/utf8>>, gleam@json:string(<<"array"/utf8>>)},
{<<"items"/utf8>>, to_json(Items)}],
_pipe@1 = with_description(_pipe, Description),
gleam@json:object(_pipe@1).
-file("src/glean/schema.gleam", 279).
-spec nullable_to_json(schema()) -> gleam@json:json().
nullable_to_json(Inner) ->
gleam@json:object(
[{<<"oneOf"/utf8>>,
gleam@json:preprocessed_array([to_json(Inner), type_null()])}]
).
-file("src/glean/schema.gleam", 285).
-spec one_of_to_json(list(schema()), gleam@option:option(binary())) -> gleam@json:json().
one_of_to_json(Variants, Description) ->
Variant_jsons = gleam@list:map(Variants, fun to_json/1),
_pipe = [{<<"oneOf"/utf8>>, gleam@json:preprocessed_array(Variant_jsons)}],
_pipe@1 = with_description(_pipe, Description),
gleam@json:object(_pipe@1).