Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage): add projection parameter for BucketHandle.Objects() #3549

Merged
merged 11 commits into from Jan 22, 2021
2 changes: 1 addition & 1 deletion storage/bucket.go
Expand Up @@ -1204,7 +1204,7 @@ func (it *ObjectIterator) Next() (*ObjectAttrs, error) {
func (it *ObjectIterator) fetch(pageSize int, pageToken string) (string, error) {
req := it.bucket.c.raw.Objects.List(it.bucket.name)
setClientHeader(req.Header())
req.Projection("full")
req.Projection(it.query.GetProjection())
req.Delimiter(it.query.Delimiter)
req.Prefix(it.query.Prefix)
req.StartOffset(it.query.StartOffset)
Expand Down
28 changes: 28 additions & 0 deletions storage/bucket_test.go
Expand Up @@ -663,3 +663,31 @@ func TestNewBucket(t *testing.T) {
t.Errorf("got=-, want=+:\n%s", diff)
}
}

func TestObjectsWithProjection(t *testing.T) {
ava12 marked this conversation as resolved.
Show resolved Hide resolved
samples := []struct {
req, expect string
} {
{"", ProjectionFull},
{ProjectionFull, ProjectionFull},
{ProjectionNoAcl, ProjectionNoAcl},
}
res := &http.Response{StatusCode: http.StatusInternalServerError}
transport := &mockTransport{}
client := mockClient(t, transport)
bucket := client.Bucket("mock-bucket")
ctx := context.Background()

for _, sample := range samples {
transport.addResult(res, nil)
query := &Query{Projection: sample.req}
bucket.Objects(ctx, query).Next()

projection := transport.gotReq.URL.Query()["projection"]
if len(projection) == 0 {
t.Errorf("requesting %q projection: no projection parameter generated", sample.req)
} else if projection[0] != sample.expect {
t.Errorf("requesting %q projection: expecting %q, got %q", sample.req, sample.expect, projection[0])
}
}
}
22 changes: 22 additions & 0 deletions storage/storage.go
Expand Up @@ -1306,6 +1306,14 @@ func encodeUint32(u uint32) string {
return base64.StdEncoding.EncodeToString(b)
}

const (
// ProjectionFull returns all fields of object(s).
ProjectionFull = "full"

// ProjectionNoAcl returns all fields of object(s) except for owner and acl.
ava12 marked this conversation as resolved.
Show resolved Hide resolved
ProjectionNoAcl = "noAcl"
ava12 marked this conversation as resolved.
Show resolved Hide resolved
)

// Query represents a query to filter objects from a bucket.
type Query struct {
// Delimiter returns results in a directory-like fashion.
Expand Down Expand Up @@ -1341,6 +1349,10 @@ type Query struct {
// lexicographically before endOffset. If startOffset is also set, the objects
// listed will have names between startOffset (inclusive) and endOffset (exclusive).
EndOffset string

// Projection defines set of properties to return. This may be either
ava12 marked this conversation as resolved.
Show resolved Hide resolved
// ProjectionFull or ProjectionNoAcl, zero value means ProjectionFull.
Projection string
ava12 marked this conversation as resolved.
Show resolved Hide resolved
}

// attrToFieldMap maps the field names of ObjectAttrs to the underlying field
Expand Down Expand Up @@ -1411,6 +1423,16 @@ func (q *Query) SetAttrSelection(attrs []string) error {
return nil
}

// GetProjection returns either value of Projection field or
// default projection when this field is empty.
func (q *Query) GetProjection() string {
ava12 marked this conversation as resolved.
Show resolved Hide resolved
if q.Projection == "" {
return ProjectionFull
} else {
return q.Projection
}
}

// Conditions constrain methods to act on specific generations of
// objects.
//
Expand Down