[#129] Fix root objects listing

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2021-07-04 23:25:53 +03:00
parent daed0978a6
commit e145effd17
2 changed files with 58 additions and 9 deletions

View file

@ -285,15 +285,7 @@ func (n *layer) ListObjects(ctx context.Context, p *ListObjectsParams) (*ListObj
result.NextMarker = result.Objects[len(result.Objects)-1].Name result.NextMarker = result.Objects[len(result.Objects)-1].Name
} }
index := 0 fillPrefixes(&result, uniqNames)
for _, oi := range result.Objects {
if isDir := uniqNames[oi.Name]; isDir {
result.Objects = append(result.Objects[:index], result.Objects[index+1:]...)
result.Prefixes = append(result.Prefixes, oi.Name)
} else {
index++
}
}
if needDirectoryAsKey { if needDirectoryAsKey {
res := []*ObjectInfo{{ res := []*ObjectInfo{{
Name: p.Prefix, Name: p.Prefix,
@ -306,6 +298,19 @@ func (n *layer) ListObjects(ctx context.Context, p *ListObjectsParams) (*ListObj
return &result, nil return &result, nil
} }
func fillPrefixes(result *ListObjectsInfo, directories map[string]bool) {
index := 0
for range result.Objects {
name := result.Objects[index].Name
if isDir := directories[name]; isDir {
result.Objects = append(result.Objects[:index], result.Objects[index+1:]...)
result.Prefixes = append(result.Prefixes, name)
} else {
index++
}
}
}
// GetObject from storage. // GetObject from storage.
func (n *layer) GetObject(ctx context.Context, p *GetObjectParams) error { func (n *layer) GetObject(ctx context.Context, p *GetObjectParams) error {
var ( var (

44
api/layer/layer_test.go Normal file
View file

@ -0,0 +1,44 @@
package layer
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestFillingPrefixes(t *testing.T) {
cases := []struct {
name string
list *ListObjectsInfo
directories map[string]bool
expectedPrefixes []string
expectedObjects []*ObjectInfo
}{
{
name: "3 dirs",
list: &ListObjectsInfo{
Objects: []*ObjectInfo{{Name: "dir/"}, {Name: "dir2/"}, {Name: "dir3/"}},
},
directories: map[string]bool{"dir/": true, "dir2/": true, "dir3/": true},
expectedPrefixes: []string{"dir/", "dir2/", "dir3/"},
expectedObjects: []*ObjectInfo{},
},
{
name: "1 obj, 3 dirs",
list: &ListObjectsInfo{
Objects: []*ObjectInfo{{Name: "dir/"}, {Name: "dir2/"}, {Name: "dir3/"}, {Name: "obj"}},
},
directories: map[string]bool{"dir/": true, "dir2/": true, "dir3/": true},
expectedPrefixes: []string{"dir/", "dir2/", "dir3/"},
expectedObjects: []*ObjectInfo{{Name: "obj"}},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
fillPrefixes(tc.list, tc.directories)
require.Equal(t, tc.expectedPrefixes, tc.list.Prefixes)
require.Equal(t, tc.expectedObjects, tc.list.Objects)
})
}
}