mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-05-23 00:03:36 +00:00
Prepare for v3
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package protection
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/modern-go/reflect2"
|
||||
@@ -13,9 +14,8 @@ type Key struct {
|
||||
V int64 `bson:"_v" json:"_v"`
|
||||
}
|
||||
|
||||
// PutToDocument extends the doc with protection key values
|
||||
func (k *Key) PutToDocument(doc primitive.M) {
|
||||
|
||||
// Inject extends the doc with protection key values
|
||||
func (k *Key) Inject(doc primitive.M) {
|
||||
if reflect2.IsNil(doc) {
|
||||
return
|
||||
}
|
||||
@@ -34,3 +34,35 @@ func (k *Key) Restate() {
|
||||
k.X = primitive.NewObjectID()
|
||||
k.V = time.Now().Unix()
|
||||
}
|
||||
|
||||
// Get finds protection field in the source document otherwise returns nil
|
||||
func Get(source interface{}) (key *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 *Key:
|
||||
key = field.Interface().(*Key)
|
||||
case Key:
|
||||
ptr := field.Addr()
|
||||
key = ptr.Interface().(*Key)
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
return key
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
package protection_test
|
||||
|
||||
// TODO:
|
||||
Reference in New Issue
Block a user