-
Notifications
You must be signed in to change notification settings - Fork 55
/
schema_prepared_specific.go
53 lines (46 loc) · 1.4 KB
/
schema_prepared_specific.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
package avro
import "reflect"
func specificDecoder(entry *structFieldPlan) preparedDecoder {
switch entry.schema.Type() {
case Record:
return recordDec(entry.schema)
case Enum:
return enumDec(entry.schema.(*EnumSchema))
default:
// Generic decoders get less drastic speedups, but we can add more later.
return genericDec(entry.schema)
}
}
// structFieldPlan is a plan that assists in decoding
type structFieldPlan struct {
name string
index []int
schema Schema
dec preparedDecoder
}
type preparedDecoder func(reflectField reflect.Value, dec Decoder) (reflect.Value, error)
func genericDec(schema Schema) preparedDecoder {
return func(reflectField reflect.Value, dec Decoder) (reflect.Value, error) {
return sdr.readValue(schema, reflectField, dec)
}
}
func enumDec(schema *EnumSchema) preparedDecoder {
symbolsToIndex := NewGenericEnum(schema.Symbols).symbolsToIndex
return func(reflectField reflect.Value, dec Decoder) (reflect.Value, error) {
enumIndex, err := dec.ReadEnum()
if err != nil {
return reflect.ValueOf(enumIndex), err
}
enum := &GenericEnum{
Symbols: schema.Symbols,
symbolsToIndex: symbolsToIndex,
index: enumIndex,
}
return reflect.ValueOf(enum), nil
}
}
func recordDec(schema Schema) preparedDecoder {
return func(reflectField reflect.Value, dec Decoder) (reflect.Value, error) {
return sdr.mapRecord(schema, reflectField, dec)
}
}