Skip to content

Commit 4761952

Browse files
committed
Fix field error
1 parent 09e6469 commit 4761952

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

src/gleam/dynamic.gleam

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,9 @@ if javascript {
437437
///
438438
pub fn field(named name: a, of inner_type: Decoder(t)) -> Decoder(t) {
439439
fn(value) {
440-
try value = decode_field(value, name)
441-
inner_type(value)
440+
value
441+
|> decode_field(name)
442+
|> result.then(inner_type)
442443
|> map_errors(push_path(_, name))
443444
}
444445
}

src/gleam_stdlib.erl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ iodata_append(Iodata, String) -> [Iodata, String].
4949

5050
identity(X) -> X.
5151

52-
decode_error_msg(Expected, Data) ->
53-
{error, [{decode_error, Expected, classify_dynamic(Data), []}]}.
52+
decode_error_msg(Expected, Data) when is_binary(Expected) ->
53+
decode_error(Expected, classify_dynamic(Data)).
54+
decode_error(Expected, Got) when is_binary(Expected) andalso is_binary(Got) ->
55+
{error, [{decode_error, Expected, Got, []}]}.
5456

5557
classify_dynamic(X) when is_atom(X) -> <<"Atom">>;
5658
classify_dynamic(X) when is_binary(X) -> <<"String">>;
@@ -94,7 +96,8 @@ decode_list(Data) -> decode_error_msg(<<"List">>, Data).
9496
decode_field(Data, Key) ->
9597
case Data of
9698
#{Key := Value} -> {ok, Value};
97-
_ -> decode_error_msg("object", Data)
99+
_ ->
100+
decode_error(<<"field"/utf8>>, <<"nothing"/utf8>>)
98101
end.
99102

100103
size_of_tuple(Data) -> tuple_size(Data).

src/gleam_stdlib.mjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,10 +494,12 @@ export function classify_dynamic(data) {
494494
}
495495

496496
function decoder_error(expected, got) {
497+
return decoder_error_no_classify(expected, classify_dynamic(got));
498+
}
499+
500+
function decoder_error_no_classify(expected, got) {
497501
return new Error(
498-
List.fromArray([
499-
new DecodeError(expected, classify_dynamic(got), List.fromArray([])),
500-
])
502+
List.fromArray([new DecodeError(expected, got, List.fromArray([]))])
501503
);
502504
}
503505

@@ -560,7 +562,7 @@ export function decode_option(data, decoder) {
560562
}
561563

562564
export function decode_field(value, name) {
563-
let error = () => decoder_error("object", value);
565+
let error = () => decoder_error_no_classify("field", "nothing");
564566
if (value instanceof Map) {
565567
let entry = value.get(name);
566568
return entry.isOk() ? entry : error();

test/gleam/dynamic_test.gleam

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ if javascript {
260260
|> dynamic.from
261261
|> dynamic.field("Nope", dynamic.int)
262262
|> should.equal(Error([
263-
DecodeError(expected: "object", found: "Result", path: []),
263+
DecodeError(expected: "field", found: "nothing", path: ["Nope"]),
264264
]))
265265
}
266266
}
@@ -289,22 +289,30 @@ pub fn field_test() {
289289
|> map.insert("ok", 3)
290290
|> dynamic.from
291291
|> dynamic.field("ok", dynamic.string)
292-
|> should.be_error
292+
|> should.equal(Error([
293+
DecodeError(expected: "String", found: "Int", path: ["ok"]),
294+
]))
293295

294296
map.new()
295297
|> dynamic.from
296298
|> dynamic.field("ok", dynamic.int)
297-
|> should.be_error
299+
|> should.equal(Error([
300+
DecodeError(expected: "field", found: "nothing", path: ["ok"]),
301+
]))
298302

299303
1
300304
|> dynamic.from
301305
|> dynamic.field("ok", dynamic.int)
302-
|> should.be_error
306+
|> should.equal(Error([
307+
DecodeError(expected: "field", found: "nothing", path: ["ok"]),
308+
]))
303309

304310
[]
305311
|> dynamic.from
306312
|> dynamic.field("ok", dynamic.int)
307-
|> should.be_error
313+
|> should.equal(Error([
314+
DecodeError(expected: "field", found: "nothing", path: ["ok"]),
315+
]))
308316
}
309317

310318
pub fn element_test() {

0 commit comments

Comments
 (0)