From db3aedd5218e68d06705f1aef53484f6c7376c19 Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Thu, 7 Feb 2019 23:35:36 +0100 Subject: [PATCH] Protection structure --- mongox/base/getprotection.go | 33 +++++++++++++++++++++++++++++++++ mongox/base/protection.go | 11 +++++++++++ 2 files changed, 44 insertions(+) create mode 100644 mongox/base/getprotection.go create mode 100644 mongox/base/protection.go 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"` +}