Skip to content

Commit

Permalink
Add CustomFirestoreClient to subscriber/publisher config
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslavo committed May 14, 2020
1 parent 04be856 commit 78d4cb0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
15 changes: 12 additions & 3 deletions pkg/firestore/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type PublisherConfig struct {

// Marshaler marshals message from Watermill to Firestore format and vice versa.
Marshaler Marshaler

// CustomFirestoreClient can be used to override a default client.
CustomFirestoreClient *firestore.Client
}

func (c *PublisherConfig) setDefaults() {
Expand Down Expand Up @@ -80,9 +83,15 @@ type subscriptionsCacheEntry struct {
func NewPublisher(config PublisherConfig, logger watermill.LoggerAdapter) (*Publisher, error) {
config.setDefaults()

client, err := firestore.NewClient(context.Background(), config.ProjectID, config.GoogleClientOpts...)
if err != nil {
return nil, err
var client *firestore.Client
if config.CustomFirestoreClient != nil {
client = config.CustomFirestoreClient
} else {
var err error
client, err = firestore.NewClient(context.Background(), config.ProjectID, config.GoogleClientOpts...)
if err != nil {
return nil, errors.Wrap(err, "cannot create default firestore client")
}
}

return &Publisher{
Expand Down
17 changes: 13 additions & 4 deletions pkg/firestore/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package firestore

import (
"context"
"errors"
"sync"
"time"

"cloud.google.com/go/firestore"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/pkg/errors"
"google.golang.org/api/option"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -45,6 +45,9 @@ type SubscriberConfig struct {

// Marshaler marshals message from Watermill to Firestore format and vice versa.
Marshaler Marshaler

// CustomFirestoreClient can be used to override a default client.
CustomFirestoreClient *firestore.Client
}

func (c *SubscriberConfig) setDefaults() {
Expand Down Expand Up @@ -84,9 +87,15 @@ type Subscriber struct {
func NewSubscriber(config SubscriberConfig, logger watermill.LoggerAdapter) (*Subscriber, error) {
config.setDefaults()

client, err := firestore.NewClient(context.Background(), config.ProjectID, config.GoogleClientOpts...)
if err != nil {
return nil, err
var client *firestore.Client
if config.CustomFirestoreClient != nil {
client = config.CustomFirestoreClient
} else {
var err error
client, err = firestore.NewClient(context.Background(), config.ProjectID, config.GoogleClientOpts...)
if err != nil {
return nil, errors.Wrap(err, "cannot create default firestore client")
}
}

return &Subscriber{
Expand Down

0 comments on commit 78d4cb0

Please sign in to comment.