Skip to content
This repository has been archived by the owner on Mar 10, 2021. It is now read-only.

Commit

Permalink
Linters fix
Browse files Browse the repository at this point in the history
  • Loading branch information
obalunenko committed Nov 25, 2019
1 parent 709eced commit 921af83
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
6 changes: 4 additions & 2 deletions cmd/logs-converter-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ func main() {
log.Fatalf("failed to connect to database: %v", err)
}

if err := dbc.Drop(cfg.DropDB); err != nil {
log.Fatal(err)
if cfg.DropDB {
if err := dbc.Drop(); err != nil {
log.Fatal(err)
}
}

resChan := make(chan *models.LogModel)
Expand Down
34 changes: 13 additions & 21 deletions internal/db/mongo/db.go → internal/db/mongo.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package mongo implements mongo db interactions functionality.
package mongo
// Package db implements database interactions.
package db

import (
"time"
Expand All @@ -12,19 +12,15 @@ import (
"github.com/oleg-balunenko/logs-converter/internal/models"
)

// db stores mongo db connection details
type db struct {
// mongoDB stores mongo mongoDB connection details
type mongoDB struct {
session *mgo.Session
database *mgo.Database
collection *mgo.Collection
}

// NewMongoDBConnection establishes connection with mongoDB and return DBName object
func NewMongoDBConnection(url, dbName, collectionName, username, password string) (*db, error) {
return newConnection(url, dbName, collectionName, username, password)
}

func newConnection(url, dbName, collectionName, username, password string) (*db, error) {
// newMongoDBConnection establishes connection with mongoDB and return DBName object
func newMongoDBConnection(url, dbName, collectionName, username, password string) (*mongoDB, error) {
mongoDBDialInfo := &mgo.DialInfo{
Addrs: []string{url},
Timeout: 60 * time.Second,
Expand All @@ -41,7 +37,7 @@ func newConnection(url, dbName, collectionName, username, password string) (*db,
database := session.DB(dbName)
collection := database.C(collectionName)

return &db{
return &mongoDB{
session: session,
database: database,
collection: collection,
Expand All @@ -50,7 +46,7 @@ func newConnection(url, dbName, collectionName, username, password string) (*db,

// Store stores model in database with unique id
// return id and error
func (db *db) Store(model *models.LogModel) (string, error) {
func (db *mongoDB) Store(model *models.LogModel) (string, error) {
log.Debugf("Storing model [%+v] to collection [%+v]", model, db.collection)

id := bson.NewObjectId().Hex()
Expand All @@ -65,28 +61,24 @@ func (db *db) Store(model *models.LogModel) (string, error) {
return model.ID, nil
}

// Delete deletes model from db by id
func (db *db) Delete(id string) error {
// Delete deletes model from mongoDB by id
func (db *mongoDB) Delete(id string) error {
return db.collection.RemoveId(id)
}

// Update updates existed model by id
func (db *db) Update(id string, logModel models.LogModel) error {
func (db *mongoDB) Update(id string, logModel models.LogModel) error {
return db.collection.UpdateId(id, logModel)
}

// Close closes mongo connection
func (db *db) Close() {
func (db *mongoDB) Close() {
log.Infof("Closing connection...")
db.session.Close()
}

// Drop drops database collection
func (db *db) Drop(shouldDrop bool) error {
if !shouldDrop {
return nil
}

func (db *mongoDB) Drop() error {
databases, err := db.session.DatabaseNames()
if err != nil {
return errors.Wrap(err, "failed to list databases")
Expand Down
7 changes: 3 additions & 4 deletions internal/db/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package db
import (
"github.com/pkg/errors"

"github.com/oleg-balunenko/logs-converter/internal/db/mongo"
"github.com/oleg-balunenko/logs-converter/internal/models"
)

Expand All @@ -15,7 +14,7 @@ type StorageType uint
const ( // database types
storageTypeUnknown StorageType = iota

// StorageTypeMongo - mongo db type
// StorageTypeMongo - mongo mongoDB type
StorageTypeMongo

storageTypeSentinel // should be always last
Expand All @@ -31,7 +30,7 @@ type Repository interface {
Store(logModel *models.LogModel) (string, error)
Update(id string, logModel models.LogModel) error
Delete(id string) error
Drop(bool) error
Drop() error
Close()
}

Expand All @@ -48,7 +47,7 @@ type Params struct {
func Connect(dbType StorageType, params Params) (Repository, error) {
switch dbType {
case StorageTypeMongo:
return mongo.NewMongoDBConnection(params.URL, params.DB, params.Collection, params.Username, params.Password)
return newMongoDBConnection(params.URL, params.DB, params.Collection, params.Username, params.Password)
default:
return nil, errors.Errorf("not supported database type [%s]", dbType)
}
Expand Down

0 comments on commit 921af83

Please sign in to comment.