Skip to content

Commit

Permalink
chore: refactor blob storage factory (#55)
Browse files Browse the repository at this point in the history
Consistent factory style. #44 #45
  • Loading branch information
bestmike007 committed Oct 30, 2023
1 parent b18a24c commit a30d608
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 37 deletions.
34 changes: 0 additions & 34 deletions internal/storage/blobstorage/blobstorage.go

This file was deleted.

19 changes: 19 additions & 0 deletions internal/storage/blobstorage/internal/blobstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"

"go.uber.org/fx"
"golang.org/x/xerrors"

"github.com/coinbase/chainstorage/internal/config"
"github.com/coinbase/chainstorage/internal/utils/fxparams"
api "github.com/coinbase/chainstorage/protos/coinbase/chainstorage"
)
Expand All @@ -25,3 +27,20 @@ type (
S3 BlobStorageFactory `name:"blobstorage/s3"`
}
)

func WithBlobStorageFactory(params BlobStorageFactoryParams) (BlobStorage, error) {
var factory BlobStorageFactory
storageType := params.Config.StorageType.BlobStorageType
switch storageType {
case config.BlobStorageType_UNSPECIFIED, config.BlobStorageType_S3:
factory = params.S3
}
if factory == nil {
return nil, xerrors.Errorf("blob storage type is not implemented: %v", storageType)
}
result, err := factory.Create()
if err != nil {
return nil, xerrors.Errorf("failed to create blob storage of type %v: %w", storageType, err)
}
return result, nil
}
11 changes: 8 additions & 3 deletions internal/storage/blobstorage/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package blobstorage
import (
"go.uber.org/fx"

"github.com/coinbase/chainstorage/internal/storage/blobstorage/downloader"
"github.com/coinbase/chainstorage/internal/storage/blobstorage/internal"
"github.com/coinbase/chainstorage/internal/storage/blobstorage/s3"
)

type (
BlobStorage = internal.BlobStorage
BlobStorageFactory = internal.BlobStorageFactory
BlobStorageFactoryParams = internal.BlobStorageFactoryParams
)

var Module = fx.Options(
fx.Provide(New),
fx.Provide(internal.WithBlobStorageFactory),
s3.Module,
downloader.Module,
)

0 comments on commit a30d608

Please sign in to comment.