aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2022-01-09 18:13:11 +0000
committerLouis Pilfold <louis@lpil.uk>2022-01-09 18:13:13 +0000
commit1d9085ea75c850ef270e1bec56c4d74721c6e9a0 (patch)
tree6a05a702fc023f754a283573af8886ce4cf7461a
parent462f4cd3d451b352a5afa1351cd2a3e99c77a4be (diff)
downloadgleam_stdlib-1d9085ea75c850ef270e1bec56c4d74721c6e9a0.tar.gz
gleam_stdlib-1d9085ea75c850ef270e1bec56c4d74721c6e9a0.zip
decode8
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/gleam/dynamic.gleam69
-rw-r--r--test/gleam/dynamic_test.gleam32
3 files changed, 102 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b4de85b..356f433 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,7 +18,7 @@
- The `dynamic.any`, `dynamic.element` and `dynamic.tuple*` functions are now
partially applied.
- The `dynamic` module gains the `decode2`, `decode3`, `decode4`, `decode5`,
- `decode6`, `decode7` functions.
+ `decode6`, `decode7`, and `decode8` 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 dfd1220..1f56172 100644
--- a/src/gleam/dynamic.gleam
+++ b/src/gleam/dynamic.gleam
@@ -1178,6 +1178,75 @@ pub fn decode7(
}
}
+/// Decode 8 values from a `Dynamic` value.
+///
+/// ## Examples
+///
+/// ```gleam
+/// > from(#(1, 2.1, "3", "4", "5", "6", "7", "8"))
+/// > |> 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),
+/// > )
+/// Ok(MyRecord(1, 2.1, "3", "4", "5", "6", "7", "8"))
+/// ```
+///
+/// ```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),
+/// > )
+/// Error([
+/// DecodeError(expected: "Int", found: "String", path: ["0"]),
+/// DecodeError(expected: "Float", found: "String", path: ["1"]),
+/// ])
+/// ```
+///
+pub fn decode8(
+ constructor: fn(t1, t2, t3, t4, t5, t6, t7, t8) -> 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),
+) -> Decoder(t) {
+ fn(x: Dynamic) {
+ case t1(x), t2(x), t3(x), t4(x), t5(x), t6(x), t7(x), t8(x) {
+ Ok(a), Ok(b), Ok(c), Ok(d), Ok(e), Ok(f), Ok(g), Ok(h) ->
+ Ok(constructor(a, b, c, d, e, f, g, h))
+ a, b, c, d, e, f, g, h ->
+ 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),
+ ]))
+ }
+ }
+}
+
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 94e4e5d..b94db28 100644
--- a/test/gleam/dynamic_test.gleam
+++ b/test/gleam/dynamic_test.gleam
@@ -956,3 +956,35 @@ pub fn decode7_test() {
DecodeError(expected: "String", found: "Float", path: ["1"]),
]))
}
+
+type Eight(a, b, c, d, e, f, g, h) {
+ Eight(a, b, c, d, e, f, g, h)
+}
+
+pub fn decode8_test() {
+ let decoder =
+ dynamic.decode8(
+ Eight,
+ 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),
+ dynamic.element(5, dynamic.int),
+ dynamic.element(6, dynamic.int),
+ dynamic.element(7, dynamic.int),
+ )
+
+ #(1, "2", 3, 4, 5, 6, 7, 8)
+ |> dynamic.from
+ |> decoder
+ |> should.equal(Ok(Eight(1, "2", 3, 4, 5, 6, 7, 8)))
+
+ #(1.3, 2.1, 3, 4, 5, 6, 7, 8)
+ |> dynamic.from
+ |> decoder
+ |> should.equal(Error([
+ DecodeError(expected: "Int", found: "Float", path: ["0"]),
+ DecodeError(expected: "String", found: "Float", path: ["1"]),
+ ]))
+}