Packages

Typed environment variable readers for Gleam built on top of envoy.

Current section

Files

Jump to
envoker src utils@env.erl
Raw

src/utils@env.erl

-module(utils@env).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src\\utils\\env.gleam").
-export([read_required_with_parser/4, read_required_int/2, read_required_float/2, read_required_bool/2, read_required_string/2, read_optional_with_parser/4, read_optional_int/2, read_optional_float/2, read_optional_bool/2, read_optional_string/2, read_required_with_parser_detailed/4, read_optional_with_parser_detailed/4]).
-export_type([presence/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 presence() :: required | optional.
-file("src\\utils\\env.gleam", 165).
?DOC(" Parses `true` / `false` values without case sensitivity.\n").
-spec parse_bool(binary()) -> {ok, boolean()} | {error, nil}.
parse_bool(Field) ->
case string:lowercase(Field) of
<<"true"/utf8>> ->
{ok, true};
<<"false"/utf8>> ->
{ok, false};
_ ->
{error, nil}
end.
-file("src\\utils\\env.gleam", 197).
-spec unwrap_required(
binary(),
{ok, gleam@option:option(DOF)} | {error, envoker@error:env_field_error()}
) -> {ok, DOF} | {error, envoker@error:env_field_error()}.
unwrap_required(Field_name, Value) ->
case Value of
{ok, {some, Value@1}} ->
{ok, Value@1};
{ok, none} ->
{error, {missing_field_error, Field_name}};
{error, Error} ->
{error, Error}
end.
-file("src\\utils\\env.gleam", 208).
-spec handle_missing_or_empty(
binary(),
presence(),
gleam@option:option(DOL),
boolean()
) -> {ok, gleam@option:option(DOL)} | {error, envoker@error:env_field_error()}.
handle_missing_or_empty(Field_name, Presence, Default, Is_empty) ->
case Default of
{some, Default@1} ->
{ok, {some, Default@1}};
none ->
case Presence of
optional ->
{ok, none};
required ->
{error, case Is_empty of
true ->
{empty_field_error, Field_name};
false ->
{missing_field_error, Field_name}
end}
end
end.
-file("src\\utils\\env.gleam", 173).
-spec read_value(
binary(),
presence(),
gleam@option:option(DNX),
binary(),
fun((binary()) -> {ok, DNX} | {error, gleam@option:option(binary())})
) -> {ok, gleam@option:option(DNX)} | {error, envoker@error:env_field_error()}.
read_value(Field_name, Presence, Default, Expected_type, Parser) ->
case envoy_ffi:get(Field_name) of
{ok, Raw_field} ->
Field = gleam@string:trim(Raw_field),
case Field of
<<""/utf8>> ->
handle_missing_or_empty(Field_name, Presence, Default, true);
_ ->
_pipe = Parser(Field),
_pipe@1 = gleam@result:map(
_pipe,
fun(Field@0) -> {some, Field@0} end
),
gleam@result:map_error(
_pipe@1,
fun(Details) ->
{parse_field_error,
Field_name,
Expected_type,
Field,
Details}
end
)
end;
{error, _} ->
handle_missing_or_empty(Field_name, Presence, Default, false)
end.
-file("src\\utils\\env.gleam", 104).
?DOC(
" Reads a required environment variable with a custom parser.\n"
" The parser can only return the fact of an error, without details.\n"
).
-spec read_required_with_parser(
binary(),
gleam@option:option(DMV),
binary(),
fun((binary()) -> {ok, DMV} | {error, nil})
) -> {ok, DMV} | {error, envoker@error:env_field_error()}.
read_required_with_parser(Field_name, Default, Expected_type, Parser) ->
unwrap_required(
Field_name,
read_value(
Field_name,
required,
Default,
Expected_type,
fun(Value) -> _pipe = Parser(Value),
gleam@result:map_error(_pipe, fun(_) -> none end) end
)
).
-file("src\\utils\\env.gleam", 19).
?DOC(
" Reads a required integer environment variable.\n"
" The value is normalized with `string.trim` before parsing.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_required_int(binary(), gleam@option:option(integer())) -> {ok,
integer()} |
{error, envoker@error:env_field_error()}.
read_required_int(Field_name, Default) ->
read_required_with_parser(
Field_name,
Default,
<<"Int"/utf8>>,
fun gleam_stdlib:parse_int/1
).
-file("src\\utils\\env.gleam", 39).
?DOC(
" Reads a required float environment variable.\n"
" The value is normalized with `string.trim` before parsing.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_required_float(binary(), gleam@option:option(float())) -> {ok,
float()} |
{error, envoker@error:env_field_error()}.
read_required_float(Field_name, Default) ->
read_required_with_parser(
Field_name,
Default,
<<"Float"/utf8>>,
fun gleam_stdlib:parse_float/1
).
-file("src\\utils\\env.gleam", 60).
?DOC(
" Reads a required boolean environment variable.\n"
" The value is normalized with `string.trim` before parsing.\n"
" Parsing is case-insensitive.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_required_bool(binary(), gleam@option:option(boolean())) -> {ok,
boolean()} |
{error, envoker@error:env_field_error()}.
read_required_bool(Field_name, Default) ->
read_required_with_parser(
Field_name,
Default,
<<"Bool"/utf8>>,
fun parse_bool/1
).
-file("src\\utils\\env.gleam", 81).
?DOC(
" Reads a required string environment variable.\n"
" This is not a raw read: the value always goes through `string.trim`.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_required_string(binary(), gleam@option:option(binary())) -> {ok,
binary()} |
{error, envoker@error:env_field_error()}.
read_required_string(Field_name, Default) ->
read_required_with_parser(
Field_name,
Default,
<<"String"/utf8>>,
fun(Field) -> {ok, Field} end
).
-file("src\\utils\\env.gleam", 121).
?DOC(
" Reads an optional environment variable with a custom parser.\n"
" The parser can only return the fact of an error, without details.\n"
).
-spec read_optional_with_parser(
binary(),
gleam@option:option(DNB),
binary(),
fun((binary()) -> {ok, DNB} | {error, nil})
) -> {ok, gleam@option:option(DNB)} | {error, envoker@error:env_field_error()}.
read_optional_with_parser(Field_name, Default, Expected_type, Parser) ->
read_value(
Field_name,
optional,
Default,
Expected_type,
fun(Value) -> _pipe = Parser(Value),
gleam@result:map_error(_pipe, fun(_) -> none end) end
).
-file("src\\utils\\env.gleam", 29).
?DOC(
" Reads an optional integer environment variable.\n"
" The value is normalized with `string.trim` before parsing.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_optional_int(binary(), gleam@option:option(integer())) -> {ok,
gleam@option:option(integer())} |
{error, envoker@error:env_field_error()}.
read_optional_int(Field_name, Default) ->
read_optional_with_parser(
Field_name,
Default,
<<"Int"/utf8>>,
fun gleam_stdlib:parse_int/1
).
-file("src\\utils\\env.gleam", 49).
?DOC(
" Reads an optional float environment variable.\n"
" The value is normalized with `string.trim` before parsing.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_optional_float(binary(), gleam@option:option(float())) -> {ok,
gleam@option:option(float())} |
{error, envoker@error:env_field_error()}.
read_optional_float(Field_name, Default) ->
read_optional_with_parser(
Field_name,
Default,
<<"Float"/utf8>>,
fun gleam_stdlib:parse_float/1
).
-file("src\\utils\\env.gleam", 71).
?DOC(
" Reads an optional boolean environment variable.\n"
" The value is normalized with `string.trim` before parsing.\n"
" Parsing is case-insensitive.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_optional_bool(binary(), gleam@option:option(boolean())) -> {ok,
gleam@option:option(boolean())} |
{error, envoker@error:env_field_error()}.
read_optional_bool(Field_name, Default) ->
read_optional_with_parser(
Field_name,
Default,
<<"Bool"/utf8>>,
fun parse_bool/1
).
-file("src\\utils\\env.gleam", 93).
?DOC(
" Reads an optional string environment variable.\n"
" This is not a raw read: the value always goes through `string.trim`.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_optional_string(binary(), gleam@option:option(binary())) -> {ok,
gleam@option:option(binary())} |
{error, envoker@error:env_field_error()}.
read_optional_string(Field_name, Default) ->
read_optional_with_parser(
Field_name,
Default,
<<"String"/utf8>>,
fun(Field) -> {ok, Field} end
).
-file("src\\utils\\env.gleam", 135).
?DOC(
" Reads a required environment variable with a custom parser.\n"
" The parser can return a text reason that is written into `details`.\n"
).
-spec read_required_with_parser_detailed(
binary(),
gleam@option:option(DNI),
binary(),
fun((binary()) -> {ok, DNI} | {error, binary()})
) -> {ok, DNI} | {error, envoker@error:env_field_error()}.
read_required_with_parser_detailed(Field_name, Default, Expected_type, Parser) ->
unwrap_required(
Field_name,
read_value(
Field_name,
required,
Default,
Expected_type,
fun(Value) -> _pipe = Parser(Value),
gleam@result:map_error(
_pipe,
fun(Field@0) -> {some, Field@0} end
) end
)
).
-file("src\\utils\\env.gleam", 152).
?DOC(
" Reads an optional environment variable with a custom parser.\n"
" The parser can return a text reason that is written into `details`.\n"
).
-spec read_optional_with_parser_detailed(
binary(),
gleam@option:option(DNO),
binary(),
fun((binary()) -> {ok, DNO} | {error, binary()})
) -> {ok, gleam@option:option(DNO)} | {error, envoker@error:env_field_error()}.
read_optional_with_parser_detailed(Field_name, Default, Expected_type, Parser) ->
read_value(
Field_name,
optional,
Default,
Expected_type,
fun(Value) -> _pipe = Parser(Value),
gleam@result:map_error(_pipe, fun(Field@0) -> {some, Field@0} end) end
).