[HUDI-3633] Allow non-string values to be set in TypedProperties (#5045)
* [HUDI-3633] Allow non-string values to be set in TypedProperties * Override getProperty to ignore instanceof string check
This commit is contained in:
@@ -20,6 +20,7 @@ package org.apache.hudi.common.config;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Enumeration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
@@ -36,12 +37,23 @@ public class TypedProperties extends Properties implements Serializable {
|
|||||||
|
|
||||||
public TypedProperties(Properties defaults) {
|
public TypedProperties(Properties defaults) {
|
||||||
if (Objects.nonNull(defaults)) {
|
if (Objects.nonNull(defaults)) {
|
||||||
for (String key : defaults.stringPropertyNames()) {
|
for (Enumeration<?> e = defaults.propertyNames(); e.hasMoreElements(); ) {
|
||||||
put(key, defaults.getProperty(key));
|
Object k = e.nextElement();
|
||||||
|
Object v = defaults.get(k);
|
||||||
|
if (v != null) {
|
||||||
|
put(k, v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getProperty(String key) {
|
||||||
|
Object oval = super.get(key);
|
||||||
|
String sval = (oval != null) ? String.valueOf(oval) : null;
|
||||||
|
return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
|
||||||
|
}
|
||||||
|
|
||||||
private void checkKey(String property) {
|
private void checkKey(String property) {
|
||||||
if (!containsKey(property)) {
|
if (!containsKey(property)) {
|
||||||
throw new IllegalArgumentException("Property " + property + " not found");
|
throw new IllegalArgumentException("Property " + property + " not found");
|
||||||
|
|||||||
@@ -83,5 +83,27 @@ public class TestTypedProperties {
|
|||||||
assertTrue(typedProperties.getBoolean("key1"));
|
assertTrue(typedProperties.getBoolean("key1"));
|
||||||
assertTrue(typedProperties.getBoolean("key1", false));
|
assertTrue(typedProperties.getBoolean("key1", false));
|
||||||
assertFalse(typedProperties.getBoolean("key2", false));
|
assertFalse(typedProperties.getBoolean("key2", false));
|
||||||
|
// test getBoolean with non-string value for key2
|
||||||
|
properties.put("key2", true);
|
||||||
|
typedProperties = new TypedProperties(properties);
|
||||||
|
assertTrue(typedProperties.getBoolean("key1", false));
|
||||||
|
assertTrue(typedProperties.getBoolean("key2", false));
|
||||||
|
// put non-string value in TypedProperties
|
||||||
|
typedProperties.put("key3", true);
|
||||||
|
assertTrue(typedProperties.getBoolean("key3", false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTypedPropertiesWithNonStringValue() {
|
||||||
|
Properties properties = new Properties();
|
||||||
|
properties.put("key1", "1");
|
||||||
|
properties.put("key2", 2);
|
||||||
|
|
||||||
|
TypedProperties props = new TypedProperties(properties);
|
||||||
|
assertEquals(1, props.getInteger("key1"));
|
||||||
|
assertEquals(2, props.getInteger("key2"));
|
||||||
|
// put non-string value in TypedProperties
|
||||||
|
props.put("key2", 3);
|
||||||
|
assertEquals(3, props.getInteger("key2"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user