-
Notifications
You must be signed in to change notification settings - Fork 50
/
message_encoding.go
175 lines (134 loc) · 4.71 KB
/
message_encoding.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package stream
import (
"context"
"fmt"
"github.com/justtrackio/gosoline/pkg/encoding/base64"
"github.com/justtrackio/gosoline/pkg/mdl"
)
type EncodeHandler interface {
Encode(ctx context.Context, data interface{}, attributes map[string]string) (context.Context, map[string]string, error)
Decode(ctx context.Context, data interface{}, attributes map[string]string) (context.Context, map[string]string, error)
}
var defaultEncodeHandlers = make([]EncodeHandler, 0)
func AddDefaultEncodeHandler(handler EncodeHandler) {
defaultEncodeHandlers = append(defaultEncodeHandlers, handler)
}
type MessageEncoderSettings struct {
Encoding EncodingType
Compression CompressionType
EncodeHandlers []EncodeHandler
}
//go:generate mockery --name MessageEncoder
type MessageEncoder interface {
Encode(ctx context.Context, data interface{}, attributeSets ...map[string]string) (*Message, error)
Decode(ctx context.Context, msg *Message, out interface{}) (context.Context, map[string]string, error)
}
type messageEncoder struct {
encoding EncodingType
compression CompressionType
encodeHandlers []EncodeHandler
}
func NewMessageEncoder(config *MessageEncoderSettings) *messageEncoder {
if config.Encoding == "" {
config.Encoding = defaultMessageBodyEncoding
}
if config.Compression == "" {
config.Compression = CompressionNone
}
if len(config.EncodeHandlers) == 0 {
config.EncodeHandlers = defaultEncodeHandlers
}
return &messageEncoder{
encoding: config.Encoding,
compression: config.Compression,
encodeHandlers: config.EncodeHandlers,
}
}
func (e *messageEncoder) Encode(ctx context.Context, data interface{}, attributeSets ...map[string]string) (*Message, error) {
var err error
var body []byte
attributes := make(map[string]string)
if body, err = e.encodeBody(attributes, data); err != nil {
return nil, fmt.Errorf("could not encode message body: %w", err)
}
if body, err = e.compressBody(attributes, body); err != nil {
return nil, fmt.Errorf("could not compress message body: %w", err)
}
if attributes, err = e.mergeAttributes(attributes, attributeSets); err != nil {
return nil, err
}
for _, handler := range e.encodeHandlers {
if ctx, attributes, err = handler.Encode(ctx, data, attributes); err != nil {
return nil, fmt.Errorf("can not apply encoding handler on message: %w", err)
}
}
msg := &Message{
Attributes: attributes,
Body: string(body),
}
return msg, nil
}
func (e *messageEncoder) encodeBody(attributes map[string]string, data interface{}) ([]byte, error) {
body, err := EncodeMessage(e.encoding, data)
if err != nil {
return nil, err
}
attributes[AttributeEncoding] = e.encoding.String()
return body, nil
}
func (e *messageEncoder) compressBody(attributes map[string]string, body []byte) ([]byte, error) {
if e.compression == CompressionNone {
return body, nil
}
compressed, err := CompressMessage(e.compression, body)
if err != nil {
return nil, err
}
compressedBase64 := base64.Encode(compressed)
attributes[AttributeCompression] = e.compression.String()
return compressedBase64, nil
}
func (e *messageEncoder) mergeAttributes(attributes map[string]string, attributeSets []map[string]string) (map[string]string, error) {
for _, set := range attributeSets {
for k, v := range set {
if _, ok := attributes[k]; ok {
return nil, fmt.Errorf("duplicate attribute '%s' on message", k)
}
attributes[k] = v
}
}
return attributes, nil
}
func (e *messageEncoder) Decode(ctx context.Context, msg *Message, out interface{}) (context.Context, map[string]string, error) {
var err error
var body []byte
attributes := msg.Attributes
body = []byte(msg.Body)
if body, err = e.decompressBody(attributes, body); err != nil {
return ctx, attributes, err
}
if err = e.decodeBody(attributes, body, out); err != nil {
return ctx, attributes, fmt.Errorf("can not decode message body: %w", err)
}
for _, handler := range e.encodeHandlers {
if ctx, attributes, err = handler.Decode(ctx, out, attributes); err != nil {
return ctx, attributes, fmt.Errorf("can not apply encoding handler on message: %w", err)
}
}
return ctx, attributes, nil
}
func (e *messageEncoder) decompressBody(attributes map[string]string, body []byte) ([]byte, error) {
compression := GetCompressionAttribute(attributes)
if compression == nil {
return body, nil
}
base64Decoded, err := base64.Decode(body)
if err != nil {
return nil, fmt.Errorf("can not base64 decode the body: %w", err)
}
return DecompressMessage(*compression, base64Decoded)
}
func (e *messageEncoder) decodeBody(attributes map[string]string, body []byte, out interface{}) error {
encoding := mdl.Unbox(GetEncodingAttribute(attributes), e.encoding)
return DecodeMessage(encoding, body, out)
}