Skip to content

Commit 0f7aa26

Browse files
committed
Provide more informative error for dynamic.dict when key is a string
1 parent ec9cc3f commit 0f7aa26

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/gleam/dynamic.gleam

+5-1
Original file line numberDiff line numberDiff line change
@@ -976,13 +976,17 @@ pub fn dict(
976976
|> dict.to_list
977977
|> list.try_map(fn(pair) {
978978
let #(k, v) = pair
979+
let value_error = case string(k) {
980+
Ok(k) -> "values[" <> k <> "]"
981+
_ -> "values"
982+
}
979983
use k <- result.try(
980984
key_type(k)
981985
|> map_errors(push_path(_, "keys")),
982986
)
983987
use v <- result.try(
984988
value_type(v)
985-
|> map_errors(push_path(_, "values")),
989+
|> map_errors(push_path(_, value_error)),
986990
)
987991
Ok(#(k, v))
988992
}),

test/gleam/dynamic_test.gleam

+7
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,13 @@ pub fn dict_test() {
11381138
dict.from_list([#("a", 1), #("b", 2)])
11391139
|> dynamic.from
11401140
|> dynamic.dict(dynamic.string, dynamic.string)
1141+
|> should.equal(
1142+
Error([DecodeError(expected: "String", found: "Int", path: ["values[a]"])]),
1143+
)
1144+
1145+
dict.from_list([#(1, 1), #(2, 2)])
1146+
|> dynamic.from
1147+
|> dynamic.dict(dynamic.int, dynamic.string)
11411148
|> should.equal(
11421149
Error([DecodeError(expected: "String", found: "Int", path: ["values"])]),
11431150
)

0 commit comments

Comments
 (0)