-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add common interface for field selectors #807
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
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.gcloud; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.base.Joiner; | ||
import com.google.common.collect.Lists; | ||
import com.google.common.collect.Sets; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* Interface for Google Cloud resource's fields. Implementations of this interface can be used to | ||
* select only desired fields from a returned Google Cloud resource. | ||
*/ | ||
public interface FieldSelector { | ||
|
||
/** | ||
* Returns a string selector. This selector is passed to a Google Cloud service (possibly with | ||
* other field selectors) to specify which resource fields should be returned by an API call. | ||
*/ | ||
String selector(); | ||
|
||
/** | ||
* A helper class used to build composite selectors given a number of fields. This class is not | ||
* supposed to be used directly by users. | ||
*/ | ||
class SelectorHelper { | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
private SelectorHelper() {} | ||
|
||
private static final Function<FieldSelector, String> FIELD_TO_STRING_FUNCTION = | ||
new Function<FieldSelector, String>() { | ||
@Override | ||
public String apply(FieldSelector fieldSelector) { | ||
return fieldSelector.selector(); | ||
} | ||
}; | ||
|
||
private static String selector(List<FieldSelector> required, FieldSelector[] others, | ||
String... extraResourceFields) { | ||
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); | ||
fieldStrings.addAll(Lists.transform(required, FIELD_TO_STRING_FUNCTION)); | ||
fieldStrings.addAll(Lists.transform(Arrays.asList(others), FIELD_TO_STRING_FUNCTION)); | ||
fieldStrings.addAll(Arrays.asList(extraResourceFields)); | ||
return Joiner.on(',').join(fieldStrings); | ||
} | ||
|
||
/** | ||
* 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. | ||
*/ | ||
public static String selector(List<FieldSelector> required, FieldSelector... others) { | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
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. | ||
*/ | ||
public static String selector(String containerName, List<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. | ||
*/ | ||
public static String selector(String containerName, List<FieldSelector> required, | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
FieldSelector[] others, String... extraResourceFields) { | ||
return "nextPageToken," + containerName + '(' | ||
+ selector(required, others, extraResourceFields) + ')'; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.