Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return index of undecodable element in dynamic.list DecodeError path #628

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions src/gleam/dynamic.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,36 @@ pub fn result(
}
}

fn do_try_map_with_index(
list: List(a),
fun: fn(a) -> Result(b, e),
acc: List(b),
index: Int,
) -> Result(List(b), #(Int, e)) {
case list {
[] -> Ok(list.reverse(acc))
[x, ..xs] ->
case fun(x) {
Ok(y) -> do_try_map_with_index(xs, fun, [y, ..acc], index + 1)
Error(error) -> Error(#(index, error))
}
}
}

/// This is the same as list.try_map but rather than just returning an error it
/// returns a tuple of the index of the element that failed and the error.
///
/// ```gleam
/// try_map_with_index([[1], [], [2]], first)
/// // -> Error(#(1, Nil))
/// ```
fn try_map_with_index(
over list: List(a),
with fun: fn(a) -> Result(b, e),
) -> Result(List(b), #(Int, e)) {
do_try_map_with_index(list, fun, [], 0)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These generic functions are still here, please remove them


/// Checks to see whether a `Dynamic` value is a list of a particular type, and
/// returns that list if it is.
///
Expand All @@ -309,7 +339,7 @@ pub fn result(
///
/// ```gleam
/// from([1, 2, 3]) |> list(of: string)
/// // -> Error([DecodeError(expected: "String", found: "Int", path: ["*"])])
/// // -> Error([DecodeError(expected: "String", found: "Int", path: ["0"])])
/// ```
///
/// ```gleam
Expand All @@ -322,9 +352,11 @@ pub fn list(
) -> Decoder(List(inner)) {
fn(dynamic) {
use list <- result.try(shallow_list(dynamic))
list
|> list.try_map(decoder_type)
|> map_errors(push_path(_, "*"))
let result = try_map_with_index(list, decoder_type)
case result {
Ok(values) -> Ok(values)
Error(#(index, errors)) -> Error(list.map(errors, push_path(_, index)))
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/gleam/dynamic_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,14 @@ pub fn list_test() {
|> dynamic.from
|> dynamic.list(dynamic.int)
|> should.equal(
Error([DecodeError(expected: "Int", found: "String", path: ["*"])]),
Error([DecodeError(expected: "Int", found: "String", path: ["0"])]),
)

[dynamic.from(1), dynamic.from("not an int")]
|> dynamic.from
|> dynamic.list(dynamic.int)
|> should.equal(
Error([DecodeError(expected: "Int", found: "String", path: ["*"])]),
Error([DecodeError(expected: "Int", found: "String", path: ["1"])]),
)
}

Expand Down
Loading