forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_compatibility.go
241 lines (192 loc) · 5.12 KB
/
schema_compatibility.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
package avro
import (
"errors"
"fmt"
"sync"
)
type recursionError struct{}
func (e recursionError) Error() string {
return ""
}
type compatKey struct {
reader [32]byte
writer [32]byte
}
// SchemaCompatibility determines the compatibility of schemas.
type SchemaCompatibility struct {
cache sync.Map // map[compatKey]error
}
// NewSchemaCompatibility creates a new schema compatibility instance.
func NewSchemaCompatibility() *SchemaCompatibility {
return &SchemaCompatibility{}
}
// Compatible determines the compatibility if the reader and writer schemas.
func (c *SchemaCompatibility) Compatible(reader, writer Schema) error {
return c.compatible(reader, writer)
}
func (c *SchemaCompatibility) compatible(reader, writer Schema) error {
key := compatKey{reader: reader.Fingerprint(), writer: writer.Fingerprint()}
if err, ok := c.cache.Load(key); ok {
if _, ok := err.(recursionError); ok {
// Break the recursion here.
return nil
}
if err == nil {
return nil
}
return err.(error)
}
c.cache.Store(key, recursionError{})
err := c.match(reader, writer)
if err != nil {
// We dont want to pay the cost of fmt.Errorf every time
err = errors.New(err.Error())
}
c.cache.Store(key, err)
return err
}
func (c *SchemaCompatibility) match(reader, writer Schema) error {
// If the schema is a reference, get the actual schema
if reader.Type() == Ref {
reader = reader.(*RefSchema).Schema()
}
if writer.Type() == Ref {
writer = writer.(*RefSchema).Schema()
}
if reader.Type() != writer.Type() {
if writer.Type() == Union {
// Reader must be compatible with all types in writer
for _, schema := range writer.(*UnionSchema).Types() {
if err := c.compatible(reader, schema); err != nil {
return err
}
}
return nil
}
if reader.Type() == Union {
// Writer must be compatible with at least one reader schema
var err error
for _, schema := range reader.(*UnionSchema).Types() {
err = c.compatible(schema, writer)
if err == nil {
return nil
}
}
return fmt.Errorf("reader union lacking writer schema %s", writer.Type())
}
switch writer.Type() {
case Int:
if reader.Type() == Long || reader.Type() == Float || reader.Type() == Double {
return nil
}
case Long:
if reader.Type() == Float || reader.Type() == Double {
return nil
}
case Float:
if reader.Type() == Double {
return nil
}
case String:
if reader.Type() == Bytes {
return nil
}
case Bytes:
if reader.Type() == String {
return nil
}
}
return fmt.Errorf("reader schema %s not compatible with writer schema %s", reader.Type(), writer.Type())
}
switch reader.Type() {
case Array:
return c.compatible(reader.(*ArraySchema).Items(), writer.(*ArraySchema).Items())
case Map:
return c.compatible(reader.(*MapSchema).Values(), writer.(*MapSchema).Values())
case Fixed:
r := reader.(*FixedSchema)
w := writer.(*FixedSchema)
if err := c.checkSchemaName(r, w); err != nil {
return err
}
if err := c.checkFixedSize(r, w); err != nil {
return err
}
case Enum:
r := reader.(*EnumSchema)
w := writer.(*EnumSchema)
if err := c.checkSchemaName(r, w); err != nil {
return err
}
if err := c.checkEnumSymbols(r, w); err != nil {
return err
}
case Record:
r := reader.(*RecordSchema)
w := writer.(*RecordSchema)
if err := c.checkSchemaName(r, w); err != nil {
return err
}
if err := c.checkRecordFields(r, w); err != nil {
return err
}
case Union:
for _, schema := range writer.(*UnionSchema).Types() {
if err := c.compatible(reader, schema); err != nil {
return err
}
}
}
return nil
}
func (c *SchemaCompatibility) checkSchemaName(reader, writer NamedSchema) error {
if reader.FullName() != writer.FullName() {
return fmt.Errorf("reader schema %s and writer schema %s names do match", reader.FullName(), writer.FullName())
}
return nil
}
func (c *SchemaCompatibility) checkFixedSize(reader, writer *FixedSchema) error {
if reader.Size() != writer.Size() {
return fmt.Errorf("%s reader and writer fixed sizes do not match", reader.FullName())
}
return nil
}
func (c *SchemaCompatibility) checkEnumSymbols(reader, writer *EnumSchema) error {
for _, symbol := range writer.Symbols() {
if !c.contains(reader.Symbols(), symbol) {
return fmt.Errorf("reader %s is missing symbol %s", reader.FullName(), symbol)
}
}
return nil
}
func (c *SchemaCompatibility) checkRecordFields(reader, writer *RecordSchema) error {
for _, field := range reader.Fields() {
f, ok := c.getField(writer.Fields(), field)
if !ok {
if field.HasDefault() {
continue
}
return fmt.Errorf("reader field %s is missing in writer schema and has no default", field.Name())
}
if err := c.compatible(field.Type(), f.Type()); err != nil {
return err
}
}
return nil
}
func (c *SchemaCompatibility) contains(a []string, s string) bool {
for _, str := range a {
if str == s {
return true
}
}
return false
}
func (c *SchemaCompatibility) getField(a []*Field, f *Field) (*Field, bool) {
for _, field := range a {
if field.Name() == f.Name() {
return field, true
}
}
return nil, false
}