diff options
author | Louis Pilfold <louis@lpil.uk> | 2022-01-09 18:08:09 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-01-09 18:08:09 +0000 |
commit | 462f4cd3d451b352a5afa1351cd2a3e99c77a4be (patch) | |
tree | 1bec93b0eb79a03a06f62da9dc0e649b294eeab7 /src | |
parent | 40ce5630785a5860f9e0daea82758bb125252464 (diff) | |
download | gleam_stdlib-462f4cd3d451b352a5afa1351cd2a3e99c77a4be.tar.gz gleam_stdlib-462f4cd3d451b352a5afa1351cd2a3e99c77a4be.zip |
decode7
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/dynamic.gleam | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index 4e2a29e..dfd1220 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -1113,6 +1113,71 @@ pub fn decode6( } } +/// Decode 7 values from a `Dynamic` value. +/// +/// ## Examples +/// +/// ```gleam +/// > from(#(1, 2.1, "3", "4", "5", "6")) +/// > |> decode7( +/// > MyRecord, +/// > element(0, int), +/// > element(1, float), +/// > element(2, string), +/// > element(3, string), +/// > element(4, string), +/// > element(5, string), +/// > element(6, string), +/// > ) +/// Ok(MyRecord(1, 2.1, "3", "4", "5", "6", "7")) +/// ``` +/// +/// ```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), +/// > ) +/// Error([ +/// DecodeError(expected: "Int", found: "String", path: ["0"]), +/// DecodeError(expected: "Float", found: "String", path: ["1"]), +/// ]) +/// ``` +/// +pub fn decode7( + constructor: fn(t1, t2, t3, t4, t5, t6, t7) -> t, + t1: Decoder(t1), + t2: Decoder(t2), + t3: Decoder(t3), + t4: Decoder(t4), + t5: Decoder(t5), + t6: Decoder(t6), + t7: Decoder(t7), +) -> Decoder(t) { + fn(x: Dynamic) { + case t1(x), t2(x), t3(x), t4(x), t5(x), t6(x), t7(x) { + Ok(a), Ok(b), Ok(c), Ok(d), Ok(e), Ok(f), Ok(g) -> + Ok(constructor(a, b, c, d, e, f, g)) + a, b, c, d, e, f, g -> + Error(list.flatten([ + all_errors(a), + all_errors(b), + all_errors(c), + all_errors(d), + all_errors(e), + all_errors(f), + all_errors(g), + ])) + } + } +} + fn all_errors(result: Result(a, List(DecodeError))) -> List(DecodeError) { case result { Ok(_) -> [] |