From a23d83434e87f719ee65c6ee752c3ffad82e46d3 Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Wed, 14 May 2025 18:36:13 -0400 Subject: [PATCH 1/2] Support encoding comments by exposing the type Prior to this it was not possible to publish comments using the encoder --- codec_test.go | 3 +-- encoder.go | 4 ++-- encoder_test.go | 2 +- server.go | 8 +++----- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/codec_test.go b/codec_test.go index 9c5827c..6ed02f1 100644 --- a/codec_test.go +++ b/codec_test.go @@ -47,9 +47,8 @@ func TestEncodeComment(t *testing.T) { buf := new(bytes.Buffer) enc := NewEncoder(buf, false) text := "This is a comment" - comm := comment{value: "This is a comment"} expected := ":" + text + "\n" - if err := enc.Encode(comm); err != nil { + if err := enc.Encode(Comment(text)); err != nil { t.Fatal(err) } if buf.String() != expected { diff --git a/encoder.go b/encoder.go index a78df8a..df431c1 100644 --- a/encoder.go +++ b/encoder.go @@ -61,8 +61,8 @@ func (enc *Encoder) Encode(ec eventOrComment) error { if _, err := io.WriteString(enc.w, "\n"); err != nil { return fmt.Errorf("eventsource encode: %v", err) } - case comment: - line := ":" + item.value + "\n" + case Comment: + line := ":" + string(item) + "\n" if _, err := io.WriteString(enc.w, line); err != nil { return fmt.Errorf("eventsource encode: %v", err) } diff --git a/encoder_test.go b/encoder_test.go index c32e2dc..3dae6bc 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -60,7 +60,7 @@ func TestEncoderMultiLineData(t *testing.T) { func TestEncoderComment(t *testing.T) { buf := bytes.NewBuffer(nil) - c := comment{value: "hello"} + c := Comment("hello") NewEncoder(buf, false).Encode(c) assert.Equal(t, ":hello\n", string(buf.Bytes())) } diff --git a/server.go b/server.go index b2f9a89..331a611 100644 --- a/server.go +++ b/server.go @@ -31,9 +31,7 @@ type unregistration struct { forceDisconnect bool } -type comment struct { - value string -} +type Comment string type eventBatch struct { events <-chan Event @@ -243,10 +241,10 @@ func (srv *Server) PublishWithAcknowledgment(channels []string, ev Event) <-chan } // PublishComment publishes a comment to one or more channels. -func (srv *Server) PublishComment(channels []string, text string) { +func (srv *Server) PublishComment(channels []string, text Comment) { srv.pub <- &outbound{ channels: channels, - eventOrComment: comment{value: text}, + eventOrComment: text, } } From b5b7b03baa73ca965a420ede2566826dbfabdee3 Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Wed, 14 May 2025 18:40:19 -0400 Subject: [PATCH 2/2] don't break public interface --- server.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.go b/server.go index 331a611..600b0c0 100644 --- a/server.go +++ b/server.go @@ -241,10 +241,10 @@ func (srv *Server) PublishWithAcknowledgment(channels []string, ev Event) <-chan } // PublishComment publishes a comment to one or more channels. -func (srv *Server) PublishComment(channels []string, text Comment) { +func (srv *Server) PublishComment(channels []string, text string) { srv.pub <- &outbound{ channels: channels, - eventOrComment: text, + eventOrComment: Comment(text), } }