|
| 1 | +/// A dynamically-typed Erlang term with composable decoders for extracting |
| 2 | +/// typed values. Use this instead of bare `obj` when the runtime shape is |
| 3 | +/// unknown and must be validated. |
| 4 | +/// |
| 5 | +/// Inspired by Gleam's `gleam/dynamic/decode` module. |
| 6 | +namespace Fable.Beam |
| 7 | + |
| 8 | +open Fable.Core |
| 9 | + |
| 10 | +/// An Erlang term of unknown static type. Construct from untyped Erlang |
| 11 | +/// responses and narrow with the combinators in the `Decode` module. |
| 12 | +[<Erase>] |
| 13 | +type Dynamic = Dynamic of obj |
| 14 | + |
| 15 | +/// Decoder combinators for extracting typed values from a `Dynamic`. |
| 16 | +/// Each decoder returns `Result<'T, string>` where the error is a human-readable message. |
| 17 | +/// |
| 18 | +/// WORKAROUND: Emit expressions wrapped in `(fun() -> ... end)()` to prevent |
| 19 | +/// Erlang "unsafe variable" errors. Remove IIFEs once fixed in Fable. |
| 20 | +module Decode = |
| 21 | + |
| 22 | + /// Decode a Dynamic as an integer. |
| 23 | + [<Emit("(fun() -> case erlang:is_integer($0) of true -> {ok, $0}; false -> {error, <<\"expected integer\">>} end end)()")>] |
| 24 | + let int (d: Dynamic) : Result<int, string> = nativeOnly |
| 25 | + |
| 26 | + /// Decode a Dynamic as a float. |
| 27 | + [<Emit("(fun() -> case erlang:is_float($0) of true -> {ok, $0}; false -> {error, <<\"expected float\">>} end end)()")>] |
| 28 | + let float (d: Dynamic) : Result<float, string> = nativeOnly |
| 29 | + |
| 30 | + /// Decode a Dynamic as a boolean (atoms `true` or `false`). |
| 31 | + [<Emit("(fun() -> case erlang:is_boolean($0) of true -> {ok, $0}; false -> {error, <<\"expected boolean\">>} end end)()")>] |
| 32 | + let bool (d: Dynamic) : Result<bool, string> = nativeOnly |
| 33 | + |
| 34 | + /// Decode a Dynamic as an atom. |
| 35 | + [<Emit("(fun() -> case erlang:is_atom($0) of true -> {ok, $0}; false -> {error, <<\"expected atom\">>} end end)()")>] |
| 36 | + let atom (d: Dynamic) : Result<Atom, string> = nativeOnly |
| 37 | + |
| 38 | + /// Decode a Dynamic as a binary string. |
| 39 | + [<Emit("(fun() -> case erlang:is_binary($0) of true -> {ok, $0}; false -> {error, <<\"expected binary string\">>} end end)()")>] |
| 40 | + let string (d: Dynamic) : Result<string, string> = nativeOnly |
| 41 | + |
| 42 | + /// Return the Dynamic unchanged. Useful as a terminal decoder in `field` |
| 43 | + /// chains when you want to hand off further decoding to the caller. |
| 44 | + [<Emit("{ok, $0}")>] |
| 45 | + let dynamic (d: Dynamic) : Result<Dynamic, string> = nativeOnly |
| 46 | + |
| 47 | + /// Extract a field from an Erlang map and decode it with the given decoder. |
| 48 | + /// Returns Error if the map doesn't contain the key or the inner decoder fails. |
| 49 | + /// Uses System.Func for the decoder — matches the Lists.fs convention for |
| 50 | + /// callbacks and avoids curried-arg substitution issues with Emit. |
| 51 | + [<Emit("(fun() -> case maps:find($0, $2) of {ok, FieldVal__} -> $1(FieldVal__); error -> {error, erlang:list_to_binary(io_lib:format(<<\"missing field ~p\">>, [$0]))} end end)()")>] |
| 52 | + let field (key: Atom) (decoder: System.Func<Dynamic, Result<'V, string>>) (d: Dynamic) : Result<'V, string> = |
| 53 | + nativeOnly |
| 54 | + |
| 55 | + /// Decode a Dynamic as a list of values, decoding each element with `decoder`. |
| 56 | + /// Short-circuits on the first decode error. |
| 57 | + [<Emit("(fun() -> case erlang:is_list($1) of true -> DecodeListFold__ = fun (_, {error, _} = E__) -> E__; (Elem__, {ok, Acc__}) -> case $0(Elem__) of {ok, V__} -> {ok, [V__ | Acc__]}; {error, _} = E__ -> E__ end end, case lists:foldl(DecodeListFold__, {ok, []}, $1) of {ok, Rev__} -> {ok, fable_utils:new_ref(lists:reverse(Rev__))}; {error, _} = E__ -> E__ end; false -> {error, <<\"expected list\">>} end end)()")>] |
| 58 | + let list (decoder: System.Func<Dynamic, Result<'V, string>>) (d: Dynamic) : Result<'V array, string> = nativeOnly |
| 59 | + |
| 60 | + /// Decode a Dynamic that may be the atom `undefined` (mapped to None) or |
| 61 | + /// a value decoded by the inner decoder (mapped to Some). |
| 62 | + [<Emit("(fun() -> case $1 of undefined -> {ok, undefined}; V__ -> case $0(V__) of {ok, V2__} -> {ok, V2__}; {error, _} = E__ -> E__ end end end)()")>] |
| 63 | + let optional (decoder: System.Func<Dynamic, Result<'V, string>>) (d: Dynamic) : Result<'V option, string> = |
| 64 | + nativeOnly |
| 65 | + |
| 66 | + /// Decode a 2-tuple by applying each decoder to the corresponding element. |
| 67 | + [<Emit("(fun() -> case erlang:is_tuple($2) andalso erlang:tuple_size($2) =:= 2 of true -> case $0(erlang:element(1, $2)) of {ok, A__} -> case $1(erlang:element(2, $2)) of {ok, B__} -> {ok, {A__, B__}}; {error, _} = E__ -> E__ end; {error, _} = E__ -> E__ end; false -> {error, <<\"expected 2-tuple\">>} end end)()")>] |
| 68 | + let tuple2 |
| 69 | + (decA: System.Func<Dynamic, Result<'A, string>>) |
| 70 | + (decB: System.Func<Dynamic, Result<'B, string>>) |
| 71 | + (d: Dynamic) |
| 72 | + : Result<'A * 'B, string> = |
| 73 | + nativeOnly |
| 74 | + |
| 75 | + /// Lift a plain value into the Result monad. Useful for building records |
| 76 | + /// after extracting all fields successfully. |
| 77 | + let inline succeed (value: 'V) : Result<'V, string> = Ok value |
| 78 | + |
| 79 | + /// Map the successful value of a decode result through a function. |
| 80 | + let inline map (f: 'A -> 'B) (r: Result<'A, string>) : Result<'B, string> = Result.map f r |
| 81 | + |
| 82 | + /// Chain decode results: run the first, and if it succeeds, run the second |
| 83 | + /// with its value. |
| 84 | + let inline andThen (f: 'A -> Result<'B, string>) (r: Result<'A, string>) : Result<'B, string> = Result.bind f r |
0 commit comments