pulsar-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [pulsar] wolfstudy commented on a change in pull request #3904: [go schema] support go schema for pulsar-client-go
Date Wed, 17 Apr 2019 06:51:16 GMT
wolfstudy commented on a change in pull request #3904: [go schema] support go schema for pulsar-client-go
URL: https://github.com/apache/pulsar/pull/3904#discussion_r276100532
 
 

 ##########
 File path: pulsar-client-go/pulsar/schema.go
 ##########
 @@ -0,0 +1,494 @@
+//
+// 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 pulsar
+
+import (
+	"bytes"
+	"encoding/json"
+	"errors"
+	"reflect"
+	"unsafe"
+
+	log "github.com/apache/pulsar/pulsar-client-go/logutil"
+
+	"github.com/gogo/protobuf/proto"
+	"github.com/linkedin/goavro"
+)
+
+type SchemaType int
+
+const (
+	NONE         SchemaType = iota //No schema defined
+	STRING                         //Simple String encoding with UTF-8
+	JSON                           //JSON object encoding and validation
+	PROTOBUF                       //Protobuf message encoding and decoding
+	AVRO                           //Serialize and deserialize via Avro
+	BOOLEAN                        //
+	INT8                           //A 8-byte integer.
+	INT16                          //A 16-byte integer.
+	INT32                          //A 32-byte integer.
+	INT64                          //A 64-byte integer.
+	FLOAT                          //A float number.
+	DOUBLE                         //A double number
+	_                              //
+	_                              //
+	_                              //
+	KEY_VALUE                      //A Schema that contains Key Schema and Value Schema.
+	BYTES        = -1              //A bytes array.
+	AUTO         = -2              //
+	AUTO_CONSUME = -3              //Auto Consume Type.
+	AUTO_PUBLISH = -4              // Auto Publish Type.
+)
+
+// Encapsulates data around the schema definition
+type SchemaInfo struct {
+	Name   string
+	Schema string
+	Type   SchemaType
+}
+
+func NewSchemaInfo(name string, schema string, schemaType SchemaType) *SchemaInfo {
+	si := &SchemaInfo{
+		Name:   name,
+		Schema: schema,
+		Type:   schemaType,
+	}
+	return si
+}
+
+type Schema interface {
+	Serialize(v interface{}) ([]byte, error)
+	UnSerialize(data []byte, v interface{}) error
+	Validate(message []byte) error
+	GetSchemaInfo() *SchemaInfo
+}
+
+type SchemaDefinition struct {
+	SchemaDef *goavro.Codec
+}
+
+func NewSchemaDefinition(schema *goavro.Codec) *SchemaDefinition {
+	schemaDef := &SchemaDefinition{
+		SchemaDef: schema,
+	}
+	return schemaDef
+}
+
+// initCodec returns a Codec used to translate between a byte slice of either
+// binary or textual Avro data and native Go data.
+func initCodec(codec string) (*goavro.Codec, error) {
+	return goavro.NewCodec(codec)
+}
+
+type JsonSchema struct {
+	SchemaDefinition
+	SchemaInfo
+}
+
+func NewJsonSchema(codec string) *JsonSchema {
+	js := new(JsonSchema)
+	schema, err := initCodec(codec)
+	if err != nil {
+		log.Fatalf("init codec error:%v", err)
+	}
+	schemaDef := NewSchemaDefinition(schema)
+	js.SchemaInfo.Schema = schemaDef.SchemaDef.Schema()
+	js.SchemaInfo.Type = JSON
+	return js
+}
+
+func (js *JsonSchema) Serialize(data interface{}) ([]byte, error) {
+	return json.Marshal(data)
+}
+
+func (js *JsonSchema) UnSerialize(data []byte, v interface{}) error {
+	return json.Unmarshal(data, v)
+}
+
+func (js *JsonSchema) Validate(message []byte) error {
+	return js.UnSerialize(message, nil)
+}
+
+func (js *JsonSchema) GetSchemaInfo() *SchemaInfo {
+	jsonSchema := NewSchemaInfo("Json", js.SchemaInfo.Schema, JSON)
+	return jsonSchema
+}
+
+type ProtoSchema struct {
+	SchemaDefinition
+	SchemaInfo
+}
+
+func NewProtoSchema(codec string) *ProtoSchema {
+	ps := new(ProtoSchema)
+	schema, err := initCodec(codec)
+	if err != nil {
+		log.Fatalf("init codec error:%v", err)
+	}
+	schemaDef := NewSchemaDefinition(schema)
+	ps.SchemaDefinition.SchemaDef = schemaDef.SchemaDef
+	ps.SchemaInfo.Schema = schemaDef.SchemaDef.Schema()
+	ps.SchemaInfo.Type = PROTOBUF
+	return ps
+}
+
+func (ps *ProtoSchema) Serialize(data interface{}) ([]byte, error) {
+	return proto.Marshal(data.(proto.Message))
+}
+
+func (ps *ProtoSchema) UnSerialize(data []byte, v interface{}) error {
+	return proto.Unmarshal(data, v.(proto.Message))
+}
+
+func (ps *ProtoSchema) Validate(message []byte) error {
+	return ps.UnSerialize(message, nil)
+}
+
+func (ps *ProtoSchema) GetSchemaInfo() *SchemaInfo {
+	jsonSchema := NewSchemaInfo("Proto", ps.SchemaDef.Schema(), PROTOBUF)
+	return jsonSchema
+}
+
+type AvroSchema struct {
+	SchemaDefinition
+	SchemaInfo
+}
+
+func NewAvroSchema(codec string) *AvroSchema {
+	as := new(AvroSchema)
+	schema, err := initCodec(codec)
+	if err != nil {
+		log.Fatalf("init codec error:%v", err)
+	}
+	schemaDef := NewSchemaDefinition(schema)
+	as.SchemaDefinition.SchemaDef = schemaDef.SchemaDef
+	as.SchemaInfo.Schema = schemaDef.SchemaDef.Schema()
+	as.SchemaInfo.Type = AVRO
+	return as
+}
+
+func (as *AvroSchema) Serialize(data interface{}) ([]byte, error) {
+	textual, err := json.Marshal(data)
+	if err != nil {
+		log.Errorf("serialize data error:%s", err.Error())
+		return nil, err
+	}
+	native, _, err := as.SchemaDef.NativeFromTextual(textual)
+	if err != nil {
+		log.Errorf("convert native Go form to binary Avro data error:%s", err.Error())
+		return nil, err
+	}
+	return as.SchemaDef.BinaryFromNative(nil, native)
+}
+
+func (as *AvroSchema) UnSerialize(data []byte, v interface{}) error {
 
 Review comment:
   As you might think, I only used this at first, but the value I got was nil. so we need
line line 207-217

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


With regards,
Apache Git Services

Mime
View raw message