diff options
-rw-r--r-- | src/gleam/dynamic.gleam | 62 | ||||
-rw-r--r-- | src/gleam_stdlib.erl | 27 | ||||
-rw-r--r-- | test/gleam/dynamic_test.gleam | 64 |
3 files changed, 80 insertions, 73 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index a3d1549..d2c019a 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -42,7 +42,7 @@ if erlang { /// True /// /// > bit_string(from(123)) - /// Error(DecoderError(expected: "bit_string", found: "int")) + /// Error(DecoderError(expected: "BitString", found: "Int")) /// pub external fn bit_string(from: Dynamic) -> Result(BitString, DecodeError) = "gleam_stdlib" "decode_bit_string" @@ -56,15 +56,21 @@ if erlang { /// Ok("Hello") /// /// > string(from(123)) - /// Error(DecoderError(expected: "string", found: "int")) + /// Error(DecoderError(expected: "String", found: "Int")) /// pub fn string(from: Dynamic) -> Result(String, DecodeError) { bit_string(from) + |> result.map_error(fn(error) { + case error { + DecodeError(_expected, found) -> + // Convert error so it doesn't say expected 'BitString' + DecodeError(expected: "String", found: found) + } + }) |> result.then(fn(raw) { case bit_string.to_string(raw) { Ok(string) -> Ok(string) - Error(Nil) -> - Error(DecodeError(expected: "string", found: "bit_string")) + Error(Nil) -> Error(DecodeError(expected: "String", found: "BitString")) } }) } @@ -78,7 +84,7 @@ if erlang { /// Ok(123) /// /// > int(from("Hello")) - /// Error(DecoderError(expected: "int", found: "string")) + /// Error(DecoderError(expected: "Int", found: "String")) /// pub external fn int(from: Dynamic) -> Result(Int, DecodeError) = "gleam_stdlib" "decode_int" @@ -92,7 +98,7 @@ if erlang { /// Ok(2.0) /// /// > float(from(123)) - /// Error(DecoderError(expected: "float", found: "int")) + /// Error(DecoderError(expected: "Float", found: "Int")) /// pub external fn float(from: Dynamic) -> Result(Float, DecodeError) = "gleam_stdlib" "decode_float" @@ -106,7 +112,7 @@ if erlang { /// Ok(True) /// /// > bool(from(123)) - /// Error(DecoderError(expected: "bool", found: "int")) + /// Error(DecoderError(expected: "bool", found: "Int")) /// pub external fn bool(from: Dynamic) -> Result(Bool, DecodeError) = "gleam_stdlib" "decode_bool" @@ -122,7 +128,7 @@ if erlang { /// True /// /// > thunk(from(123)) - /// Error(DecoderError(expected: "zero arity function", found: "int")) + /// Error(DecoderError(expected: "zero arity function", found: "Int")) /// pub external fn thunk(from: Dynamic) -> Result(fn() -> Dynamic, DecodeError) = "gleam_stdlib" "decode_thunk" @@ -139,7 +145,7 @@ if erlang { /// Ok([from("a"), from("b"), from("c")]) /// /// > list(1) - /// Error(DecoderError(expected: "int", found: "binary")) + /// Error(DecoderError(expected: "Int", found: "Int")) /// pub external fn list(from: Dynamic) -> Result(List(Dynamic), DecodeError) = "gleam_stdlib" "decode_list" @@ -156,7 +162,7 @@ if erlang { /// Ok(Error(from("boom"))) /// /// > result(from(123)) - /// Error(DecoderError(expected: "2 element tuple", found: "int")) + /// Error(DecoderError(expected: "2 element tuple", found: "Int")) /// pub external fn result( Dynamic, @@ -178,7 +184,7 @@ if erlang { /// Ok(Error("boom")) /// /// > typed_result(of: from(123), ok: int, error: string) - /// Error(DecoderError(expected: "2 element tuple", found: "int")) + /// Error(DecoderError(expected: "2 element tuple", found: "Int")) /// pub fn typed_result( of dynamic: Dynamic, @@ -215,10 +221,10 @@ if erlang { /// Ok(["a", "b", "c"]) /// /// > typed_list(from([1, 2, 3]), of: string) - /// Error(DecoderError(expected: "int", found: "binary")) + /// Error(DecoderError(expected: "String", found: "Int")) /// /// > typed_list(from("ok"), of: string) - /// Error(DecoderError(expected: "list", found: "binary")) + /// Error(DecoderError(expected: "List", found: "String")) /// pub fn typed_list( from dynamic: Dynamic, @@ -250,7 +256,7 @@ if erlang { /// Ok(None) /// /// > option(from(123), string) - /// Error(DecoderError(expected: "bit_string", found: "int")) + /// Error(DecoderError(expected: "BitString", found: "Int")) /// pub external fn optional( from: Dynamic, @@ -270,7 +276,7 @@ if erlang { /// Ok(Dynamic) /// /// > field(from(123), "Hello") - /// Error(DecoderError(expected: "map", found: "int")) + /// Error(DecoderError(expected: "Map", found: "Int")) /// pub external fn field(from: Dynamic, named: a) -> Result(Dynamic, DecodeError) = "gleam_stdlib" "decode_field" @@ -287,7 +293,7 @@ if erlang { /// Error(DecoderError(expected: "3 element tuple", found: "2 element tuple")) /// /// > element(from(""), 2) - /// Error(DecoderError(expected: "tuple", found: "binary")) + /// Error(DecoderError(expected: "Tuple", found: "String")) /// pub external fn element( from: Dynamic, @@ -309,7 +315,7 @@ if erlang { /// Error(DecoderError(expected: "2 element tuple", found: "3 element tuple")) /// /// > tuple2(from("")) - /// Error(DecoderError(expected: "2 element tuple", found: "binary")) + /// Error(DecoderError(expected: "2 element tuple", found: "String")) /// pub external fn tuple2( from: Dynamic, @@ -334,7 +340,7 @@ if erlang { /// Error(DecoderError(expected: "2 element tuple", found: "3 element tuple")) /// /// > typed_tuple2(from(""), int, float) - /// Error(DecoderError(expected: "2 element tuple", found: "binary")) + /// Error(DecoderError(expected: "2 element tuple", found: "String")) /// pub fn typed_tuple2( from tup: Dynamic, @@ -361,7 +367,7 @@ if erlang { /// Error(DecoderError(expected: "3 element tuple", found: "3 element tuple")) /// /// > tuple3(from("")) - /// Error(DecoderError(expected: "3 element tuple", found: "binary")) + /// Error(DecoderError(expected: "3 element tuple", found: "String")) /// pub external fn tuple3( from: Dynamic, @@ -386,7 +392,7 @@ if erlang { /// Error(DecoderError(expected: "3 element tuple", found: "2 element tuple")) /// /// > typed_tuple3(from(""), int, float, string) - /// Error(DecoderError(expected: "3 element tuple", found: "binary")) + /// Error(DecoderError(expected: "3 element tuple", found: "String")) /// pub fn typed_tuple3( from tup: Dynamic, @@ -415,7 +421,7 @@ if erlang { /// Error(DecoderError(expected: "4 element tuple", found: "2 element tuple")) /// /// > tuple4(from("")) - /// Error(DecoderError(expected: "4 element tuple", found: "binary")) + /// Error(DecoderError(expected: "4 element tuple", found: "String")) /// pub external fn tuple4( from: Dynamic, @@ -441,7 +447,7 @@ if erlang { /// Error(DecoderError(expected: "4 element tuple", found: "2 element tuple")) /// /// > typed_tuple4(from(""), int, float, string, int) - /// Error(DecoderError(expected: "4 element tuple", found: "binary")) + /// Error(DecoderError(expected: "4 element tuple", found: "String")) /// pub fn typed_tuple4( from tup: Dynamic, @@ -472,7 +478,7 @@ if erlang { /// Error(DecoderError(expected: "5 element tuple", found: "2 element tuple")) /// /// > tuple5(from("")) - /// Error(DecoderError(expected: "5 element tuple", found: "binary")) + /// Error(DecoderError(expected: "5 element tuple", found: "String")) /// pub external fn tuple5( from: Dynamic, @@ -497,7 +503,7 @@ if erlang { /// Error(DecoderError(expected: "5 element tuple", found: "2 element tuple")) /// /// > typed_tuple5(from(""), int, float, string, int, int) - /// Error(DecoderError(expected: "5 element tuple", found: "binary")) + /// Error(DecoderError(expected: "5 element tuple", found: "String")) /// pub fn typed_tuple5( from tup: Dynamic, @@ -530,7 +536,7 @@ if erlang { /// Error(DecoderError(expected: "6 element tuple", found: "2 element tuple")) /// /// > tuple6(from("")) - /// Error(DecoderError(expected: "6 element tuple", found: "binary")) + /// Error(DecoderError(expected: "6 element tuple", found: "String")) /// pub external fn tuple6( from: Dynamic, @@ -558,7 +564,7 @@ if erlang { /// Error(DecoderError(expected: "6 element tuple", found: "2 element tuple")) /// /// > typed_tuple6(from(""), int, float, string, int, int, int) - /// Error(DecoderError(expected: "6 element tuple", found: "binary")) + /// Error(DecoderError(expected: "6 element tuple", found: "String")) /// pub fn typed_tuple6( from tup: Dynamic, @@ -588,10 +594,10 @@ if erlang { /// Ok(map.new()) /// /// > map(from(1)) - /// Error(DecoderError(expected: "map", found: "int")) + /// Error(DecoderError(expected: "Map", found: "Int")) /// /// > map(from("")) - /// Error(DecoderError(expected: "map", found: "binary")) + /// Error(DecoderError(expected: "Map", found: "String")) /// pub external fn map( from: Dynamic, diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl index a3148c8..d960a4f 100644 --- a/src/gleam_stdlib.erl +++ b/src/gleam_stdlib.erl @@ -39,12 +39,13 @@ identity(X) -> X. decode_error_msg(Expected, Data) -> {error, {decode_error, Expected, classify(Data)}}. -classify(X) when is_atom(X) -> <<"atom">>; -classify(X) when is_binary(X) -> <<"binary">>; -classify(X) when is_integer(X) -> <<"int">>; -classify(X) when is_float(X) -> <<"float">>; -classify(X) when is_list(X) -> <<"list">>; -classify(X) when is_boolean(X) -> <<"bool">>; +classify(X) when is_atom(X) -> <<"Atom">>; +classify(X) when is_binary(X) -> <<"String">>; +classify(X) when is_bitstring(X) -> <<"BitString">>; +classify(X) when is_integer(X) -> <<"Int">>; +classify(X) when is_float(X) -> <<"Float">>; +classify(X) when is_list(X) -> <<"List">>; +classify(X) when is_boolean(X) -> <<"Bool">>; classify(X) when is_function(X, 0) -> <<"zero arity function">>; classify(X) when is_tuple(X) -> iolist_to_binary([integer_to_list(tuple_size(X)), " element tuple"]); classify(_) -> "some other type". @@ -65,25 +66,25 @@ decode_tuple6({_, _, _, _, _, _} = T) -> {ok, T}; decode_tuple6(Data) -> decode_error_msg(<<"6 element tuple">>, Data). decode_map(Data) when is_map(Data) -> {ok, Data}; -decode_map(Data) -> decode_error_msg(<<"map">>, Data). +decode_map(Data) -> decode_error_msg(<<"Map">>, Data). decode_bit_string(Data) when is_bitstring(Data) -> {ok, Data}; -decode_bit_string(Data) -> decode_error_msg(<<"bit_string">>, Data). +decode_bit_string(Data) -> decode_error_msg(<<"BitString">>, Data). decode_int(Data) when is_integer(Data) -> {ok, Data}; -decode_int(Data) -> decode_error_msg(<<"int">>, Data). +decode_int(Data) -> decode_error_msg(<<"Int">>, Data). decode_float(Data) when is_float(Data) -> {ok, Data}; -decode_float(Data) -> decode_error_msg(<<"float">>, Data). +decode_float(Data) -> decode_error_msg(<<"Float">>, Data). decode_bool(Data) when is_boolean(Data) -> {ok, Data}; -decode_bool(Data) -> decode_error_msg(<<"bool">>, Data). +decode_bool(Data) -> decode_error_msg(<<"Bool">>, Data). decode_thunk(Data) when is_function(Data, 0) -> {ok, Data}; decode_thunk(Data) -> decode_error_msg("zero arity function", Data). decode_list(Data) when is_list(Data) -> {ok, Data}; -decode_list(Data) -> decode_error_msg(<<"list">>, Data). +decode_list(Data) -> decode_error_msg(<<"List">>, Data). decode_field(Data, Key) -> case Data of @@ -102,7 +103,7 @@ decode_element(Data, Position) when is_tuple(Data) -> Value -> {ok, Value} end; -decode_element(Data, _Position) -> decode_error_msg("a tuple", Data). +decode_element(Data, _Position) -> decode_error_msg(<<"Tuple">>, Data). decode_optional(Term, F) -> Decode = fun(Inner) -> diff --git a/test/gleam/dynamic_test.gleam b/test/gleam/dynamic_test.gleam index 8f90635..5f6ae21 100644 --- a/test/gleam/dynamic_test.gleam +++ b/test/gleam/dynamic_test.gleam @@ -26,12 +26,12 @@ if erlang { 1 |> dynamic.from |> dynamic.bit_string - |> should.equal(Error(DecodeError(expected: "bit_string", found: "int"))) + |> should.equal(Error(DecodeError(expected: "BitString", found: "Int"))) [] |> dynamic.from |> dynamic.bit_string - |> should.equal(Error(DecodeError(expected: "bit_string", found: "list"))) + |> should.equal(Error(DecodeError(expected: "BitString", found: "List"))) } pub fn string_test() { @@ -48,17 +48,17 @@ if erlang { <<65535:16>> |> dynamic.from |> dynamic.string - |> should.equal(Error(DecodeError(expected: "string", found: "bit_string"))) + |> should.equal(Error(DecodeError(expected: "String", found: "BitString"))) 1 |> dynamic.from |> dynamic.string - |> should.equal(Error(DecodeError(expected: "bit_string", found: "int"))) + |> should.equal(Error(DecodeError(expected: "String", found: "Int"))) [] |> dynamic.from |> dynamic.string - |> should.equal(Error(DecodeError(expected: "bit_string", found: "list"))) + |> should.equal(Error(DecodeError(expected: "String", found: "List"))) } pub fn int_test() { @@ -75,12 +75,12 @@ if erlang { 1.0 |> dynamic.from |> dynamic.int - |> should.equal(Error(DecodeError(expected: "int", found: "float"))) + |> should.equal(Error(DecodeError(expected: "Int", found: "Float"))) [] |> dynamic.from |> dynamic.int - |> should.equal(Error(DecodeError(expected: "int", found: "list"))) + |> should.equal(Error(DecodeError(expected: "Int", found: "List"))) } pub fn float_test() { @@ -97,12 +97,12 @@ if erlang { 1 |> dynamic.from |> dynamic.float - |> should.equal(Error(DecodeError(expected: "float", found: "int"))) + |> should.equal(Error(DecodeError(expected: "Float", found: "Int"))) [] |> dynamic.from |> dynamic.float - |> should.equal(Error(DecodeError(expected: "float", found: "list"))) + |> should.equal(Error(DecodeError(expected: "Float", found: "List"))) } pub fn thunk_test() { @@ -147,12 +147,12 @@ if erlang { 1 |> dynamic.from |> dynamic.bool - |> should.equal(Error(DecodeError(expected: "bool", found: "int"))) + |> should.equal(Error(DecodeError(expected: "Bool", found: "Int"))) [] |> dynamic.from |> dynamic.bool - |> should.equal(Error(DecodeError(expected: "bool", found: "list"))) + |> should.equal(Error(DecodeError(expected: "Bool", found: "List"))) } pub fn typed_list_test() { @@ -306,7 +306,7 @@ if erlang { 1 |> dynamic.from |> dynamic.tuple2 - |> should.equal(Error(DecodeError(expected: "2 element tuple", found: "int"))) + |> should.equal(Error(DecodeError(expected: "2 element tuple", found: "Int"))) } pub fn typed_tuple2_test() { @@ -323,7 +323,7 @@ if erlang { #(1, "") |> dynamic.from |> dynamic.typed_tuple2(dynamic.int, dynamic.int) - |> should.equal(Error(DecodeError(expected: "int", found: "binary"))) + |> should.equal(Error(DecodeError(expected: "Int", found: "String"))) #(1, 2, 3) |> dynamic.from @@ -336,7 +336,7 @@ if erlang { 1 |> dynamic.from |> dynamic.typed_tuple2(dynamic.int, dynamic.int) - |> should.equal(Error(DecodeError(expected: "2 element tuple", found: "int"))) + |> should.equal(Error(DecodeError(expected: "2 element tuple", found: "Int"))) } pub fn tuple3_test() { @@ -361,7 +361,7 @@ if erlang { 1 |> dynamic.from |> dynamic.tuple3 - |> should.equal(Error(DecodeError(expected: "3 element tuple", found: "int"))) + |> should.equal(Error(DecodeError(expected: "3 element tuple", found: "Int"))) } pub fn typed_tuple3_test() { @@ -378,7 +378,7 @@ if erlang { #(1, 2, "") |> dynamic.from |> dynamic.typed_tuple3(dynamic.int, dynamic.int, dynamic.int) - |> should.equal(Error(DecodeError(expected: "int", found: "binary"))) + |> should.equal(Error(DecodeError(expected: "Int", found: "String"))) #(1, 2) |> dynamic.from @@ -391,7 +391,7 @@ if erlang { 1 |> dynamic.from |> dynamic.typed_tuple3(dynamic.int, dynamic.int, dynamic.int) - |> should.equal(Error(DecodeError(expected: "3 element tuple", found: "int"))) + |> should.equal(Error(DecodeError(expected: "3 element tuple", found: "Int"))) } pub fn tuple4_test() { @@ -426,7 +426,7 @@ if erlang { 1 |> dynamic.from |> dynamic.tuple4 - |> should.equal(Error(DecodeError(expected: "4 element tuple", found: "int"))) + |> should.equal(Error(DecodeError(expected: "4 element tuple", found: "Int"))) } pub fn typed_tuple4_test() { @@ -448,7 +448,7 @@ if erlang { #(1, 2, 3, "") |> dynamic.from |> dynamic.typed_tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int) - |> should.equal(Error(DecodeError(expected: "int", found: "binary"))) + |> should.equal(Error(DecodeError(expected: "Int", found: "String"))) #(1, 2) |> dynamic.from @@ -461,7 +461,7 @@ if erlang { 1 |> dynamic.from |> dynamic.typed_tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int) - |> should.equal(Error(DecodeError(expected: "4 element tuple", found: "int"))) + |> should.equal(Error(DecodeError(expected: "4 element tuple", found: "Int"))) } pub fn tuple5_test() { @@ -498,7 +498,7 @@ if erlang { 1 |> dynamic.from |> dynamic.tuple5 - |> should.equal(Error(DecodeError(expected: "5 element tuple", found: "int"))) + |> should.equal(Error(DecodeError(expected: "5 element tuple", found: "Int"))) } pub fn typed_tuple5_test() { @@ -533,7 +533,7 @@ if erlang { dynamic.int, dynamic.int, ) - |> should.equal(Error(DecodeError(expected: "int", found: "binary"))) + |> should.equal(Error(DecodeError(expected: "Int", found: "String"))) #(1, 2) |> dynamic.from @@ -558,7 +558,7 @@ if erlang { dynamic.int, dynamic.int, ) - |> should.equal(Error(DecodeError(expected: "5 element tuple", found: "int"))) + |> should.equal(Error(DecodeError(expected: "5 element tuple", found: "Int"))) } pub fn tuple6_test() { @@ -597,7 +597,7 @@ if erlang { 1 |> dynamic.from |> dynamic.tuple6 - |> should.equal(Error(DecodeError(expected: "6 element tuple", found: "int"))) + |> should.equal(Error(DecodeError(expected: "6 element tuple", found: "Int"))) } pub fn typed_tuple6_test() { @@ -635,7 +635,7 @@ if erlang { dynamic.int, dynamic.int, ) - |> should.equal(Error(DecodeError(expected: "int", found: "binary"))) + |> should.equal(Error(DecodeError(expected: "Int", found: "String"))) #(1, 2) |> dynamic.from @@ -662,7 +662,7 @@ if erlang { dynamic.int, dynamic.int, ) - |> should.equal(Error(DecodeError(expected: "6 element tuple", found: "int"))) + |> should.equal(Error(DecodeError(expected: "6 element tuple", found: "Int"))) } pub fn map_test() { @@ -674,7 +674,7 @@ if erlang { 1 |> dynamic.from |> dynamic.map - |> should.equal(Error(DecodeError(expected: "map", found: "int"))) + |> should.equal(Error(DecodeError(expected: "Map", found: "Int"))) } pub fn list_test() { @@ -696,7 +696,7 @@ if erlang { 1 |> dynamic.from |> dynamic.list - |> should.equal(Error(DecodeError(expected: "list", found: "int"))) + |> should.equal(Error(DecodeError(expected: "List", found: "Int"))) } pub fn result_test() { @@ -713,7 +713,7 @@ if erlang { 1 |> dynamic.from |> dynamic.result - |> should.equal(Error(DecodeError(expected: "result tuple", found: "int"))) + |> should.equal(Error(DecodeError(expected: "result tuple", found: "Int"))) #("bad", "value") |> dynamic.from @@ -738,16 +738,16 @@ if erlang { Ok("1") |> dynamic.from |> dynamic.typed_result(ok: dynamic.int, error: dynamic.string) - |> should.equal(Error(DecodeError(expected: "int", found: "binary"))) + |> should.equal(Error(DecodeError(expected: "Int", found: "String"))) Error(1) |> dynamic.from |> dynamic.typed_result(ok: dynamic.int, error: dynamic.string) - |> should.equal(Error(DecodeError(expected: "bit_string", found: "int"))) + |> should.equal(Error(DecodeError(expected: "String", found: "Int"))) 1 |> dynamic.from |> dynamic.typed_result(ok: dynamic.int, error: dynamic.string) - |> should.equal(Error(DecodeError(expected: "result tuple", found: "int"))) + |> should.equal(Error(DecodeError(expected: "result tuple", found: "Int"))) } } |