Skip to content

Commit a19bca6

Browse files
Tulsishahtritone
andauthored
feat(storage): add ComponentCount as part of ObjectAttrs (#7230)
* adding component count attribute in objectAttrs and related unit test and integration test * formating the file * formating the file * fixing integration test for compose method * fixing comments - rephrasing component count descriotion, adding got and want in integration test * removing extra line * fixing presubmit issues * fixing comments- ComponentCount to component_count Co-authored-by: Chris Cotter <cjcotter@google.com>
1 parent 27b3398 commit a19bca6

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

storage/integration_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,18 +1659,26 @@ func TestIntegration_Compose(t *testing.T) {
16591659
// Compose should work even if the user sets no destination attributes.
16601660
compDst := b.Object("composed1")
16611661
c := compDst.ComposerFrom(compSrcs...)
1662-
if _, err := c.Run(ctx); err != nil {
1662+
attrs, err := c.Run(ctx)
1663+
if err != nil {
16631664
t.Fatalf("ComposeFrom error: %v", err)
16641665
}
1666+
if attrs.ComponentCount != int64(len(objects)) {
1667+
t.Errorf("mismatching ComponentCount: got %v, want %v", attrs.ComponentCount, int64(len(objects)))
1668+
}
16651669
checkCompose(compDst, "application/octet-stream")
16661670

16671671
// It should also work if we do.
16681672
compDst = b.Object("composed2")
16691673
c = compDst.ComposerFrom(compSrcs...)
16701674
c.ContentType = "text/json"
1671-
if _, err := c.Run(ctx); err != nil {
1675+
attrs, err = c.Run(ctx)
1676+
if err != nil {
16721677
t.Fatalf("ComposeFrom error: %v", err)
16731678
}
1679+
if attrs.ComponentCount != int64(len(objects)) {
1680+
t.Errorf("mismatching ComponentCount: got %v, want %v", attrs.ComponentCount, int64(len(objects)))
1681+
}
16741682
checkCompose(compDst, "text/json")
16751683
})
16761684
}

storage/storage.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,11 @@ type ObjectAttrs struct {
13151315
// later value but not to an earlier one. For more information see
13161316
// https://cloud.google.com/storage/docs/metadata#custom-time .
13171317
CustomTime time.Time
1318+
1319+
// ComponentCount is the number of objects contained within a composite object.
1320+
// For non-composite objects, the value will be zero.
1321+
// This field is read-only.
1322+
ComponentCount int64
13181323
}
13191324

13201325
// convertTime converts a time in RFC3339 format to time.Time.
@@ -1385,6 +1390,7 @@ func newObject(o *raw.Object) *ObjectAttrs {
13851390
Updated: convertTime(o.Updated),
13861391
Etag: o.Etag,
13871392
CustomTime: convertTime(o.CustomTime),
1393+
ComponentCount: o.ComponentCount,
13881394
}
13891395
}
13901396

@@ -1419,6 +1425,7 @@ func newObjectFromProto(o *storagepb.Object) *ObjectAttrs {
14191425
Deleted: convertProtoTime(o.GetDeleteTime()),
14201426
Updated: convertProtoTime(o.GetUpdateTime()),
14211427
CustomTime: convertProtoTime(o.GetCustomTime()),
1428+
ComponentCount: int64(o.ComponentCount),
14221429
}
14231430
}
14241431

@@ -1547,6 +1554,7 @@ var attrToFieldMap = map[string]string{
15471554
"Updated": "updated",
15481555
"Etag": "etag",
15491556
"CustomTime": "customTime",
1557+
"ComponentCount": "componentCount",
15501558
}
15511559

15521560
// attrToProtoFieldMap maps the field names of ObjectAttrs to the underlying field
@@ -1578,6 +1586,7 @@ var attrToProtoFieldMap = map[string]string{
15781586
"Owner": "owner",
15791587
"CustomerKeySHA256": "customer_encryption",
15801588
"CustomTime": "custom_time",
1589+
"ComponentCount": "component_count",
15811590
// MediaLink was explicitly excluded from the proto as it is an HTTP-ism.
15821591
// "MediaLink": "mediaLink",
15831592
}

storage/storage_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,7 @@ func TestRawObjectToObjectAttrs(t *testing.T) {
17471747
TimeCreated: "2019-03-31T19:32:10Z",
17481748
TimeDeleted: "2019-03-31T19:33:39Z",
17491749
TemporaryHold: true,
1750+
ComponentCount: 2,
17501751
},
17511752
want: &ObjectAttrs{
17521753
Bucket: "Test",
@@ -1763,6 +1764,7 @@ func TestRawObjectToObjectAttrs(t *testing.T) {
17631764
RetentionExpirationTime: time.Date(2019, 3, 31, 19, 33, 36, 0, time.UTC),
17641765
Size: 1 << 20,
17651766
TemporaryHold: true,
1767+
ComponentCount: 2,
17661768
},
17671769
},
17681770
}
@@ -1833,6 +1835,7 @@ func TestProtoObjectToObjectAttrs(t *testing.T) {
18331835
CreateTime: timestamppb.New(now),
18341836
DeleteTime: timestamppb.New(now),
18351837
TemporaryHold: true,
1838+
ComponentCount: 2,
18361839
},
18371840
want: &ObjectAttrs{
18381841
Bucket: "Test",
@@ -1848,6 +1851,7 @@ func TestProtoObjectToObjectAttrs(t *testing.T) {
18481851
RetentionExpirationTime: now,
18491852
Size: 1 << 20,
18501853
TemporaryHold: true,
1854+
ComponentCount: 2,
18511855
},
18521856
},
18531857
}

0 commit comments

Comments
 (0)