Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Notealot 2021-11-22 13:50:00 +08:00
commit 61a624715a
3 changed files with 14 additions and 14 deletions

View File

@ -18,10 +18,10 @@ type defaultValidator struct {
validate *validator.Validate validate *validator.Validate
} }
type sliceValidateError []error type SliceValidationError []error
// Error concatenates all error elements in sliceValidateError into a single string separated by \n. // Error concatenates all error elements in SliceValidationError into a single string separated by \n.
func (err sliceValidateError) Error() string { func (err SliceValidationError) Error() string {
n := len(err) n := len(err)
switch n { switch n {
case 0: case 0:
@ -59,7 +59,7 @@ func (v *defaultValidator) ValidateStruct(obj interface{}) error {
return v.validateStruct(obj) return v.validateStruct(obj)
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
count := value.Len() count := value.Len()
validateRet := make(sliceValidateError, 0) validateRet := make(SliceValidationError, 0)
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
if err := v.ValidateStruct(value.Index(i).Interface()); err != nil { if err := v.ValidateStruct(value.Index(i).Interface()); err != nil {
validateRet = append(validateRet, err) validateRet = append(validateRet, err)

View File

@ -6,10 +6,10 @@ import (
"testing" "testing"
) )
func BenchmarkSliceValidateError(b *testing.B) { func BenchmarkSliceValidationError(b *testing.B) {
const size int = 100 const size int = 100
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
e := make(sliceValidateError, size) e := make(SliceValidationError, size)
for j := 0; j < size; j++ { for j := 0; j < size; j++ {
e[j] = errors.New(strconv.Itoa(j)) e[j] = errors.New(strconv.Itoa(j))
} }

View File

@ -9,24 +9,24 @@ import (
"testing" "testing"
) )
func TestSliceValidateError(t *testing.T) { func TestSliceValidationError(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
err sliceValidateError err SliceValidationError
want string want string
}{ }{
{"has nil elements", sliceValidateError{errors.New("test error"), nil}, "[0]: test error"}, {"has nil elements", SliceValidationError{errors.New("test error"), nil}, "[0]: test error"},
{"has zero elements", sliceValidateError{}, ""}, {"has zero elements", SliceValidationError{}, ""},
{"has one element", sliceValidateError{errors.New("test one error")}, "[0]: test one error"}, {"has one element", SliceValidationError{errors.New("test one error")}, "[0]: test one error"},
{"has two elements", {"has two elements",
sliceValidateError{ SliceValidationError{
errors.New("first error"), errors.New("first error"),
errors.New("second error"), errors.New("second error"),
}, },
"[0]: first error\n[1]: second error", "[0]: first error\n[1]: second error",
}, },
{"has many elements", {"has many elements",
sliceValidateError{ SliceValidationError{
errors.New("first error"), errors.New("first error"),
errors.New("second error"), errors.New("second error"),
nil, nil,
@ -40,7 +40,7 @@ func TestSliceValidateError(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if got := tt.err.Error(); got != tt.want { if got := tt.err.Error(); got != tt.want {
t.Errorf("sliceValidateError.Error() = %v, want %v", got, tt.want) t.Errorf("SliceValidationError.Error() = %v, want %v", got, tt.want)
} }
}) })
} }