Skip to content

Commit d6f0ccc

Browse files
authored
Allow setting String[] and int[] properties (#2291)
Nice! Thanks.
1 parent 0bff26e commit d6f0ccc

File tree

3 files changed

+124
-4
lines changed

3 files changed

+124
-4
lines changed

src/main/java/com/zaxxer/hikari/util/PropertyElf.java

+56
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
*/
3232
public final class PropertyElf
3333
{
34+
private static final char ESCAPE_CHAR = '\\';
35+
private static final char SEPARATOR_CHAR = ',';
3436
private static final Pattern DURATION_PATTERN = Pattern.compile("^(?<number>\\d+)(?<unit>ms|s|m|h|d)$");
3537

3638
private PropertyElf() {
@@ -160,6 +162,12 @@ else if (paramClass == boolean.class || paramClass == Boolean.class) {
160162
else if (paramClass.isArray() && char.class.isAssignableFrom(paramClass.getComponentType())) {
161163
writeMethod.invoke(target, value.toCharArray());
162164
}
165+
else if (paramClass.isArray() && int.class.isAssignableFrom(paramClass.getComponentType())) {
166+
writeMethod.invoke(target, parseIntArray(value));
167+
}
168+
else if (paramClass.isArray() && String.class.isAssignableFrom(paramClass.getComponentType())) {
169+
writeMethod.invoke(target, new Object[]{parseStringArray(value)});
170+
}
163171
else if (paramClass == String.class) {
164172
writeMethod.invoke(target, value);
165173
}
@@ -186,6 +194,54 @@ private static String capitalizedPropertyName(String propertyName)
186194
return propertyName.substring(0, 1).toUpperCase(Locale.ENGLISH) + propertyName.substring(1);
187195
}
188196

197+
private static int[] parseIntArray(String value)
198+
{
199+
if (value == null || value.isEmpty() ) {
200+
return new int[0];
201+
}
202+
203+
var split = value.split(",");
204+
var intArray = new int[split.length];
205+
for (int i = 0; i < split.length; i++) {
206+
intArray[i] = Integer.parseInt(split[i]);
207+
}
208+
return intArray;
209+
}
210+
211+
private static String[] parseStringArray(String value)
212+
{
213+
if (value == null || value.isEmpty()) {
214+
return new String[0];
215+
}
216+
217+
var resultList = new ArrayList<String>();
218+
var inEscape = false;
219+
var currentField = new StringBuilder();
220+
for (var c : value.toCharArray())
221+
{
222+
if (inEscape) {
223+
currentField.append(c);
224+
inEscape = false;
225+
}
226+
else if (c == ESCAPE_CHAR) {
227+
inEscape = true;
228+
} else if (c == SEPARATOR_CHAR) {
229+
resultList.add(currentField.toString());
230+
currentField.setLength(0);
231+
}
232+
else {
233+
currentField.append(c);
234+
}
235+
}
236+
237+
if (inEscape) {
238+
throw new IllegalArgumentException(String.format("Unterminated escape sequence in property value: %s", value));
239+
}
240+
241+
resultList.add(currentField.toString());
242+
return resultList.toArray(new String[0]);
243+
}
244+
189245
private static Optional<Duration> parseDuration(String value)
190246
{
191247
var matcher = DURATION_PATTERN.matcher(value);

src/test/java/com/zaxxer/hikari/mocks/TestObject.java

+22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public class TestObject
66
private String string;
77
private short shortRaw;
88
private char[] charArray;
9+
private String[] stringArray;
10+
private int[] intArray;
911

1012
public void setTestObject(TestObject testObject)
1113
{
@@ -44,4 +46,24 @@ public char[] getCharArray()
4446
{
4547
return charArray;
4648
}
49+
50+
public void setStringArray(String[] stringArray)
51+
{
52+
this.stringArray = stringArray;
53+
}
54+
55+
public String[] getStringArray()
56+
{
57+
return stringArray;
58+
}
59+
60+
public void setIntArray(int[] intArray)
61+
{
62+
this.intArray = intArray;
63+
}
64+
65+
public int[] getIntArray()
66+
{
67+
return intArray;
68+
}
4769
}

src/test/java/com/zaxxer/hikari/util/PropertyElfTest.java

+46-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66
import java.util.Properties;
77

8-
import static org.junit.Assert.assertArrayEquals;
9-
import static org.junit.Assert.assertEquals;
10-
import static org.junit.Assert.assertNotSame;
11-
import static org.junit.Assert.fail;
8+
import static org.junit.Assert.*;
129

1310
public class PropertyElfTest
1411
{
@@ -44,4 +41,49 @@ public void setTargetFromPropertiesNotAClass() throws Exception
4441
assertEquals("argument type mismatch", e.getCause().getMessage());
4542
}
4643
}
44+
45+
@Test
46+
public void setStringArray()
47+
{
48+
Properties properties = new Properties();
49+
TestObject testObject = new TestObject();
50+
51+
properties.setProperty("stringArray", "abc,123");
52+
PropertyElf.setTargetFromProperties(testObject, properties);
53+
assertArrayEquals(new String[] {"abc", "123"}, testObject.getStringArray());
54+
55+
properties.setProperty("stringArray", "abc\\,123");
56+
PropertyElf.setTargetFromProperties(testObject, properties);
57+
assertArrayEquals(new String[] {"abc,123"}, testObject.getStringArray());
58+
59+
properties.setProperty("stringArray", "abc\\\\,123");
60+
PropertyElf.setTargetFromProperties(testObject, properties);
61+
assertArrayEquals(new String[] {"abc\\","123"}, testObject.getStringArray());
62+
63+
properties.setProperty("stringArray", "");
64+
PropertyElf.setTargetFromProperties(testObject, properties);
65+
assertArrayEquals(new String[] {}, testObject.getStringArray());
66+
67+
properties.setProperty("stringArray", "abc,12\\3");
68+
PropertyElf.setTargetFromProperties(testObject, properties);
69+
assertArrayEquals(new String[] {"abc","123"}, testObject.getStringArray());
70+
71+
properties.setProperty("stringArray", "abc,123\\");
72+
assertThrows(RuntimeException.class, () -> PropertyElf.setTargetFromProperties(testObject, properties));
73+
}
74+
75+
@Test
76+
public void setIntArray()
77+
{
78+
Properties properties = new Properties();
79+
TestObject testObject = new TestObject();
80+
81+
properties.setProperty("intArray", "1,2,3");
82+
PropertyElf.setTargetFromProperties(testObject, properties);
83+
assertArrayEquals(new int[] {1,2,3}, testObject.getIntArray());
84+
85+
properties.setProperty("intArray", "");
86+
PropertyElf.setTargetFromProperties(testObject, properties);
87+
assertArrayEquals(new int[] {}, testObject.getIntArray());
88+
}
4789
}

0 commit comments

Comments
 (0)