mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-05-22 15:53:36 +00:00
Improve base types
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
)
|
||||
|
||||
var _ mongox.ObjectBased = (*Object)(nil)
|
||||
var _ mongox.ObjectIDBased = (*ObjectID)(nil)
|
||||
var _ mongox.StringBased = (*String)(nil)
|
||||
|
||||
// Object is a structure with object as an _id field
|
||||
type Object primitive.D
|
||||
|
||||
// GetID returns an _id
|
||||
func (db *Object) GetID() primitive.D {
|
||||
return primitive.D(*db)
|
||||
}
|
||||
|
||||
// SetID sets an _id
|
||||
func (db *Object) SetID(id primitive.D) {
|
||||
*db = Object(id)
|
||||
}
|
||||
|
||||
// ObjectID is a structure with objectId as an _id field
|
||||
type ObjectID primitive.ObjectID
|
||||
|
||||
// GetID returns an _id
|
||||
func (db *ObjectID) GetID() primitive.ObjectID {
|
||||
return primitive.ObjectID(*db)
|
||||
}
|
||||
|
||||
// SetID sets an _id
|
||||
func (db *ObjectID) SetID(id primitive.ObjectID) {
|
||||
*db = ObjectID(id)
|
||||
}
|
||||
|
||||
// String is a structure with string as an _id field
|
||||
type String string
|
||||
|
||||
// GetID returns an _id
|
||||
func (db *String) GetID() string {
|
||||
return string(*db)
|
||||
}
|
||||
|
||||
// SetID sets an _id
|
||||
func (db *String) SetID(id string) {
|
||||
*db = String(id)
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
func TestObject_GetID(t *testing.T) {
|
||||
|
||||
type DocWithObject struct {
|
||||
Object `bson:"_id" json:"_id" collection:"2"`
|
||||
}
|
||||
|
||||
doc := &DocWithObject{Object: Object(primitive.D{{"1", "one"}, {"2", "two"}})}
|
||||
|
||||
assert.Equal(t, primitive.D{{"1", "one"}, {"2", "two"}}, doc.GetID())
|
||||
}
|
||||
|
||||
func TestObject_SetID(t *testing.T) {
|
||||
|
||||
type DocWithObject struct {
|
||||
Object `bson:"_id" json:"_id" collection:"2"`
|
||||
}
|
||||
|
||||
doc := &DocWithObject{Object: Object(primitive.D{{"1", "one"}, {"2", "two"}})}
|
||||
|
||||
doc.SetID(primitive.D{{"3", "three"}, {"4", "you"}})
|
||||
|
||||
assert.Equal(t, primitive.D{{"3", "three"}, {"4", "you"}}, primitive.D(doc.Object))
|
||||
assert.Equal(t, primitive.D{{"3", "three"}, {"4", "you"}}, primitive.D(doc.GetID()))
|
||||
}
|
||||
|
||||
func TestString_GetID(t *testing.T) {
|
||||
|
||||
type DocWithString struct {
|
||||
String `bson:"_id" json:"_id" collection:"3"`
|
||||
}
|
||||
|
||||
doc := &DocWithString{String: String("foobar")}
|
||||
|
||||
assert.Equal(t, "foobar", doc.GetID())
|
||||
}
|
||||
|
||||
func TestString_SetID(t *testing.T) {
|
||||
|
||||
type DocWithString struct {
|
||||
String `bson:"_id" json:"_id" collection:"3"`
|
||||
}
|
||||
|
||||
doc := &DocWithString{String: String("foobar")}
|
||||
|
||||
doc.SetID("rockrockrock")
|
||||
|
||||
assert.Equal(t, "rockrockrock", string(doc.String))
|
||||
assert.Equal(t, "rockrockrock", doc.GetID())
|
||||
}
|
||||
|
||||
func TestObjectID_GetID(t *testing.T) {
|
||||
|
||||
type DocWithObjectID struct {
|
||||
ObjectID `bson:"_id" json:"_id" collection:"1"`
|
||||
}
|
||||
|
||||
doc := &DocWithObjectID{ObjectID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}}
|
||||
|
||||
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), doc.GetID())
|
||||
}
|
||||
|
||||
func TestObjectID_SetID(t *testing.T) {
|
||||
|
||||
type DocWithObjectID struct {
|
||||
ObjectID `bson:"_id" json:"_id" collection:"1"`
|
||||
}
|
||||
|
||||
doc := &DocWithObjectID{}
|
||||
|
||||
doc.SetID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2})
|
||||
|
||||
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), primitive.ObjectID(doc.ObjectID))
|
||||
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), doc.GetID())
|
||||
}
|
||||
@@ -12,18 +12,18 @@ import (
|
||||
func GetID(source interface{}) (id interface{}) {
|
||||
|
||||
switch doc := source.(type) {
|
||||
case mongox.BaseObjectID:
|
||||
case mongox.ObjectIDBased:
|
||||
return getObjectIDOrGenerate(doc)
|
||||
case mongox.BaseString:
|
||||
case mongox.StringBased:
|
||||
return getStringIDOrPanic(doc)
|
||||
case mongox.BaseObject:
|
||||
case mongox.ObjectBased:
|
||||
return getObjectOrPanic(doc)
|
||||
default:
|
||||
panic(fmt.Errorf("source contains malformed document, %v", source))
|
||||
}
|
||||
}
|
||||
|
||||
func getObjectIDOrGenerate(source mongox.BaseObjectID) (id primitive.ObjectID) {
|
||||
func getObjectIDOrGenerate(source mongox.ObjectIDBased) (id primitive.ObjectID) {
|
||||
|
||||
id = source.GetID()
|
||||
if id != primitive.NilObjectID {
|
||||
@@ -36,7 +36,7 @@ func getObjectIDOrGenerate(source mongox.BaseObjectID) (id primitive.ObjectID) {
|
||||
return
|
||||
}
|
||||
|
||||
func getStringIDOrPanic(source mongox.BaseString) (id string) {
|
||||
func getStringIDOrPanic(source mongox.StringBased) (id string) {
|
||||
|
||||
id = source.GetID()
|
||||
if id != "" {
|
||||
@@ -46,7 +46,7 @@ func getStringIDOrPanic(source mongox.BaseString) (id string) {
|
||||
panic(fmt.Errorf("source contains malformed document, %v", source))
|
||||
}
|
||||
|
||||
func getObjectOrPanic(source mongox.BaseObject) (id primitive.D) {
|
||||
func getObjectOrPanic(source mongox.ObjectBased) (id primitive.D) {
|
||||
|
||||
id = source.GetID()
|
||||
if id != nil {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
func TestGetID(t *testing.T) {
|
||||
|
||||
type DocWithObjectID struct {
|
||||
ObjectID `bson:"_id" json:"_id" collection:"1"`
|
||||
}
|
||||
type DocWithObject struct {
|
||||
Object `bson:"_id" json:"_id" collection:"2"`
|
||||
}
|
||||
type DocWithString struct {
|
||||
String `bson:"_id" json:"_id" collection:"3"`
|
||||
}
|
||||
|
||||
GetID(&DocWithObjectID{ObjectID: ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2})})
|
||||
GetID(&DocWithObject{Object: Object(primitive.D{{"1", "2"}})})
|
||||
GetID(&DocWithString{String: String("foobar")})
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
)
|
||||
|
||||
var _ mongox.BaseObjectID = &ObjectID{}
|
||||
|
||||
// ObjectID is a structure with objectId as an _id field
|
||||
type ObjectID struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty" json:"_id,omitempty"`
|
||||
}
|
||||
|
||||
// GetID returns an _id
|
||||
func (db *ObjectID) GetID() primitive.ObjectID {
|
||||
return db.ID
|
||||
}
|
||||
|
||||
// SetID sets an _id
|
||||
func (db *ObjectID) SetID(id primitive.ObjectID) {
|
||||
db.ID = id
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
)
|
||||
|
||||
var _ mongox.BaseString = &String{}
|
||||
|
||||
// String is a structure with string as an _id field
|
||||
type String struct {
|
||||
ID string `bson:"_id,omitempty" json:"_id,omitempty"`
|
||||
}
|
||||
|
||||
// GetID returns an _id
|
||||
func (db *String) GetID() string {
|
||||
return db.ID
|
||||
}
|
||||
|
||||
// SetID sets an _id
|
||||
func (db *String) SetID(id string) {
|
||||
db.ID = id
|
||||
}
|
||||
+6
-6
@@ -80,20 +80,20 @@ type Resetter interface {
|
||||
Reset()
|
||||
}
|
||||
|
||||
// BaseObjectID is an interface for documents that have objectId type for the _id field
|
||||
type BaseObjectID interface {
|
||||
// ObjectIDBased is an interface for documents that have objectId type for the _id field
|
||||
type ObjectIDBased interface {
|
||||
GetID() primitive.ObjectID
|
||||
SetID(id primitive.ObjectID)
|
||||
}
|
||||
|
||||
// BaseString is an interface for documents that have string type for the _id field
|
||||
type BaseString interface {
|
||||
// StringBased is an interface for documents that have string type for the _id field
|
||||
type StringBased interface {
|
||||
GetID() string
|
||||
SetID(id string)
|
||||
}
|
||||
|
||||
// BaseObject is an interface for documents that have object type for the _id field
|
||||
type BaseObject interface {
|
||||
// ObjectBased is an interface for documents that have object type for the _id field
|
||||
type ObjectBased interface {
|
||||
GetID() primitive.D
|
||||
SetID(id primitive.D)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user