-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemers.go
111 lines (100 loc) · 2.73 KB
/
schemers.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
package avrox
import (
"encoding/json"
"github.com/hamba/avro/v2"
"reflect"
"strings"
)
type Schemer interface {
NamespaceID() NamespaceID
SchemaID() SchemaID
Schema() string
}
func Marshal(src Schemer, cID CompressionID, schema avro.Schema) ([]byte, error) {
if schema == nil {
var parseErr error
schema, parseErr = avro.Parse(src.Schema())
if parseErr != nil {
return nil, ErrSchemaInvalid
}
}
return MarshalAny(src, schema, src.NamespaceID(), src.SchemaID(), cID)
}
// UnmarshalSchemer expects a slice with pre-allocated schemers and uses the magic
// in the data to unmarshal the correct one. It will return the used schema as any
// If no schema fits it will return an error
func UnmarshalSchemer(src []byte, schemers ...Schemer) (any, error) {
if len(src) == 0 {
return nil, nil
}
if len(src) < MagicLen {
return nil, ErrNotAvroX
}
nID, sID, _, err := DecodeMagic(src[:MagicLen])
if err != nil {
return nil, err
}
for _, schemer := range schemers {
if schemer.NamespaceID() == nID && schemer.SchemaID() == sID {
// When we only get a nil pointer of the Schemer type, we allocate one
v := reflect.ValueOf(schemer)
if v.Kind() == reflect.Ptr {
if v.IsNil() {
schemer = reflect.New(v.Type().Elem()).Interface().(Schemer)
}
} else {
return nil, ErrNoPointerDestination
}
return schemer, Unmarshal(src, schemer, nil)
}
}
return nil, ErrSchemerNotFound
}
// Unmarshal uses the give schema for unmarshalling and checks if
// it fits to the decode data. This function is faster if the schema is given
// When the schema is not given it will parse the Schemer info.
// If the schema is given, it will check that this matches to the Schemer info
func Unmarshal(data []byte, dst Schemer, schema avro.Schema) error {
if len(data) == 0 {
return ErrNoData
}
if data[0] == '{' {
// TODO: JSON We need to get the namespace and schema from the json data
return json.Unmarshal(data, dst)
}
data, nID, sID, errHelper := unmarshalHelper(data)
if errHelper != nil {
return errHelper
}
if schema == nil {
var errSchema error
schema, errSchema = avro.Parse(dst.Schema())
if errSchema != nil {
return errSchema
}
}
if nID != dst.NamespaceID() {
return ErrWrongNamespace
}
if sID != dst.SchemaID() {
return ErrWrongSchema
}
return avro.Unmarshal(schema, data, dst)
}
// JoinedSchemas returns a json array of all schemers in the arguments
func JoinedSchemas(schemers ...Schemer) string {
var sb strings.Builder
if len(schemers) > 1 {
sb.WriteString("[")
for idx, schema := range schemers {
if idx > 0 {
sb.WriteRune(',')
}
sb.WriteString(schema.Schema())
}
sb.WriteString("]")
} else if len(schemers) == 1 {
sb.WriteString(schemers[0].Schema())
}
return sb.String()
}