diff options
author | Louis Pilfold <louis@lpil.uk> | 2022-02-22 17:53:29 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-02-22 17:53:29 +0000 |
commit | e64e7ea6f398172f58f6402b040b8daa363ff6a5 (patch) | |
tree | 1686b7e014d390b1882764a789dfb458bbf8bdbb /src | |
parent | dce080faaeb236f531516fc0ef16563bac6ab99e (diff) | |
download | gleam_stdlib-e64e7ea6f398172f58f6402b040b8daa363ff6a5.tar.gz gleam_stdlib-e64e7ea6f398172f58f6402b040b8daa363ff6a5.zip |
dynamic.decode9
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/dynamic.gleam | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index b2a4981..c6d8758 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -1283,6 +1283,79 @@ pub fn decode8( } } +/// Decode 9 values from a `Dynamic` value. +/// +/// ## Examples +/// +/// ```gleam +/// > from(#(1, 2.1, "3", "4", "5", "6", "7", "8", "9")) +/// > |> decode7( +/// > MyRecord, +/// > element(0, int), +/// > element(1, float), +/// > element(2, string), +/// > element(3, string), +/// > element(4, string), +/// > element(5, string), +/// > element(6, string), +/// > element(7, string), +/// > element(8, string), +/// > ) +/// Ok(MyRecord(1, 2.1, "3", "4", "5", "6", "7", "8", "9")) +/// ``` +/// +/// ```gleam +/// > from(#("", "", "", "", "", "", "", "", "")) +/// > |> decode7( +/// > MyRecord, +/// > element(0, int), +/// > element(1, float), +/// > element(2, string), +/// > element(3, string), +/// > element(4, string), +/// > element(5, string), +/// > element(6, string), +/// > element(7, string), +/// > element(8, string), +/// > ) +/// Error([ +/// DecodeError(expected: "Int", found: "String", path: ["0"]), +/// DecodeError(expected: "Float", found: "String", path: ["1"]), +/// ]) +/// ``` +/// +pub fn decode9( + constructor: fn(t1, t2, t3, t4, t5, t6, t7, t8, t9) -> t, + t1: Decoder(t1), + t2: Decoder(t2), + t3: Decoder(t3), + t4: Decoder(t4), + t5: Decoder(t5), + t6: Decoder(t6), + t7: Decoder(t7), + t8: Decoder(t8), + t9: Decoder(t9), +) -> Decoder(t) { + fn(x: Dynamic) { + case t1(x), t2(x), t3(x), t4(x), t5(x), t6(x), t7(x), t8(x), t9(x) { + Ok(a), Ok(b), Ok(c), Ok(d), Ok(e), Ok(f), Ok(g), Ok(h), Ok(i) -> + Ok(constructor(a, b, c, d, e, f, g, h, i)) + a, b, c, d, e, f, g, h, i -> + Error(list.flatten([ + all_errors(a), + all_errors(b), + all_errors(c), + all_errors(d), + all_errors(e), + all_errors(f), + all_errors(g), + all_errors(h), + all_errors(i), + ])) + } + } +} + fn all_errors(result: Result(a, List(DecodeError))) -> List(DecodeError) { case result { Ok(_) -> [] |