aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2022-02-22 17:53:29 +0000
committerLouis Pilfold <louis@lpil.uk>2022-02-22 17:53:29 +0000
commite64e7ea6f398172f58f6402b040b8daa363ff6a5 (patch)
tree1686b7e014d390b1882764a789dfb458bbf8bdbb
parentdce080faaeb236f531516fc0ef16563bac6ab99e (diff)
downloadgleam_stdlib-e64e7ea6f398172f58f6402b040b8daa363ff6a5.tar.gz
gleam_stdlib-e64e7ea6f398172f58f6402b040b8daa363ff6a5.zip
dynamic.decode9
-rw-r--r--src/gleam/dynamic.gleam73
-rw-r--r--test/gleam/dynamic_test.gleam33
2 files changed, 106 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(_) -> []
diff --git a/test/gleam/dynamic_test.gleam b/test/gleam/dynamic_test.gleam
index 6fa2673..3e7cbd2 100644
--- a/test/gleam/dynamic_test.gleam
+++ b/test/gleam/dynamic_test.gleam
@@ -1015,3 +1015,36 @@ pub fn decode8_test() {
DecodeError(expected: "String", found: "Float", path: ["1"]),
]))
}
+
+type Nine(a, b, c, d, e, f, g, h, i) {
+ Nine(a, b, c, d, e, f, g, h, i)
+}
+
+pub fn decode9_test() {
+ let decoder =
+ dynamic.decode9(
+ Nine,
+ 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),
+ dynamic.element(8, dynamic.int),
+ )
+
+ #(1, "2", 3, 4, 5, 6, 7, 8, 9)
+ |> dynamic.from
+ |> decoder
+ |> should.equal(Ok(Nine(1, "2", 3, 4, 5, 6, 7, 8, 9)))
+
+ #(1.3, 2.1, 3, 4, 5, 6, 7, 8, 9)
+ |> dynamic.from
+ |> decoder
+ |> should.equal(Error([
+ DecodeError(expected: "Int", found: "Float", path: ["0"]),
+ DecodeError(expected: "String", found: "Float", path: ["1"]),
+ ]))
+}