Skip to content

Commit 2059841

Browse files
committed
Update FieldMismatch.String to correctly say "is an int", "is null", etc.
1 parent 731f2a0 commit 2059841

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

schema.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ type FieldMismatch struct {
4242

4343
// String returns a user friendly message explaining the type mismatch.
4444
func (f FieldMismatch) String() string {
45-
return fmt.Sprintf(`expected "%s" to be a %s but it's a %s`, f.Field, f.Expected, f.Actual)
45+
return fmt.Sprintf(
46+
`expected "%s" to be %s but it's %s`,
47+
f.Field,
48+
typeNameWithArticle(f.Expected),
49+
typeNameWithArticle(f.Actual),
50+
)
4651
}
4752

4853
// CompareOpts can be used to configure how CompareMapToStruct works.
@@ -295,3 +300,31 @@ func parseField(f reflect.StructField) (name string, ignore bool) {
295300

296301
return tag, false
297302
}
303+
304+
// typeNameStartsWithVowel returns true if the type name starts with a vowel.
305+
// This doesn't include "u" as a vowel since words like "user" should be "a user"
306+
// and not "an user".
307+
func typeNameStartsWithVowel(t string) bool {
308+
t = strings.TrimLeft(t, "*")
309+
310+
switch strings.ToLower(t[:1]) {
311+
case "a", "e", "i", "o":
312+
return true
313+
}
314+
315+
return false
316+
}
317+
318+
// typeNameWithArticle returns the type name with an indefinite article ("a" or "an").
319+
// If the type name is "null" it just returns "null".
320+
func typeNameWithArticle(t string) string {
321+
if t == "null" {
322+
return t
323+
}
324+
325+
if typeNameStartsWithVowel(t) {
326+
return "an " + t
327+
} else {
328+
return "a " + t
329+
}
330+
}

schema_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,14 +553,14 @@ func TestCompareResults_AsMap(t *testing.T) {
553553
{
554554
srcJson: `{"Foo":null}`,
555555
expected: map[string]interface{}{
556-
"Foo": `expected "Foo" to be a string but it's a null`,
556+
"Foo": `expected "Foo" to be a string but it's null`,
557557
},
558558
},
559559
{
560-
srcJson: `{"Foo":null,"Bar":true}`,
560+
srcJson: `{"Foo":1.23,"Bar":true}`,
561561
expected: map[string]interface{}{
562-
"Foo": `expected "Foo" to be a string but it's a null`,
563-
"Bar": `expected "Bar" to be a int but it's a bool`,
562+
"Foo": `expected "Foo" to be a string but it's a float64`,
563+
"Bar": `expected "Bar" to be an int but it's a bool`,
564564
},
565565
},
566566
}

0 commit comments

Comments
 (0)