-
Notifications
You must be signed in to change notification settings - Fork 485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for parsing Collections and Maps from the Spring YML format #739
base: 2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
import java.lang.reflect.Type; | ||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
|
@@ -298,6 +299,29 @@ protected <T> T getValue(Type type, String key) { | |
@SuppressWarnings("unchecked") | ||
protected <T> T getValueWithDefault(Type type, String key, T defaultValue) { | ||
Object rawProp = getRawProperty(key); | ||
if (rawProp == null && type instanceof ArchaiusType) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't ... hate this, but let's think about it a bit more. I'd really love it if there was some way we could call the existing decoders here instead of marshaling the entire map into a single string just to unmarshal it back a nanosecond later. The same goes for the List support. |
||
ArchaiusType archaiusType = (ArchaiusType) type; | ||
if (archaiusType.isMap()) { | ||
List<String> vals = new ArrayList<>(); | ||
String keyAndDelimiter = key + "."; | ||
for (String k : keys()) { | ||
if (k.startsWith(keyAndDelimiter)) { | ||
String val = getString(k); | ||
if (val.contains("=") || val.contains(",")) { | ||
log.warn( | ||
"For map resolution of key {}, skipping subkey {} because value {}" | ||
+ " contains an invalid character (=/,)", | ||
key, k, val); | ||
} else { | ||
vals.add(String.format("%s=%s", k.substring(keyAndDelimiter.length()), getString(k))); | ||
} | ||
} | ||
} | ||
rawProp = vals.isEmpty() ? null : String.join(",", vals); | ||
} else if (archaiusType.isCollection()) { | ||
rawProp = createListStringForKey(key); | ||
} | ||
} | ||
|
||
// Not found. Return the default. | ||
if (rawProp == null) { | ||
|
@@ -365,6 +389,28 @@ protected <T> T getValueWithDefault(Type type, String key, T defaultValue) { | |
new IllegalArgumentException("Property " + rawProp + " is not convertible to " + type.getTypeName())); | ||
} | ||
|
||
private String createListStringForKey(String key) { | ||
List<String> vals = new ArrayList<>(); | ||
int counter = 0; | ||
while (true) { | ||
String checkKey = String.format("%s[%s]", key, counter++); | ||
if (containsKey(checkKey)) { | ||
String val = getString(checkKey); | ||
if (val.contains(",")) { | ||
log.warn( | ||
"For collection resolution of key {}, skipping subkey {} because value {}" | ||
+ " contains an invalid character (,)", | ||
key, checkKey, val); | ||
} else { | ||
vals.add(getString(checkKey)); | ||
} | ||
} else { | ||
break; | ||
} | ||
} | ||
return vals.isEmpty() ? null : String.join(",", vals); | ||
} | ||
|
||
@Override | ||
public String resolve(String value) { | ||
return interpolator.create(getLookup()).resolve(value); | ||
|
@@ -468,6 +514,9 @@ public Byte getByte(String key, Byte defaultValue) { | |
@Override | ||
public <T> List<T> getList(String key, Class<T> type) { | ||
Object value = getRawProperty(key); | ||
if (value == null) { | ||
value = createListStringForKey(key); | ||
} | ||
if (value == null) { | ||
return notFound(key); | ||
} | ||
|
@@ -490,6 +539,9 @@ public List<?> getList(String key) { | |
@SuppressWarnings("rawtypes") // Required by legacy API | ||
public List getList(String key, List defaultValue) { | ||
Object value = getRawProperty(key); | ||
if (value == null) { | ||
value = createListStringForKey(key); | ||
} | ||
if (value == null) { | ||
return notFound(key, defaultValue); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe move these two to be private static helper methods in AbstractConfig? Then things would work with any
ParametrizedType
, not just withArchaiusType
.