-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathschema.go
645 lines (552 loc) · 17.4 KB
/
schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
// 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"
"fmt"
"hash/maphash"
"reflect"
"sync"
"unsafe"
log "github.com/sirupsen/logrus"
"github.com/hamba/avro/v2"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
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
_ //
_ //
_ //
KeyValue //A Schema that contains Key Schema and Value Schema.
BYTES = 0 //A bytes array.
AUTO = -2 //
AutoConsume = -3 //Auto Consume Type.
AutoPublish = -4 // Auto Publish Type.
ProtoNative = 20 //Protobuf native message encoding and decoding
)
// Encapsulates data around the schema definition
type SchemaInfo struct {
Name string
Schema string
Type SchemaType
Properties map[string]string
hashVal uint64
hashOnce sync.Once
}
func (s *SchemaInfo) hash() uint64 {
s.hashOnce.Do(func() {
h := maphash.Hash{}
h.SetSeed(seed)
h.Write([]byte(s.Schema))
s.hashVal = h.Sum64()
})
return s.hashVal
}
type Schema interface {
Encode(v interface{}) ([]byte, error)
Decode(data []byte, v interface{}) error
Validate(message []byte) error
GetSchemaInfo() *SchemaInfo
}
func NewSchema(schemaType SchemaType, schemaData []byte, properties map[string]string) (schema Schema, err error) {
var schemaDef = string(schemaData)
var s Schema
switch schemaType {
case BYTES:
s = NewBytesSchema(properties)
case STRING:
s = NewStringSchema(properties)
case JSON:
s = NewJSONSchema(schemaDef, properties)
case PROTOBUF:
s = NewProtoSchema(schemaDef, properties)
case AVRO:
s = NewAvroSchema(schemaDef, properties)
case INT8:
s = NewInt8Schema(properties)
case INT16:
s = NewInt16Schema(properties)
case INT32:
s = NewInt32Schema(properties)
case INT64:
s = NewInt64Schema(properties)
case FLOAT:
s = NewFloatSchema(properties)
case DOUBLE:
s = NewDoubleSchema(properties)
case ProtoNative:
s = newProtoNativeSchema(schemaDef, properties)
default:
err = fmt.Errorf("not support schema type of %v", schemaType)
}
schema = s
return
}
// initAvroCodec returns a Codec used to translate between a byte slice of either
// binary or textual Avro data and native Go data.
func initAvroCodec(schemaDef string) (avro.Schema, error) {
return avro.Parse(schemaDef)
}
type JSONSchema struct {
SchemaInfo
}
// NewJSONSchema creates a new JSONSchema
// Note: the function will panic if creation of codec fails
func NewJSONSchema(jsonAvroSchemaDef string, properties map[string]string) *JSONSchema {
js, err := NewJSONSchemaWithValidation(jsonAvroSchemaDef, properties)
if err != nil {
log.Fatalf("JSONSchema init codec error:%v", err)
}
return js
}
// NewJSONSchemaWithValidation creates a new JSONSchema and error to indicate codec failure
func NewJSONSchemaWithValidation(jsonAvroSchemaDef string, properties map[string]string) (*JSONSchema, error) {
js := new(JSONSchema)
avroCodec, err := initAvroCodec(jsonAvroSchemaDef)
if err != nil {
return nil, err
}
resolvedSchema, err := json.Marshal(avroCodec)
if err != nil {
return nil, err
}
js.SchemaInfo.Schema = string(resolvedSchema)
js.SchemaInfo.Type = JSON
js.SchemaInfo.Properties = properties
js.SchemaInfo.Name = "JSON"
return js, nil
}
func (js *JSONSchema) Encode(data interface{}) ([]byte, error) {
return json.Marshal(data)
}
func (js *JSONSchema) Decode(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
func (js *JSONSchema) Validate(message []byte) error {
return js.Decode(message, nil)
}
func (js *JSONSchema) GetSchemaInfo() *SchemaInfo {
return &js.SchemaInfo
}
type ProtoSchema struct {
SchemaInfo
}
var seed = maphash.MakeSeed()
// NewProtoSchema creates a new ProtoSchema
// Note: the function will panic if creation of codec fails
func NewProtoSchema(protoAvroSchemaDef string, properties map[string]string) *ProtoSchema {
ps, err := NewProtoSchemaWithValidation(protoAvroSchemaDef, properties)
if err != nil {
log.Fatalf("ProtoSchema init codec error:%v", err)
}
return ps
}
// NewProtoSchemaWithValidation creates a new ProtoSchema and error to indicate codec failure
func NewProtoSchemaWithValidation(protoAvroSchemaDef string, properties map[string]string) (*ProtoSchema, error) {
ps := new(ProtoSchema)
avroCodec, err := initAvroCodec(protoAvroSchemaDef)
if err != nil {
return nil, err
}
resolvedSchema, err := json.Marshal(avroCodec)
if err != nil {
return nil, err
}
ps.SchemaInfo.Schema = string(resolvedSchema)
ps.SchemaInfo.Type = PROTOBUF
ps.SchemaInfo.Properties = properties
ps.SchemaInfo.Name = "Proto"
return ps, nil
}
func (ps *ProtoSchema) Encode(data interface{}) ([]byte, error) {
return proto.Marshal(data.(proto.Message))
}
func (ps *ProtoSchema) Decode(data []byte, v interface{}) error {
return proto.Unmarshal(data, v.(proto.Message))
}
func (ps *ProtoSchema) Validate(message []byte) error {
return ps.Decode(message, nil)
}
func (ps *ProtoSchema) GetSchemaInfo() *SchemaInfo {
return &ps.SchemaInfo
}
type ProtoNativeSchema struct {
SchemaInfo
}
func NewProtoNativeSchemaWithMessage(message proto.Message, properties map[string]string) *ProtoNativeSchema {
schemaDef, err := getProtoNativeSchemaInfo(message)
if err != nil {
log.Fatalf("Get ProtoNative schema info error:%v", err)
}
return newProtoNativeSchema(schemaDef, properties)
}
func newProtoNativeSchema(protoNativeSchemaDef string, properties map[string]string) *ProtoNativeSchema {
pns := new(ProtoNativeSchema)
pns.SchemaInfo.Schema = protoNativeSchemaDef
pns.SchemaInfo.Type = ProtoNative
pns.SchemaInfo.Properties = properties
pns.SchemaInfo.Name = "ProtoNative"
return pns
}
func getProtoNativeSchemaInfo(message proto.Message) (string, error) {
fileDesc := message.ProtoReflect().Descriptor().ParentFile()
fileProtoMap := make(map[string]*descriptorpb.FileDescriptorProto)
getFileProto(fileDesc, fileProtoMap)
fileDescList := make([]*descriptorpb.FileDescriptorProto, 0, len(fileProtoMap))
for _, v := range fileProtoMap {
fileDescList = append(fileDescList, v)
}
fileDescSet := descriptorpb.FileDescriptorSet{
File: fileDescList,
}
bytesData, err := proto.Marshal(&fileDescSet)
if err != nil {
return "", err
}
schemaData := ProtoNativeSchemaData{
FileDescriptorSet: bytesData,
RootMessageTypeName: string(message.ProtoReflect().Descriptor().FullName()),
RootFileDescriptorName: fileDesc.Path(),
}
jsonData, err := json.Marshal(schemaData)
if err != nil {
return "", err
}
return string(jsonData), nil
}
type ProtoNativeSchemaData struct {
FileDescriptorSet []byte `json:"fileDescriptorSet"`
RootMessageTypeName string `json:"rootMessageTypeName"`
RootFileDescriptorName string `json:"rootFileDescriptorName"`
}
func getFileProto(fileDesc protoreflect.FileDescriptor, protoMap map[string]*descriptorpb.FileDescriptorProto) {
for i := 0; i < fileDesc.Imports().Len(); i++ {
getFileProto(fileDesc.Imports().Get(i).ParentFile(), protoMap)
}
protoMap[fileDesc.Path()] = protodesc.ToFileDescriptorProto(fileDesc)
}
func (ps *ProtoNativeSchema) Encode(data interface{}) ([]byte, error) {
return proto.Marshal(data.(proto.Message))
}
func (ps *ProtoNativeSchema) Decode(data []byte, v interface{}) error {
return proto.Unmarshal(data, v.(proto.Message))
}
func (ps *ProtoNativeSchema) Validate(message []byte) error {
return ps.Decode(message, nil)
}
func (ps *ProtoNativeSchema) GetSchemaInfo() *SchemaInfo {
return &ps.SchemaInfo
}
type AvroSchema struct {
Codec avro.Schema
SchemaInfo
}
// NewAvroSchema creates a new AvroSchema
// Note: the function will panic if creation of codec fails
func NewAvroSchema(avroSchemaDef string, properties map[string]string) *AvroSchema {
ps, err := NewAvroSchemaWithValidation(avroSchemaDef, properties)
if err != nil {
log.Fatalf("AvroSchema init codec error:%v", err)
}
return ps
}
// NewAvroSchemaWithValidation creates a new AvroSchema and error to indicate codec failure
func NewAvroSchemaWithValidation(avroSchemaDef string, properties map[string]string) (*AvroSchema, error) {
as := new(AvroSchema)
avroCodec, err := initAvroCodec(avroSchemaDef)
if err != nil {
return nil, err
}
as.Codec = avroCodec
resolvedSchema, err := json.Marshal(avroCodec)
if err != nil {
return nil, err
}
as.SchemaInfo.Schema = string(resolvedSchema)
as.SchemaInfo.Type = AVRO
as.SchemaInfo.Name = "Avro"
as.SchemaInfo.Properties = properties
return as, nil
}
func (as *AvroSchema) Encode(data interface{}) ([]byte, error) {
bin, err := avro.Marshal(as.Codec, data)
if err != nil {
log.Errorf("convert Go form to binary Avro data error:%s", err.Error())
return nil, err
}
return bin, nil
}
func (as *AvroSchema) Decode(data []byte, v interface{}) error {
err := avro.Unmarshal(as.Codec, data, v)
if err != nil {
log.Errorf("convert binary Avro data back to native Go form error:%s", err.Error())
return err
}
return nil
}
func (as *AvroSchema) Validate(message []byte) error {
return as.Decode(message, nil)
}
func (as *AvroSchema) GetSchemaInfo() *SchemaInfo {
return &as.SchemaInfo
}
type StringSchema struct {
SchemaInfo
}
func NewStringSchema(properties map[string]string) *StringSchema {
strSchema := new(StringSchema)
strSchema.SchemaInfo.Properties = properties
strSchema.SchemaInfo.Name = "String"
strSchema.SchemaInfo.Type = STRING
strSchema.SchemaInfo.Schema = ""
return strSchema
}
func (ss *StringSchema) Encode(v interface{}) ([]byte, error) {
return []byte(v.(string)), nil
}
// Decode convert from byte slice to string without allocating a new string
func (ss *StringSchema) Decode(data []byte, v interface{}) error {
strPtr := (*string)(unsafe.Pointer(&data))
reflect.ValueOf(v).Elem().Set(reflect.ValueOf(strPtr))
return nil
}
func (ss *StringSchema) Validate(message []byte) error {
return ss.Decode(message, nil)
}
func (ss *StringSchema) GetSchemaInfo() *SchemaInfo {
return &ss.SchemaInfo
}
type BytesSchema struct {
SchemaInfo
}
func NewBytesSchema(properties map[string]string) *BytesSchema {
bytesSchema := new(BytesSchema)
bytesSchema.SchemaInfo.Properties = properties
bytesSchema.SchemaInfo.Name = "Bytes"
bytesSchema.SchemaInfo.Type = BYTES
bytesSchema.SchemaInfo.Schema = ""
return bytesSchema
}
func (bs *BytesSchema) Encode(data interface{}) ([]byte, error) {
return data.([]byte), nil
}
func (bs *BytesSchema) Decode(data []byte, v interface{}) error {
reflect.ValueOf(v).Elem().Set(reflect.ValueOf(data))
return nil
}
func (bs *BytesSchema) Validate(message []byte) error {
return bs.Decode(message, nil)
}
func (bs *BytesSchema) GetSchemaInfo() *SchemaInfo {
return &bs.SchemaInfo
}
type Int8Schema struct {
SchemaInfo
}
func NewInt8Schema(properties map[string]string) *Int8Schema {
int8Schema := new(Int8Schema)
int8Schema.SchemaInfo.Properties = properties
int8Schema.SchemaInfo.Schema = ""
int8Schema.SchemaInfo.Type = INT8
int8Schema.SchemaInfo.Name = "INT8"
return int8Schema
}
func (is8 *Int8Schema) Encode(value interface{}) ([]byte, error) {
var buf bytes.Buffer
err := WriteElements(&buf, value.(int8))
return buf.Bytes(), err
}
func (is8 *Int8Schema) Decode(data []byte, v interface{}) error {
buf := bytes.NewReader(data)
return ReadElements(buf, v)
}
func (is8 *Int8Schema) Validate(message []byte) error {
if len(message) != 1 {
return newError(InvalidMessage, "size of data received by Int8Schema is not 1")
}
return nil
}
func (is8 *Int8Schema) GetSchemaInfo() *SchemaInfo {
return &is8.SchemaInfo
}
type Int16Schema struct {
SchemaInfo
}
func NewInt16Schema(properties map[string]string) *Int16Schema {
int16Schema := new(Int16Schema)
int16Schema.SchemaInfo.Properties = properties
int16Schema.SchemaInfo.Name = "INT16"
int16Schema.SchemaInfo.Type = INT16
int16Schema.SchemaInfo.Schema = ""
return int16Schema
}
func (is16 *Int16Schema) Encode(value interface{}) ([]byte, error) {
var buf bytes.Buffer
err := WriteElements(&buf, value.(int16))
return buf.Bytes(), err
}
func (is16 *Int16Schema) Decode(data []byte, v interface{}) error {
buf := bytes.NewReader(data)
return ReadElements(buf, v)
}
func (is16 *Int16Schema) Validate(message []byte) error {
if len(message) != 2 {
return newError(InvalidMessage, "size of data received by Int16Schema is not 2")
}
return nil
}
func (is16 *Int16Schema) GetSchemaInfo() *SchemaInfo {
return &is16.SchemaInfo
}
type Int32Schema struct {
SchemaInfo
}
func NewInt32Schema(properties map[string]string) *Int32Schema {
int32Schema := new(Int32Schema)
int32Schema.SchemaInfo.Properties = properties
int32Schema.SchemaInfo.Schema = ""
int32Schema.SchemaInfo.Name = "INT32"
int32Schema.SchemaInfo.Type = INT32
return int32Schema
}
func (is32 *Int32Schema) Encode(value interface{}) ([]byte, error) {
var buf bytes.Buffer
err := WriteElements(&buf, value.(int32))
return buf.Bytes(), err
}
func (is32 *Int32Schema) Decode(data []byte, v interface{}) error {
buf := bytes.NewReader(data)
return ReadElements(buf, v)
}
func (is32 *Int32Schema) Validate(message []byte) error {
if len(message) != 4 {
return newError(InvalidMessage, "size of data received by Int32Schema is not 4")
}
return nil
}
func (is32 *Int32Schema) GetSchemaInfo() *SchemaInfo {
return &is32.SchemaInfo
}
type Int64Schema struct {
SchemaInfo
}
func NewInt64Schema(properties map[string]string) *Int64Schema {
int64Schema := new(Int64Schema)
int64Schema.SchemaInfo.Properties = properties
int64Schema.SchemaInfo.Name = "INT64"
int64Schema.SchemaInfo.Type = INT64
int64Schema.SchemaInfo.Schema = ""
return int64Schema
}
func (is64 *Int64Schema) Encode(value interface{}) ([]byte, error) {
var buf bytes.Buffer
err := WriteElements(&buf, value.(int64))
return buf.Bytes(), err
}
func (is64 *Int64Schema) Decode(data []byte, v interface{}) error {
buf := bytes.NewReader(data)
return ReadElements(buf, v)
}
func (is64 *Int64Schema) Validate(message []byte) error {
if len(message) != 8 {
return newError(InvalidMessage, "size of data received by Int64Schema is not 8")
}
return nil
}
func (is64 *Int64Schema) GetSchemaInfo() *SchemaInfo {
return &is64.SchemaInfo
}
type FloatSchema struct {
SchemaInfo
}
func NewFloatSchema(properties map[string]string) *FloatSchema {
floatSchema := new(FloatSchema)
floatSchema.SchemaInfo.Properties = properties
floatSchema.SchemaInfo.Type = FLOAT
floatSchema.SchemaInfo.Name = "FLOAT"
floatSchema.SchemaInfo.Schema = ""
return floatSchema
}
func (fs *FloatSchema) Encode(value interface{}) ([]byte, error) {
return BinarySerializer.PutFloat(value)
}
func (fs *FloatSchema) Decode(data []byte, v interface{}) error {
floatValue, err := BinarySerializer.Float32(data)
if err != nil {
log.Errorf("unSerialize float error:%s", err.Error())
return err
}
reflect.ValueOf(v).Elem().Set(reflect.ValueOf(floatValue))
return nil
}
func (fs *FloatSchema) Validate(message []byte) error {
if len(message) != 4 {
return newError(InvalidMessage, "size of data received by FloatSchema is not 4")
}
return nil
}
func (fs *FloatSchema) GetSchemaInfo() *SchemaInfo {
return &fs.SchemaInfo
}
type DoubleSchema struct {
SchemaInfo
}
func NewDoubleSchema(properties map[string]string) *DoubleSchema {
doubleSchema := new(DoubleSchema)
doubleSchema.SchemaInfo.Properties = properties
doubleSchema.SchemaInfo.Type = DOUBLE
doubleSchema.SchemaInfo.Name = "DOUBLE"
doubleSchema.SchemaInfo.Schema = ""
return doubleSchema
}
func (ds *DoubleSchema) Encode(value interface{}) ([]byte, error) {
return BinarySerializer.PutDouble(value)
}
func (ds *DoubleSchema) Decode(data []byte, v interface{}) error {
doubleValue, err := BinarySerializer.Float64(data)
if err != nil {
log.Errorf("unSerialize double value error:%s", err.Error())
return err
}
reflect.ValueOf(v).Elem().Set(reflect.ValueOf(doubleValue))
return nil
}
func (ds *DoubleSchema) Validate(message []byte) error {
if len(message) != 8 {
return newError(InvalidMessage, "size of data received by DoubleSchema is not 8")
}
return nil
}
func (ds *DoubleSchema) GetSchemaInfo() *SchemaInfo {
return &ds.SchemaInfo
}