Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

treat issue problem index out of bounds! #13

Merged
merged 5 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions mgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type collection interface {
ReplaceOne(ctx context.Context, filter interface{}, replacement interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)
Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult
}

//DBClient is a mongodb Client instance
Expand Down Expand Up @@ -98,16 +99,18 @@ func (c *DBClient) GetMonthlyInfo(agencies []Agency, year int) (map[string][]Age
return result, nil
}

//GetDataForSecondScreen GetDataForSecondScreen
//GetDataForSecondScreen Search if DB has a match for filters
func (c *DBClient) GetDataForSecondScreen(month int, year int, agency string) (*AgencyMonthlyInfo, error) {
c.Collection(c.monthlyInfoCol)
resultMonthly, err := c.col.Find(context.TODO(), bson.D{{Key: "aid", Value: agency}, {Key: "year", Value: year}, {Key: "month", Value: month}})
var resultMonthly AgencyMonthlyInfo
err := c.col.FindOne(context.TODO(), bson.D{{Key: "aid", Value: agency}, {Key: "year", Value: year}, {Key: "month", Value: month}}).Decode(&resultMonthly)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
err := c.col.FindOne(context.TODO(), bson.D{{Key: "aid", Value: agency}, {Key: "year", Value: year}, {Key: "month", Value: month}}).Decode(&resultMonthly)
err := c.col.FindOne(context.TODO(), bson.D{
{Key: "aid", Value: agency},
{Key: "year", Value: year},
{Key: "month", Value: month}
}).Decode(&resultMonthly)

if err != nil {
return nil, fmt.Errorf("Error in GetMonthlyInfo %v", err)
// ErrNoDocuments means that the filter did not match any documents in the collection
if err == mongo.ErrNoDocuments {
return nil, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Por favor não retorne um elemento de outro pacote que você está abstraindo do usuário. Fazendo isso você está expondo um detalhe de implementação: a forma como seu dado é armazenado.

É super simples criar uma constante dentro do seu próprio pacote e retornar ela

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entendi, vou ter mais cuidado a partir de agora.

}
}
var mr []AgencyMonthlyInfo
resultMonthly.All(context.TODO(), &mr)
return &mr[0], nil
return &resultMonthly, nil
}

//Collection Changes active collection
Expand Down
4 changes: 4 additions & 0 deletions mgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ func (c *checkCollection) Find(ctx context.Context, filter interface{}, opts ...
return nil, nil
}

func (c *checkCollection) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult {
return nil
}

func (cs *checkStorage) ObjectPut(container string, objectName string, contents io.Reader, checkHash bool, Hash string, contentType string, h swift.Headers) (headers swift.Headers, err error) {
if cs.check {
assert.Equal(cs.t, cs.container, container)
Expand Down