Skip to content

Commit 63d2de2

Browse files
committed
feat(rc): Add Remote Config Parameter Value Type support
1 parent 9f57c9a commit 63d2de2

File tree

7 files changed

+115
-11
lines changed

7 files changed

+115
-11
lines changed

src/main/java/com/google/firebase/remoteconfig/Parameter.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

21+
import com.google.common.base.Strings;
2122
import com.google.firebase.internal.NonNull;
2223
import com.google.firebase.internal.Nullable;
2324
import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterResponse;
@@ -37,6 +38,7 @@ public final class Parameter {
3738
private ParameterValue defaultValue;
3839
private String description;
3940
private Map<String, ParameterValue> conditionalValues;
41+
private ParameterValueType valueType;
4042

4143
/**
4244
* Creates a new {@link Parameter}.
@@ -59,6 +61,9 @@ public Parameter() {
5961
this.defaultValue = (responseDefaultValue == null) ? null
6062
: ParameterValue.fromParameterValueResponse(responseDefaultValue);
6163
this.description = parameterResponse.getDescription();
64+
if (!Strings.isNullOrEmpty(parameterResponse.getValueType())) {
65+
this.valueType = ParameterValueType.valueOf(parameterResponse.getValueType());
66+
}
6267
}
6368

6469
/**
@@ -93,6 +98,16 @@ public Map<String, ParameterValue> getConditionalValues() {
9398
return conditionalValues;
9499
}
95100

101+
/**
102+
* Gets the data type of the parameter value.
103+
*
104+
* @return The data type of the parameter value or null.
105+
*/
106+
@Nullable
107+
public ParameterValueType getValueType() {
108+
return valueType;
109+
}
110+
96111
/**
97112
* Sets the default value of the parameter.
98113
* This is the value to set the parameter to, when none of the named conditions
@@ -133,6 +148,18 @@ public Parameter setConditionalValues(
133148
return this;
134149
}
135150

151+
/**
152+
* Sets the data type of the parameter value.
153+
* Defaults to `ParameterValueType.STRING` if unspecified.
154+
*
155+
* @param valueType The data type of the parameter value.
156+
* @return This {@link Parameter}.
157+
*/
158+
public Parameter setValueType(@Nullable ParameterValueType valueType) {
159+
this.valueType = valueType;
160+
return this;
161+
}
162+
136163
ParameterResponse toParameterResponse() {
137164
Map<String, ParameterValueResponse> conditionalResponseValues = new HashMap<>();
138165
for (Map.Entry<String, ParameterValue> entry : conditionalValues.entrySet()) {
@@ -143,7 +170,8 @@ ParameterResponse toParameterResponse() {
143170
return new ParameterResponse()
144171
.setDefaultValue(defaultValueResponse)
145172
.setDescription(description)
146-
.setConditionalValues(conditionalResponseValues);
173+
.setConditionalValues(conditionalResponseValues)
174+
.setValueType(this.valueType == null ? null : this.valueType.getValueType());
147175
}
148176

149177
@Override
@@ -157,11 +185,12 @@ public boolean equals(Object o) {
157185
Parameter parameter = (Parameter) o;
158186
return Objects.equals(defaultValue, parameter.defaultValue)
159187
&& Objects.equals(description, parameter.description)
160-
&& Objects.equals(conditionalValues, parameter.conditionalValues);
188+
&& Objects.equals(conditionalValues, parameter.conditionalValues)
189+
&& Objects.equals(valueType, parameter.valueType);
161190
}
162191

163192
@Override
164193
public int hashCode() {
165-
return Objects.hash(defaultValue, description, conditionalValues);
194+
return Objects.hash(defaultValue, description, conditionalValues, valueType);
166195
}
167196
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.remoteconfig;
18+
19+
/**
20+
* Data types that are associated with parameter values.
21+
*/
22+
public enum ParameterValueType {
23+
STRING("STRING"),
24+
BOOLEAN("BOOLEAN"),
25+
NUMBER("NUMBER"),
26+
JSON("JSON");
27+
28+
private final String valueType;
29+
30+
ParameterValueType(String valueType) {
31+
this.valueType = valueType;
32+
}
33+
34+
public String getValueType() {
35+
return valueType;
36+
}
37+
}

src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ public static final class ParameterResponse {
107107
@Key("conditionalValues")
108108
private Map<String, ParameterValueResponse> conditionalValues;
109109

110+
@Key("valueType")
111+
private String valueType;
112+
110113
public ParameterValueResponse getDefaultValue() {
111114
return defaultValue;
112115
}
@@ -119,6 +122,10 @@ public Map<String, ParameterValueResponse> getConditionalValues() {
119122
return conditionalValues;
120123
}
121124

125+
public String getValueType() {
126+
return valueType;
127+
}
128+
122129
public ParameterResponse setDefaultValue(
123130
ParameterValueResponse defaultValue) {
124131
this.defaultValue = defaultValue;
@@ -135,6 +142,11 @@ public ParameterResponse setConditionalValues(
135142
this.conditionalValues = conditionalValues;
136143
return this;
137144
}
145+
146+
public ParameterResponse setValueType(String valueType) {
147+
this.valueType = valueType;
148+
return this;
149+
}
138150
}
139151

140152
/**

src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ public class FirebaseRemoteConfigClientImplTest {
8585
.setConditionalValues(ImmutableMap.<String, ParameterValue>of(
8686
"ios_en", ParameterValue.of("welcome to app en")
8787
))
88-
.setDescription("text for welcome message!"),
88+
.setDescription("text for welcome message!")
89+
.setValueType(ParameterValueType.STRING),
8990
"header_text", new Parameter()
9091
.setDefaultValue(ParameterValue.inAppDefault())
92+
.setValueType(ParameterValueType.STRING)
9193
);
9294

9395
private static final Map<String, ParameterGroup> EXPECTED_PARAMETER_GROUPS = ImmutableMap.of(
@@ -97,6 +99,7 @@ public class FirebaseRemoteConfigClientImplTest {
9799
"pumpkin_spice_season", new Parameter()
98100
.setDefaultValue(ParameterValue.of("true"))
99101
.setDescription("Whether it's currently pumpkin spice season.")
102+
.setValueType(ParameterValueType.BOOLEAN)
100103
)
101104
)
102105
);

src/test/java/com/google/firebase/remoteconfig/ParameterTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public void testConstructor() {
3838
assertTrue(parameter.getConditionalValues().isEmpty());
3939
assertNull(parameter.getDefaultValue());
4040
assertNull(parameter.getDescription());
41+
assertNull(parameter.getValueType());
4142
}
4243

4344
@Test(expected = NullPointerException.class)
@@ -84,8 +85,23 @@ public void testEquality() {
8485
.setConditionalValues(conditionalValues);
8586

8687
assertEquals(parameterFive, parameterSix);
88+
89+
final Parameter parameterSeven = new Parameter()
90+
.setDefaultValue(ParameterValue.inAppDefault())
91+
.setDescription("greeting text")
92+
.setConditionalValues(conditionalValues)
93+
.setValueType(ParameterValueType.STRING);
94+
final Parameter parameterEight = new Parameter()
95+
.setDefaultValue(ParameterValue.inAppDefault())
96+
.setDescription("greeting text")
97+
.setConditionalValues(conditionalValues)
98+
.setValueType(ParameterValueType.STRING);
99+
100+
assertEquals(parameterSeven, parameterEight);
87101
assertNotEquals(parameterOne, parameterThree);
88102
assertNotEquals(parameterOne, parameterFive);
103+
assertNotEquals(parameterOne, parameterSeven);
89104
assertNotEquals(parameterThree, parameterFive);
105+
assertNotEquals(parameterThree, parameterSeven);
90106
}
91107
}

src/test/java/com/google/firebase/remoteconfig/TemplateTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ public class TemplateTest {
4949
"greeting_header", new Parameter()
5050
.setDefaultValue(ParameterValue.inAppDefault())
5151
.setDescription("greeting header text")
52-
.setConditionalValues(CONDITIONAL_VALUES),
52+
.setConditionalValues(CONDITIONAL_VALUES)
53+
.setValueType(ParameterValueType.STRING),
5354
"greeting_text", new Parameter()
5455
.setDefaultValue(ParameterValue.inAppDefault())
5556
.setDescription("greeting text")
5657
.setConditionalValues(CONDITIONAL_VALUES)
58+
.setValueType(ParameterValueType.STRING)
5759
);
5860

5961
private static final Map<String, ParameterGroup> PARAMETER_GROUPS = ImmutableMap.of(
@@ -274,15 +276,17 @@ public void testToJSONWithEmptyTemplate() {
274276
public void testToJSONWithParameterValues() {
275277
Template t = new Template();
276278
t.getParameters()
277-
.put("with_value", new Parameter().setDefaultValue(ParameterValue.of("hello")));
279+
.put("with_value", new Parameter().setDefaultValue(ParameterValue.of("hello"))
280+
.setValueType(ParameterValueType.NUMBER));
278281
t.getParameters()
279282
.put("with_inApp", new Parameter().setDefaultValue(ParameterValue.inAppDefault()));
280283
String jsonString = t.toJSON();
281284

282285
assertEquals("{\"conditions\":[],\"parameterGroups\":{},"
283286
+ "\"parameters\":{\"with_value\":{\"conditionalValues\":{},"
284-
+ "\"defaultValue\":{\"value\":\"hello\"}},\"with_inApp\":{\"conditionalValues\":{},"
285-
+ "\"defaultValue\":{\"useInAppDefault\":true}}}}", jsonString);
287+
+ "\"defaultValue\":{\"value\":\"hello\"},\"valueType\":\"NUMBER\"},\"with_inApp\":{"
288+
+ "\"conditionalValues\":{},\"defaultValue\":{\"useInAppDefault\":true}"
289+
+ "}}}", jsonString);
286290
}
287291

288292
@Test

src/test/resources/getRemoteConfig.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
"value": "welcome to app en"
2121
}
2222
},
23-
"description": "text for welcome message!"
23+
"description": "text for welcome message!",
24+
"valueType": "STRING"
2425
},
2526
"header_text": {
2627
"defaultValue": {
2728
"useInAppDefault": true
28-
}
29+
},
30+
"valueType": "STRING"
2931
}
3032
},
3133
"parameterGroups": {
@@ -36,7 +38,8 @@
3638
"defaultValue": {
3739
"value": "true"
3840
},
39-
"description": "Whether it's currently pumpkin spice season."
41+
"description": "Whether it's currently pumpkin spice season.",
42+
"valueType": "BOOLEAN"
4043
}
4144
}
4245
}

0 commit comments

Comments
 (0)