Skip to content

Commit

Permalink
Add prefixes field to field selector when listing blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Apr 15, 2016
1 parent 13ab36f commit 8c9a352
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
37 changes: 27 additions & 10 deletions gcloud-java-core/src/main/java/com/google/cloud/FieldSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,51 @@ private static String selector(List<? extends FieldSelector> required, FieldSele
}

/**
* Returns a composite selector given a number of fields. The string selector returned by this
* method can be used for field selection in API calls that return a single resource. This
* method is not supposed to be used directly by users.
* Returns a composite selector given a number of resource fields. The string selector returned
* by this method can be used for field selection in API calls that return a single resource.
* This method is not supposed to be used directly by users.
*/
public static String selector(List<? extends FieldSelector> required, FieldSelector... others) {
return selector(required, others, new String[]{});
}

/**
* Returns a composite selector given a number of fields and a container name. The string
* selector returned by this method can be used for field selection in API calls that return a
* list of resources. This method is not supposed to be used directly by users.
* Returns a composite selector given a number of resource fields and a container name. The
* string selector returned by this method can be used for field selection in API calls that
* return a list of resources. This method is not supposed to be used directly by users.
*/
public static String listSelector(String containerName, List<? extends FieldSelector> required,
FieldSelector... others) {
return "nextPageToken," + containerName + '(' + selector(required, others) + ')';
}

/**
* Returns a composite selector given a number of fields and a container name. This methods also
* takes an {@code extraResourceFields} parameter to specify some extra fields as strings. The
* string selector returned by this method can be used for field selection in API calls that
* return a list of resources. This method is not supposed to be used directly by users.
* Returns a composite selector given a number of resource fields and a container name. This
* method also takes an {@code extraResourceFields} parameter to specify some extra resource
* fields as strings. The string selector returned by this method can be used for field
* selection in API calls that return a list of resources. This method is not supposed to be
* used directly by users.
*/
public static String listSelector(String containerName, List<? extends FieldSelector> required,
FieldSelector[] others, String... extraResourceFields) {
return "nextPageToken," + containerName + '('
+ selector(required, others, extraResourceFields) + ')';
}

/**
* Returns a composite selector given a number of first level fields as strings, a number of
* resource fields and a container name. This method also takes an {@code extraResourceFields}
* parameter to specify some extra resource fields as strings. The string selector returned by
* this method can be used for field selection in API calls that return a list of resources.
* This method is not supposed to be used directly by users.
*/
public static String listSelector(String[] firstLevelFields, String containerName,
List<? extends FieldSelector> required, FieldSelector[] others,
String... extraResourceFields) {
Set<String> firstLevelStrings = Sets.newHashSetWithExpectedSize(firstLevelFields.length + 1);
firstLevelStrings.addAll(Lists.asList("nextPageToken", firstLevelFields));
return Joiner.on(',').join(firstLevelStrings) + "," + containerName + '('
+ selector(required, others, extraResourceFields) + ')';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public String selector() {
return "field3";
}
};
private static final String[] FIRST_LEVEL_FIELDS = {"firstLevel1", "firstLevel2"};
private static final List<FieldSelector> REQUIRED_FIELDS = ImmutableList.of(FIELD1, FIELD2);
private static final String CONTAINER = "container";

Expand Down Expand Up @@ -81,4 +82,20 @@ public void testListSelectorWithExtraFields() {
assertTrue(selector.endsWith(")"));
assertEquals(52, selector.length());
}

