Protection structure

v2
Nikita Tokarchuk 5 years ago
parent c34b3cc9f9
commit db3aedd521
  1. 33
      mongox/base/getprotection.go
  2. 11
      mongox/base/protection.go

@ -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"`
}
Loading…
Cancel
Save