eolivelli commented on a change in pull request #9895: URL: https://github.com/apache/pulsar/pull/9895#discussion_r593719356
########## File path: pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/ObjectSchema.java ########## @@ -0,0 +1,166 @@ +/** + * 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 lombok.extern.slf4j.Slf4j; +import org.apache.pulsar.client.api.Schema; +import org.apache.pulsar.client.api.SchemaSerializationException; +import org.apache.pulsar.client.api.schema.GenericRecord; +import org.apache.pulsar.client.api.schema.GenericSchema; +import org.apache.pulsar.client.api.schema.SchemaInfoProvider; +import org.apache.pulsar.client.impl.schema.generic.GenericAvroSchema; +import org.apache.pulsar.client.impl.schema.generic.GenericJsonSchema; +import org.apache.pulsar.client.impl.schema.generic.GenericProtobufNativeSchema; +import org.apache.pulsar.client.impl.schema.generic.GenericSchemaImpl; +import org.apache.pulsar.common.schema.KeyValue; +import org.apache.pulsar.common.schema.SchemaInfo; + +import java.util.concurrent.ExecutionException; + +import static com.google.common.base.Preconditions.checkState; + +/** + * Auto detect schema. + */ +@Slf4j +public class ObjectSchema implements Schema<Object> { + + private Schema<Object> schema; + + private String topicName; + + private String componentName; + + private SchemaInfoProvider schemaInfoProvider; + + public void setSchema(Schema<Object> schema) { + this.schema = schema; + } + + private void ensureSchemaInitialized() { + checkState(null != schema, "Schema is not initialized before used"); + } + + @Override + public void validate(byte[] message) { + ensureSchemaInitialized(); + + schema.validate(message); + } + + @Override + public boolean supportSchemaVersioning() { + return true; + } + + @Override + public byte[] encode(Object message) { + ensureSchemaInitialized(); + + return schema.encode(message); + } + + @Override + public Object decode(byte[] bytes, byte[] schemaVersion) { Review comment: @sijie thank you for taking a look to this prototype so early. From your comments I understand that this work is interesting for you and we are on the same page. This patch is only a prototype, my idea is to create a common class for AutoConsumeSchema and ObjectSchema, now I only copy/pasted from AutoConsumeSchema in order to just show to the community what I am doing. Unfortunately we cannot switch AutoConsumeSchema from `Schema<GenericRecord>` to `Schema<Object>` because it will be an incompatible change (at least at the source level) and this will cause lot of troubles to users that are expecting only to receive GenericRecord instances from AUTO_CONSUME. I will finish the work and I will ping you when I am done ---------------------------------------------------------------- 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: [email protected]
