/
k_source.go
77 lines (62 loc) · 1.55 KB
/
k_source.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
package kstream
import (
"context"
"github.com/tryfix/errors"
"github.com/tryfix/kstream/kstream/encoding"
"github.com/tryfix/kstream/kstream/topology"
)
type kSource struct {
Id int32
keyEncoder encoding.Encoder
valEncoder encoding.Encoder
name string
topic string
}
type kSourceBuilder struct {
keyEncoderBuilder encoding.Builder
valEncoderBuilder encoding.Builder
name string
topic string
info map[string]string
}
func (b *kSourceBuilder) Build() (topology.Source, error) {
return &kSource{
name: b.name,
topic: b.topic,
keyEncoder: b.keyEncoderBuilder(),
valEncoder: b.valEncoderBuilder(),
}, nil
}
func (b *kSourceBuilder) Name() string {
return b.name
}
func (b *kSourceBuilder) SourceType() string {
return `kafka`
}
func (b *kSourceBuilder) Info() map[string]string {
return b.info
}
func (s *kSource) Name() string {
return s.name
}
func (s *kSource) Run(ctx context.Context, kIn, vIn []byte) (kOut, vOut interface{}, err error) {
return s.decodeRecord(kIn, vIn)
}
func (s *kSource) decodeRecord(key []byte, val []byte) (interface{}, interface{}, error) {
k, err := s.keyEncoder.Decode(key)
if err != nil {
return nil, nil, errors.WithPrevious(err, `key decode error`)
}
v, err := s.valEncoder.Decode(val)
if err != nil {
return nil, nil, errors.WithPrevious(err, `value decode error`)
}
return k, v, nil
}
func (s *kSource) Close() {}
func (s *kSource) Next() bool {
return true
}
func (s *kSource) Type() topology.Type {
return topology.TypeSource
}