-
Notifications
You must be signed in to change notification settings - Fork 218
/
write_message.go
129 lines (113 loc) · 3.01 KB
/
write_message.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
/*
Copyright 2023 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package mqtt_paho
import (
"bytes"
"context"
"io"
"github.com/cloudevents/sdk-go/v2/binding"
"github.com/cloudevents/sdk-go/v2/binding/format"
"github.com/cloudevents/sdk-go/v2/binding/spec"
"github.com/cloudevents/sdk-go/v2/types"
"github.com/eclipse/paho.golang/paho"
)
// WritePubMessage fills the provided pubMessage with the message m.
// Using context you can tweak the encoding processing (more details on binding.Write documentation).
func WritePubMessage(ctx context.Context, m binding.Message, pubMessage *paho.Publish, transformers ...binding.Transformer) error {
structuredWriter := (*pubMessageWriter)(pubMessage)
binaryWriter := (*pubMessageWriter)(pubMessage)
_, err := binding.Write(
ctx,
m,
structuredWriter,
binaryWriter,
transformers...,
)
return err
}
type pubMessageWriter paho.Publish
var (
_ binding.StructuredWriter = (*pubMessageWriter)(nil)
_ binding.BinaryWriter = (*pubMessageWriter)(nil)
)
func (b *pubMessageWriter) SetStructuredEvent(ctx context.Context, f format.Format, event io.Reader) error {
if b.Properties == nil {
b.Properties = &paho.PublishProperties{}
}
b.Properties.ContentType = f.MediaType()
var buf bytes.Buffer
_, err := io.Copy(&buf, event)
if err != nil {
return err
}
b.Payload = buf.Bytes()
return nil
}
func (b *pubMessageWriter) Start(ctx context.Context) error {
if b.Properties == nil {
b.Properties = &paho.PublishProperties{}
}
// the UserProperties of publish message is used to load event extensions
b.Properties.User = make([]paho.UserProperty, 0)
return nil
}
func (b *pubMessageWriter) End(ctx context.Context) error {
return nil
}
func (b *pubMessageWriter) SetData(reader io.Reader) error {
buf, ok := reader.(*bytes.Buffer)
if !ok {
buf = new(bytes.Buffer)
_, err := io.Copy(buf, reader)
if err != nil {
return err
}
}
b.Payload = buf.Bytes()
return nil
}
func (b *pubMessageWriter) SetAttribute(attribute spec.Attribute, value interface{}) error {
if attribute.Kind() == spec.DataContentType {
if value == nil {
b.Properties.ContentType = ""
}
s, err := types.Format(value)
if err != nil {
return err
}
b.Properties.ContentType = s
} else {
if value == nil {
b.removeProperty(prefix + attribute.Name())
}
return b.addProperty(prefix+attribute.Name(), value)
}
return nil
}
func (b *pubMessageWriter) SetExtension(name string, value interface{}) error {
if value == nil {
b.removeProperty(prefix + name)
}
return b.addProperty(prefix+name, value)
}
func (b *pubMessageWriter) removeProperty(key string) {
for i, v := range b.Properties.User {
if v.Key == key {
b.Properties.User = append(b.Properties.User[:i], b.Properties.User[i+1:]...)
break
}
}
}
func (b *pubMessageWriter) addProperty(key string, value interface{}) error {
s, err := types.Format(value)
if err != nil {
return err
}
b.Properties.User = append(b.Properties.User, paho.UserProperty{
Key: key,
Value: s,
})
return nil
}