You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
mongox-go-driver/mongox/base/getid.go

56 lines
1.1 KiB

package base
import (
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mainnika/mongox-go-driver/mongox"
"github.com/mainnika/mongox-go-driver/mongox/errors"
)
// 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)
case mongox.BaseObject:
return getObjectOrPanic(doc)
default:
panic(errors.Malformedf("source contains malformed document, %v", source))
}
}
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))
}
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))
}