Skip to content

Commit 848e2b2

Browse files
committed
Improve error message on type mismatch
Before: Key 'group.d' is not an date-local but map[type:date-local value:1979-05-27]: Expected: map[string]interface {}{"type":"date-local", "value":"1979-05-27"} Your encoder: map[string]interface {}{"type":"date", "value":"1979-05-27"} After: Key "group.d" is not "date-local" but "date": Expected: map[string]any{"type":"date-local", "value":"1979-05-27"} Your encoder: map[string]any{"type":"date", "value":"1979-05-27"}
1 parent a7ddf40 commit 848e2b2

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

json.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,15 @@ func isValue(m map[string]any) bool {
241241
}
242242

243243
func (r Test) mismatch(wantType string, want, have any) Test {
244-
return r.fail("Key '%[1]s' is not an %[2]s but %[5]s:\n"+
245-
" Expected: %#[3]v\n"+
246-
" Your encoder: %#[4]v",
247-
r.Key, wantType, want, have, fmtType(have))
244+
return r.fail("Key %[1]q is not %[2]q but %[5]q:\n"+
245+
" Expected: %s\n"+
246+
" Your encoder: %s",
247+
r.Key, wantType, fmtHashV(want), fmtHashV(have), fmtType(have))
248248
}
249249

250250
func (r Test) valMismatch(wantType, haveType string, want, have any) Test {
251-
return r.fail("Key '%s' is not an %s but %s:\n"+
252-
" Expected: %#[3]v\n"+
253-
" Your encoder: %#[4]v",
254-
r.Key, wantType, want, have)
251+
return r.fail("Key %q is not %q but %q:\n"+
252+
" Expected: %s\n"+
253+
" Your encoder: %s",
254+
r.Key, wantType, haveType, fmtHashV(want), fmtHashV(have))
255255
}

toml.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,5 @@ func isTomlValue(v any) bool {
135135
}
136136

137137
// fmt %T with "interface {}" replaced with "any", which is far more readable.
138-
func fmtType(t any) string {
139-
return strings.ReplaceAll(fmt.Sprintf("%T", t), "interface {}", "any")
140-
}
138+
func fmtType(t any) string { return strings.ReplaceAll(fmt.Sprintf("%T", t), "interface {}", "any") }
139+
func fmtHashV(t any) string { return strings.ReplaceAll(fmt.Sprintf("%#v", t), "interface {}", "any") }

0 commit comments

Comments
 (0)