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 3 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,35 @@ pub fn result(
}
}

fn do_try_map_with_index(
list: List(a),
fun: fn(a) -> Result(b, e),
acc: List(b),
) -> 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])
Error(error) -> Error(#(list.length(acc), error))
johtso marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

/// 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, [])
}
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 +338,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 +351,12 @@ 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(_, int.to_string(index))))
}
Copy link
Member

Choose a reason for hiding this comment

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

Let's make the helper function specific

Suggested change
let result = try_map_with_index(list, decoder_type)
case result {
Ok(values) -> Ok(values)
Error(#(index, errors)) ->
Error(list.map(errors, push_path(_, int.to_string(index))))
}
let result = decode_list(list, decoder_type, 0, [])
case result {
Ok(values) -> Ok(values)
Error(#(index, errors)) ->
Error(list.map(errors, push_path(_, index)))
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You want it to be typed to take a Decoder rather than just an a -> b function? We already have a function called decode_list.

If you have an idea for a name that doesn't conflict with the existing decode_list..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lpil do you think it's alright as it is now?

}
}

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