Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
fix: fix prefixing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lavkesh committed Sep 13, 2023
1 parent a9fc7cd commit 121ec7f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
15 changes: 5 additions & 10 deletions internal/server/gcs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ func NewClient(keyFilePath string) (*Client, error) {
return &Client{storageClient: SClient{gcsClient: client}}, nil
}

var errWrongPath = errors.New("object is not in correct path, It should be topic/date/file-name")

func wrongPath(name string) error {
return fmt.Errorf("%w: Path %s", errWrongPath, name)
}

func (client Client) ListTopicDates(bucketInfo BucketInfo) (map[string]map[string]int64, error) {
bucket := bucketInfo.BucketName
prefix := bucketInfo.Prefix
Expand All @@ -52,11 +46,12 @@ func (client Client) ListTopicDates(bucketInfo BucketInfo) (map[string]map[strin
return nil, fmt.Errorf("Bucket(%q).Objects(): %w", bucket, err)
}
splits := strings.Split(attrs.Name, "/")
if len(splits) != 3 {
return nil, wrongPath(attrs.Name)
if len(splits) != 4 {
continue
}
topicName := splits[0]
date := splits[1]
// prefix/topic-name/date/object-name
topicName := splits[1]
date := splits[2]
if topicDateMap[topicName] == nil {
topicDateMap[topicName] = make(map[string]int64)
}
Expand Down
25 changes: 6 additions & 19 deletions internal/server/gcs/gcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func (*MockStorageClient) Objects(context.Context, string, *storage.Query) Wrapp
}

func TestListTopicDates(t *testing.T) {
mockIterator.On("Next").Return(&storage.ObjectAttrs{Name: "test-topic1/2023-08-26/file1", Size: 123}, nil).Once()
mockIterator.On("Next").Return(&storage.ObjectAttrs{Name: "test-topic1/2023-08-26/file2", Size: 456}, nil).Once()
mockIterator.On("Next").Return(&storage.ObjectAttrs{Name: "test-topic1/2023-08-27/file3", Size: 789}, nil).Once()
mockIterator.On("Next").Return(&storage.ObjectAttrs{Name: "test-topic1/2023-08-27/file4", Size: 101}, nil).Once()
mockIterator.On("Next").Return(&storage.ObjectAttrs{Name: "test-topic2/2023-08-28/file5", Size: 707}, nil).Once()
mockIterator.On("Next").Return(&storage.ObjectAttrs{Name: "test-topic2/2023-08-28/file6", Size: 989}, nil).Once()
mockIterator.On("Next").Return(&storage.ObjectAttrs{Name: "prefix/test-topic1/2023-08-26/file1", Size: 123}, nil).Once()
mockIterator.On("Next").Return(&storage.ObjectAttrs{Name: "prefix/test-topic1/2023-08-26/file2", Size: 456}, nil).Once()
mockIterator.On("Next").Return(&storage.ObjectAttrs{Name: "prefix/test-topic1/2023-08-27/file3", Size: 789}, nil).Once()
mockIterator.On("Next").Return(&storage.ObjectAttrs{Name: "prefix/test-topic1/2023-08-27/file4", Size: 101}, nil).Once()
mockIterator.On("Next").Return(&storage.ObjectAttrs{Name: "prefix/test-topic2/2023-08-28/file5", Size: 707}, nil).Once()
mockIterator.On("Next").Return(&storage.ObjectAttrs{Name: "prefix/test-topic2/2023-08-28/file6", Size: 989}, nil).Once()
mockIterator.On("Next").Return(nil, iterator.Done).Once()
client := Client{storageClient: &MockStorageClient{}}
topicDates, err := client.ListTopicDates(BucketInfo{
Expand Down Expand Up @@ -69,16 +69,3 @@ func TestErrorOnListTopic(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, "Bucket(\"test-bucket\").Objects(): test-error", err.Error())
}

func TestErrorForWrongPath(t *testing.T) {
mockIterator.On("Next").Return(&storage.ObjectAttrs{Name: "test-topic1/31", Size: 123}, nil).Once()
client := Client{storageClient: &MockStorageClient{}}
topicDates, err := client.ListTopicDates(BucketInfo{
BucketName: "test-bucket",
Prefix: "prefix",
Delim: "",
})
assert.Nil(t, topicDates)
assert.Error(t, err)
assert.Equal(t, "object is not in correct path, It should be topic/date/file-name: Path test-topic1/31", err.Error())
}

0 comments on commit 121ec7f

Please sign in to comment.