-
Notifications
You must be signed in to change notification settings - Fork 289
Add Remote Config conditions to template #489
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 all 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
171 changes: 171 additions & 0 deletions
171
src/main/java/com/google/firebase/remoteconfig/Condition.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,171 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* 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.firebase.remoteconfig; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.common.base.Strings; | ||
import com.google.firebase.internal.NonNull; | ||
import com.google.firebase.internal.Nullable; | ||
import com.google.firebase.remoteconfig.internal.TemplateResponse.ConditionResponse; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Represents a Remote Config condition that can be included in a {@link Template}. | ||
* A condition targets a specific group of users. A list of these conditions make up | ||
* part of a Remote Config template. | ||
*/ | ||
public final class Condition { | ||
|
||
private String name; | ||
private String expression; | ||
private TagColor tagColor; | ||
|
||
/** | ||
* Creates a new {@link Condition}. | ||
* | ||
* @param name A non-null, non-empty, and unique name of this condition. | ||
* @param expression A non-null and non-empty expression of this condition. | ||
*/ | ||
public Condition(@NonNull String name, @NonNull String expression) { | ||
this(name, expression, null); | ||
} | ||
|
||
/** | ||
* Creates a new {@link Condition}. | ||
* | ||
* @param name A non-null, non-empty, and unique name of this condition. | ||
* @param expression A non-null and non-empty expression of this condition. | ||
* @param tagColor A color associated with this condition for display purposes in the | ||
* Firebase Console. Not specifying this value results in the console picking an | ||
* arbitrary color to associate with the condition. | ||
*/ | ||
public Condition(@NonNull String name, @NonNull String expression, @Nullable TagColor tagColor) { | ||
checkArgument(!Strings.isNullOrEmpty(name), "condition name must not be null or empty"); | ||
checkArgument(!Strings.isNullOrEmpty(expression), | ||
"condition expression must not be null or empty"); | ||
this.name = name; | ||
this.expression = expression; | ||
this.tagColor = tagColor; | ||
} | ||
|
||
Condition(@NonNull ConditionResponse conditionResponse) { | ||
checkNotNull(conditionResponse); | ||
this.name = conditionResponse.getName(); | ||
this.expression = conditionResponse.getExpression(); | ||
if (conditionResponse.getTagColor() == null) { | ||
this.tagColor = TagColor.UNSPECIFIED; | ||
} else { | ||
this.tagColor = TagColor.valueOf(conditionResponse.getTagColor()); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the name of the condition. | ||
* | ||
* @return The name of the condition. | ||
*/ | ||
@NonNull | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Gets the expression of the condition. | ||
* | ||
* @return The expression of the condition. | ||
*/ | ||
@NonNull | ||
public String getExpression() { | ||
return expression; | ||
} | ||
|
||
/** | ||
* Gets the tag color of the condition used for display purposes in the Firebase Console. | ||
* | ||
* @return The tag color of the condition. | ||
*/ | ||
@NonNull | ||
public TagColor getTagColor() { | ||
return tagColor; | ||
} | ||
|
||
/** | ||
* Sets the name of the condition. | ||
* | ||
* @param name A non-empty and unique name of this condition. | ||
* @return This {@link Condition}. | ||
*/ | ||
public Condition setName(@NonNull String name) { | ||
checkArgument(!Strings.isNullOrEmpty(name), "condition name must not be null or empty"); | ||
this.name = name; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the expression of the condition. | ||
* | ||
* <p>See <a href="https://firebase.google.com/docs/remote-config/condition-reference"> | ||
* condition expressions</a> for the expected syntax of this field. | ||
* | ||
* @param expression The logic of this condition. | ||
* @return This {@link Condition}. | ||
*/ | ||
public Condition setExpression(@NonNull String expression) { | ||
checkArgument(!Strings.isNullOrEmpty(expression), | ||
"condition expression must not be null or empty"); | ||
this.expression = expression; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the tag color of the condition. | ||
* | ||
* <p>The color associated with this condition for display purposes in the Firebase Console. | ||
* Not specifying this value results in the console picking an arbitrary color to associate | ||
* with the condition. | ||
* | ||
* @param tagColor The tag color of this condition. | ||
* @return This {@link Condition}. | ||
*/ | ||
public Condition setTagColor(@Nullable TagColor tagColor) { | ||
this.tagColor = tagColor; | ||
return this; | ||
} | ||
|
||
ConditionResponse toConditionResponse() { | ||
return new ConditionResponse() | ||
.setName(this.name) | ||
.setExpression(this.expression) | ||
.setTagColor(this.tagColor == null ? null : this.tagColor.getColor()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
Condition condition = (Condition) o; | ||
return Objects.equals(name, condition.name) | ||
&& Objects.equals(expression, condition.expression) && tagColor == condition.tagColor; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/google/firebase/remoteconfig/TagColor.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,45 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* 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.firebase.remoteconfig; | ||
hiranya911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Colors that are associated with conditions for display purposes in the Firebase Console. | ||
*/ | ||
public enum TagColor { | ||
BLUE("BLUE"), | ||
BROWN("BROWN"), | ||
CYAN("CYAN"), | ||
DEEP_ORANGE("DEEP_ORANGE"), | ||
GREEN("GREEN"), | ||
INDIGO("INDIGO"), | ||
LIME("LIME"), | ||
ORANGE("ORANGE"), | ||
PINK("PINK"), | ||
PURPLE("PURPLE"), | ||
TEAL("TEAL"), | ||
UNSPECIFIED("CONDITION_DISPLAY_COLOR_UNSPECIFIED"); | ||
hiranya911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private final String color; | ||
|
||
TagColor(String color) { | ||
hiranya911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.color = color; | ||
} | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need a constructor that accepts the color as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a constructor with color. Should we amend the API proposal as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to include a note. Looks like an oversight if it's not already there.