Skip to content

Commit

Permalink
create a helper method to shorten the implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Kengo TODA <skypencil+github@gmail.com>
  • Loading branch information
KengoTODA committed Aug 23, 2022
1 parent c661d59 commit d35e88e
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions gcs/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,18 @@ func NewStorage(config *Config, client Client) (*Storage, error) {
}

func (s *Storage) Write(ctx context.Context, b []byte) error {
if s.client == nil {
client, err := gcStorage.NewClient(ctx)
if err != nil {
return err
}
s.client = Adapter{
config: *s.config,
client: client,
}
err := s.init(ctx)
if err != nil {
return err
}

return s.client.Write(ctx, b)
}

func (s *Storage) Read(ctx context.Context) ([]byte, error) {
if s.client == nil {
client, err := gcStorage.NewClient(ctx)
if err != nil {
return nil, err
}
s.client = Adapter{
config: *s.config,
client: client,
}
err := s.init(ctx)
if err != nil {
return nil, err
}

r, err := s.client.Read(ctx)
Expand All @@ -62,3 +50,17 @@ func (s *Storage) Read(ctx context.Context) ([]byte, error) {
}
return r, nil
}

func (s *Storage) init(ctx context.Context) error {
if s.client == nil {
client, err := gcStorage.NewClient(ctx)
if err != nil {
return err
}
s.client = Adapter{
config: *s.config,
client: client,
}
}
return nil
}

0 comments on commit d35e88e

Please sign in to comment.