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

39 lines
733 B

package base
import (
"reflect"
"github.com/mainnika/mongox-go-driver/v2/mongox/base/protection"
)
// GetProtection function finds protection field in the source document otherwise returns nil
func GetProtection(source interface{}) *protection.Key {
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)
if !field.CanInterface() {
continue
}
switch field.Interface().(type) {
case *protection.Key:
return field.Interface().(*protection.Key)
case protection.Key:
ptr := field.Addr()
return ptr.Interface().(*protection.Key)
default:
continue
}
}
return nil
}