mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 05:16:35 +08:00
Simplify SliceValidationError Error method
This commit is contained in:
parent
7a865dcf1d
commit
317d1181ec
@ -22,25 +22,16 @@ type SliceValidationError []error
|
|||||||
|
|
||||||
// Error concatenates all error elements in SliceValidationError into a single string separated by \n.
|
// Error concatenates all error elements in SliceValidationError into a single string separated by \n.
|
||||||
func (err SliceValidationError) Error() string {
|
func (err SliceValidationError) Error() string {
|
||||||
n := len(err)
|
var b strings.Builder
|
||||||
switch n {
|
for i := range err {
|
||||||
case 0:
|
if err[i] != nil {
|
||||||
return ""
|
if b.Len() > 0 {
|
||||||
default:
|
b.WriteString("\n")
|
||||||
var b strings.Builder
|
|
||||||
if err[0] != nil {
|
|
||||||
fmt.Fprintf(&b, "[%d]: %s", 0, err[0].Error())
|
|
||||||
}
|
|
||||||
if n > 1 {
|
|
||||||
for i := 1; i < n; i++ {
|
|
||||||
if err[i] != nil {
|
|
||||||
b.WriteString("\n")
|
|
||||||
fmt.Fprintf(&b, "[%d]: %s", i, err[i].Error())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
fmt.Fprintf(&b, "[%d]: %s", i, err[i].Error())
|
||||||
}
|
}
|
||||||
return b.String()
|
|
||||||
}
|
}
|
||||||
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ StructValidator = (*defaultValidator)(nil)
|
var _ StructValidator = (*defaultValidator)(nil)
|
||||||
|
@ -12,11 +12,15 @@ import (
|
|||||||
|
|
||||||
func BenchmarkSliceValidationError(b *testing.B) {
|
func BenchmarkSliceValidationError(b *testing.B) {
|
||||||
const size int = 100
|
const size int = 100
|
||||||
|
e := make(SliceValidationError, size)
|
||||||
|
for j := 0; j < size; j++ {
|
||||||
|
e[j] = errors.New(strconv.Itoa(j))
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ReportAllocs()
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
e := make(SliceValidationError, size)
|
|
||||||
for j := 0; j < size; j++ {
|
|
||||||
e[j] = errors.New(strconv.Itoa(j))
|
|
||||||
}
|
|
||||||
if len(e.Error()) == 0 {
|
if len(e.Error()) == 0 {
|
||||||
b.Errorf("error")
|
b.Errorf("error")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user