Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
xJack21 committed Mar 17, 2024
1 parent ecbea09 commit d60eaf6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
26 changes: 20 additions & 6 deletions event.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sseread

import (
"bytes"
"encoding/json"
"strings"
)
Expand Down Expand Up @@ -33,11 +32,26 @@ func (e *Event) ParseEventLine(lineType string, lineData []byte) {
case "retry":
e.ID = string(lineData) // If the event type is "retry", update the Retry field.
case "data":
if bytes.EqualFold(lineData, []byte("[DONE]")) {
e.Data = nil
} else {
e.Data = lineData // If the event type is "data", update the Data field.
}
e.Data = lineData // If the event type is "data", update the Data field.

}

}

// IsSkip is a method of the Event struct that checks if the event should be skipped.
// It returns true if the Data field of the event is empty, null, undefined, or "[DONE]".
// Otherwise, it returns false.
func (e *Event) IsSkip() bool {
// Check if the Data field is empty.
if len(e.Data) == 0 {
return true
}
// Convert the Data field to a string and remove leading and trailing spaces.
str := strings.TrimSpace(string(e.Data))
// Check if the Data field is "", "null", "undefined", or "[DONE]".
if str == "" || str == "null" || str == "undefined" || str == "[DONE]" {
return true
}
// If none of the above conditions are met, return false.
return false
}
2 changes: 1 addition & 1 deletion sseread_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestReadFromCloudflareLama2(t *testing.T) {

// Iterate over the events from the channel
for event := range channel {
if event == nil {
if event == nil || event.IsSkip() {
continue
}

Expand Down

0 comments on commit d60eaf6

Please sign in to comment.