Skip to content

Commit

Permalink
feat: decode option
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackder committed Aug 29, 2024
1 parent b28716d commit 25be75b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
25 changes: 24 additions & 1 deletion src/toy.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub fn list(item: Decoder(a)) -> Decoder(List(a)) {
@external(javascript, "./toy_ffi.mjs", "is_nullish")
fn is_nullish(data: a) -> Bool

pub fn nullable(dec: Decoder(a)) -> Decoder(Option(a)) {
pub fn nullable(of dec: Decoder(a)) -> Decoder(Option(a)) {
fn(data) {
case is_nullish(data) {
True -> #(None, Ok(None))
Expand All @@ -246,6 +246,29 @@ pub fn nullable(dec: Decoder(a)) -> Decoder(Option(a)) {
}
}

@external(erlang, "toy_ffi", "decode_option")
@external(javascript, "./toy_ffi.mjs", "decode_option")
fn decode_option(value: dynamic.Dynamic) -> Result(Option(dynamic.Dynamic), Nil)

pub fn option(of dec: Decoder(a)) -> Decoder(Option(a)) {
fn(data) {
case decode_option(data) {
Ok(Some(value)) ->
case dec(value) {
#(_default, Ok(value)) -> #(None, Ok(Some(value)))
#(_default, Error(errors)) -> #(None, Error(errors))
}
Ok(None) -> #(None, Ok(None))
Error(_) -> #(
None,
Error([
ToyError(error: InvalidType("Option", string.inspect(data)), path: []),
]),
)
}
}
}

// String validation

pub fn string_email(dec: Decoder(String)) -> Decoder(String) {
Expand Down
11 changes: 9 additions & 2 deletions src/toy_ffi.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-module(toy_ffi).
-export([index/2, is_nullish/1]).
-export([index/2, is_nullish/1, decode_option/1]).

index(Tuple, Index) when is_tuple(Tuple) andalso is_integer(Index) ->
try
Expand All @@ -23,6 +23,13 @@ is_nullish(Value) ->
undefined -> true;
null -> true;
nil -> true;
none -> true;
_ -> false
end.

decode_option(Value) ->
case Value of
{some, Inner} -> {ok, {some, Inner}};
none -> {ok, none};
_ -> {error, nil}
end.

12 changes: 12 additions & 0 deletions src/toy_ffi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@ export function index(data, key) {
export function is_nullish(value) {
return value === null || value === undefined;
}

export function decode_option(value) {
if (value instanceof None) {
return new Ok(new None());
}

if (value instanceof Some) {
return new Ok(value);
}

return new Error(undefined);
}
31 changes: 31 additions & 0 deletions test/toy_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,37 @@ pub fn int_map_test() {
|> should.equal(Ok(43))
}

pub fn nullable_nil_test() {
let decoder = toy.int |> toy.nullable
let data = dynamic.from(Nil)

toy.decode(data, decoder) |> should.equal(Ok(None))
}

pub fn nullable_validated_test() {
let decoder = toy.int |> toy.int_min(10) |> toy.nullable

dynamic.from(11)
|> toy.decode(decoder)
|> should.equal(Ok(Some(11)))
}

pub fn option_none_test() {
let decoder = toy.int |> toy.option

dynamic.from(None)
|> toy.decode(decoder)
|> should.equal(Ok(None))
}

pub fn option_validated_test() {
let decoder = toy.int |> toy.int_min(10) |> toy.option

dynamic.from(Some(11))
|> toy.decode(decoder)
|> should.equal(Ok(Some(11)))
}

pub type Address {
Address(street: String, city: String, zip: Int)
}
Expand Down

0 comments on commit 25be75b

Please sign in to comment.