From 0f7aa264d8f1c66ec385c5aacc6859d8dc1b9b4b Mon Sep 17 00:00:00 2001 From: Sebastian Porto Date: Wed, 20 Nov 2024 12:25:16 +1100 Subject: [PATCH] Provide more informative error for dynamic.dict when key is a string --- src/gleam/dynamic.gleam | 6 +++++- test/gleam/dynamic_test.gleam | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index f916016d..42db485b 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -976,13 +976,17 @@ pub fn dict( |> dict.to_list |> list.try_map(fn(pair) { let #(k, v) = pair + let value_error = case string(k) { + Ok(k) -> "values[" <> k <> "]" + _ -> "values" + } use k <- result.try( key_type(k) |> map_errors(push_path(_, "keys")), ) use v <- result.try( value_type(v) - |> map_errors(push_path(_, "values")), + |> map_errors(push_path(_, value_error)), ) Ok(#(k, v)) }), diff --git a/test/gleam/dynamic_test.gleam b/test/gleam/dynamic_test.gleam index fd51212c..8415f278 100644 --- a/test/gleam/dynamic_test.gleam +++ b/test/gleam/dynamic_test.gleam @@ -1138,6 +1138,13 @@ pub fn dict_test() { dict.from_list([#("a", 1), #("b", 2)]) |> dynamic.from |> dynamic.dict(dynamic.string, dynamic.string) + |> should.equal( + Error([DecodeError(expected: "String", found: "Int", path: ["values[a]"])]), + ) + + dict.from_list([#(1, 1), #(2, 2)]) + |> dynamic.from + |> dynamic.dict(dynamic.int, dynamic.string) |> should.equal( Error([DecodeError(expected: "String", found: "Int", path: ["values"])]), )