You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mongox-go-driver/mongox/base/getprotection.go

34 lines
601 B

package base
import (
"reflect"
)
// GetProtection function finds protection field in the source document otherwise returns nil
func GetProtection(source interface{}) *Protection {
v := reflect.ValueOf(source)
if v.Kind() != reflect.Ptr || v.IsNil() {
return nil
}
el := v.Elem()
numField := el.NumField()
for i := 0; i < numField; i++ {
field := el.Field(i)
switch field.Interface().(type) {
case *Protection:
return field.Interface().(*Protection)
case Protection:
ptr := field.Addr()
return ptr.Interface().(*Protection)
default:
continue
}
}
return nil
}