feat: Implement stripQuotes method to clean CSV values in CsvAgentResponseHandler and CsvUnmarshallingAgentResponseHandler

This commit is contained in:
extio-de
2025-12-09 21:56:16 +01:00
parent 353870dd79
commit e0cd2a56c8
2 changed files with 40 additions and 9 deletions

View File

@@ -128,7 +128,7 @@ public class CsvAgentResponseHandler implements AgentResponseHandler {
}
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);
}
rowCount++;
@@ -158,4 +158,19 @@ public class CsvAgentResponseHandler implements AgentResponseHandler {
turn.type(),
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;
}
}

View File

@@ -191,9 +191,10 @@ public class CsvUnmarshallingAgentResponseHandler implements AgentResponseHandle
}
private Object parseValue(final String value, final Field field) {
final String cleanValue = this.stripQuotes(value);
final Class<?> fieldType = field.getType();
if (value.isEmpty() || value.equalsIgnoreCase("null")) {
if (cleanValue.isEmpty() || cleanValue.equalsIgnoreCase("null")) {
if (fieldType == List.class) {
return new ArrayList<>();
}
@@ -201,23 +202,23 @@ public class CsvUnmarshallingAgentResponseHandler implements AgentResponseHandle
}
if (fieldType == String.class) {
return value;
return cleanValue;
}
if (fieldType == Integer.class || fieldType == int.class) {
return Integer.parseInt(value);
return Integer.parseInt(cleanValue);
}
if (fieldType == Long.class || fieldType == long.class) {
return Long.parseLong(value);
return Long.parseLong(cleanValue);
}
if (fieldType == Double.class || fieldType == double.class) {
return Double.parseDouble(value);
return Double.parseDouble(cleanValue);
}
if (fieldType == Boolean.class || fieldType == boolean.class) {
return Boolean.parseBoolean(value);
return Boolean.parseBoolean(cleanValue);
}
if (fieldType == List.class) {
@@ -231,11 +232,11 @@ public class CsvUnmarshallingAgentResponseHandler implements AgentResponseHandle
final Type[] typeArgs = paramType.getActualTypeArguments();
if (typeArgs.length > 0 && typeArgs[0] == String.class) {
if (value.isEmpty()) {
if (cleanValue.isEmpty()) {
return new ArrayList<String>();
}
final String[] items = value.split(this.listSeparatorQuoted);
final String[] items = cleanValue.split(this.listSeparatorQuoted);
final List<String> list = new ArrayList<>();
for (final String item : items) {
final String trimmed = item.trim();
@@ -257,4 +258,19 @@ public class CsvUnmarshallingAgentResponseHandler implements AgentResponseHandle
turn.type(),
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;
}
}