Skip to content

feat(rc): Add Remote Config Parameter Value Type support #591

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 2 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions src/main/java/com/google/firebase/remoteconfig/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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.ParameterResponse;
Expand All @@ -37,6 +38,7 @@ public final class Parameter {
private ParameterValue defaultValue;
private String description;
private Map<String, ParameterValue> conditionalValues;
private ParameterValueType valueType;

/**
* Creates a new {@link Parameter}.
Expand All @@ -59,6 +61,9 @@ public Parameter() {
this.defaultValue = (responseDefaultValue == null) ? null
: ParameterValue.fromParameterValueResponse(responseDefaultValue);
this.description = parameterResponse.getDescription();
if (!Strings.isNullOrEmpty(parameterResponse.getValueType())) {
this.valueType = ParameterValueType.valueOf(parameterResponse.getValueType());
}
}

/**
Expand Down Expand Up @@ -93,6 +98,16 @@ public Map<String, ParameterValue> getConditionalValues() {
return conditionalValues;
}

/**
* Gets the data type of the parameter value.
*
* @return The data type of the parameter value or null.
*/
@Nullable
public ParameterValueType getValueType() {
return valueType;
}

/**
* Sets the default value of the parameter.
* This is the value to set the parameter to, when none of the named conditions
Expand Down Expand Up @@ -133,6 +148,18 @@ public Parameter setConditionalValues(
return this;
}

/**
* Sets the data type of the parameter value.
* Defaults to `ParameterValueType.STRING` if unspecified.
*
* @param valueType The data type of the parameter value.
* @return This {@link Parameter}.
*/
public Parameter setValueType(@Nullable ParameterValueType valueType) {
this.valueType = valueType;
return this;
}

ParameterResponse toParameterResponse() {
Map<String, ParameterValueResponse> conditionalResponseValues = new HashMap<>();
for (Map.Entry<String, ParameterValue> entry : conditionalValues.entrySet()) {
Expand All @@ -143,7 +170,8 @@ ParameterResponse toParameterResponse() {
return new ParameterResponse()
.setDefaultValue(defaultValueResponse)
.setDescription(description)
.setConditionalValues(conditionalResponseValues);
.setConditionalValues(conditionalResponseValues)
.setValueType(this.valueType == null ? null : this.valueType.getValueType());
}

@Override
Expand All @@ -157,11 +185,12 @@ public boolean equals(Object o) {
Parameter parameter = (Parameter) o;
return Objects.equals(defaultValue, parameter.defaultValue)
&& Objects.equals(description, parameter.description)
&& Objects.equals(conditionalValues, parameter.conditionalValues);
&& Objects.equals(conditionalValues, parameter.conditionalValues)
&& Objects.equals(valueType, parameter.valueType);
}

@Override
public int hashCode() {
return Objects.hash(defaultValue, description, conditionalValues);
return Objects.hash(defaultValue, description, conditionalValues, valueType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2021 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;

/**
* Data types that are associated with parameter values.
*/
public enum ParameterValueType {
STRING("STRING"),
BOOLEAN("BOOLEAN"),
NUMBER("NUMBER"),
JSON("JSON");

private final String valueType;

ParameterValueType(String valueType) {
this.valueType = valueType;
}

public String getValueType() {
return valueType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ public static final class ParameterResponse {
@Key("conditionalValues")
private Map<String, ParameterValueResponse> conditionalValues;

@Key("valueType")
private String valueType;

public ParameterValueResponse getDefaultValue() {
return defaultValue;
}
Expand All @@ -119,6 +122,10 @@ public Map<String, ParameterValueResponse> getConditionalValues() {
return conditionalValues;
}

public String getValueType() {
return valueType;
}

public ParameterResponse setDefaultValue(
ParameterValueResponse defaultValue) {
this.defaultValue = defaultValue;
Expand All @@ -135,6 +142,11 @@ public ParameterResponse setConditionalValues(
this.conditionalValues = conditionalValues;
return this;
}

public ParameterResponse setValueType(String valueType) {
this.valueType = valueType;
return this;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ public class FirebaseRemoteConfigClientImplTest {
.setConditionalValues(ImmutableMap.<String, ParameterValue>of(
"ios_en", ParameterValue.of("welcome to app en")
))
.setDescription("text for welcome message!"),
.setDescription("text for welcome message!")
.setValueType(ParameterValueType.STRING),
"header_text", new Parameter()
.setDefaultValue(ParameterValue.inAppDefault())
.setValueType(ParameterValueType.STRING)
);

private static final Map<String, ParameterGroup> EXPECTED_PARAMETER_GROUPS = ImmutableMap.of(
Expand All @@ -97,6 +99,7 @@ public class FirebaseRemoteConfigClientImplTest {
"pumpkin_spice_season", new Parameter()
.setDefaultValue(ParameterValue.of("true"))
.setDescription("Whether it's currently pumpkin spice season.")
.setValueType(ParameterValueType.BOOLEAN)
)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,11 @@ private Map<String, Parameter> getParameters() {
"ios_en",
ParameterValue.of(String.format("welcome to app en %s", timestamp))
))
.setDescription("text for welcome message!"),
.setDescription("text for welcome message!")
.setValueType(ParameterValueType.STRING),
"header_text", new Parameter()
.setDefaultValue(ParameterValue.inAppDefault()));
.setDefaultValue(ParameterValue.inAppDefault())
.setValueType(ParameterValueType.STRING));
}

private Map<String, ParameterGroup> getParameterGroups() {
Expand All @@ -262,7 +264,8 @@ private Map<String, ParameterGroup> getParameterGroups() {
.setParameters(ImmutableMap.of(
"pumpkin_spice_season", new Parameter()
.setDefaultValue(ParameterValue.of("true"))
.setDescription("Whether it's currently pumpkin spice season."))
.setDescription("Whether it's currently pumpkin spice season.")
.setValueType(ParameterValueType.STRING))
));
}

Expand Down
16 changes: 16 additions & 0 deletions src/test/java/com/google/firebase/remoteconfig/ParameterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void testConstructor() {
assertTrue(parameter.getConditionalValues().isEmpty());
assertNull(parameter.getDefaultValue());
assertNull(parameter.getDescription());
assertNull(parameter.getValueType());
}

@Test(expected = NullPointerException.class)
Expand Down Expand Up @@ -84,8 +85,23 @@ public void testEquality() {
.setConditionalValues(conditionalValues);

assertEquals(parameterFive, parameterSix);

final Parameter parameterSeven = new Parameter()
.setDefaultValue(ParameterValue.inAppDefault())
.setDescription("greeting text")
.setConditionalValues(conditionalValues)
.setValueType(ParameterValueType.STRING);
final Parameter parameterEight = new Parameter()
.setDefaultValue(ParameterValue.inAppDefault())
.setDescription("greeting text")
.setConditionalValues(conditionalValues)
.setValueType(ParameterValueType.STRING);

assertEquals(parameterSeven, parameterEight);
assertNotEquals(parameterOne, parameterThree);
assertNotEquals(parameterOne, parameterFive);
assertNotEquals(parameterOne, parameterSeven);
assertNotEquals(parameterThree, parameterFive);
assertNotEquals(parameterThree, parameterSeven);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be also test for Equals() with parameter value types?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 100 has an assertEquals() for parameter value types. Would that work?

}
}
11 changes: 7 additions & 4 deletions src/test/java/com/google/firebase/remoteconfig/TemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public class TemplateTest {
"greeting_header", new Parameter()
.setDefaultValue(ParameterValue.inAppDefault())
.setDescription("greeting header text")
.setConditionalValues(CONDITIONAL_VALUES),
.setConditionalValues(CONDITIONAL_VALUES)
.setValueType(ParameterValueType.STRING),
"greeting_text", new Parameter()
.setDefaultValue(ParameterValue.inAppDefault())
.setDescription("greeting text")
Expand Down Expand Up @@ -274,15 +275,17 @@ public void testToJSONWithEmptyTemplate() {
public void testToJSONWithParameterValues() {
Template t = new Template();
t.getParameters()
.put("with_value", new Parameter().setDefaultValue(ParameterValue.of("hello")));
.put("with_value", new Parameter().setDefaultValue(ParameterValue.of("hello"))
.setValueType(ParameterValueType.NUMBER));
t.getParameters()
.put("with_inApp", new Parameter().setDefaultValue(ParameterValue.inAppDefault()));
String jsonString = t.toJSON();

assertEquals("{\"conditions\":[],\"parameterGroups\":{},"
+ "\"parameters\":{\"with_value\":{\"conditionalValues\":{},"
+ "\"defaultValue\":{\"value\":\"hello\"}},\"with_inApp\":{\"conditionalValues\":{},"
+ "\"defaultValue\":{\"useInAppDefault\":true}}}}", jsonString);
+ "\"defaultValue\":{\"value\":\"hello\"},\"valueType\":\"NUMBER\"},\"with_inApp\":{"
+ "\"conditionalValues\":{},\"defaultValue\":{\"useInAppDefault\":true}"
+ "}}}", jsonString);
}

@Test
Expand Down
9 changes: 6 additions & 3 deletions src/test/resources/getRemoteConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
"value": "welcome to app en"
}
},
"description": "text for welcome message!"
"description": "text for welcome message!",
"valueType": "STRING"
},
"header_text": {
"defaultValue": {
"useInAppDefault": true
}
},
"valueType": "STRING"
}
},
"parameterGroups": {
Expand All @@ -36,7 +38,8 @@
"defaultValue": {
"value": "true"
},
"description": "Whether it's currently pumpkin spice season."
"description": "Whether it's currently pumpkin spice season.",
"valueType": "BOOLEAN"
}
}
}
Expand Down