Skip to content

Commit

Permalink
Merge branch 'feature/collection-name'
Browse files Browse the repository at this point in the history
  • Loading branch information
victorguarana committed Apr 30, 2024
2 parents ad23bb2 + af96174 commit f15e6bb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ type Collection[T any] interface {
ListIndexes(ctx context.Context) ([]Index, error)

Drop(ctx context.Context) error

Name() string
}
```

Expand Down
9 changes: 8 additions & 1 deletion gomongo/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ type Collection[T any] interface {
ListIndexes(ctx context.Context) ([]Index, error)

Drop(ctx context.Context) error

Name() string
}

type collection[T any] struct {
mongoCollection *mongo.Collection
}

func NewCollection[T any](database *Database, collectionName string) (Collection[T], error) {
if database == nil || database.mongoDatabase == nil {
if err := validateDatabase(database); err != nil {
return nil, ErrConnectionNotInitialized
}

Expand Down Expand Up @@ -196,6 +198,11 @@ func (c *collection[T]) Drop(ctx context.Context) error {
return drop(ctx, c.mongoCollection)
}

// Name returns the name of a collection
func (c *collection[T]) Name() string {
return c.mongoCollection.Name()
}

func validateReceivedID(id ID) error {
if id == nil {
return ErrEmptyID
Expand Down
6 changes: 6 additions & 0 deletions gomongo/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,12 @@ var _ = Describe("collection{}", Ordered, func() {
})
})
})

Describe("Name", func() {
It("should return collection name", func() {
Expect(sut.Name()).To(Equal(collectionName))
})
})
})

func initializeCollection(ctx context.Context, mongoURI, databaseName, collectionName string) (collection[DummyStruct], error) {
Expand Down
8 changes: 8 additions & 0 deletions gomongo/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ func pingMongoServer(cs *ConnectionSettings, mongoClient *mongo.Client, ctx cont

return mongoClient.Ping(ctx, nil)
}

func validateDatabase(database *Database) error {
if database == nil || database.mongoDatabase == nil {
return ErrConnectionNotInitialized
}

return nil
}

0 comments on commit f15e6bb

Please sign in to comment.