mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
adapter API improvements
This commit is contained in:
@@ -20,10 +20,7 @@ package io.nosqlbench.nb.api.config.params;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
@@ -272,6 +269,21 @@ public class ParamsParser {
|
||||
return parms;
|
||||
}
|
||||
|
||||
public static Map<String, String> parseToMap(Object src, String mainField) {
|
||||
if (src instanceof Map) {
|
||||
return (Map) src;
|
||||
} else if (src instanceof CharSequence) {
|
||||
String input = ((CharSequence) src).toString();
|
||||
if (hasValues(input)) {
|
||||
return parse(input, false);
|
||||
} else {
|
||||
return new HashMap<>(Map.of(mainField, input));
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("can't parseToMap(...) on an object that is neither Map nor CharSequence");
|
||||
}
|
||||
}
|
||||
|
||||
private enum ParseState {
|
||||
expectingName,
|
||||
readingName,
|
||||
@@ -280,4 +292,6 @@ public class ParamsParser {
|
||||
readingSquotedVal,
|
||||
readingDquotedVal
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -38,7 +38,8 @@ public class NBTypeConverter {
|
||||
if (outc.isAssignableFrom(input.getClass())) return true; // assignable
|
||||
if (ClassUtils.isAssignable(input.getClass(), outc, true)) return true; // assignable with boxing
|
||||
if (String.class.isAssignableFrom(outc)) return true; // all things can be strings
|
||||
if (outc.isPrimitive() && outc != boolean.class && outc != void.class && (input instanceof Number)) return true; // via Number conversions
|
||||
if (outc.isPrimitive() && outc != boolean.class && outc != void.class && (input instanceof Number))
|
||||
return true; // via Number conversions
|
||||
return (lookup(input.getClass(), outc) != null); // fall-through to helper method lookup
|
||||
}
|
||||
|
||||
@@ -80,6 +81,13 @@ public class NBTypeConverter {
|
||||
return Optional.ofNullable(converted);
|
||||
}
|
||||
|
||||
public static <T> T convertOr(Object input, T defaultValue) {
|
||||
if (input == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
return convert(input, (Class<T>) defaultValue.getClass());
|
||||
}
|
||||
|
||||
public static <T> T convert(Object input, Class<T> outType) {
|
||||
|
||||
T converted = do_convert(input, outType);
|
||||
@@ -155,7 +163,7 @@ public class NBTypeConverter {
|
||||
Object result = converter.invoke(null, input);
|
||||
return (T) result;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to convert (" + input + ") to " + outType.getSimpleName() + ": " + e,e);
|
||||
throw new RuntimeException("Unable to convert (" + input + ") to " + outType.getSimpleName() + ": " + e, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user