[Roll] interface name changing

This commit is contained in:
wuhuizuo 2020-03-26 07:55:35 +00:00
parent 50f5b16990
commit abced1b159
2 changed files with 10 additions and 10 deletions

View File

@ -45,27 +45,27 @@ type BindingUri interface {
BindUri(map[string][]string, interface{}) error BindUri(map[string][]string, interface{}) error
} }
// Validater is the minimal interface which needs to be implemented in // StructValidator is the minimal interface which needs to be implemented in
// order for it to be used as the validator engine for ensuring the correctness // order for it to be used as the validator engine for ensuring the correctness
// of the request. Gin provides a default implementation for this using // of the request. Gin provides a default implementation for this using
// https://github.com/go-playground/validator/tree/v8.18.2. // https://github.com/go-playground/validator/tree/v8.18.2.
type Validater interface { type StructValidator interface {
// If the received type is a slice|array, the validation should be performed travel on every element. // If the received type is a slice|array, the validation should be performed travel on every element.
// If the received type is not a struct or slice|array, any validation should be skipped and nil must be returned. // If the received type is not a struct or slice|array, any validation should be skipped and nil must be returned.
// If the received type is a struct or pointer to a struct, the validation should be performed. // If the received type is a struct or pointer to a struct, the validation should be performed.
// If the struct is not valid or the validation itself fails, a descriptive error should be returned. // If the struct is not valid or the validation itself fails, a descriptive error should be returned.
// Otherwise nil must be returned. // Otherwise nil must be returned.
Validate(interface{}) error ValidateStruct(interface{}) error
// Engine returns the underlying validator engine which powers the // Engine returns the underlying validator engine which powers the
// StructValidator implementation. // Validater implementation.
Engine() interface{} Engine() interface{}
} }
// Validator is the default validator which implements the StructValidator // Validator is the default validator which implements the StructValidator
// interface. It uses https://github.com/go-playground/validator/tree/v8.18.2 // interface. It uses https://github.com/go-playground/validator/tree/v8.18.2
// under the hood. // under the hood.
var Validator Validater = &defaultValidator{} var Validator StructValidator = &defaultValidator{}
// These implement the Binding interface and can be used to bind the data // These implement the Binding interface and can be used to bind the data
// present in the request to struct instances. // present in the request to struct instances.
@ -112,5 +112,5 @@ func validate(obj interface{}) error {
if Validator == nil { if Validator == nil {
return nil return nil
} }
return Validator.Validate(obj) return Validator.ValidateStruct(obj)
} }

View File

@ -31,10 +31,10 @@ func (err sliceValidateError) Error() string {
return strings.Join(errMsgs, "\n") return strings.Join(errMsgs, "\n")
} }
var _ Validater = &defaultValidator{} var _ StructValidator = &defaultValidator{}
// ValidateStruct receives any kind of type, but only performed struct or pointer to struct type. // ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.
func (v *defaultValidator) Validate(obj interface{}) error { func (v *defaultValidator) ValidateStruct(obj interface{}) error {
if obj == nil { if obj == nil {
return nil return nil
} }
@ -42,7 +42,7 @@ func (v *defaultValidator) Validate(obj interface{}) error {
value := reflect.ValueOf(obj) value := reflect.ValueOf(obj)
valueType := value.Kind() valueType := value.Kind()
if valueType == reflect.Ptr { if valueType == reflect.Ptr {
return v.Validate(value.Elem().Interface()) return v.ValidateStruct(value.Elem().Interface())
} }
switch valueType { switch valueType {
@ -52,7 +52,7 @@ func (v *defaultValidator) Validate(obj interface{}) error {
count := value.Len() count := value.Len()
validateRet := make(sliceValidateError, 0) validateRet := make(sliceValidateError, 0)
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
if err := v.Validate(value.Index(i).Interface()); err != nil { if err := v.ValidateStruct(value.Index(i).Interface()); err != nil {
validateRet = append(validateRet, err) validateRet = append(validateRet, err)
} }
} }