-
Notifications
You must be signed in to change notification settings - Fork 652
/
serde.go
237 lines (214 loc) · 6.8 KB
/
serde.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
/**
* Copyright 2022 Confluent Inc.
*
* Licensed 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 serde
import "C"
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/confluentinc/confluent-kafka-go/schemaregistry"
)
// Type represents the type of Serde
type Type = int
const (
// KeySerde denotes a key Serde
KeySerde = 1
// ValueSerde denotes a value Serde
ValueSerde = 2
)
const (
// EnableValidation enables validation
EnableValidation = true
// DisableValidation disables validation
DisableValidation = false
)
// magicByte is prepended to the serialized payload
const magicByte byte = 0x0
// MessageFactory is a factory function, which should return a pointer to
// an instance into which we will unmarshal wire data.
// For Avro, the name will be the name of the Avro type if it has one.
// For JSON Schema, the name will be empty.
// For Protobuf, the name will be the name of the message type.
type MessageFactory func(subject string, name string) (interface{}, error)
// Serializer represents a serializer
type Serializer interface {
ConfigureSerializer(client schemaregistry.Client, serdeType Type, conf *SerializerConfig) error
// Serialize will serialize the given message, which should be a pointer.
// For example, in Protobuf, messages are always a pointer to a struct and never just a struct.
Serialize(topic string, msg interface{}) ([]byte, error)
Close()
}
// Deserializer represents a deserializer
type Deserializer interface {
ConfigureDeserializer(client schemaregistry.Client, serdeType Type, conf *DeserializerConfig) error
// Deserialize will call the MessageFactory to create an object
// into which we will unmarshal data.
Deserialize(topic string, payload []byte) (interface{}, error)
// DeserializeInto will unmarshal data into the given object.
DeserializeInto(topic string, payload []byte, msg interface{}) error
Close()
}
// Serde is a common instance for both the serializers and deserializers
type Serde struct {
Client schemaregistry.Client
SerdeType Type
SubjectNameStrategy SubjectNameStrategyFunc
}
// BaseSerializer represents basic serializer info
type BaseSerializer struct {
Serde
Conf *SerializerConfig
}
// BaseDeserializer represents basic deserializer info
type BaseDeserializer struct {
Serde
Conf *DeserializerConfig
MessageFactory MessageFactory
}
// ConfigureSerializer configures the Serializer
func (s *BaseSerializer) ConfigureSerializer(client schemaregistry.Client, serdeType Type, conf *SerializerConfig) error {
if client == nil {
return fmt.Errorf("schema registry client missing")
}
s.Client = client
s.Conf = conf
s.SerdeType = serdeType
s.SubjectNameStrategy = TopicNameStrategy
return nil
}
// ConfigureDeserializer configures the Deserializer
func (s *BaseDeserializer) ConfigureDeserializer(client schemaregistry.Client, serdeType Type, conf *DeserializerConfig) error {
if client == nil {
return fmt.Errorf("schema registry client missing")
}
s.Client = client
s.Conf = conf
s.SerdeType = serdeType
s.SubjectNameStrategy = TopicNameStrategy
return nil
}
// SubjectNameStrategyFunc determines the subject for the given parameters
type SubjectNameStrategyFunc func(topic string, serdeType Type, schema schemaregistry.SchemaInfo) (string, error)
// TopicNameStrategy creates a subject name by appending -[key|value] to the topic name.
func TopicNameStrategy(topic string, serdeType Type, schema schemaregistry.SchemaInfo) (string, error) {
suffix := "-value"
if serdeType == KeySerde {
suffix = "-key"
}
return topic + suffix, nil
}
// GetID returns a schema ID for the given schema
func (s *BaseSerializer) GetID(topic string, msg interface{}, info schemaregistry.SchemaInfo) (int, error) {
autoRegister := s.Conf.AutoRegisterSchemas
useSchemaID := s.Conf.UseSchemaID
useLatest := s.Conf.UseLatestVersion
normalizeSchema := s.Conf.NormalizeSchemas
var id = -1
subject, err := s.SubjectNameStrategy(topic, s.SerdeType, info)
if err != nil {
return -1, err
}
if autoRegister {
id, err = s.Client.Register(subject, info, normalizeSchema)
if err != nil {
return -1, err
}
} else if useSchemaID >= 0 {
info, err = s.Client.GetBySubjectAndID(subject, useSchemaID)
if err != nil {
return -1, err
}
_, err := s.Client.GetID(subject, info, false)
if err != nil {
return -1, err
}
} else if useLatest {
metadata, err := s.Client.GetLatestSchemaMetadata(subject)
if err != nil {
return -1, err
}
info = schemaregistry.SchemaInfo{
Schema: metadata.Schema,
SchemaType: metadata.SchemaType,
References: metadata.References,
}
id, err = s.Client.GetID(subject, info, false)
if err != nil {
return -1, err
}
} else {
id, err = s.Client.GetID(subject, info, normalizeSchema)
if err != nil {
return -1, err
}
}
return id, nil
}
// WriteBytes writes the serialized payload prepended by the magicByte
func (s *BaseSerializer) WriteBytes(id int, msgBytes []byte) ([]byte, error) {
var buf bytes.Buffer
err := buf.WriteByte(magicByte)
if err != nil {
return nil, err
}
idBytes := make([]byte, 4)
binary.BigEndian.PutUint32(idBytes, uint32(id))
_, err = buf.Write(idBytes)
if err != nil {
return nil, err
}
_, err = buf.Write(msgBytes)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// GetSchema returns a schema for a payload
func (s *BaseDeserializer) GetSchema(topic string, payload []byte) (schemaregistry.SchemaInfo, error) {
info := schemaregistry.SchemaInfo{}
if payload[0] != magicByte {
return info, fmt.Errorf("unknown magic byte")
}
id := binary.BigEndian.Uint32(payload[1:5])
subject, err := s.SubjectNameStrategy(topic, s.SerdeType, info)
if err != nil {
return info, err
}
return s.Client.GetBySubjectAndID(subject, int(id))
}
// ResolveReferences resolves schema references
func ResolveReferences(c schemaregistry.Client, schema schemaregistry.SchemaInfo, deps map[string]string) error {
for _, ref := range schema.References {
metadata, err := c.GetSchemaMetadata(ref.Subject, ref.Version)
if err != nil {
return err
}
info := schemaregistry.SchemaInfo{
Schema: metadata.Schema,
SchemaType: metadata.SchemaType,
References: metadata.References,
}
deps[ref.Name] = metadata.Schema
err = ResolveReferences(c, info, deps)
if err != nil {
return err
}
}
return nil
}
// Close closes the Serde
func (s *Serde) Close() {
}