-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserde_avro.go
73 lines (64 loc) · 1.89 KB
/
serde_avro.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
package kafkapipe
import (
"io"
"net/http"
"strings"
"github.com/hamba/avro/v2"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
"github.com/egsam98/kafka-pipe/internal/validate"
)
type Avro struct {
schemas map[string]avro.Schema // Map Kafka topic to schema
}
func NewAvro(schemas map[string]string) (*Avro, error) {
a := &Avro{schemas: make(map[string]avro.Schema)}
for topic, schemaUrl := range schemas {
var schema avro.Schema
switch {
case strings.HasPrefix(schemaUrl, "file://"):
var err error
if schema, err = avro.ParseFiles(schemaUrl[7:]); err != nil {
return nil, errors.Wrap(err, "Avro: Parse file")
}
case strings.HasPrefix(schemaUrl, "http://"), strings.HasPrefix(schemaUrl, "https://"):
log.Info().Msgf("Avro: Downloading schema from %s...", schemaUrl)
res, err := http.Get(schemaUrl)
if err != nil {
return nil, errors.Wrap(err, "Avro: Request schema")
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "Avro: read response body")
}
if schema, err = avro.ParseBytes(body); err != nil {
return nil, errors.Wrap(err, "Avro: Parse bytes from response body")
}
default:
return nil, errors.Errorf("Avro: Unsupported URI: %s. Supported http schemas: [file, http(-s)]", schemaUrl)
}
a.schemas[topic] = schema
}
return a, nil
}
func newAvroFromYAML(value yaml.Node) (*Avro, error) {
var cfg struct {
Schemas map[string]string `yaml:"schemas" validate:"required"`
}
if err := validate.StructFromYAML(&cfg, value); err != nil {
return nil, err
}
return NewAvro(cfg.Schemas)
}
func (a *Avro) Deserialize(dst any, topic string, src []byte) error {
schema, ok := a.schemas[topic]
if !ok {
return errors.Errorf("Avro: no corresponding schema for topic %q", topic)
}
return avro.Unmarshal(schema, src, dst)
}
func (*Avro) Tag() string {
return "avro"
}