From 45a41c6c6c6bc31009599b6f16e8cc61ada5528c Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Thu, 5 Mar 2020 00:00:25 +0100 Subject: [PATCH] Improve base types --- go.mod | 1 + go.sum | 3 ++ mongox/base/base.go | 50 +++++++++++++++++++++++ mongox/base/base_test.go | 83 +++++++++++++++++++++++++++++++++++++++ mongox/base/getid.go | 12 +++--- mongox/base/getid_test.go | 24 +++++++++++ mongox/base/object.go | 24 ----------- mongox/base/objectid.go | 24 ----------- mongox/base/string.go | 22 ----------- mongox/mongox.go | 12 +++--- 10 files changed, 173 insertions(+), 82 deletions(-) create mode 100644 mongox/base/base.go create mode 100644 mongox/base/base_test.go create mode 100644 mongox/base/getid_test.go delete mode 100644 mongox/base/object.go delete mode 100644 mongox/base/objectid.go delete mode 100644 mongox/base/string.go diff --git a/go.mod b/go.mod index 262a24e..9b6e5d6 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ require ( github.com/google/go-cmp v0.3.0 // indirect github.com/klauspost/compress v1.10.1 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/stretchr/testify v1.5.1 github.com/xdg/stringprep v1.0.0 // indirect go.mongodb.org/mongo-driver v1.3.0 golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d // indirect diff --git a/go.sum b/go.sum index 143c472..7cdf22d 100644 --- a/go.sum +++ b/go.sum @@ -71,6 +71,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk= @@ -113,4 +115,5 @@ golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgw gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/mongox/base/base.go b/mongox/base/base.go new file mode 100644 index 0000000..42fd735 --- /dev/null +++ b/mongox/base/base.go @@ -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) +} diff --git a/mongox/base/base_test.go b/mongox/base/base_test.go new file mode 100644 index 0000000..b9243e5 --- /dev/null +++ b/mongox/base/base_test.go @@ -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()) +} diff --git a/mongox/base/getid.go b/mongox/base/getid.go index 928b6ea..3515ca6 100644 --- a/mongox/base/getid.go +++ b/mongox/base/getid.go @@ -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 { diff --git a/mongox/base/getid_test.go b/mongox/base/getid_test.go new file mode 100644 index 0000000..2cb8ee9 --- /dev/null +++ b/mongox/base/getid_test.go @@ -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")}) +} diff --git a/mongox/base/object.go b/mongox/base/object.go deleted file mode 100644 index cf3f2f0..0000000 --- a/mongox/base/object.go +++ /dev/null @@ -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 -} diff --git a/mongox/base/objectid.go b/mongox/base/objectid.go deleted file mode 100644 index d747175..0000000 --- a/mongox/base/objectid.go +++ /dev/null @@ -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 -} diff --git a/mongox/base/string.go b/mongox/base/string.go deleted file mode 100644 index 08cd18b..0000000 --- a/mongox/base/string.go +++ /dev/null @@ -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 -} diff --git a/mongox/mongox.go b/mongox/mongox.go index 4021dc9..de9452a 100644 --- a/mongox/mongox.go +++ b/mongox/mongox.go @@ -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) }