You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2021/02/23 16:49:55 UTC

[GitHub] [camel-kafka-connector] omarsmak commented on a change in pull request #1052: Convert Struct to Map

omarsmak commented on a change in pull request #1052:
URL: https://github.com/apache/camel-kafka-connector/pull/1052#discussion_r581206194



##########
File path: core/src/main/java/org/apache/camel/kafkaconnector/CamelSinkTask.java
##########
@@ -225,15 +230,47 @@ If the CamelMainSupport instance fails to be instantiated (ie.: due to missing c
         }
     }
 
-    private static void mapHeader(Header header, String prefix, Map<String, Object> destination) {
+    private void mapHeader(Header header, String prefix, Map<String, Object> destination) {
         final String key = StringHelper.after(header.key(), prefix, header.key());
         final Schema schema = header.schema();
 
         if (schema.type().equals(Schema.BYTES_SCHEMA.type()) && Objects.equals(schema.name(), Decimal.LOGICAL_NAME)) {
             destination.put(key, Decimal.toLogical(schema, (byte[]) header.value()));
         } else {
-            destination.put(key, header.value());
+            destination.put(key, convertValueFromStruct(header.schema(), header.value()));
+        }
+    }
+
+    private Object convertValueFromStruct(Schema schema, Object value) {
+        // if we have have the struct to map flag enabled and
+        // if we have a schema of type Struct, we convert it to map, otherwise
+        // we just return the value as it is
+        if (convertStructToMap && schema != null && value != null && Schema.Type.STRUCT == schema.type()) {
+            return toMap((Struct) value);
         }
+
+        return value;
+    }
+
+    private static Map<String, Object> toMap(final Struct struct) {
+        final HashMap<String, Object> fieldsToValues = new HashMap<>();
+
+        struct.schema().fields().forEach(field -> {
+            try {
+                Object value = struct.get(field);
+
+                // recursive call if we have nested structs
+                if (value instanceof Struct) {
+                    fieldsToValues.put(field.name(), toMap((Struct) value));
+                } else {
+                    fieldsToValues.put(field.name(), value);
+                }
+            } catch (DataException e) {

Review comment:
       it is happening when Struct is looking up for the field name, if it doesn't find the field name, it will just throw DataException




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org