@Test
public void testListSelectorWithFirstLevelFields() {
String selector = Helper.listSelector(FIRST_LEVEL_FIELDS, CONTAINER, REQUIRED_FIELDS,
new FieldSelector[]{FIELD3}, "field4");
assertTrue(selector.contains("firstLevel1"));
assertTrue(selector.contains("firstLevel2"));
assertTrue(selector.contains("nextPageToken"));
assertTrue(selector.contains("container("));
assertTrue(selector.contains("field1"));
assertTrue(selector.contains("field2"));
assertTrue(selector.contains("field3"));
assertTrue(selector.contains("field4"));
assertTrue(selector.endsWith(")"));
assertEquals(76, selector.length());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ public static BucketListOption fields(BucketField... fields) {
*/
class BlobListOption extends Option {

private static final String[] FIRST_LEVEL_FIELDS = {"prefixes"};
private static final long serialVersionUID = 9083383524788661294L;

private BlobListOption(StorageRpc.Option option, Object value) {
Expand Down Expand Up @@ -713,7 +714,7 @@ public static BlobListOption versions(boolean versions) {
*/
public static BlobListOption fields(BlobField... fields) {
return new BlobListOption(StorageRpc.Option.FIELDS,
Helper.listSelector("items", BlobField.REQUIRED_FIELDS, fields));
Helper.listSelector(FIRST_LEVEL_FIELDS, "items", BlobField.REQUIRED_FIELDS, fields));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,13 @@ public void testListBucketsWithSelectedFields() {
initializeService();
ImmutableList<Bucket> bucketList = ImmutableList.of(expectedBucket1, expectedBucket2);
Page<Bucket> page = storage.list(BUCKET_LIST_FIELDS);
String selector = (String) capturedOptions.getValue().get(BLOB_LIST_FIELDS.rpcOption());
assertTrue(selector.contains("items"));
String selector = (String) capturedOptions.getValue().get(BUCKET_LIST_FIELDS.rpcOption());
assertTrue(selector.contains("items("));
assertTrue(selector.contains("name"));
assertTrue(selector.contains("acl"));
assertTrue(selector.contains("location"));
assertTrue(selector.contains("nextPageToken"));
assertTrue(selector.endsWith(")"));
assertEquals(38, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(bucketList.toArray(), Iterables.toArray(page.values(), Bucket.class));
Expand All @@ -607,10 +608,11 @@ public void testListBucketsWithEmptyFields() {
initializeService();
ImmutableList<Bucket> bucketList = ImmutableList.of(expectedBucket1, expectedBucket2);
Page<Bucket> page = storage.list(BUCKET_LIST_EMPTY_FIELDS);
String selector = (String) capturedOptions.getValue().get(BLOB_LIST_FIELDS.rpcOption());
assertTrue(selector.contains("items"));
String selector = (String) capturedOptions.getValue().get(BUCKET_LIST_EMPTY_FIELDS.rpcOption());
assertTrue(selector.contains("items("));
assertTrue(selector.contains("name"));
assertTrue(selector.contains("nextPageToken"));
assertTrue(selector.endsWith(")"));
assertEquals(25, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(bucketList.toArray(), Iterables.toArray(page.values(), Bucket.class));
Expand Down Expand Up @@ -679,13 +681,15 @@ public void testListBlobsWithSelectedFields() {
assertEquals(BLOB_LIST_PREFIX.value(),
capturedOptions.getValue().get(BLOB_LIST_PREFIX.rpcOption()));
String selector = (String) capturedOptions.getValue().get(BLOB_LIST_FIELDS.rpcOption());
assertTrue(selector.contains("items"));
assertTrue(selector.contains("prefixes"));
assertTrue(selector.contains("items("));
assertTrue(selector.contains("bucket"));
assertTrue(selector.contains("name"));
assertTrue(selector.contains("contentType"));
assertTrue(selector.contains("md5Hash"));
assertTrue(selector.contains("nextPageToken"));
assertEquals(52, selector.length());
assertTrue(selector.endsWith(")"));
assertEquals(61, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(blobList.toArray(), Iterables.toArray(page.values(), Blob.class));
}
Expand All @@ -710,11 +714,13 @@ public void testListBlobsWithEmptyFields() {
assertEquals(BLOB_LIST_PREFIX.value(),
capturedOptions.getValue().get(BLOB_LIST_PREFIX.rpcOption()));
String selector = (String) capturedOptions.getValue().get(BLOB_LIST_EMPTY_FIELDS.rpcOption());
assertTrue(selector.contains("items"));
assertTrue(selector.contains("prefixes"));
assertTrue(selector.contains("items("));
assertTrue(selector.contains("bucket"));
assertTrue(selector.contains("name"));
assertTrue(selector.contains("nextPageToken"));
assertEquals(32, selector.length());
assertTrue(selector.endsWith(")"));
assertEquals(41, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(blobList.toArray(), Iterables.toArray(page.values(), Blob.class));
}
Expand Down

0 comments on commit 8c9a352

Please sign in to comment.