EndzeitBegins commented on code in PR #9151:
URL: https://github.com/apache/nifi/pull/9151#discussion_r1707521489
##########
nifi-extension-bundles/nifi-gcp-bundle/nifi-gcp-processors/src/main/java/org/apache/nifi/processors/gcp/bigquery/proto/ProtoUtils.java:
##########
@@ -46,8 +48,22 @@ public static DynamicMessage
createMessage(Descriptors.Descriptor descriptor, Ma
switch (field.getType()) {
case MESSAGE:
if (field.isRepeated()) {
- Collection collection = value.getClass().isArray() ?
Arrays.asList((Object[]) value) : (Collection) value;
- collection.forEach(act -> builder.addRepeatedField(field,
createMessage(field.getMessageType(), (Map<String, Object>) act, tableSchema)));
+ final Collection<Map<String, Object>> valueMaps;
+ if (value instanceof Object[] arrayValue) {
+ valueMaps = Arrays.stream(arrayValue)
+ .map(item -> (Map<String, Object>)
item).toList();
+ } else if (value instanceof HashMap<?, ?> hashMapValue) {
+ valueMaps = new ArrayList<>();
+ for (Map.Entry<?, ?> entry : hashMapValue.entrySet()) {
+ Map<String, Object> map = new HashMap<>();
+ map.put("key", entry.getKey());
+ map.put("value", entry.getValue());
+ valueMaps.add(map);
+ }
Review Comment:
This code is totally fine, but we could simplify it further by using
`Map.of` as introduced in Java 9.
```suggestion
valueMaps = new ArrayList<>();
for (Map.Entry<?, ?> entry : hashMapValue.entrySet())
{
valueMaps.add(Map.of(
"key", entry.getKey(),
"value", entry.getValue()
));
}
```
Alternatively we could replace the imperative `for` loop with a more
functional approach, but that's just down to personal preference I assume.
```suggestion
valueMaps = mapValue.entrySet().stream()
.map(entry -> Map.of(
"key", entry.getKey(),
"value", entry.getValue()
)).toList();
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]