1
0

[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:
Sagar Sumit
2022-03-16 00:03:22 +05:30
committed by GitHub
parent 5e8ff8d793
commit d514570e90
2 changed files with 36 additions and 2 deletions

View File

@@ -20,6 +20,7 @@ package org.apache.hudi.common.config;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
@@ -36,12 +37,23 @@ public class TypedProperties extends Properties implements Serializable {
public TypedProperties(Properties defaults) {
if (Objects.nonNull(defaults)) {
for (String key : defaults.stringPropertyNames()) {
put(key, defaults.getProperty(key));
for (Enumeration<?> e = defaults.propertyNames(); e.hasMoreElements(); ) {
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) {
if (!containsKey(property)) {
throw new IllegalArgumentException("Property " + property + " not found");

View File

@@ -83,5 +83,27 @@ public class TestTypedProperties {
assertTrue(typedProperties.getBoolean("key1"));
assertTrue(typedProperties.getBoolean("key1", 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"));
}
}