Skip to content

Commit

Permalink
Improve the transformation of the config properties to JSON to avoid …
Browse files Browse the repository at this point in the history
…loosing mixed-cased properties
  • Loading branch information
cescoffier committed Nov 4, 2021
1 parent 9e5c646 commit 9c2a34b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ public static JsonObject asJsonObject(Config config) {
JsonObject json = new JsonObject();
Iterable<String> propertyNames = config.getPropertyNames();
for (String originalKey : propertyNames) {
// Transform keys that may comes from environment variables.
// Transform keys that may come from environment variables.
// As kafka properties use `.`, transform "_" into "."
String key = originalKey.toLowerCase().replace("_", ".");
String key = originalKey;
if (key.contains("_") || allCaps(key)) {
key = originalKey.toLowerCase().replace("_", ".");
}
try {
Optional<Integer> i = config.getOptionalValue(key, Integer.class);
if (!i.isPresent()) {
i = config.getOptionalValue(originalKey, Integer.class);
}

if (i.isPresent() && i.get() instanceof Integer) {
json.put(key, i.get());
continue;
Expand All @@ -30,6 +37,9 @@ public static JsonObject asJsonObject(Config config) {

try {
Optional<Double> d = config.getOptionalValue(key, Double.class);
if (!d.isPresent()) {
d = config.getOptionalValue(originalKey, Double.class);
}
if (d.isPresent() && d.get() instanceof Double) {
json.put(key, d.get());
continue;
Expand All @@ -39,9 +49,10 @@ public static JsonObject asJsonObject(Config config) {
}

try {
Optional<String> s = config.getOptionalValue(key, String.class);
if (s.isPresent()) {
String value = s.get().trim();
String s = config.getOptionalValue(key, String.class)
.orElseGet(() -> config.getOptionalValue(originalKey, String.class).orElse(null));
if (s != null) {
String value = s.trim();
if (value.equalsIgnoreCase("false")) {
json.put(key, false);
} else if (value.equalsIgnoreCase("true")) {
Expand All @@ -55,15 +66,24 @@ public static JsonObject asJsonObject(Config config) {
// Ignore me
}

// We need to do boolean last, as it would return `false` for any non parsable object.
// We need to do boolean last, as it would return `false` for any non-parsable object.
try {
Optional<Boolean> d = config.getOptionalValue(key, Boolean.class);
d.ifPresent(v -> json.put(key, v));
if (!d.isPresent()) {
d = config.getOptionalValue(originalKey, Boolean.class);
}
if (d.isPresent()) {
json.put(key, d.get());
}
} catch (ClassCastException | IllegalArgumentException e) {
// Ignore the entry
}

}
return json;
}

private static boolean allCaps(String key) {
return key.toUpperCase().equals(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,22 @@ public void test() {
.put("boolean.t", true)
.put("boolean.f", false)
.put("FOO_BAR", "value")
.put("dataFormat", "DF")
.put("UP", "up")
.build();

JsonObject object = JsonHelper.asJsonObject(config);
assertThat(object.getString("key")).isEqualTo("value");
assertThat(object.getString("dataFormat")).isEqualTo("DF");
assertThat(object.getInteger("int")).isEqualTo(10);
assertThat(object.getDouble("double")).isEqualTo(23.4);
assertThat(object.getBoolean("trueasstring")).isTrue();
assertThat(object.getBoolean("falseasstring")).isFalse();
assertThat(object.getString("foo.bar")).isNull();
assertThat(object.getString("foo.bar")).isEqualTo("value");
assertThat(object.getString("FOO_BAR")).isNull();
assertThat(object.getString("UP")).isNull();
assertThat(object.getString("up")).isEqualTo("up");

assertThat(object.getString("bootstrap.servers")).isEqualTo("not-important");

assertThat(object.getBoolean("boolean.t")).isTrue();
Expand Down

0 comments on commit 9c2a34b

Please sign in to comment.