mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-07-03 17:22:33 +00:00
Export getId into function
This commit is contained in:
@@ -0,0 +1,45 @@
|
|||||||
|
package base
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/mainnika/mongox-go-driver/mongox"
|
||||||
|
"github.com/mainnika/mongox-go-driver/mongox/errors"
|
||||||
|
"github.com/mongodb/mongo-go-driver/bson/primitive"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetID returns source document id
|
||||||
|
func GetID(source interface{}) (id interface{}) {
|
||||||
|
|
||||||
|
switch doc := source.(type) {
|
||||||
|
case mongox.BaseObjectID:
|
||||||
|
return getObjectIdOrGenerate(doc)
|
||||||
|
case mongox.BaseString:
|
||||||
|
return getStringIdOrPanic(doc)
|
||||||
|
default:
|
||||||
|
panic(errors.Malformedf("source contains malformed document, %v", source))
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func getObjectIdOrGenerate(source mongox.BaseObjectID) (id primitive.ObjectID) {
|
||||||
|
|
||||||
|
id = source.GetID()
|
||||||
|
if id != primitive.NilObjectID {
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
|
id = primitive.NewObjectID()
|
||||||
|
source.SetID(id)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func getStringIdOrPanic(source mongox.BaseString) (id string) {
|
||||||
|
|
||||||
|
id = source.GetID()
|
||||||
|
if id != "" {
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
|
panic(errors.Malformedf("victim contains malformed document, %v", source))
|
||||||
|
}
|
||||||
@@ -13,27 +13,11 @@ func SaveOne(db *mongox.Database, source interface{}) error {
|
|||||||
|
|
||||||
collection := db.GetCollectionOf(source)
|
collection := db.GetCollectionOf(source)
|
||||||
opts := &options.FindOneAndReplaceOptions{}
|
opts := &options.FindOneAndReplaceOptions{}
|
||||||
|
id := base.GetID(source)
|
||||||
|
|
||||||
opts.SetUpsert(true)
|
opts.SetUpsert(true)
|
||||||
opts.SetReturnDocument(options.After)
|
opts.SetReturnDocument(options.After)
|
||||||
|
|
||||||
var id interface{}
|
|
||||||
|
|
||||||
switch doc := source.(type) {
|
|
||||||
case mongox.BaseObjectID:
|
|
||||||
id = doc.GetID()
|
|
||||||
if id == primitive.NilObjectID {
|
|
||||||
id = primitive.NewObjectID()
|
|
||||||
}
|
|
||||||
case mongox.BaseString:
|
|
||||||
id = doc.GetID()
|
|
||||||
if id == "" {
|
|
||||||
panic(errors.Malformedf("source contains malformed document, %v", source))
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
panic(errors.Malformedf("source contains malformed document, %v", source))
|
|
||||||
}
|
|
||||||
|
|
||||||
result := collection.FindOneAndReplace(db.Context(), bson.M{"_id": id}, source, opts)
|
result := collection.FindOneAndReplace(db.Context(), bson.M{"_id": id}, source, opts)
|
||||||
if result.Err() != nil {
|
if result.Err() != nil {
|
||||||
return errors.NotFoundErrorf("%s", result.Err())
|
return errors.NotFoundErrorf("%s", result.Err())
|
||||||
|
|||||||
Reference in New Issue
Block a user