From d94af857ac0a3ac87d31ae970bfa2c0bd4aca237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=AAnnis=20Dantas?= Date: Thu, 19 Apr 2018 03:20:04 -0300 Subject: [PATCH] refactor(mongo): Re-import bson types to mongo :art: I've re-imported M and ObjectId types to mongo package, to avoid using mgo packages on outer projects. --- README.md | 14 +++++++------- acceptance_test.go | 19 +++++++++---------- doc.go | 14 +++++++------- documenter.go | 8 +++----- documenter_test.go | 24 +++++++++++------------- handle.go | 23 +++++++++++------------ handle_test.go | 33 ++++++++++++++++----------------- helpers_test.go | 25 ++++++++++++------------- mock.go | 13 ++++++------- util.go | 36 ++++++++++++++++++++++++++++++------ 10 files changed, 112 insertions(+), 97 deletions(-) diff --git a/README.md b/README.md index a5b004c..8b4f087 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ The Documenter can be used like this: ```go // Create a type representing the Document type type Product struct { - IDV bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"` + IDV ObjectId `json:"_id,omitempty" bson:"_id,omitempty"` CreatedOnV int64 `json:"created_on,omitempty" bson:"created_on,omitempty"` UpdatedOnV int64 `json:"updated_on,omitempty" bson:"updated_on,omitempty"` NameV string `json:"name" form:"name" binding:"required" bson:"name"` @@ -89,7 +89,7 @@ type Product struct { } // Implement the Documenter interface. -func (p *Product) ID() (id bson.ObjectId) { +func (p *Product) ID() (id ObjectId) { id = p.IDV return } @@ -111,12 +111,12 @@ func (p *Product) New() (doc mongo.Documenter) { // On these methods, you can use the functions implemented mongo // package. -func (p *Product) Map() (out bson.M, err error) { +func (p *Product) Map() (out M, err error) { out, err = mongo.MapDocumenter(p) return } -func (p *Product) Init(in bson.M) (err error) { +func (p *Product) Init(in M) (err error) { var doc mongo.Documenter = p err = mongo.InitDocumenter(in, &doc) return @@ -140,14 +140,14 @@ p.CalculateCreatedOn() t := p.CreatedOn() ``` -You can also mock some other functions of this package, by mocking some called functions time.Now and bson.NewObjectId. Use the MockModelSetup presented on this package (only in test environment), like: +You can also mock some other functions of this package, by mocking some called functions time.Now and NewObjectId. Use the MockModelSetup presented on this package (only in test environment), like: ```go create, _ := mongo.NewMockModelSetup(t) defer create.Finish() create.Now().Returns(time.Parse("02-01-2006", "22/12/2006")) -create.NewID().Returns(bson.ObjectIdHex("anyID")) +create.NewID().Returns(ObjectIdHex("anyID")) var d mongo.Documenter // Call any needed methods ... @@ -227,7 +227,7 @@ func (p *ProductHandle) Clean() { The Update function uses an id as an argument: ```go -func (p *ProductHandle) Update(id bson.ObjectId) (err error) { +func (p *ProductHandle) Update(id ObjectId) (err error) { err = p.Handle.Update(id, p.Document()) return } diff --git a/acceptance_test.go b/acceptance_test.go index a6529bb..e3cfa46 100644 --- a/acceptance_test.go +++ b/acceptance_test.go @@ -9,7 +9,6 @@ import ( "github.com/ddspog/mongo/elements" "github.com/ddspog/mspec/bdd" - "github.com/globalsign/mgo/bson" ) const ( @@ -98,19 +97,19 @@ func Test_Manipulate_data_on_MongoDB(t *testing.T) { }) }) - var newId bson.ObjectId + var newId ObjectId when(fmt.Sprintf("using p.Insert() to add documents with ids: '%[1]s', '%[2]s', '%[3]s', and a new one", testid01, testid02, testid03), func(it bdd.It) { p.DocumentV = newProduct() - p.DocumentV.IDV = bson.ObjectIdHex(testid01) + p.DocumentV.IDV = ObjectIdHex(testid01) errInsertDoc01 := p.Insert() p.DocumentV = newProduct() - p.DocumentV.IDV = bson.ObjectIdHex(testid02) + p.DocumentV.IDV = ObjectIdHex(testid02) errInsertDoc02 := p.Insert() p.DocumentV = newProduct() - p.DocumentV.IDV = bson.ObjectIdHex(testid03) + p.DocumentV.IDV = ObjectIdHex(testid03) errInsertDoc03 := p.Insert() p.DocumentV = newProduct() @@ -134,21 +133,21 @@ func Test_Manipulate_data_on_MongoDB(t *testing.T) { it("should have p.Find() return all documents", func(assert bdd.Assert) { p.DocumentV = newProduct() - p.DocumentV.IDV = bson.ObjectIdHex(testid01) + p.DocumentV.IDV = ObjectIdHex(testid01) product01, errFindDoc01 := p.Find() assert.NoError(errFindDoc01) assert.Equal(testid01, product01.ID().Hex()) p.DocumentV = newProduct() - p.DocumentV.IDV = bson.ObjectIdHex(testid02) + p.DocumentV.IDV = ObjectIdHex(testid02) product02, errFindDoc02 := p.Find() assert.NoError(errFindDoc02) assert.Equal(testid02, product02.ID().Hex()) p.DocumentV = newProduct() - p.DocumentV.IDV = bson.ObjectIdHex(testid03) + p.DocumentV.IDV = ObjectIdHex(testid03) product03, errFindDoc03 := p.Find() assert.NoError(errFindDoc03) @@ -287,7 +286,7 @@ func Test_Read_data_on_MongoDB(t *testing.T) { p.Link(db) when("using p.Find() with '%[1]v' as document id'", func(it bdd.It, args ...interface{}) { - p.DocumentV.IDV = bson.ObjectIdHex(args[0].(string)) + p.DocumentV.IDV = ObjectIdHex(args[0].(string)) product, errFind := p.Find() it("should run without errors", func(assert bdd.Assert) { @@ -323,7 +322,7 @@ func Test_Read_data_on_MongoDB(t *testing.T) { }) when("using p.FindAll() on a Document with id '%[1]v'", func(it bdd.It, args ...interface{}) { - p.DocumentV.IDV = bson.ObjectIdHex(args[0].(string)) + p.DocumentV.IDV = ObjectIdHex(args[0].(string)) products, errFindAll := p.FindAll() it("should run without errors", func(assert bdd.Assert) { diff --git a/doc.go b/doc.go index ac721bf..5fc53e6 100644 --- a/doc.go +++ b/doc.go @@ -68,7 +68,7 @@ The Documenter can be used like this: // Create a type representing the Document type type Product struct { - IDV bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"` + IDV ObjectId `json:"_id,omitempty" bson:"_id,omitempty"` CreatedOnV int64 `json:"created_on,omitempty" bson:"created_on,omitempty"` UpdatedOnV int64 `json:"updated_on,omitempty" bson:"updated_on,omitempty"` NameV string `json:"name" form:"name" binding:"required" bson:"name"` @@ -76,7 +76,7 @@ The Documenter can be used like this: } // Implement the Documenter interface. - func (p *Product) ID() (id bson.ObjectId) { + func (p *Product) ID() (id ObjectId) { id = p.IDV return } @@ -98,12 +98,12 @@ The Documenter can be used like this: // On these methods, you can use the functions implemented mongo // package. - func (p *Product) Map() (out bson.M, err error) { + func (p *Product) Map() (out M, err error) { out, err = mongo.MapDocumenter(p) return } - func (p *Product) Init(in bson.M) (err error) { + func (p *Product) Init(in M) (err error) { var doc mongo.Documenter = p err = mongo.InitDocumenter(in, &doc) return @@ -127,14 +127,14 @@ The Documenter can be used like this: t := p.CreatedOn() You can also mock some other functions of this package, by mocking some -called functions time.Now and bson.NewObjectId. Use the MockModelSetup +called functions time.Now and NewObjectId. Use the MockModelSetup presented on this package (only in test environment), like: create, _ := mongo.NewMockModelSetup(t) defer create.Finish() create.Now().Returns(time.Parse("02-01-2006", "22/12/2006")) - create.NewID().Returns(bson.ObjectIdHex("anyID")) + create.NewID().Returns(ObjectIdHex("anyID")) var d mongo.Documenter // Call any needed methods ... @@ -210,7 +210,7 @@ The Clean function is simple and helps a lot: The Update function uses an id as an argument: - func (p *ProductHandle) Update(id bson.ObjectId) (err error) { + func (p *ProductHandle) Update(id ObjectId) (err error) { err = p.Handle.Update(id, p.Document()) return } diff --git a/documenter.go b/documenter.go index 0ffbb25..ab04194 100644 --- a/documenter.go +++ b/documenter.go @@ -1,16 +1,14 @@ package mongo -import "github.com/globalsign/mgo/bson" - // Documenter it's an interface that could be common to any documents // types used to store values on a MongoDB. It contains getters and // generates to important documents values: _id, created_on and // updated_on type Documenter interface { New() Documenter - Map() (bson.M, error) - Init(bson.M) error - ID() bson.ObjectId + Map() (M, error) + Init(M) error + ID() ObjectId CreatedOn() int64 UpdatedOn() int64 GenerateID() diff --git a/documenter_test.go b/documenter_test.go index cb883c9..869d995 100644 --- a/documenter_test.go +++ b/documenter_test.go @@ -5,8 +5,6 @@ import ( "time" "github.com/ddspog/mspec/bdd" - - "github.com/globalsign/mgo/bson" ) // Feature Enable embedding with Document @@ -18,7 +16,7 @@ func Test_Enable_embedding_with_Document(t *testing.T) { given(t, "a new embedded Product p with ID '%[1]v'", func(when bdd.When, args ...interface{}) { p := newProduct() - p.IDV = bson.ObjectIdHex(args[0].(string)) + p.IDV = ObjectIdHex(args[0].(string)) when("casting to Documenter interface d", func(it bdd.It) { var d Documenter = p @@ -40,7 +38,7 @@ func Test_Create_Document_with_functional_Getters(t *testing.T) { given(t, "a Product p with ID '%[1]v', CreatedOn = %[2]v, UpdatedOn = %[3]v", func(when bdd.When, args ...interface{}) { p := newProduct() - p.IDV = bson.ObjectIdHex(args[0].(string)) + p.IDV = ObjectIdHex(args[0].(string)) p.CreatedOnV = args[1].(int64) p.UpdatedOnV = args[2].(int64) @@ -76,8 +74,8 @@ func Test_Create_Document_with_functional_Setters(t *testing.T) { given(t, "a Product p with ID '%[1]v', CreatedOn = %[2]v, UpdatedOn = %[3]v", func(when bdd.When, args ...interface{}) { p := newProduct() - when("p.IDV = bson.ObjectIdHex(%[1]v)", func(it bdd.It) { - p.IDV = bson.ObjectIdHex(args[0].(string)) + when("p.IDV = ObjectIdHex(%[1]v)", func(it bdd.It) { + p.IDV = ObjectIdHex(args[0].(string)) it("p.ID().Hex() should return '%[1]v'", func(assert bdd.Assert) { assert.Equal(p.ID().Hex(), args[0].(string)) }) @@ -146,7 +144,7 @@ func Test_Generate_ID_of_Document(t *testing.T) { p := newProduct() when("p.GenerateID() is called", func(it bdd.It) { - create.NewID().Returns(bson.ObjectIdHex(args[0].(string))) + create.NewID().Returns(ObjectIdHex(args[0].(string))) p.GenerateID() it("p.ID().Hex() should return %[1]v", func(assert bdd.Assert) { assert.Equal(p.ID().Hex(), args[0].(string)) @@ -159,14 +157,14 @@ func Test_Generate_ID_of_Document(t *testing.T) { // Feature Encoding to map object. // - As a developer, -// - I want that Documenter to be able to convert to bson.M object, +// - I want that Documenter to be able to convert to M object, // - So that I can use to ease call on mgo methods. func Test_Encoding_to_map_object(t *testing.T) { given, like, s := bdd.Sentences() given(t, "a Product p with id '%[1]s'", func(when bdd.When, args ...interface{}) { p := newProduct() - p.IDV = bson.ObjectIdHex(args[0].(string)) + p.IDV = ObjectIdHex(args[0].(string)) when("out, errMap := p.Map() is called", func(it bdd.It) { out, errMap := p.Map() @@ -175,8 +173,8 @@ func Test_Encoding_to_map_object(t *testing.T) { assert.NoError(errMap) }) - it("out['_id'] should be bson.ObjectId equal to '%[1]s'", func(assert bdd.Assert) { - id, ok := out["_id"].(bson.ObjectId) + it("out['_id'] should be ObjectId equal to '%[1]s'", func(assert bdd.Assert) { + id, ok := out["_id"].(ObjectId) assert.True(ok) assert.Equal(id.Hex(), args[0].(string)) }) @@ -186,8 +184,8 @@ func Test_Encoding_to_map_object(t *testing.T) { )) given(t, "a map m with m['_id'] equal to id '%[1]s' and p an empty Product", func(when bdd.When, args ...interface{}) { - m := bson.M{ - "_id": bson.ObjectIdHex(args[0].(string)), + m := M{ + "_id": ObjectIdHex(args[0].(string)), } p := newProduct() diff --git a/handle.go b/handle.go index 2e6740a..b7920e4 100644 --- a/handle.go +++ b/handle.go @@ -4,7 +4,6 @@ import ( "errors" "github.com/ddspog/mongo/elements" - "github.com/globalsign/mgo/bson" ) var ( @@ -22,7 +21,7 @@ var ( // of taking documents and using them to manipulate collections. type Handle struct { collectionV elements.Collectioner - SearchMV bson.M + SearchMV M } // NewHandle creates a new Handle to be embedded onto handle for other types. @@ -67,7 +66,7 @@ func (h *Handle) Count() (n int, err error) { // connected to Handle. func (h *Handle) Find(doc Documenter, out Documenter) (err error) { if err = h.checkLink(); err == nil { - var mapped bson.M + var mapped M if h.IsSearchEmpty() { mapped, err = doc.Map() @@ -78,7 +77,7 @@ func (h *Handle) Find(doc Documenter, out Documenter) (err error) { if err == nil { var result interface{} if err = h.collectionV.Find(mapped).One(&result); err == nil { - err = out.Init(result.(bson.M)) + err = out.Init(result.(M)) } } } @@ -89,7 +88,7 @@ func (h *Handle) Find(doc Documenter, out Documenter) (err error) { // collection connected to Handle. func (h *Handle) FindAll(doc Documenter, out *[]Documenter) (err error) { if err = h.checkLink(); err == nil { - var mapped bson.M + var mapped M if h.IsSearchEmpty() { mapped, err = doc.Map() @@ -104,7 +103,7 @@ func (h *Handle) FindAll(doc Documenter, out *[]Documenter) (err error) { for i := range result { //noinspection GoNilContainerIndexing tempArr[i] = doc.New() - if err := tempArr[i].Init(result[i].(bson.M)); err != nil { + if err := tempArr[i].Init(result[i].(M)); err != nil { break } } @@ -126,7 +125,7 @@ func (h *Handle) Insert(doc Documenter) (err error) { doc.CalculateCreatedOn() if err = h.checkLink(); err == nil { - var mapped bson.M + var mapped M if mapped, err = doc.Map(); err == nil { err = h.collectionV.Insert(mapped) } @@ -136,7 +135,7 @@ func (h *Handle) Insert(doc Documenter) (err error) { // Remove delete a document on collection connected to Handle, matching // id received. -func (h *Handle) Remove(id bson.ObjectId) (err error) { +func (h *Handle) Remove(id ObjectId) (err error) { if id.Hex() == "" { err = ErrIDNotDefined } else { @@ -151,7 +150,7 @@ func (h *Handle) Remove(id bson.ObjectId) (err error) { // matching the doc data. func (h *Handle) RemoveAll(doc Documenter) (info *elements.ChangeInfo, err error) { if err = h.checkLink(); err == nil { - var mapped bson.M + var mapped M if h.IsSearchEmpty() { mapped, err = doc.Map() @@ -168,14 +167,14 @@ func (h *Handle) RemoveAll(doc Documenter) (info *elements.ChangeInfo, err error // Update updates a document on collection connected to Handle, // matching id received, updating with the information on doc. -func (h *Handle) Update(id bson.ObjectId, doc Documenter) (err error) { +func (h *Handle) Update(id ObjectId, doc Documenter) (err error) { if id.Hex() == "" { err = ErrIDNotDefined } else { doc.CalculateUpdatedOn() if err = h.checkLink(); err == nil { - var mapped bson.M + var mapped M if mapped, err = doc.Map(); err == nil { err = h.collectionV.UpdateID(id, mapped) } @@ -185,7 +184,7 @@ func (h *Handle) Update(id bson.ObjectId, doc Documenter) (err error) { } // SearchM return the search map value of Handle. -func (h *Handle) SearchM() (s bson.M) { +func (h *Handle) SearchM() (s M) { s = h.SearchMV return } diff --git a/handle_test.go b/handle_test.go index aba3df7..d8dbc62 100644 --- a/handle_test.go +++ b/handle_test.go @@ -8,7 +8,6 @@ import ( "github.com/ddspog/mongo/elements" "github.com/ddspog/mongo/mocks" "github.com/ddspog/mspec/bdd" - "github.com/globalsign/mgo/bson" ) // Feature Enable embedding with Handle @@ -40,7 +39,7 @@ func Test_Create_Handle_with_functional_Getters(t *testing.T) { given(t, "a ProductHandler h with ID '%[1]v'", func(when bdd.When, args ...interface{}) { p := newProduct() - p.IDV = bson.ObjectIdHex(args[0].(string)) + p.IDV = ObjectIdHex(args[0].(string)) ph := newProductHandle() ph.DocumentV = p @@ -128,7 +127,7 @@ func Test_Clean_documents_with_Handle(t *testing.T) { given(t, "a ProductHandler h with Document with ID '%[1]v'", func(when bdd.When, args ...interface{}) { var h productHandler = newProductHandle() - h.Document().IDV = bson.ObjectIdHex(args[0].(string)) + h.Document().IDV = ObjectIdHex(args[0].(string)) when("h.Clean() is called", func(it bdd.It) { h.Clean() @@ -160,9 +159,9 @@ func Test_Find_documents_with_Handle(t *testing.T) { return create.DatabaseMock("products", func(mcl *mocks.MockCollectioner) { switch args[0] { case p[0].ID().Hex(): - mcl.ExpectFindReturn(bson.M{"_id": p[0].IDV, "created_on": p[0].CreatedOnV, "updated_on": p[0].UpdatedOnV}) + mcl.ExpectFindReturn(M{"_id": p[0].IDV, "created_on": p[0].CreatedOnV, "updated_on": p[0].UpdatedOnV}) case p[1].ID().Hex(): - mcl.ExpectFindReturn(bson.M{"_id": p[1].IDV, "created_on": p[1].CreatedOnV, "updated_on": p[1].UpdatedOnV}) + mcl.ExpectFindReturn(M{"_id": p[1].IDV, "created_on": p[1].CreatedOnV, "updated_on": p[1].UpdatedOnV}) default: mcl.ExpectFindFail(anyReason) } @@ -173,7 +172,7 @@ func Test_Find_documents_with_Handle(t *testing.T) { _ = h.Link(db()) when("d, err := h.Find() is called with Document id '%[1]v'", func(it bdd.It) { - h.Document().IDV = bson.ObjectIdHex(args[0].(string)) + h.Document().IDV = ObjectIdHex(args[0].(string)) d, err := h.Find() if args[1].(bool) { @@ -197,7 +196,7 @@ func Test_Find_documents_with_Handle(t *testing.T) { _ = h.Link(db()) when("d, err := h.Find() is called with Search '_id' equal '%[1]v'", func(it bdd.It) { - h.SearchM()["_id"] = bson.ObjectIdHex(args[0].(string)) + h.SearchM()["_id"] = ObjectIdHex(args[0].(string)) d, err := h.Find() if args[1].(bool) { @@ -244,16 +243,16 @@ func Test_Find_various_documents_with_Handle(t *testing.T) { switch args[0] { case "": mcl.ExpectFindAllReturn([]interface{}{ - bson.M{"_id": p[0].IDV, "created_on": p[0].CreatedOnV, "updated_on": p[0].UpdatedOnV}, - bson.M{"_id": p[1].IDV, "created_on": p[1].CreatedOnV, "updated_on": p[1].UpdatedOnV}, + M{"_id": p[0].IDV, "created_on": p[0].CreatedOnV, "updated_on": p[0].UpdatedOnV}, + M{"_id": p[1].IDV, "created_on": p[1].CreatedOnV, "updated_on": p[1].UpdatedOnV}, }) case p[0].ID().Hex(): mcl.ExpectFindAllReturn([]interface{}{ - bson.M{"_id": p[0].IDV, "created_on": p[0].CreatedOnV, "updated_on": p[0].UpdatedOnV}, + M{"_id": p[0].IDV, "created_on": p[0].CreatedOnV, "updated_on": p[0].UpdatedOnV}, }) case p[1].ID().Hex(): mcl.ExpectFindAllReturn([]interface{}{ - bson.M{"_id": p[1].IDV, "created_on": p[1].CreatedOnV, "updated_on": p[1].UpdatedOnV}, + M{"_id": p[1].IDV, "created_on": p[1].CreatedOnV, "updated_on": p[1].UpdatedOnV}, }) default: mcl.ExpectFindAllFail(anyReason) @@ -266,7 +265,7 @@ func Test_Find_various_documents_with_Handle(t *testing.T) { when("da, err := h.FindAll() is called with document id '%[1]v'", func(it bdd.It) { if args[0].(string) != "" { - h.Document().IDV = bson.ObjectIdHex(args[0].(string)) + h.Document().IDV = ObjectIdHex(args[0].(string)) } da, err := h.FindAll() @@ -301,7 +300,7 @@ func Test_Find_various_documents_with_Handle(t *testing.T) { when("da, err := h.FindAll() is called with Search '_id' equal '%[1]v'", func(it bdd.It) { if args[0].(string) != "" { - h.SearchM()["_id"] = bson.ObjectIdHex(args[0].(string)) + h.SearchM()["_id"] = ObjectIdHex(args[0].(string)) } da, err := h.FindAll() @@ -359,7 +358,7 @@ func Test_Insert_documents_with_Handle(t *testing.T) { var h productHandler = newProductHandle() _ = h.Link(db) if args[0].(string) != "" { - h.Document().IDV = bson.ObjectIdHex(args[0].(string)) + h.Document().IDV = ObjectIdHex(args[0].(string)) } when("h.Insert() is called", func(it bdd.It) { @@ -401,7 +400,7 @@ func Test_Remove_documents_with_Handle(t *testing.T) { var h productHandler = newProductHandle() _ = h.Link(db) if args[0].(string) != "" { - h.Document().IDV = bson.ObjectIdHex(args[0].(string)) + h.Document().IDV = ObjectIdHex(args[0].(string)) } when("h.Remove() is called", func(it bdd.It) { @@ -441,7 +440,7 @@ func Test_Remove_various_documents_with_Handle(t *testing.T) { var h productHandler = newProductHandle() _ = h.Link(db) if args[0].(string) != "" { - h.Document().IDV = bson.ObjectIdHex(args[0].(string)) + h.Document().IDV = ObjectIdHex(args[0].(string)) } when("h.RemoveAll() is called", func(it bdd.It) { @@ -478,7 +477,7 @@ func Test_Update_documents_with_Handle(t *testing.T) { var h productHandler = newProductHandle() _ = h.Link(db) if args[0].(string) != "" { - h.Document().IDV = bson.ObjectIdHex(args[0].(string)) + h.Document().IDV = ObjectIdHex(args[0].(string)) } when("h.Update() is called", func(it bdd.It) { diff --git a/helpers_test.go b/helpers_test.go index 845ab9f..95879e2 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -4,7 +4,6 @@ import ( "time" "github.com/ddspog/mongo/elements" - "github.com/globalsign/mgo/bson" ) const ( @@ -23,9 +22,9 @@ var productCollection = []*product{ // product it's a type embedding the Document struct. type product struct { - IDV bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"` - CreatedOnV int64 `json:"created_on,omitempty" bson:"created_on,omitempty"` - UpdatedOnV int64 `json:"updated_on,omitempty" bson:"updated_on,omitempty"` + IDV ObjectId `json:"_id,omitempty" bson:"_id,omitempty"` + CreatedOnV int64 `json:"created_on,omitempty" bson:"created_on,omitempty"` + UpdatedOnV int64 `json:"updated_on,omitempty" bson:"updated_on,omitempty"` } // newProduct returns a empty product. @@ -51,24 +50,24 @@ func (p *product) New() (doc Documenter) { return } -// Map translates a product to a bson.M object, more easily read by mgo +// Map translates a product to a M object, more easily read by mgo // methods. -func (p *product) Map() (out bson.M, err error) { +func (p *product) Map() (out M, err error) { out, err = MapDocumenter(p) return } -// Init translates a bson.M received, to the product structure. It +// Init translates a M received, to the product structure. It // fills the structure fields with the values of each key in the -// bson.M received. -func (p *product) Init(in bson.M) (err error) { +// M received. +func (p *product) Init(in M) (err error) { var doc Documenter = p err = InitDocumenter(in, &doc) return } // ID returns the _id attribute of a Document. -func (p *product) ID() (id bson.ObjectId) { +func (p *product) ID() (id ObjectId) { id = p.IDV return } @@ -113,9 +112,9 @@ type productHandler interface { Insert() error Remove() error RemoveAll() (*elements.ChangeInfo, error) - Update(bson.ObjectId) error + Update(ObjectId) error Document() *product - SearchM() bson.M + SearchM() M Name() string } @@ -198,7 +197,7 @@ func (p *productHandle) RemoveAll() (info *elements.ChangeInfo, err error) { // Update updates document from connected collection matching the id // received, and uses document info to update. -func (p *productHandle) Update(id bson.ObjectId) (err error) { +func (p *productHandle) Update(id ObjectId) (err error) { err = p.Handle.Update(id, p.Document()) return } diff --git a/mock.go b/mock.go index f0b8b8e..817cfe6 100644 --- a/mock.go +++ b/mock.go @@ -6,7 +6,6 @@ import ( "time" "github.com/ddspog/mongo/elements" - "github.com/globalsign/mgo/bson" ) // MockMongoSetup it's a setup type for configuring mocking for connect @@ -230,14 +229,14 @@ func (f *fakeNow) GetFunction() (fn func() time.Time) { // fakeNewID it's a type that enable mocking of function NewID. type fakeNewID struct { - returnV *bson.ObjectId + returnV *ObjectId mockModelSetupP *MockModelSetup } // FakeNewIDer it's a function mocking object, needed for mock purposes. type FakeNewIDer interface { - Returns(bson.ObjectId) - GetFunction() func() bson.ObjectId + Returns(ObjectId) + GetFunction() func() ObjectId } // newFakeNewID returns a new FakeNewID object. @@ -250,14 +249,14 @@ func newFakeNewID() (f *fakeNewID) { } // Returns ensures a value to be returned on calls to NewID. -func (f *fakeNewID) Returns(id bson.ObjectId) { +func (f *fakeNewID) Returns(id ObjectId) { *f.returnV = id f.mockModelSetupP.updateNewID() } // getFunction creates a version of NewID that returns value demanded. -func (f *fakeNewID) GetFunction() (fn func() bson.ObjectId) { - fn = func() (id bson.ObjectId) { +func (f *fakeNewID) GetFunction() (fn func() ObjectId) { + fn = func() (id ObjectId) { id = *f.returnV return } diff --git a/util.go b/util.go index 3b3262d..a986732 100644 --- a/util.go +++ b/util.go @@ -15,6 +15,30 @@ var ( newID = bson.NewObjectId ) +// M is a convenient alias for a map[string]interface{} map, useful for +// dealing with BSON in a native way. For instance: +// +// M{"a": 1, "b": true} +// +// There's no special handling for this type in addition to what's done anyway +// for an equivalent map type. Elements in the map will be dumped in an +// undefined ordered. +type M = bson.M + +// ObjectId is a unique ID identifying a BSON value. It must be exactly 12 bytes +// long. MongoDB objects by default have such a property set in their "_id" +// property. +// +// http://www.mongodb.org/display/DOCS/Object+Ids +type ObjectId = bson.ObjectId + +// ObjectIdHex returns an ObjectId from the provided hex representation. +// Calling this function with an invalid hex representation will +// cause a runtime panic. +func ObjectIdHex(s string) ObjectId { + return bson.ObjectIdHex(s) +} + // NowInMilli returns the actual time, in a int64 value in Millisecond // unit, used by the updaters of created_on and updated_on. func NowInMilli() (t int64) { @@ -28,10 +52,10 @@ func NewID() (id bson.ObjectId) { return } -// InitDocumenter translates a bson.M received, to the Documenter +// InitDocumenter translates a M received, to the Documenter // structure received as a pointer. It fills the structure fields with -// the values of each key in the bson.M received. -func InitDocumenter(in bson.M, out *Documenter) (err error) { +// the values of each key in the M received. +func InitDocumenter(in M, out *Documenter) (err error) { var marshalled []byte if marshalled, err = bson.Marshal(in); err == nil { @@ -41,15 +65,15 @@ func InitDocumenter(in bson.M, out *Documenter) (err error) { } // MapDocumenter translates a Documenter in whatever structure -// it has, to a bson.M object, more easily read by mgo.Collection +// it has, to a M object, more easily read by mgo.Collection // methods. -func MapDocumenter(in Documenter) (out bson.M, err error) { +func MapDocumenter(in Documenter) (out M, err error) { var buf []byte var target interface{} if buf, err = bson.Marshal(in); err == nil { if err = bson.Unmarshal(buf, &target); err == nil { - out = target.(bson.M) + out = target.(M) } }