Current section
Files
Jump to
Current section
Files
src/yodel@context.erl
-module(yodel@context).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/1, get_string/2, parse_string/2, get_string_or/3, get_int/2, parse_int/2, get_int_or/3, get_float/2, parse_float/2, get_float_or/3, get_bool/2, parse_bool/2, get_bool_or/3]).
-export_type([context/0]).
-opaque context() :: {context, yodel@properties:properties()}.
-file("src/yodel/context.gleam", 14).
-spec new(yodel@properties:properties()) -> context().
new(Properties) ->
{context, Properties}.
-file("src/yodel/context.gleam", 18).
-spec get_string(context(), binary()) -> {ok, binary()} |
{error, yodel@properties:properties_error()}.
get_string(Ctx, Path) ->
case yodel@properties:get(erlang:element(2, Ctx), Path) of
{ok, {string_value, Value}} ->
{ok, Value};
{ok, Value@1} ->
{error, {type_error, Path, {expected_string, Value@1}}};
{error, E} ->
{error, E}
end.
-file("src/yodel/context.gleam", 26).
-spec parse_string(context(), binary()) -> {ok, binary()} |
{error, yodel@properties:properties_error()}.
parse_string(Ctx, Path) ->
case get_string(Ctx, Path) of
{ok, Value} ->
{ok, Value};
{error, {type_error, _, _}} ->
case yodel@properties:get(erlang:element(2, Ctx), Path) of
{ok, Value@1} ->
{ok, gleam@string:inspect(Value@1)};
{error, E} ->
{error, E}
end;
{error, E@1} ->
{error, E@1}
end.
-file("src/yodel/context.gleam", 42).
-spec get_string_or(context(), binary(), binary()) -> binary().
get_string_or(Ctx, Path, Default) ->
case get_string(Ctx, Path) of
{ok, Value} ->
Value;
{error, _} ->
Default
end.
-file("src/yodel/context.gleam", 49).
-spec get_int(context(), binary()) -> {ok, integer()} |
{error, yodel@properties:properties_error()}.
get_int(Ctx, Path) ->
case yodel@properties:get(erlang:element(2, Ctx), Path) of
{ok, {int_value, Value}} ->
{ok, Value};
{ok, Value@1} ->
{error, {type_error, Path, {expected_int, Value@1}}};
{error, E} ->
{error, E}
end.
-file("src/yodel/context.gleam", 57).
-spec parse_int(context(), binary()) -> {ok, integer()} |
{error, yodel@properties:properties_error()}.
parse_int(Ctx, Path) ->
case get_int(Ctx, Path) of
{ok, Value} ->
{ok, Value};
{error, {type_error, _, _}} ->
case parse_string(Ctx, Path) of
{ok, Value@1} ->
_pipe = gleam_stdlib:parse_int(Value@1),
gleam@result:map_error(
_pipe,
fun(_) ->
{type_error,
Path,
{expected_int, {string_value, Value@1}}}
end
);
{error, E} ->
{error, E}
end;
{error, E@1} ->
{error, E@1}
end.
-file("src/yodel/context.gleam", 74).
-spec get_int_or(context(), binary(), integer()) -> integer().
get_int_or(Ctx, Path, Default) ->
case get_int(Ctx, Path) of
{ok, Value} ->
Value;
{error, _} ->
Default
end.
-file("src/yodel/context.gleam", 81).
-spec get_float(context(), binary()) -> {ok, float()} |
{error, yodel@properties:properties_error()}.
get_float(Ctx, Path) ->
case yodel@properties:get(erlang:element(2, Ctx), Path) of
{ok, {float_value, Value}} ->
{ok, Value};
{ok, Value@1} ->
{error, {type_error, Path, {expected_float, Value@1}}};
{error, E} ->
{error, E}
end.
-file("src/yodel/context.gleam", 89).
-spec parse_float(context(), binary()) -> {ok, float()} |
{error, yodel@properties:properties_error()}.
parse_float(Ctx, Path) ->
case get_float(Ctx, Path) of
{ok, Value} ->
{ok, Value};
{error, {type_error, _, _}} ->
case parse_string(Ctx, Path) of
{ok, Value@1} ->
_pipe = gleam_stdlib:parse_float(Value@1),
gleam@result:map_error(
_pipe,
fun(_) ->
{type_error,
Path,
{expected_float, {string_value, Value@1}}}
end
);
{error, E} ->
{error, E}
end;
{error, E@1} ->
{error, E@1}
end.
-file("src/yodel/context.gleam", 106).
-spec get_float_or(context(), binary(), float()) -> float().
get_float_or(Ctx, Path, Default) ->
case get_float(Ctx, Path) of
{ok, Value} ->
Value;
{error, _} ->
Default
end.
-file("src/yodel/context.gleam", 113).
-spec get_bool(context(), binary()) -> {ok, boolean()} |
{error, yodel@properties:properties_error()}.
get_bool(Ctx, Path) ->
case yodel@properties:get(erlang:element(2, Ctx), Path) of
{ok, {bool_value, Value}} ->
{ok, Value};
{ok, Value@1} ->
{error, {type_error, Path, {expected_bool, Value@1}}};
{error, E} ->
{error, E}
end.
-file("src/yodel/context.gleam", 121).
-spec parse_bool(context(), binary()) -> {ok, boolean()} |
{error, yodel@properties:properties_error()}.
parse_bool(Ctx, Path) ->
case get_string(Ctx, Path) of
{ok, Value} ->
case begin
_pipe = Value,
_pipe@1 = string:lowercase(_pipe),
gleam@string:trim(_pipe@1)
end of
<<"true"/utf8>> ->
{ok, true};
<<"false"/utf8>> ->
{ok, false};
_ ->
{error,
{type_error,
Path,
{expected_bool, {string_value, Value}}}}
end;
{error, E} ->
{error, E}
end.
-file("src/yodel/context.gleam", 134).
-spec get_bool_or(context(), binary(), boolean()) -> boolean().
get_bool_or(Ctx, Path, Default) ->
case get_bool(Ctx, Path) of
{ok, Value} ->
Value;
{error, _} ->
Default
end.