Object base

v2
Nikita Tokarchuk 6 years ago
parent bbbdf1a6ec
commit 5a3ee358b5
  1. 12
      mongox/base/getid.go
  2. 23
      mongox/base/object.go
  3. 5
      mongox/common.go

@ -14,6 +14,8 @@ func GetID(source interface{}) (id interface{}) {
return getObjectIdOrGenerate(doc) return getObjectIdOrGenerate(doc)
case mongox.BaseString: case mongox.BaseString:
return getStringIdOrPanic(doc) return getStringIdOrPanic(doc)
case mongox.BaseObject:
return getObjectOrPanic(doc)
default: default:
panic(errors.Malformedf("source contains malformed document, %v", source)) panic(errors.Malformedf("source contains malformed document, %v", source))
} }
@ -43,3 +45,13 @@ func getStringIdOrPanic(source mongox.BaseString) (id string) {
panic(errors.Malformedf("victim contains malformed document, %v", source)) panic(errors.Malformedf("victim contains malformed document, %v", source))
} }
func getObjectOrPanic(source mongox.BaseObject) (id primitive.D) {
id = source.GetID()
if id != nil {
return id
}
panic(errors.Malformedf("victim contains malformed document, %v", source))
}

@ -0,0 +1,23 @@
package base
import (
"github.com/mainnika/mongox-go-driver/mongox"
"github.com/mongodb/mongo-go-driver/bson/primitive"
)
var _ mongox.BaseObject = &Object{}
// Object is a structure with object as an _id field
type Object struct {
ID primitive.D `bson:"_id,omitempty" json:"_id,omitempty"`
}
// GetID returns an _id
func (db *Object) GetID() primitive.D {
return db.ID
}
// SetID sets an _id
func (db *Object) SetID(id primitive.D) {
db.ID = id
}

@ -25,3 +25,8 @@ type BaseString interface {
GetID() string GetID() string
SetID(id string) SetID(id string)
} }
type BaseObject interface {
GetID() primitive.D
SetID(id primitive.D)
}

Loading…
Cancel
Save