kafka-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ewe...@apache.org
Subject kafka git commit: KAFKA-3864: make field.get return field's default value when needed
Date Mon, 20 Jun 2016 19:32:50 GMT
Repository: kafka
Updated Branches:
  refs/heads/trunk b9f1c6032 -> 4544ee448


KAFKA-3864: make field.get return field's default value when needed

And not the containing struct's default value.

The contribution is my original work and that I license the work to the project under the
project's open source license.

ewencp

Author: Rollulus <roelboel@xs4all.nl>

Reviewers: Ewen Cheslack-Postava <ewen@confluent.io>

Closes #1528 from rollulus/kafka-3864


Project: http://git-wip-us.apache.org/repos/asf/kafka/repo
Commit: http://git-wip-us.apache.org/repos/asf/kafka/commit/4544ee44
Tree: http://git-wip-us.apache.org/repos/asf/kafka/tree/4544ee44
Diff: http://git-wip-us.apache.org/repos/asf/kafka/diff/4544ee44

Branch: refs/heads/trunk
Commit: 4544ee448703b8c1900adcb7a605380eb99a00a2
Parents: b9f1c60
Author: Rollulus <roelboel@xs4all.nl>
Authored: Mon Jun 20 12:30:27 2016 -0700
Committer: Ewen Cheslack-Postava <me@ewencp.org>
Committed: Mon Jun 20 12:30:27 2016 -0700

----------------------------------------------------------------------
 .../java/org/apache/kafka/connect/data/Struct.java   |  4 ++--
 .../org/apache/kafka/connect/data/StructTest.java    | 15 +++++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kafka/blob/4544ee44/connect/api/src/main/java/org/apache/kafka/connect/data/Struct.java
----------------------------------------------------------------------
diff --git a/connect/api/src/main/java/org/apache/kafka/connect/data/Struct.java b/connect/api/src/main/java/org/apache/kafka/connect/data/Struct.java
index a598259..f0bf865 100644
--- a/connect/api/src/main/java/org/apache/kafka/connect/data/Struct.java
+++ b/connect/api/src/main/java/org/apache/kafka/connect/data/Struct.java
@@ -85,8 +85,8 @@ public class Struct {
      */
     public Object get(Field field) {
         Object val = values[field.index()];
-        if (val == null && schema.defaultValue() != null) {
-            val = schema.defaultValue();
+        if (val == null && field.schema().defaultValue() != null) {
+            val = field.schema().defaultValue();
         }
         return val;
     }

http://git-wip-us.apache.org/repos/asf/kafka/blob/4544ee44/connect/api/src/test/java/org/apache/kafka/connect/data/StructTest.java
----------------------------------------------------------------------
diff --git a/connect/api/src/test/java/org/apache/kafka/connect/data/StructTest.java b/connect/api/src/test/java/org/apache/kafka/connect/data/StructTest.java
index c73992b..11c9fb0 100644
--- a/connect/api/src/test/java/org/apache/kafka/connect/data/StructTest.java
+++ b/connect/api/src/test/java/org/apache/kafka/connect/data/StructTest.java
@@ -28,6 +28,7 @@ import java.util.Map;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNull;
 
 public class StructTest {
 
@@ -160,6 +161,20 @@ public class StructTest {
         struct.validate();
     }
 
+    @Test
+    public void testMissingFieldWithDefaultValue() {
+        Schema schema = SchemaBuilder.struct().field("field", DEFAULT_FIELD_SCHEMA).build();
+        Struct struct = new Struct(schema);
+        assertEquals((byte) 0, struct.get("field"));
+    }
+
+    @Test
+    public void testMissingFieldWithoutDefaultValue() {
+        Schema schema = SchemaBuilder.struct().field("field", REQUIRED_FIELD_SCHEMA).build();
+        Struct struct = new Struct(schema);
+        assertNull(struct.get("field"));
+    }
+
 
     @Test
     public void testEquals() {


Mime
View raw message