srkukarni closed pull request #1908:  adding protobuf schema
URL: https://github.com/apache/incubator-pulsar/pull/1908
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pulsar-client/pom.xml b/pulsar-client/pom.xml
index 653c17d88e..16ff286341 100644
--- a/pulsar-client/pom.xml
+++ b/pulsar-client/pom.xml
@@ -90,6 +90,13 @@
          </exclusion>
       </exclusions>
     </dependency>
+
+    <dependency>
+      <groupId>com.google.protobuf</groupId>
+      <artifactId>protobuf-java</artifactId>
+      <version>${protobuf3.version}</version>
+      <scope>provided</scope>
+    </dependency>
     
     <!-- httpclient-hostname-verification depends on below dependencies  --> 
     <dependency>
@@ -108,6 +115,13 @@
       <artifactId>jackson-module-jsonSchema</artifactId>
     </dependency>
 
+    <!-- Testing dependencies -->
+    <dependency>
+      <groupId>org.apache.pulsar</groupId>
+      <artifactId>pulsar-functions-proto</artifactId>
+      <version>${project.parent.version}</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
diff --git 
a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/ProtobufSchema.java
 
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/ProtobufSchema.java
new file mode 100644
index 0000000000..42c933182e
--- /dev/null
+++ 
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/ProtobufSchema.java
@@ -0,0 +1,81 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.client.impl.schema;
+
+import com.google.protobuf.Parser;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.SchemaSerializationException;
+import org.apache.pulsar.common.schema.SchemaInfo;
+import org.apache.pulsar.common.schema.SchemaType;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.Collections;
+import java.util.Map;
+
+public class ProtobufSchema<T extends com.google.protobuf.GeneratedMessageV3> 
implements Schema<T> {
+
+    private SchemaInfo schemaInfo;
+    private Parser<T> tParser;
+
+    private ProtobufSchema(SchemaInfo schemaInfo, Class<T> pojo) {
+        this.schemaInfo = schemaInfo;
+        try {
+            T protoMessageInstance = (T) 
pojo.getMethod("getDefaultInstance").invoke(null);
+            tParser = (Parser<T>) protoMessageInstance.getParserForType();
+        } catch (IllegalAccessException | InvocationTargetException | 
NoSuchMethodException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
+
+    @Override
+    public byte[] encode(T message) {
+        return message.toByteArray();
+    }
+
+    @Override
+    public T decode(byte[] bytes) {
+        try {
+            return this.tParser.parseFrom(bytes);
+        } catch (Exception e) {
+            throw new RuntimeException(new SchemaSerializationException(e));
+        }
+    }
+
+    @Override
+    public SchemaInfo getSchemaInfo() {
+        return schemaInfo;
+    }
+
+    public static <T extends com.google.protobuf.GeneratedMessageV3> 
ProtobufSchema<T> of(Class<T> pojo) {
+        return of(pojo, Collections.emptyMap());
+    }
+
+    public static <T extends com.google.protobuf.GeneratedMessageV3> 
ProtobufSchema<T> of(
+            Class<T> pojo, Map<String, String> properties){
+
+        SchemaInfo info = new SchemaInfo();
+        info.setName("");
+        info.setProperties(properties);
+        info.setType(SchemaType.PROTOBUF);
+
+        //TODO determine best method to extract schema from a protobuf message
+        info.setSchema(null);
+        return new ProtobufSchema<>(info, pojo);
+    }
+}
diff --git 
a/pulsar-client/src/test/java/org/apache/pulsar/client/schemas/ProtobufSchemaTest.java
 
b/pulsar-client/src/test/java/org/apache/pulsar/client/schemas/ProtobufSchemaTest.java
new file mode 100644
index 0000000000..fe2c116acd
--- /dev/null
+++ 
b/pulsar-client/src/test/java/org/apache/pulsar/client/schemas/ProtobufSchemaTest.java
@@ -0,0 +1,42 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.client.schemas;
+
+import org.apache.pulsar.client.impl.schema.ProtobufSchema;
+import org.apache.pulsar.functions.proto.Function;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class ProtobufSchemaTest {
+
+    private static final String NAME = "foo";
+
+    @Test
+    public void testEncodeAndDecode() {
+        Function.FunctionDetails functionDetails = 
Function.FunctionDetails.newBuilder().setName(NAME).build();
+
+        ProtobufSchema<Function.FunctionDetails> protobufSchema = 
ProtobufSchema.of(Function.FunctionDetails.class);
+
+        byte[] bytes = protobufSchema.encode(functionDetails);
+
+        Function.FunctionDetails message = protobufSchema.decode(bytes);
+
+        Assert.assertEquals(message.getName(), NAME);
+    }
+}
diff --git 
a/pulsar-common/src/main/java/org/apache/pulsar/common/schema/SchemaType.java 
b/pulsar-common/src/main/java/org/apache/pulsar/common/schema/SchemaType.java
index ad56bf29fe..44d78c9bc9 100644
--- 
a/pulsar-common/src/main/java/org/apache/pulsar/common/schema/SchemaType.java
+++ 
b/pulsar-common/src/main/java/org/apache/pulsar/common/schema/SchemaType.java
@@ -35,5 +35,7 @@
     /**
      * JSON object encoding and validation
      */
-    JSON
+    JSON,
+
+    PROTOBUF
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to