parent
db6c11f455
commit
45a41c6c6c
@ -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()) |
||||
} |
@ -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 |
||||
} |
Loading…
Reference in new issue