Skip to content

Commit

Permalink
Improve error message on type mismatch
Browse files Browse the repository at this point in the history
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"}
  • Loading branch information
arp242 committed Sep 29, 2023
1 parent a7ddf40 commit 848e2b2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
16 changes: 8 additions & 8 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ func isValue(m map[string]any) bool {
}

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

Check failure on line 244 in json.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

(github.com/toml-lang/toml-test.Test).fail format %s reads arg #6, but call has 5 args
" Expected: %s\n"+
" Your encoder: %s",
r.Key, wantType, fmtHashV(want), fmtHashV(have), fmtType(have))
}

func (r Test) valMismatch(wantType, haveType string, want, have any) Test {
return r.fail("Key '%s' is not an %s but %s:\n"+
" Expected: %#[3]v\n"+
" Your encoder: %#[4]v",
r.Key, wantType, want, have)
return r.fail("Key %q is not %q but %q:\n"+
" Expected: %s\n"+
" Your encoder: %s",
r.Key, wantType, haveType, fmtHashV(want), fmtHashV(have))
}
5 changes: 2 additions & 3 deletions toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,5 @@ func isTomlValue(v any) bool {
}

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

0 comments on commit 848e2b2

Please sign in to comment.