Skip to content

Commit

Permalink
Merge branch 'release/v2.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ddspog committed May 13, 2018
2 parents 2b0848f + dcb1efb commit a613156
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 217 deletions.
49 changes: 11 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ func (p *Product) New() (doc mongo.Documenter) {
return
}

func (p *Product) Validate() (err error) {
return
}

// On these methods, you can use the functions implemented mongo
// package.
func (p *Product) Map() (out M, err error) {
Expand Down Expand Up @@ -182,7 +186,6 @@ The package should be used to create new types. Use the Handler type for creatin
```go
type ProductHandle struct {
*mongo.Handle
DocumentV *product.Product
}
```

Expand All @@ -195,8 +198,7 @@ For each new type, a constructor may be needed, and for that Handler has a basic
// }
func New() (p *ProductHandle) {
p = &ProductHandle{
Handle: mongo.NewHandle(product.CollectionName, product.CollectionIndexes...),
DocumentV: product.New(),
Handle: mongo.NewHandle(product.CollectionName, product.New(), product.CollectionIndexes...),
}
return
}
Expand All @@ -213,7 +215,6 @@ func (p *ProductHandle) Safely() (ph *ProductHandle) {

func (p *ProductHandle) Clean() (ph *ProductHandle) {
p.Handle.Clean()
p.DocumentV = product.New()
ph = p
return
}
Expand All @@ -223,66 +224,38 @@ Create a Document, or SearchMap getter and setters functions improving use of Ha

```go
func (p *ProductHandle) SetDocument(d *product.Product) (r *ProductHandle) {
p.DocumentV = d
p.Handle.SetDocument(d)
r = p
return
}

func (p *ProductHandle) Document() (d *product.Product) {
d = p.DocumentV
d = p.Handle.Document().(*product.Product)
return
}

// Note that SearchMap, the getter is already defined on Handle.
func (p *ProductHandle) SearchFor(s mongo.M) (r *ProductHandle) {
p.SearchMapV = s
p.Handle.SearchFor(s)
r = p
return
}
```

The creation of Insert, Remove and RemoveAll are trivial.

```go
func (p *ProductHandle) Insert() (err error) {
err = p.Handle.Insert(p.Document())
return
}

func (p *ProductHandle) Remove() (err error) {
err = p.Handle.Remove(p.Document().ID())
return
}

func (p *ProductHandle) RemoveAll() (info *mgo.ChangeInfo, err error) {
info, err = p.Handle.RemoveAll(p.Document())
return
}
```

The Update function uses an id as an argument:

```go
func (p *ProductHandle) Update(id mongo.ObjectId) (err error) {
err = p.Handle.Update(id, p.Document())
return
}
```

The complicated functions are Find and FindAll which requires casting for the Document type:

```go
func (p *ProductHandle) Find() (prod *product.Product, err error) {
var doc mongo.Documenter = product.New()
err = p.Handle.Find(p.Document(), doc)
var doc mongo.Documenter
doc, err = p.Handle.Find()
prod = doc.(*product.Product)
return
}

// QueryOptions serve to add options on returning the query.
func (p *ProductHandle) FindAll(opts ...mongo.QueryOptions) (proda []*product.Product, err error) {
var da []mongo.Documenter
err = p.Handle.FindAll(p.Document(), &da, opts...)
err = p.Handle.FindAll(opts...)
proda = make([]*product.Product, len(da))
for i := range da {
//noinspection GoNilContainerIndexing
Expand Down
4 changes: 1 addition & 3 deletions acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ func Test_Manipulate_data_on_MongoDB(t *testing.T) {
})

when(fmt.Sprintf("using p.Remove() to remove doc with id '%[1]s'", newId.Hex()), func(it bdd.It) {
errRemove := p.SetDocument(&product{
IDV: newId,
}).Remove()
errRemove := p.Remove(newId)

it("should return no errors", func(assert bdd.Assert) {
assert.NoError(errRemove)
Expand Down
46 changes: 12 additions & 34 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ The Documenter can be used like this:
return
}
func (p *Product) Validate() (err error) {
return
}
// On these methods, you can use the functions implemented mongo
// package.
func (p *Product) Map() (out M, err error) {
Expand Down Expand Up @@ -177,7 +182,6 @@ for creating embedding types.
type ProductHandle struct {
*mongo.Handle
DocumentV *product.Product
}
For each new type, a constructor may be needed, and for that Handler
Expand All @@ -191,8 +195,7 @@ collection used on Handle, and optional indexes for collection.
// }
func New() (p *ProductHandle) {
p = &ProductHandle{
Handle: mongo.NewHandle(product.CollectionName, product.CollectionIndexes...),
DocumentV: product.New(),
Handle: mongo.NewHandle(product.CollectionName, product.New(), product.CollectionIndexes...),
}
return
}
Expand All @@ -209,7 +212,6 @@ optional), as it follows:
func (p *ProductHandle) Clean() (ph *ProductHandle) {
p.Handle.Clean()
p.DocumentV = product.New()
ph = p
return
}
Expand All @@ -218,61 +220,37 @@ Create a Document, or SearchMap getter and setters functions improving
use of Handle:
func (p *ProductHandle) SetDocument(d *product.Product) (r *ProductHandle) {
p.DocumentV = d
p.Handle.SetDocument(d)
r = p
return
}
func (p *ProductHandle) Document() (d *product.Product) {
d = p.DocumentV
d = p.Handle.Document().(*product.Product)
return
}
// Note that SearchMap, the getter is already defined on Handle.
func (p *ProductHandle) SearchFor(s mongo.M) (r *ProductHandle) {
p.SearchMapV = s
p.Handle.SearchFor(s)
r = p
return
}
The creation of Insert, Remove and RemoveAll are trivial.
func (p *ProductHandle) Insert() (err error) {
err = p.Handle.Insert(p.Document())
return
}
func (p *ProductHandle) Remove() (err error) {
err = p.Handle.Remove(p.Document().ID())
return
}
func (p *ProductHandle) RemoveAll() (info *mgo.ChangeInfo, err error) {
info, err = p.Handle.RemoveAll(p.Document())
return
}
The Update function uses an id as an argument:
func (p *ProductHandle) Update(id mongo.ObjectId) (err error) {
err = p.Handle.Update(id, p.Document())
return
}
The complicated functions are Find and FindAll which requires casting
for the Document type:
func (p *ProductHandle) Find() (prod *product.Product, err error) {
var doc mongo.Documenter = product.New()
err = p.Handle.Find(p.Document(), doc)
var doc mongo.Documenter
doc, err = p.Handle.Find()
prod = doc.(*product.Product)
return
}
// QueryOptions serve to add options on returning the query.
func (p *ProductHandle) FindAll(opts ...mongo.QueryOptions) (proda []*product.Product, err error) {
var da []mongo.Documenter
err = p.Handle.FindAll(p.Document(), &da, opts...)
err = p.Handle.FindAll(opts...)
proda = make([]*product.Product, len(da))
for i := range da {
//noinspection GoNilContainerIndexing
Expand Down
1 change: 1 addition & 0 deletions documenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package mongo
// updated_on
type Documenter interface {
New() Documenter
Validate() error
Map() (M, error)
Init(M) error
ID() ObjectId
Expand Down
Loading

0 comments on commit a613156

Please sign in to comment.