Prepare for v3

This commit is contained in:
2023-06-10 00:44:20 +02:00
parent 7c3e50e783
commit 23029ae710
42 changed files with 880 additions and 719 deletions
+35 -3
View File
@@ -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
}
+3
View File
@@ -0,0 +1,3 @@
package protection_test
// TODO: