diff --git a/mongox/base/getprotection.go b/mongox/base/getprotection.go new file mode 100644 index 0000000..28b4cc1 --- /dev/null +++ b/mongox/base/getprotection.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 +} diff --git a/mongox/base/protection.go b/mongox/base/protection.go new file mode 100644 index 0000000..01c0176 --- /dev/null +++ b/mongox/base/protection.go @@ -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"` +}