Make package for protection struct

v2
Nikita Tokarchuk 5 years ago
parent 6e5dccbc96
commit ab4227eaef
  1. 12
      mongox/base/getprotection.go
  2. 6
      mongox/base/protection/key.go
  3. 6
      mongox/query/compose.go

@ -2,10 +2,12 @@ package base
import ( import (
"reflect" "reflect"
"github.com/mainnika/mongox-go-driver/v2/mongox/base/protection"
) )
// GetProtection function finds protection field in the source document otherwise returns nil // GetProtection function finds protection field in the source document otherwise returns nil
func GetProtection(source interface{}) *Protection { func GetProtection(source interface{}) *protection.Key {
v := reflect.ValueOf(source) v := reflect.ValueOf(source)
if v.Kind() != reflect.Ptr || v.IsNil() { if v.Kind() != reflect.Ptr || v.IsNil() {
@ -19,11 +21,11 @@ func GetProtection(source interface{}) *Protection {
field := el.Field(i) field := el.Field(i)
switch field.Interface().(type) { switch field.Interface().(type) {
case *Protection: case *protection.Key:
return field.Interface().(*Protection) return field.Interface().(*protection.Key)
case Protection: case protection.Key:
ptr := field.Addr() ptr := field.Addr()
return ptr.Interface().(*Protection) return ptr.Interface().(*protection.Key)
default: default:
continue continue
} }

@ -1,11 +1,11 @@
package base package protection
import ( import (
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
) )
// Protection field stores unique document id and version // Key field stores unique document id and version
type Protection struct { type Key struct {
X primitive.ObjectID `bson:"_x" json:"_x" index:",hashed"` X primitive.ObjectID `bson:"_x" json:"_x" index:",hashed"`
V int64 `bson:"_v" json:"_v"` V int64 `bson:"_v" json:"_v"`
} }

@ -6,7 +6,7 @@ import (
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mainnika/mongox-go-driver/v2/mongox/base" "github.com/mainnika/mongox-go-driver/v2/mongox/base/protection"
) )
// Compose is a function to compose filters into a single query // Compose is a function to compose filters into a single query
@ -87,10 +87,10 @@ func applyProtection(q *Query, f interface{}) bool {
var v *int64 var v *int64
switch f := f.(type) { switch f := f.(type) {
case base.Protection: case protection.Key:
x = &f.X x = &f.X
v = &f.V v = &f.V
case *base.Protection: case *protection.Key:
if f == nil { if f == nil {
return false return false
} }

Loading…
Cancel
Save