Skip to content

Commit 5123e8f

Browse files
committed
Add Version information to RC template
1 parent cd5dfda commit 5123e8f

File tree

9 files changed

+500
-107
lines changed

9 files changed

+500
-107
lines changed

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.HashMap;
2626
import java.util.List;
2727
import java.util.Map;
28-
import java.util.Objects;
2928

3029
/**
3130
* Represents a Remote Config template.
@@ -36,6 +35,7 @@ public final class Template {
3635
private Map<String, Parameter> parameters;
3736
private List<Condition> conditions;
3837
private Map<String, ParameterGroup> parameterGroups;
38+
private Version version;
3939

4040
/**
4141
* Creates a new {@link Template}.
@@ -69,6 +69,9 @@ public Template() {
6969
this.parameterGroups.put(entry.getKey(), new ParameterGroup(entry.getValue()));
7070
}
7171
}
72+
if (templateResponse.getVersion() != null) {
73+
this.version = new Version(templateResponse.getVersion());
74+
}
7275
}
7376

7477
/**
@@ -111,6 +114,15 @@ public Map<String, ParameterGroup> getParameterGroups() {
111114
return parameterGroups;
112115
}
113116

117+
/**
118+
* Gets the version information of the template.
119+
*
120+
* @return The version information of the template.
121+
*/
122+
public Version getVersion() {
123+
return version;
124+
}
125+
114126
/**
115127
* Sets the map of parameters of the template.
116128
*
@@ -152,6 +164,18 @@ public Template setParameterGroups(
152164
return this;
153165
}
154166

167+
/**
168+
* Sets the version information of the template.
169+
* Only the version's description field can be specified here.
170+
*
171+
* @param version A {@link Version} instance.
172+
* @return This {@link Template} instance.
173+
*/
174+
public Template setVersion(Version version) {
175+
this.version = version;
176+
return this;
177+
}
178+
155179
Template setETag(String etag) {
156180
this.etag = etag;
157181
return this;
@@ -170,29 +194,12 @@ TemplateResponse toTemplateResponse() {
170194
for (Map.Entry<String, ParameterGroup> entry : this.parameterGroups.entrySet()) {
171195
parameterGroupResponse.put(entry.getKey(), entry.getValue().toParameterGroupResponse());
172196
}
197+
TemplateResponse.VersionResponse versionResponse = (this.version == null) ? null
198+
: this.version.toVersionResponse();
173199
return new TemplateResponse()
174200
.setParameters(parameterResponses)
175201
.setConditions(conditionResponses)
176-
.setParameterGroups(parameterGroupResponse);
177-
}
178-
179-
@Override
180-
public boolean equals(Object o) {
181-
if (this == o) {
182-
return true;
183-
}
184-
if (o == null || getClass() != o.getClass()) {
185-
return false;
186-
}
187-
Template template = (Template) o;
188-
return Objects.equals(etag, template.etag)
189-
&& Objects.equals(parameters, template.parameters)
190-
&& Objects.equals(conditions, template.conditions)
191-
&& Objects.equals(parameterGroups, template.parameterGroups);
192-
}
193-
194-
@Override
195-
public int hashCode() {
196-
return Objects.hash(etag, parameters, conditions, parameterGroups);
202+
.setParameterGroups(parameterGroupResponse)
203+
.setVersion(versionResponse);
197204
}
198205
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2020 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+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.firebase.internal.NonNull;
22+
import com.google.firebase.internal.Nullable;
23+
import com.google.firebase.remoteconfig.internal.TemplateResponse.UserResponse;
24+
25+
/**
26+
* Represents a Remote Config user. Output only.
27+
*/
28+
public final class User {
29+
30+
private String email;
31+
private String name;
32+
private String imageUrl;
33+
34+
User(@NonNull UserResponse userResponse) {
35+
checkNotNull(userResponse);
36+
this.email = userResponse.getEmail();
37+
this.name = userResponse.getName();
38+
this.imageUrl = userResponse.getImageUrl();
39+
}
40+
41+
/**
42+
* Gets the email of the user.
43+
*
44+
* @return The email of the user or null.
45+
*/
46+
@Nullable
47+
public String getEmail() {
48+
return email;
49+
}
50+
51+
/**
52+
* Gets the name of the user.
53+
*
54+
* @return The name of the user or null.
55+
*/
56+
@Nullable
57+
public String getName() {
58+
return name;
59+
}
60+
61+
/**
62+
* Gets the image URL of the user.
63+
*
64+
* @return The image URL of the user or null.
65+
*/
66+
@Nullable
67+
public String getImageUrl() {
68+
return imageUrl;
69+
}
70+
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Copyright 2020 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+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.common.base.Strings;
22+
import com.google.firebase.internal.NonNull;
23+
import com.google.firebase.internal.Nullable;
24+
import com.google.firebase.remoteconfig.internal.TemplateResponse;
25+
import com.google.firebase.remoteconfig.internal.TemplateResponse.VersionResponse;
26+
27+
import java.text.ParseException;
28+
import java.text.SimpleDateFormat;
29+
import java.util.TimeZone;
30+
31+
/**
32+
* Represents a Remote Config template version.
33+
* Output only, except for the version description. Contains metadata about a particular
34+
* version of the Remote Config template. All fields are set at the time the specified Remote
35+
* Config template is published. A version's description field may be specified when
36+
* publishing a template.
37+
*/
38+
public final class Version {
39+
40+
private String versionNumber;
41+
private long updateTime;
42+
private String updateOrigin;
43+
private String updateType;
44+
private User updateUser;
45+
private String description;
46+
private String rollbackSource;
47+
private boolean legacy;
48+
49+
/**
50+
* Creates a new {@link Version} with a description.
51+
*/
52+
public static Version withDescription(String description) {
53+
return new Version().setDescription(description);
54+
}
55+
56+
Version() {
57+
}
58+
59+
Version(@NonNull VersionResponse versionResponse) {
60+
checkNotNull(versionResponse);
61+
this.versionNumber = versionResponse.getVersionNumber();
62+
if (!Strings.isNullOrEmpty(versionResponse.getUpdateTime())) {
63+
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
64+
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
65+
try {
66+
this.updateTime = dateFormat.parse(versionResponse.getUpdateTime()).getTime();
67+
} catch (ParseException e) {
68+
this.updateTime = 0;
69+
}
70+
}
71+
this.updateOrigin = versionResponse.getUpdateOrigin();
72+
this.updateType = versionResponse.getUpdateType();
73+
TemplateResponse.UserResponse userResponse = versionResponse.getUpdateUser();
74+
this.updateUser = (userResponse != null) ? new User(userResponse) : null;
75+
this.description = versionResponse.getDescription();
76+
this.rollbackSource = versionResponse.getRollbackSource();
77+
this.legacy = versionResponse.isLegacy();
78+
}
79+
80+
/**
81+
* Gets the version number of the template.
82+
*
83+
* @return The version number or null.
84+
*/
85+
@Nullable
86+
public String getVersionNumber() {
87+
return versionNumber;
88+
}
89+
90+
/**
91+
* Gets the update time of the version. The timestamp of when this version of the Remote Config
92+
* template was written to the Remote Config backend.
93+
*
94+
* @return The update time of the version or null.
95+
*/
96+
@Nullable
97+
public long getUpdateTime() {
98+
return updateTime;
99+
}
100+
101+
/**
102+
* Gets the origin of the template update action.
103+
*
104+
* @return The origin of the template update action or null.
105+
*/
106+
@Nullable
107+
public String getUpdateOrigin() {
108+
return updateOrigin;
109+
}
110+
111+
/**
112+
* Gets the type of the template update action.
113+
*
114+
* @return The type of the template update action or null.
115+
*/
116+
@Nullable
117+
public String getUpdateType() {
118+
return updateType;
119+
}
120+
121+
/**
122+
* Gets the update user of the template.
123+
* An aggregation of all metadata fields about the account that performed the update.
124+
*
125+
* @return The update user of the template or null.
126+
*/
127+
@Nullable
128+
public User getUpdateUser() {
129+
return updateUser;
130+
}
131+
132+
/**
133+
* Gets the user-provided description of the corresponding Remote Config template.
134+
*
135+
* @return The description of the template or null.
136+
*/
137+
@Nullable
138+
public String getDescription() {
139+
return description;
140+
}
141+
142+
/**
143+
* Gets the rollback source of the template.
144+
*
145+
* <p>The version number of the Remote Config template that has become the current version
146+
* due to a rollback. Only present if this version is the result of a rollback.
147+
*
148+
* @return The rollback source of the template or null.
149+
*/
150+
@Nullable
151+
public String getRollbackSource() {
152+
return rollbackSource;
153+
}
154+
155+
/**
156+
* Indicates whether this Remote Config template was published before version history was
157+
* supported.
158+
*
159+
* @return true if the template was published before version history was supported,
160+
* and false otherwise.
161+
*/
162+
public boolean isLegacy() {
163+
return legacy;
164+
}
165+
166+
/**
167+
* Sets the user-provided description of the template.
168+
*
169+
* @param description The description of the template.
170+
* @return This {@link Version}.
171+
*/
172+
public Version setDescription(String description) {
173+
this.description = description;
174+
return this;
175+
}
176+
177+
VersionResponse toVersionResponse() {
178+
return new VersionResponse()
179+
.setDescription(this.description);
180+
}
181+
}

0 commit comments

Comments
 (0)