Skip to content

Commit

Permalink
Expose object metadata from Bucket.list (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasfj authored Nov 1, 2024
1 parent b6f6c24 commit c04a70a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
matrix:
# Add macos-latest and/or windows-latest if relevant for this package.
os: [ubuntu-latest]
sdk: [2.19.0, dev]
sdk: [3.0.0, dev]
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
Expand Down
11 changes: 11 additions & 0 deletions pkgs/gcloud/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
## 0.8.16-wip

- **Breaking** `BucketEntry` is now `sealed` anyone implementing or subclassing
will experience breakage.
- Feature `BucketEntry` objects returns from `Bucket.list` are now instances
of:
* `BucketDirectoryEntry`, or,
* `BucketObjectEntry`, which implements `ObjectInfo` exposing metadata.

This means that anyone using `Bucket.list` to find objects, does not need
to use `Bucket.info` to fetch metadata for an object.
- Minimum Dart SDK constraint bumped to `^3.0.0`.

## 0.8.15

- Update the pubspec repository field to reflect the repo move.
Expand Down
14 changes: 12 additions & 2 deletions pkgs/gcloud/lib/src/storage_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ class _ObjectPageImpl implements Page<BucketEntry> {
storage_api.Objects response)
: items = [
for (final item in response.prefixes ?? const <String>[])
BucketEntry._directory(item),
BucketDirectoryEntry._(item),
for (final item in response.items ?? const <storage_api.Object>[])
BucketEntry._object(item.name!)
_BucketObjectEntry(item)
],
_nextPageToken = response.nextPageToken;

Expand Down Expand Up @@ -430,6 +430,16 @@ class _ObjectInfoImpl implements ObjectInfo {
ObjectMetadata get metadata => _metadata;
}

class _BucketObjectEntry extends _ObjectInfoImpl implements BucketObjectEntry {
_BucketObjectEntry(storage_api.Object object) : super(object);

@override
bool get isDirectory => false;

@override
bool get isObject => true;
}

class _ObjectMetadata implements ObjectMetadata {
final storage_api.Object _object;
Acl? _cachedAcl;
Expand Down
29 changes: 22 additions & 7 deletions pkgs/gcloud/lib/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -668,23 +668,38 @@ abstract class ObjectMetadata {
/// Listing operate like a directory listing, despite the object
/// namespace being flat.
///
/// Instances of [BucketEntry] are either instances of [BucketDirectoryEntry]
/// or [BucketObjectEntry]. Casting to [BucketObjectEntry] will give access to
/// object metadata.
///
/// See [Bucket.list] for information on how the hierarchical structure
/// is determined.
class BucketEntry {
sealed class BucketEntry {
/// Whether this is information on an object.
final bool isObject;
bool get isObject;

/// Name of object or directory.
final String name;

BucketEntry._object(this.name) : isObject = true;

BucketEntry._directory(this.name) : isObject = false;
String get name;

/// Whether this is a prefix.
bool get isDirectory => !isObject;
}

/// Entry in [Bucket.list] representing a directory given the `delimiter`
/// passed.
class BucketDirectoryEntry extends BucketEntry {
@override
final String name;

BucketDirectoryEntry._(this.name);

@override
bool get isObject => false;
}

/// Entry in [Bucket.list] representing an object.
abstract class BucketObjectEntry implements BucketEntry, ObjectInfo {}

/// Access to operations on a specific cloud storage bucket.
abstract class Bucket {
/// Name of this bucket.
Expand Down
2 changes: 1 addition & 1 deletion pkgs/gcloud/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ topics:
- gcp

environment:
sdk: '>=2.19.0 <4.0.0'
sdk: '^3.0.0'

dependencies:
_discoveryapis_commons: ^1.0.0
Expand Down

0 comments on commit c04a70a

Please sign in to comment.