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

[GitHub] [kafka] mimaison commented on a change in pull request #9950: KAFKA-12170: Fix for Connect Cast SMT to correctly transform a Byte array into a string

mimaison commented on a change in pull request #9950:
URL: https://github.com/apache/kafka/pull/9950#discussion_r578550844



##########
File path: connect/transforms/src/main/java/org/apache/kafka/connect/transforms/Cast.java
##########
@@ -360,15 +361,30 @@ else if (value instanceof String)
             throw new DataException("Unexpected type in Cast transformation: " + value.getClass());
     }
 
+
     private static String castToString(Object value) {
         if (value instanceof java.util.Date) {
             java.util.Date dateValue = (java.util.Date) value;
             return Values.dateFormatFor(dateValue).format(dateValue);
+        } else if (value instanceof ByteBuffer) {
+            ByteBuffer byteBuffer = (ByteBuffer) value;
+
+            return castByteArrayToString(byteBuffer.array());
+        } else if (value.getClass() == byte[].class) {

Review comment:
       Let's use `value instanceof byte[]` so it's like the other cases

##########
File path: connect/transforms/src/main/java/org/apache/kafka/connect/transforms/Cast.java
##########
@@ -360,15 +361,30 @@ else if (value instanceof String)
             throw new DataException("Unexpected type in Cast transformation: " + value.getClass());
     }
 
+

Review comment:
       We don't need this extra blank line

##########
File path: connect/transforms/src/test/java/org/apache/kafka/connect/transforms/CastTest.java
##########
@@ -425,7 +426,21 @@ public void castLogicalToString() {
     @Test
     public void castFieldsWithSchema() {
         Date day = new Date(MILLIS_PER_DAY);
-        xformValue.configure(Collections.singletonMap(Cast.SPEC_CONFIG, "int8:int16,int16:int32,int32:int64,int64:boolean,float32:float64,float64:boolean,boolean:int8,string:int32,bigdecimal:string,date:string,optional:int32"));
+        ByteBuffer byteBuffer = ByteBuffer.allocate(8);
+        byteBuffer.put((byte) 0xFE);
+        byteBuffer.put((byte) 0xDC);
+        byteBuffer.put((byte) 0xBA);
+        byteBuffer.put((byte) 0x98);
+        byteBuffer.put((byte) 0x76);
+        byteBuffer.put((byte) 0x54);
+        byteBuffer.put((byte) 0x32);
+        byteBuffer.put((byte) 0x10);
+        byteBuffer.flip();
+
+        byte[] byteArray = Arrays.copyOf(byteBuffer.array(), byteBuffer.array().length);

Review comment:
       What about doing something like:
   ```java
   byte[] byteArray = new byte[] {(byte) 0xFE, (byte) 0xDC, (byte) 0xBA, (byte) 0x98, 0x76, 0x54, 0x32, 0x10};
   ByteBuffer byteBuffer = ByteBuffer.wrap(Arrays.copyOf(byteArray, byteArray.length));
   ```

##########
File path: connect/transforms/src/main/java/org/apache/kafka/connect/transforms/Cast.java
##########
@@ -360,15 +361,30 @@ else if (value instanceof String)
             throw new DataException("Unexpected type in Cast transformation: " + value.getClass());
     }
 
+
     private static String castToString(Object value) {
         if (value instanceof java.util.Date) {
             java.util.Date dateValue = (java.util.Date) value;
             return Values.dateFormatFor(dateValue).format(dateValue);
+        } else if (value instanceof ByteBuffer) {
+            ByteBuffer byteBuffer = (ByteBuffer) value;

Review comment:
       We can inline this like you've done for the byte[] case below




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