mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-24 10:07:21 +02:00
41 lines
761 B
Go
41 lines
761 B
Go
|
package binding
|
||
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
"sync"
|
||
|
|
||
|
"gopkg.in/bluesuncorp/validator.v5"
|
||
|
)
|
||
|
|
||
|
type defaultValidator struct {
|
||
|
once sync.Once
|
||
|
validate *validator.Validate
|
||
|
}
|
||
|
|
||
|
var _ StructValidator = &defaultValidator{}
|
||
|
|
||
|
func (v *defaultValidator) ValidateStruct(obj interface{}) error {
|
||
|
if kindOfData(obj) == reflect.Struct {
|
||
|
v.lazyinit()
|
||
|
if err := v.validate.Struct(obj); err != nil {
|
||
|
return error(err)
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (v *defaultValidator) lazyinit() {
|
||
|
v.once.Do(func() {
|
||
|
v.validate = validator.New("binding", validator.BakedInValidators)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func kindOfData(data interface{}) reflect.Kind {
|
||
|
value := reflect.ValueOf(data)
|
||
|
valueType := value.Kind()
|
||
|
if valueType == reflect.Ptr {
|
||
|
valueType = value.Elem().Kind()
|
||
|
}
|
||
|
return valueType
|
||
|
}
|