Skip to content

Commit

Permalink
[grid] AnnotatedConfig should ignore default values of primitive types
Browse files Browse the repository at this point in the history
For primitive types, it is impossible to determine whether
or not a value has been set to the default value (false for
booleans, 0 for numbers) or has merely been left blank. The
annotated configs must therefore support the ability to
ignore those values, otherwise they'll be "set", and that
prevents the CompoundConfig from working as expected.
  • Loading branch information
shs96c committed Feb 1, 2019
1 parent 1a67d56 commit 8e00cd9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openqa.selenium.grid.config;

import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Primitives;

import java.lang.reflect.Field;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -71,6 +72,21 @@ public AnnotatedConfig(Object obj) {
continue;
}

if (boolean.class.isAssignableFrom(field.getType()) && !(boolean) value) {
continue;
}

if (field.getType().isPrimitive()) {
Class<?> wrapped = Primitives.wrap(field.getType());
if (Number.class.isAssignableFrom(wrapped) && ((Number) value).floatValue() == 0) {
continue;
}
}

if (Number.class.isAssignableFrom(field.getType()) && ((Number) value).floatValue() == 0f) {

}

ConfigValue annotation = field.getAnnotation(ConfigValue.class);
Map<String, String> section = values.getOrDefault(annotation.section(), new HashMap<>());
section.put(annotation.name(), String.valueOf(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openqa.selenium.grid.config;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -111,4 +112,25 @@ class Child extends Parent {

}

@Test
public void defaultValuesForPrimitivesAreIgnored() {
// There's no way to tell the difference between the default values and the value having been
// set to the default. Best not worry about it.
class Defaults {
@ConfigValue(section = "default", name = "bool")
private boolean bool;
@ConfigValue(section = "default", name = "int")
private int integer;
@ConfigValue(section = "default", name = "string")
private String string;
}

Config config = new AnnotatedConfig(new Defaults());

assertFalse(config.get("default", "bool").isPresent());
assertFalse(config.getBool("default", "bool").isPresent());
assertFalse(config.get("default", "int").isPresent());
assertFalse(config.getInt("default", "int").isPresent());
assertFalse(config.get("default", "string").isPresent());
}
}

0 comments on commit 8e00cd9

Please sign in to comment.