Protection structure

This commit is contained in:
Nikita Tokarchuk
2019-02-07 23:35:36 +01:00
parent c34b3cc9f9
commit db3aedd521
2 changed files with 44 additions and 0 deletions
+33
View File
@@ -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
}
+11
View File
@@ -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"`
}