feat: Implement stripQuotes method to clean CSV values in CsvAgentResponseHandler and CsvUnmarshallingAgentResponseHandler
This commit is contained in:
@@ -128,7 +128,7 @@ public class CsvAgentResponseHandler implements AgentResponseHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (int col = 0; col < parts.length; col++) {
|
for (int col = 0; col < parts.length; col++) {
|
||||||
final String value = parts[col].trim();
|
final String value = this.stripQuotes(parts[col].trim());
|
||||||
columnData.get(col).add(value);
|
columnData.get(col).add(value);
|
||||||
}
|
}
|
||||||
rowCount++;
|
rowCount++;
|
||||||
@@ -158,4 +158,19 @@ public class CsvAgentResponseHandler implements AgentResponseHandler {
|
|||||||
turn.type(),
|
turn.type(),
|
||||||
turn.text() + this.errorPromptSuffix));
|
turn.text() + this.errorPromptSuffix));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String stripQuotes(final String value) {
|
||||||
|
if (value == null || value.length() < 2) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((value.startsWith("\"")
|
||||||
|
&& value.endsWith("\""))
|
||||||
|
|| (value.startsWith("'")
|
||||||
|
&& value.endsWith("'"))) {
|
||||||
|
return value.substring(1, value.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -191,9 +191,10 @@ public class CsvUnmarshallingAgentResponseHandler implements AgentResponseHandle
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Object parseValue(final String value, final Field field) {
|
private Object parseValue(final String value, final Field field) {
|
||||||
|
final String cleanValue = this.stripQuotes(value);
|
||||||
final Class<?> fieldType = field.getType();
|
final Class<?> fieldType = field.getType();
|
||||||
|
|
||||||
if (value.isEmpty() || value.equalsIgnoreCase("null")) {
|
if (cleanValue.isEmpty() || cleanValue.equalsIgnoreCase("null")) {
|
||||||
if (fieldType == List.class) {
|
if (fieldType == List.class) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
@@ -201,23 +202,23 @@ public class CsvUnmarshallingAgentResponseHandler implements AgentResponseHandle
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (fieldType == String.class) {
|
if (fieldType == String.class) {
|
||||||
return value;
|
return cleanValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fieldType == Integer.class || fieldType == int.class) {
|
if (fieldType == Integer.class || fieldType == int.class) {
|
||||||
return Integer.parseInt(value);
|
return Integer.parseInt(cleanValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fieldType == Long.class || fieldType == long.class) {
|
if (fieldType == Long.class || fieldType == long.class) {
|
||||||
return Long.parseLong(value);
|
return Long.parseLong(cleanValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fieldType == Double.class || fieldType == double.class) {
|
if (fieldType == Double.class || fieldType == double.class) {
|
||||||
return Double.parseDouble(value);
|
return Double.parseDouble(cleanValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fieldType == Boolean.class || fieldType == boolean.class) {
|
if (fieldType == Boolean.class || fieldType == boolean.class) {
|
||||||
return Boolean.parseBoolean(value);
|
return Boolean.parseBoolean(cleanValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fieldType == List.class) {
|
if (fieldType == List.class) {
|
||||||
@@ -231,11 +232,11 @@ public class CsvUnmarshallingAgentResponseHandler implements AgentResponseHandle
|
|||||||
final Type[] typeArgs = paramType.getActualTypeArguments();
|
final Type[] typeArgs = paramType.getActualTypeArguments();
|
||||||
|
|
||||||
if (typeArgs.length > 0 && typeArgs[0] == String.class) {
|
if (typeArgs.length > 0 && typeArgs[0] == String.class) {
|
||||||
if (value.isEmpty()) {
|
if (cleanValue.isEmpty()) {
|
||||||
return new ArrayList<String>();
|
return new ArrayList<String>();
|
||||||
}
|
}
|
||||||
|
|
||||||
final String[] items = value.split(this.listSeparatorQuoted);
|
final String[] items = cleanValue.split(this.listSeparatorQuoted);
|
||||||
final List<String> list = new ArrayList<>();
|
final List<String> list = new ArrayList<>();
|
||||||
for (final String item : items) {
|
for (final String item : items) {
|
||||||
final String trimmed = item.trim();
|
final String trimmed = item.trim();
|
||||||
@@ -257,4 +258,19 @@ public class CsvUnmarshallingAgentResponseHandler implements AgentResponseHandle
|
|||||||
turn.type(),
|
turn.type(),
|
||||||
turn.text() + this.errorPromptSuffix));
|
turn.text() + this.errorPromptSuffix));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String stripQuotes(final String value) {
|
||||||
|
if (value == null || value.length() < 2) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((value.startsWith("\"")
|
||||||
|
&& value.endsWith("\""))
|
||||||
|
|| (value.startsWith("'")
|
||||||
|
&& value.endsWith("'"))) {
|
||||||
|
return value.substring(1, value.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user