aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2022-01-09 18:02:34 +0000
committerLouis Pilfold <louis@lpil.uk>2022-01-09 18:02:34 +0000
commit4d363d40a0b27e058822091b4cf66fc8c398e63f (patch)
tree1445991f61c3e3f71ab8be04fac227f271502ee6
parent3ea1726ae5040af3c67bd3dc8c1b6718c69ff67b (diff)
downloadgleam_stdlib-4d363d40a0b27e058822091b4cf66fc8c398e63f.tar.gz
gleam_stdlib-4d363d40a0b27e058822091b4cf66fc8c398e63f.zip
decode5
-rw-r--r--CHANGELOG.md3
-rw-r--r--src/gleam/dynamic.gleam56
-rw-r--r--test/gleam/dynamic_test.gleam29
3 files changed, 87 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 28bbd3d..3c2dd9a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,7 +17,8 @@
- The decoder functions of the `dynamic` module now return multiple errors.
- The `dynamic.any`, `dynamic.element` and `dynamic.tuple*` functions are now
partially applied.
-- The `dynamic` module gains the `decode2`, `decode3`, `decode4` functions.
+- The `dynamic` module gains the `decode2`, `decode3`, `decode4`, `decode5`
+ functions.
- The `int` module gains the `digits` and `undigits` functions.
## v0.18.1 - 2021-12-19
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam
index b920306..b737c0a 100644
--- a/src/gleam/dynamic.gleam
+++ b/src/gleam/dynamic.gleam
@@ -996,6 +996,62 @@ pub fn decode4(
}
}
+/// Decode 5 values from a `Dynamic` value.
+///
+/// ## Examples
+///
+/// ```gleam
+/// > from(#(1, 2.1, "3", "4", "5"))
+/// > |> decode5(
+/// > MyRecord,
+/// > element(0, int),
+/// > element(1, float),
+/// > element(2, string),
+/// > element(3, string),
+/// > element(4, string),
+/// > )
+/// Ok(MyRecord(1, 2.1, "3", "4", "5"))
+/// ```
+///
+/// ```gleam
+/// > from(#("", "", "", "", ""))
+/// > |> decode5(
+/// > MyRecord,
+/// > element(0, int),
+/// > element(1, float),
+/// > element(2, string),
+/// > element(3, string),
+/// > element(4, string),
+/// > )
+/// Error([
+/// DecodeError(expected: "Int", found: "String", path: ["0"]),
+/// DecodeError(expected: "Float", found: "String", path: ["1"]),
+/// ])
+/// ```
+///
+pub fn decode5(
+ constructor: fn(t1, t2, t3, t4, t5) -> t,
+ t1: Decoder(t1),
+ t2: Decoder(t2),
+ t3: Decoder(t3),
+ t4: Decoder(t4),
+ t5: Decoder(t5),
+) -> Decoder(t) {
+ fn(x: Dynamic) {
+ case t1(x), t2(x), t3(x), t4(x), t5(x) {
+ Ok(a), Ok(b), Ok(c), Ok(d), Ok(e) -> Ok(constructor(a, b, c, d, e))
+ a, b, c, d, e ->
+ Error(list.flatten([
+ all_errors(a),
+ all_errors(b),
+ all_errors(c),
+ all_errors(d),
+ all_errors(e),
+ ]))
+ }
+ }
+}
+
fn all_errors(result: Result(a, List(DecodeError))) -> List(DecodeError) {
case result {
Ok(_) -> []
diff --git a/test/gleam/dynamic_test.gleam b/test/gleam/dynamic_test.gleam
index 245d894..800b121 100644
--- a/test/gleam/dynamic_test.gleam
+++ b/test/gleam/dynamic_test.gleam
@@ -866,3 +866,32 @@ pub fn decode4_test() {
DecodeError(expected: "String", found: "Float", path: ["1"]),
]))
}
+
+type Five(a, b, c, d, e) {
+ Five(a, b, c, d, e)
+}
+
+pub fn decode5_test() {
+ let decoder =
+ dynamic.decode5(
+ Five,
+ dynamic.element(0, dynamic.int),
+ dynamic.element(1, dynamic.string),
+ dynamic.element(2, dynamic.int),
+ dynamic.element(3, dynamic.int),
+ dynamic.element(4, dynamic.int),
+ )
+
+ #(1, "2", 3, 4, 5)
+ |> dynamic.from
+ |> decoder
+ |> should.equal(Ok(Five(1, "2", 3, 4, 5)))
+
+ #(1.3, 2.1, 3, 4, 5)
+ |> dynamic.from
+ |> decoder
+ |> should.equal(Error([
+ DecodeError(expected: "Int", found: "Float", path: ["0"]),
+ DecodeError(expected: "String", found: "Float", path: ["1"]),
+ ]))
+}