aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2022-01-09 18:05:16 +0000
committerLouis Pilfold <louis@lpil.uk>2022-01-09 18:05:16 +0000
commit40ce5630785a5860f9e0daea82758bb125252464 (patch)
tree631dc0f9ae0fbb7bb5a958cc1f7c3bbebad21cb7
parent4d363d40a0b27e058822091b4cf66fc8c398e63f (diff)
downloadgleam_stdlib-40ce5630785a5860f9e0daea82758bb125252464.tar.gz
gleam_stdlib-40ce5630785a5860f9e0daea82758bb125252464.zip
decode6
-rw-r--r--src/gleam/dynamic.gleam61
-rw-r--r--test/gleam/dynamic_test.gleam30
2 files changed, 91 insertions, 0 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam
index b737c0a..4e2a29e 100644
--- a/src/gleam/dynamic.gleam
+++ b/src/gleam/dynamic.gleam
@@ -1052,6 +1052,67 @@ pub fn decode5(
}
}
+/// Decode 6 values from a `Dynamic` value.
+///
+/// ## Examples
+///
+/// ```gleam
+/// > from(#(1, 2.1, "3", "4", "5", "6"))
+/// > |> decode6(
+/// > MyRecord,
+/// > element(0, int),
+/// > element(1, float),
+/// > element(2, string),
+/// > element(3, string),
+/// > element(4, string),
+/// > element(5, string),
+/// > )
+/// Ok(MyRecord(1, 2.1, "3", "4", "5", "6"))
+/// ```
+///
+/// ```gleam
+/// > from(#("", "", "", "", "", ""))
+/// > |> decode6(
+/// > MyRecord,
+/// > element(0, int),
+/// > element(1, float),
+/// > element(2, string),
+/// > element(3, string),
+/// > element(4, string),
+/// > element(5, string),
+/// > )
+/// Error([
+/// DecodeError(expected: "Int", found: "String", path: ["0"]),
+/// DecodeError(expected: "Float", found: "String", path: ["1"]),
+/// ])
+/// ```
+///
+pub fn decode6(
+ constructor: fn(t1, t2, t3, t4, t5, t6) -> t,
+ t1: Decoder(t1),
+ t2: Decoder(t2),
+ t3: Decoder(t3),
+ t4: Decoder(t4),
+ t5: Decoder(t5),
+ t6: Decoder(t6),
+) -> Decoder(t) {
+ fn(x: Dynamic) {
+ case t1(x), t2(x), t3(x), t4(x), t5(x), t6(x) {
+ Ok(a), Ok(b), Ok(c), Ok(d), Ok(e), Ok(f) ->
+ Ok(constructor(a, b, c, d, e, f))
+ a, b, c, d, e, f ->
+ Error(list.flatten([
+ all_errors(a),
+ all_errors(b),
+ all_errors(c),
+ all_errors(d),
+ all_errors(e),
+ all_errors(f),
+ ]))
+ }
+ }
+}
+
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 800b121..566bb15 100644
--- a/test/gleam/dynamic_test.gleam
+++ b/test/gleam/dynamic_test.gleam
@@ -895,3 +895,33 @@ pub fn decode5_test() {
DecodeError(expected: "String", found: "Float", path: ["1"]),
]))
}
+
+type Six(a, b, c, d, e, f) {
+ Six(a, b, c, d, e, f)
+}
+
+pub fn decode6_test() {
+ let decoder =
+ dynamic.decode6(
+ Six,
+ 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),
+ )
+
+ #(1, "2", 3, 4, 5, 6)
+ |> dynamic.from
+ |> decoder
+ |> should.equal(Ok(Six(1, "2", 3, 4, 5, 6)))
+
+ #(1.3, 2.1, 3, 4, 5, 6)
+ |> dynamic.from
+ |> decoder
+ |> should.equal(Error([
+ DecodeError(expected: "Int", found: "Float", path: ["0"]),
+ DecodeError(expected: "String", found: "Float", path: ["1"]),
+ ]))
+}