Skip to content

Commit

Permalink
Merge pull request #3 from wajox/feature/refactor-package-structure
Browse files Browse the repository at this point in the history
refactor package structure
  • Loading branch information
ildarusmanov authored Jan 26, 2022
2 parents 922dc7a + f709a00 commit ac5896b
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 80 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type ExampleModel struct {

## Storage usage example
```golang
storage, connErr = NewBaseCollectionStorage(context.TODO(), mongoURI, mongoDBName, mongoCollectionName)
storage, connErr = NewBaseCollection(context.TODO(), mongoURI, mongoDBName, mongoCollectionName)

m := &ExampleModel{}

Expand Down
66 changes: 33 additions & 33 deletions pkg/services/base_collection_storage.go → base_collection.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package services
package mongol

import (
"context"
Expand Down Expand Up @@ -37,7 +37,7 @@ const (
)

var (
_ Storage = (*BaseCollectionStorage)(nil)
_ Storage = (*BaseCollection)(nil)
)

// Hook
Expand All @@ -53,8 +53,8 @@ func (c *Client) GetMongoClient() *mongo.Client {
return c.mongoClient
}

// BaseCollectionStorage
type BaseCollectionStorage struct {
// BaseCollection
type BaseCollection struct {
Client *Client
DBName string
CollectionName string
Expand All @@ -80,18 +80,18 @@ func NewClient(ctx context.Context, mongoURI string) (*Client, error) {
return &Client{mongoClient: c}, nil
}

// NewBaseCollectionStorage() is a constructor for BaseCollectionStorage struct
func NewBaseCollectionStorage(ctx context.Context, mongoURI, dbName, collectionName string) (*BaseCollectionStorage, error) {
// NewBaseCollection() is a constructor for BaseCollection struct
func NewBaseCollection(ctx context.Context, mongoURI, dbName, collectionName string) (*BaseCollection, error) {
client, err := NewClient(ctx, mongoURI)
if err != nil {
return nil, err
}

return NewBaseCollectionStorageWithClient(client, dbName, collectionName), nil
return NewBaseCollectionWithClient(client, dbName, collectionName), nil
}

func NewBaseCollectionStorageWithClient(client *Client, dbName, collectionName string) *BaseCollectionStorage {
return &BaseCollectionStorage{
func NewBaseCollectionWithClient(client *Client, dbName, collectionName string) *BaseCollection {
return &BaseCollection{
Client: client,
DBName: dbName,
CollectionName: collectionName,
Expand All @@ -100,7 +100,7 @@ func NewBaseCollectionStorageWithClient(client *Client, dbName, collectionName s
}
}

func (s *BaseCollectionStorage) runBeforeHooks(ctx context.Context, methodName string) error {
func (s *BaseCollection) runBeforeHooks(ctx context.Context, methodName string) error {
hooks, ok := s.BeforeHooks[methodName]
if !ok {
return nil
Expand All @@ -115,7 +115,7 @@ func (s *BaseCollectionStorage) runBeforeHooks(ctx context.Context, methodName s
return nil
}

func (s *BaseCollectionStorage) runAfterHooks(ctx context.Context, methodName string) error {
func (s *BaseCollection) runAfterHooks(ctx context.Context, methodName string) error {
hooks, ok := s.AfterHooks[methodName]
if !ok {
return nil
Expand All @@ -130,15 +130,15 @@ func (s *BaseCollectionStorage) runAfterHooks(ctx context.Context, methodName st
return nil
}

func (s *BaseCollectionStorage) AddBeforeHook(methodName string, h Hook) {
func (s *BaseCollection) AddBeforeHook(methodName string, h Hook) {
if _, ok := s.BeforeHooks[methodName]; !ok {
s.BeforeHooks[methodName] = []Hook{}
}

s.BeforeHooks[methodName] = append(s.BeforeHooks[methodName], h)
}

func (s *BaseCollectionStorage) AddAfterHook(methodName string, h Hook) {
func (s *BaseCollection) AddAfterHook(methodName string, h Hook) {
if _, ok := s.AfterHooks[methodName]; !ok {
s.AfterHooks[methodName] = []Hook{}
}
Expand All @@ -147,16 +147,16 @@ func (s *BaseCollectionStorage) AddAfterHook(methodName string, h Hook) {
}

// Ping() the mongo server
func (s *BaseCollectionStorage) Ping(ctx context.Context) error {
func (s *BaseCollection) Ping(ctx context.Context) error {
return s.Client.GetMongoClient().Ping(ctx, nil)
}

// GetCollection() returns storage collection
func (s *BaseCollectionStorage) GetCollection() *mongo.Collection {
func (s *BaseCollection) GetCollection() *mongo.Collection {
return s.Client.GetMongoClient().Database(s.DBName).Collection(s.CollectionName)
}

func (s *BaseCollectionStorage) CreateIndex(ctx context.Context, k interface{}, o *options.IndexOptions) (string, error) {
func (s *BaseCollection) CreateIndex(ctx context.Context, k interface{}, o *options.IndexOptions) (string, error) {
if err := s.runBeforeHooks(ctx, CreateIndexMethod); err != nil {
return "", err
}
Expand All @@ -170,7 +170,7 @@ func (s *BaseCollectionStorage) CreateIndex(ctx context.Context, k interface{},
}

// InsertOne() inserts given Document and returns an ID of inserted document
func (s *BaseCollectionStorage) InsertOne(ctx context.Context, m Document, opts ...*options.InsertOneOptions) (string, error) {
func (s *BaseCollection) InsertOne(ctx context.Context, m Document, opts ...*options.InsertOneOptions) (string, error) {
if err := s.runBeforeHooks(ctx, InsertOneMethod); err != nil {
return "", err
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func (s *BaseCollectionStorage) InsertOne(ctx context.Context, m Document, opts
}

// InsertMany()
func (s *BaseCollectionStorage) InsertMany(ctx context.Context, docs []interface{}, opts ...*options.InsertManyOptions) ([]string, error) {
func (s *BaseCollection) InsertMany(ctx context.Context, docs []interface{}, opts ...*options.InsertManyOptions) ([]string, error) {
if err := s.runBeforeHooks(ctx, InsertManyMethod); err != nil {
return []string{}, err
}
Expand Down Expand Up @@ -230,7 +230,7 @@ func (s *BaseCollectionStorage) InsertMany(ctx context.Context, docs []interface
}

// UpdateOne() updates given Document
func (s *BaseCollectionStorage) UpdateOne(ctx context.Context, m Document, opts ...*options.UpdateOptions) error {
func (s *BaseCollection) UpdateOne(ctx context.Context, m Document, opts ...*options.UpdateOptions) error {
if err := s.runBeforeHooks(ctx, UpdateOneMethod); err != nil {
return err
}
Expand All @@ -241,7 +241,7 @@ func (s *BaseCollectionStorage) UpdateOne(ctx context.Context, m Document, opts
}

// UpdateByFilter() updates given Document according to provided filter
func (s *BaseCollectionStorage) UpdateManyByFilter(ctx context.Context, filter interface{}, m Document, opts ...*options.UpdateOptions) error {
func (s *BaseCollection) UpdateManyByFilter(ctx context.Context, filter interface{}, m Document, opts ...*options.UpdateOptions) error {
if err := s.runBeforeHooks(ctx, UpdateManyByFilterMethod); err != nil {
return err
}
Expand Down Expand Up @@ -271,7 +271,7 @@ func (s *BaseCollectionStorage) UpdateManyByFilter(ctx context.Context, filter i
}

// UpdateMany()
func (s *BaseCollectionStorage) UpdateMany(
func (s *BaseCollection) UpdateMany(
ctx context.Context,
filter, update interface{},
opts ...*options.UpdateOptions,
Expand All @@ -291,7 +291,7 @@ func (s *BaseCollectionStorage) UpdateMany(
}

// UpsertOne - insert or update existing record. Returns updated model.
func (s *BaseCollectionStorage) UpsertOne(
func (s *BaseCollection) UpsertOne(
ctx context.Context,
filter interface{},
update bson.M,
Expand Down Expand Up @@ -329,7 +329,7 @@ func (s *BaseCollectionStorage) UpsertOne(
}

// ReplaceOne
func (s *BaseCollectionStorage) ReplaceOne(
func (s *BaseCollection) ReplaceOne(
ctx context.Context,
filter interface{},
m Document,
Expand All @@ -352,7 +352,7 @@ func (s *BaseCollectionStorage) ReplaceOne(
}

// ReplaceOneByID()
func (s *BaseCollectionStorage) ReplaceOneByID(
func (s *BaseCollection) ReplaceOneByID(
ctx context.Context,
recordID string,
m Document,
Expand All @@ -378,7 +378,7 @@ func (s *BaseCollectionStorage) ReplaceOneByID(
}

// GetOneByID() is trying to find Document by given recordID
func (s *BaseCollectionStorage) GetOneByID(
func (s *BaseCollection) GetOneByID(
ctx context.Context,
recordID string,
m Document,
Expand All @@ -401,7 +401,7 @@ func (s *BaseCollectionStorage) GetOneByID(
}

// GetOneByFilter() is trying to find Document by provided filter
func (s *BaseCollectionStorage) GetOneByFilter(
func (s *BaseCollection) GetOneByFilter(
ctx context.Context,
filter interface{},
m Document,
Expand Down Expand Up @@ -436,7 +436,7 @@ func (s *BaseCollectionStorage) GetOneByFilter(
}

// GetManyByFilter()
func (s *BaseCollectionStorage) GetManyByFilter(
func (s *BaseCollection) GetManyByFilter(
ctx context.Context,
filter interface{},
modelBuilder func() Document,
Expand Down Expand Up @@ -483,7 +483,7 @@ func (s *BaseCollectionStorage) GetManyByFilter(
}

// FindAllByFilter()
func (s *BaseCollectionStorage) FindAllByFilter(
func (s *BaseCollection) FindAllByFilter(
ctx context.Context,
filter interface{},
docs interface{},
Expand Down Expand Up @@ -514,7 +514,7 @@ func (s *BaseCollectionStorage) FindAllByFilter(
}

// FindManyByFilter()
func (s *BaseCollectionStorage) FindManyByFilter(
func (s *BaseCollection) FindManyByFilter(
ctx context.Context,
filter interface{},
opts ...*options.FindOptions,
Expand Down Expand Up @@ -542,7 +542,7 @@ func (s *BaseCollectionStorage) FindManyByFilter(
}

// CountByFilter
func (s *BaseCollectionStorage) CountByFilter(
func (s *BaseCollection) CountByFilter(
ctx context.Context,
filter interface{},
) (int64, error) {
Expand All @@ -555,7 +555,7 @@ func (s *BaseCollectionStorage) CountByFilter(
}

// DeleteManyByFilter() documents by given filters
func (s *BaseCollectionStorage) DeleteManyByFilter(
func (s *BaseCollection) DeleteManyByFilter(
ctx context.Context,
filter interface{},
opts ...*options.DeleteOptions,
Expand All @@ -569,7 +569,7 @@ func (s *BaseCollectionStorage) DeleteManyByFilter(
}

// DeleteOneByID() deletes document by given ID
func (s *BaseCollectionStorage) DeleteOneByID(ctx context.Context, docID string) error {
func (s *BaseCollection) DeleteOneByID(ctx context.Context, docID string) error {
if err := s.runBeforeHooks(ctx, DeleteOneByIDMethod); err != nil {
return err
}
Expand All @@ -592,7 +592,7 @@ func (s *BaseCollectionStorage) DeleteOneByID(ctx context.Context, docID string)
}

// DropAll() deletes collection from database
func (s *BaseCollectionStorage) DeleteAll(ctx context.Context) error {
func (s *BaseCollection) DeleteAll(ctx context.Context) error {
if err := s.runBeforeHooks(ctx, DeleteAllMethod); err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package services_test
package mongol_test

import (
"context"
Expand All @@ -12,12 +12,11 @@ import (

timecop "github.com/bluele/go-timecop"
"github.com/google/uuid"
"github.com/wajox/mongol/pkg/models"
. "github.com/wajox/mongol/pkg/services"
. "github.com/wajox/mongol"
)

type ExampleModel struct {
models.BaseDocument `bson:",inline"`
BaseDocument `bson:",inline"`

Title string `json:"title,omitempty" bson:"title,omitempty"`
}
Expand All @@ -27,10 +26,10 @@ func NewExampleModel() *ExampleModel {
}

// nolint
var _ = Describe("BaseCollectionStorage", func() {
var _ = Describe("BaseCollection", func() {
var (
mongoURI, mongoDBName, mongoCollectionName string
storage *BaseCollectionStorage
storage *BaseCollection
connErr error
)

Expand All @@ -42,7 +41,7 @@ var _ = Describe("BaseCollectionStorage", func() {
mongoDBName = "base_models_db_test"
mongoCollectionName = "base_models_test"

storage, connErr = NewBaseCollectionStorage(context.TODO(), mongoURI, mongoDBName, mongoCollectionName)
storage, connErr = NewBaseCollection(context.TODO(), mongoURI, mongoDBName, mongoCollectionName)

if connErr != nil {
GinkgoT().Fatal(connErr)
Expand Down
5 changes: 2 additions & 3 deletions pkg/models/base_document.go → base_document.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package models
package mongol

import (
timecop "github.com/bluele/go-timecop"
"github.com/wajox/mongol/pkg/helpers"
"go.mongodb.org/mongo-driver/bson/primitive"
)

Expand All @@ -25,7 +24,7 @@ func (m *BaseDocument) GetHexID() string {

// SetHexID() sets ID of the document from the hex-string
func (m *BaseDocument) SetHexID(hexID string) error {
oid, err := helpers.StringToObjectID(hexID)
oid, err := StringToObjectID(hexID)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/models/base_model_test.go → base_model_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models_test
package mongol_test

import (
"time"
Expand All @@ -7,7 +7,7 @@ import (
. "github.com/onsi/gomega"

timecop "github.com/bluele/go-timecop"
. "github.com/wajox/mongol/pkg/models"
. "github.com/wajox/mongol"
"go.mongodb.org/mongo-driver/bson/primitive"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/services/errors.go → errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package services
package mongol

import (
"errors"
Expand Down
11 changes: 10 additions & 1 deletion pkg/services/helpers.go → helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package services
package mongol

import (
"fmt"
Expand Down Expand Up @@ -30,3 +30,12 @@ func IsValidMongoID(id string) bool {

return true
}

func StringToObjectID(hexID string) (primitive.ObjectID, error) {
oid, err := primitive.ObjectIDFromHex(hexID)
if err != nil {
return primitive.ObjectID{}, ErrInvalidObjectID
}

return oid, nil
}
6 changes: 3 additions & 3 deletions pkg/models/models_suite_test.go → mongol_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models_test
package mongol_test

import (
"testing"
Expand All @@ -7,7 +7,7 @@ import (
. "github.com/onsi/gomega"
)

func TestModels(t *testing.T) {
func TestMongol(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Models Suite")
RunSpecs(t, "Mongol Suite")
}
Loading

0 comments on commit ac5896b

Please sign in to comment.