-
Notifications
You must be signed in to change notification settings - Fork 20
feat(export): make lists and maps in spreadsheet export machine-readable #398
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 |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
*/ | ||
package org.eclipse.sw360.datahandler.common; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.base.Joiner; | ||
import com.google.common.base.Predicate; | ||
import com.google.common.base.Strings; | ||
|
@@ -360,20 +362,25 @@ public static String fieldValueAsString(Object fieldValue) { | |
return nullToEmpty((String) fieldValue); | ||
} | ||
if (fieldValue instanceof Map) { | ||
List<String> mapEntriesAsStrings = nullToEmptyMap(((Map<String, Object>) fieldValue)).entrySet().stream() | ||
.map(e -> { | ||
String valueString = e.getValue() != null ? e.getValue().toString():""; | ||
return e.getKey() + " : " + valueString; | ||
}) | ||
.collect(Collectors.toList()); | ||
return joinStrings(mapEntriesAsStrings); | ||
Map<String, Object> originalMap = nullToEmptyMap(((Map<String, Object>) fieldValue)); | ||
Map<String, String> map = Maps.transformValues(originalMap, v -> v != null ? v.toString() : ""); | ||
return serializeToJson(map); | ||
} | ||
if (fieldValue instanceof Iterable){ | ||
return joinStrings((Iterable<String>) fieldValue); | ||
return serializeToJson(fieldValue); | ||
} | ||
return fieldValue.toString(); | ||
} | ||
|
||
private static String serializeToJson(Object value) { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
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. It might improve performance to move the 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. It's a seldom used function. I wouldn't care about this performance gain at this point. |
||
try { | ||
return mapper.writeValueAsString(value); | ||
} catch (JsonProcessingException e) { | ||
throw new IllegalArgumentException(String.format("Cannot serialize field value %s to JSON", value), e); | ||
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. It might be better to use no 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. Please log the exception with 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. You're right. |
||
} | ||
} | ||
|
||
public static String displayNameFor(String name, Map<String, String> nameToDisplayName){ | ||
return nameToDisplayName.containsKey(name)? nameToDisplayName.get(name) : name; | ||
} | ||
|
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.
the function you have implemented here as lambda is already in the system as
nullToEmptyString
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.
oops! That's embarrassing. I'll fix it.