Skip to content

Commit 59239fc

Browse files
committed
Support encoding comments by exposing the type
Prior to this it was not possible to publish comments using the encoder
1 parent 4af8c72 commit 59239fc

File tree

4 files changed

+7
-10
lines changed

4 files changed

+7
-10
lines changed

codec_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ func TestEncodeComment(t *testing.T) {
4747
buf := new(bytes.Buffer)
4848
enc := NewEncoder(buf, false)
4949
text := "This is a comment"
50-
comm := comment{value: "This is a comment"}
5150
expected := ":" + text + "\n"
52-
if err := enc.Encode(comm); err != nil {
51+
if err := enc.Encode(Comment(text)); err != nil {
5352
t.Fatal(err)
5453
}
5554
if buf.String() != expected {

encoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func (enc *Encoder) Encode(ec eventOrComment) error {
6161
if _, err := io.WriteString(enc.w, "\n"); err != nil {
6262
return fmt.Errorf("eventsource encode: %v", err)
6363
}
64-
case comment:
65-
line := ":" + item.value + "\n"
64+
case Comment:
65+
line := ":" + string(item) + "\n"
6666
if _, err := io.WriteString(enc.w, line); err != nil {
6767
return fmt.Errorf("eventsource encode: %v", err)
6868
}

encoder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestEncoderMultiLineData(t *testing.T) {
6060

6161
func TestEncoderComment(t *testing.T) {
6262
buf := bytes.NewBuffer(nil)
63-
c := comment{value: "hello"}
63+
c := Comment("hello")
6464
NewEncoder(buf, false).Encode(c)
6565
assert.Equal(t, ":hello\n", string(buf.Bytes()))
6666
}

server.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ type unregistration struct {
3131
forceDisconnect bool
3232
}
3333

34-
type comment struct {
35-
value string
36-
}
34+
type Comment string
3735

3836
type eventBatch struct {
3937
events <-chan Event
@@ -243,10 +241,10 @@ func (srv *Server) PublishWithAcknowledgment(channels []string, ev Event) <-chan
243241
}
244242

245243
// PublishComment publishes a comment to one or more channels.
246-
func (srv *Server) PublishComment(channels []string, text string) {
244+
func (srv *Server) PublishComment(channels []string, text Comment) {
247245
srv.pub <- &outbound{
248246
channels: channels,
249-
eventOrComment: comment{value: text},
247+
eventOrComment: text,
250248
}
251249
}
252250

0 commit comments

Comments
 (0)