mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-06-13 01:03:35 +00:00
Protection structure
This commit is contained in:
@@ -0,0 +1,33 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package base
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/mongodb/mongo-go-driver/bson/primitive"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Protection field stores unique document id and version
|
||||||
|
type Protection struct {
|
||||||
|
X primitive.ObjectID `bson:"_x" json:"_x" index:",hashed"`
|
||||||
|
V int64 `bson:"_v" json:"_v"`
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user