diff --git a/internal/server/gcs/gcs.go b/internal/server/gcs/gcs.go index b1bd06b..d6dee66 100644 --- a/internal/server/gcs/gcs.go +++ b/internal/server/gcs/gcs.go @@ -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 @@ -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) } diff --git a/internal/server/gcs/gcs_test.go b/internal/server/gcs/gcs_test.go index 63facc6..9619b5e 100644 --- a/internal/server/gcs/gcs_test.go +++ b/internal/server/gcs/gcs_test.go @@ -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{ @@ -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()) -}