Skip to content

Commit 6fc6b4b

Browse files
authored
chore: Change 'forServer' boolean to format enum (#45)
* use 'format' instead of 'forServer' with enum
1 parent ac398ec commit 6fc6b4b

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

src/main/java/cloud/eppo/api/Configuration.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,14 @@ private Configuration(
7979
if (flagConfigJson != null && flagConfigJson.length != 0) {
8080
try {
8181
JsonNode jNode = mapper.readTree(flagConfigJson);
82-
((ObjectNode) jNode).put("forServer", !isConfigObfuscated);
82+
FlagConfigResponse.Format format =
83+
isConfigObfuscated
84+
? FlagConfigResponse.Format.CLIENT
85+
: FlagConfigResponse.Format.SERVER;
86+
((ObjectNode) jNode).put("format", format.toString());
8387
flagConfigJson = mapper.writeValueAsBytes(jNode);
8488
} catch (IOException e) {
85-
log.error("Error adding `forServer` field to FlagConfigResponse JSON");
89+
log.error("Error adding `format` field to FlagConfigResponse JSON");
8690
}
8791
}
8892
this.flagConfigJson = flagConfigJson;
@@ -183,7 +187,10 @@ public Builder(byte[] flagJson, boolean isConfigObfuscated) {
183187
}
184188

185189
public Builder(byte[] flagJson, FlagConfigResponse flagConfigResponse) {
186-
this(flagJson, flagConfigResponse, !flagConfigResponse.isForServer());
190+
this(
191+
flagJson,
192+
flagConfigResponse,
193+
flagConfigResponse.getFormat() == FlagConfigResponse.Format.CLIENT);
187194
}
188195

189196
/** Use this constructor when the FlagConfigResponse has the `forServer` field populated. */

src/main/java/cloud/eppo/ufc/dto/FlagConfigResponse.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66
public class FlagConfigResponse {
77
private final Map<String, FlagConfig> flags;
88
private final Map<String, BanditReference> banditReferences;
9-
private final boolean forServer;
10-
11-
public FlagConfigResponse(
12-
Map<String, FlagConfig> flags, Map<String, BanditReference> banditReferences) {
13-
this(flags, banditReferences, false);
14-
}
9+
private final Format format;
1510

1611
public FlagConfigResponse(
1712
Map<String, FlagConfig> flags,
1813
Map<String, BanditReference> banditReferences,
19-
boolean isConfigObfuscated) {
14+
Format dataFormat) {
2015
this.flags = flags;
2116
this.banditReferences = banditReferences;
22-
this.forServer = !isConfigObfuscated;
17+
format = dataFormat;
18+
}
19+
20+
public FlagConfigResponse(
21+
Map<String, FlagConfig> flags, Map<String, BanditReference> banditReferences) {
22+
this(flags, banditReferences, Format.SERVER);
2323
}
2424

2525
public FlagConfigResponse() {
26-
this(new ConcurrentHashMap<>(), new ConcurrentHashMap<>(), false);
26+
this(new ConcurrentHashMap<>(), new ConcurrentHashMap<>(), Format.SERVER);
2727
}
2828

2929
public Map<String, FlagConfig> getFlags() {
@@ -34,7 +34,12 @@ public Map<String, BanditReference> getBanditReferences() {
3434
return this.banditReferences;
3535
}
3636

37-
public boolean isForServer() {
38-
return this.forServer;
37+
public Format getFormat() {
38+
return format;
39+
}
40+
41+
public enum Format {
42+
SERVER,
43+
CLIENT
3944
}
4045
}

src/main/java/cloud/eppo/ufc/dto/adapters/FlagConfigResponseDeserializer.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ public FlagConfigResponse deserialize(JsonParser jp, DeserializationContext ctxt
4949
}
5050

5151
// Default is to assume that the config is not obfuscated.
52-
JsonNode forServerNode = rootNode.get("forServer");
53-
boolean isConfigObfuscated = forServerNode != null && !forServerNode.asBoolean();
52+
JsonNode formatNode = rootNode.get("format");
53+
FlagConfigResponse.Format dataFormat =
54+
formatNode == null
55+
? FlagConfigResponse.Format.SERVER
56+
: FlagConfigResponse.Format.valueOf(formatNode.asText());
5457

5558
Map<String, FlagConfig> flags = new ConcurrentHashMap<>();
5659

@@ -78,7 +81,7 @@ public FlagConfigResponse deserialize(JsonParser jp, DeserializationContext ctxt
7881
}
7982
}
8083

81-
return new FlagConfigResponse(flags, banditReferences, isConfigObfuscated);
84+
return new FlagConfigResponse(flags, banditReferences, dataFormat);
8285
}
8386

8487
private FlagConfig deserializeFlag(JsonNode jsonNode) {

src/test/java/cloud/eppo/api/ConfigurationBuilderTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cloud.eppo.api;
22

3-
import static org.junit.jupiter.api.Assertions.assertFalse;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
import static org.junit.jupiter.api.Assertions.*;
54

65
import cloud.eppo.ufc.dto.FlagConfigResponse;
76
import cloud.eppo.ufc.dto.adapters.EppoModule;
@@ -16,14 +15,14 @@ public class ConfigurationBuilderTest {
1615

1716
@Test
1817
public void testHydrateConfigFromBytesForServer_true() {
19-
byte[] jsonBytes = "{ \"forServer\": true, \"flags\":{} }".getBytes();
18+
byte[] jsonBytes = "{ \"format\": \"SERVER\", \"flags\":{} }".getBytes();
2019
Configuration config = new Configuration.Builder(jsonBytes).build();
2120
assertFalse(config.isConfigObfuscated());
2221
}
2322

2423
@Test
2524
public void testHydrateConfigFromBytesForServer_false() {
26-
byte[] jsonBytes = "{ \"forServer\": false, \"flags\":{} }".getBytes();
25+
byte[] jsonBytes = "{ \"format\": \"CLIENT\", \"flags\":{} }".getBytes();
2726
Configuration config = new Configuration.Builder(jsonBytes).build();
2827
assertTrue(config.isConfigObfuscated());
2928
}
@@ -38,7 +37,7 @@ public void testBuildConfigAddsForServer_true() throws IOException {
3837
FlagConfigResponse rehydratedConfig =
3938
mapper.readValue(serializedFlags, FlagConfigResponse.class);
4039

41-
assertTrue(rehydratedConfig.isForServer());
40+
assertEquals(rehydratedConfig.getFormat(), FlagConfigResponse.Format.SERVER);
4241
}
4342

4443
@Test
@@ -51,6 +50,6 @@ public void testBuildConfigAddsForServer_false() throws IOException {
5150
FlagConfigResponse rehydratedConfig =
5251
mapper.readValue(serializedFlags, FlagConfigResponse.class);
5352

54-
assertFalse(rehydratedConfig.isForServer());
53+
assertEquals(rehydratedConfig.getFormat(), FlagConfigResponse.Format.CLIENT);
5554
}
5655
}

0 commit comments

Comments
 (0)