mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-05-22 15:53:36 +00:00
Object base
This commit is contained in:
@@ -14,6 +14,8 @@ func GetID(source interface{}) (id interface{}) {
|
||||
return getObjectIdOrGenerate(doc)
|
||||
case mongox.BaseString:
|
||||
return getStringIdOrPanic(doc)
|
||||
case mongox.BaseObject:
|
||||
return getObjectOrPanic(doc)
|
||||
default:
|
||||
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))
|
||||
}
|
||||
|
||||
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
|
||||
SetID(id string)
|
||||
}
|
||||
|
||||
type BaseObject interface {
|
||||
GetID() primitive.D
|
||||
SetID(id primitive.D)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user