-
Notifications
You must be signed in to change notification settings - Fork 0
/
json32.go
69 lines (63 loc) · 1.76 KB
/
json32.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
package json32
import (
"github.com/adamcolton/luce/ds/bus"
"github.com/adamcolton/luce/ds/bus/listener"
"github.com/adamcolton/luce/ds/bus/serialbus"
"github.com/adamcolton/luce/serial/type32"
"github.com/adamcolton/luce/serial/wrap/json"
)
// Sender combines a type32.TypeMap and a serialbus.Sender. It is setup to send
// json serialized data with a type32 header.
type Sender struct {
type32.TypeMap
*serialbus.Sender
}
// NewSender that will write to 'out'. It is setup to use json.Serialize as the
// serializer.
func NewSender(out chan<- []byte) *Sender {
tm := type32.NewTypeMap()
return &Sender{
TypeMap: tm,
Sender: &serialbus.Sender{
TypeSerializer: tm.WriterSerializer(json.Serialize),
Chan: out,
},
}
}
// Receiver combines a serialbus.Receiver with a type32.TypeMap. It is setup to
// receive json serialized data with a type32 header.
type Receiver struct {
*serialbus.Receiver
TypeMap type32.TypeMap
Out <-chan interface{}
}
// NewReceiver on the 'in' channel. It is setup to receive json serialized data
// with a type32 header.
func NewReceiver(in <-chan []byte) *Receiver {
iCh := make(chan interface{})
tm := type32.NewTypeMap()
return &Receiver{
TypeMap: tm,
Receiver: &serialbus.Receiver{
In: in,
Out: iCh,
TypeDeserializer: tm.ReaderDeserializer(json.Deserialize),
TypeRegistrar: tm,
},
Out: iCh,
}
}
// NewHandler reads messages off the 'in' channel and sends them to the handler.
func NewHandler(in <-chan []byte, handler any) (bus.Listener, error) {
r := NewReceiver(in)
// TODO: pass through mapSize
l, err := listener.New(10, r, nil)
if err != nil {
return nil, err
}
err = bus.DefaultRegistrar.Register(l, handler)
if err != nil {
return nil, err
}
return l, nil
}