2
0

chore: improve linting and error handling across the codebase

- Update golangci-lint-action to version 7 in GitHub workflow
- Specify version 2.0 for golangci-lint-action in GitHub workflow
- Set golangci-lint configuration version to "2"
- Enable specific linters and disable default linters in golangci configuration
- Add exclusions and formatters configurations in golangci configuration
- Replace deprecated `ioutil.ReadAll` with `io.ReadAll` in sse-decoder.go
- Use grouped variable declaration for `contentType` and `noCache` in sse-encoder.go
- Add error handling for `WriteString` operations in sse-encoder.go
- Add `nolint:exhaustive` comment for `kindOfData` switch statement in sse-encoder.go
- Adjust test assertions for better readability in sse_test.go
- Add error handling for `Encode` function calls in tests and benchmarks

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu
2025-04-08 08:38:00 +08:00
parent 47d1560405
commit c719ab6b53
5 changed files with 87 additions and 33 deletions
+6 -5
View File
@@ -7,7 +7,6 @@ package sse
import (
"bytes"
"io"
"io/ioutil"
)
type decoder struct {
@@ -22,7 +21,8 @@ func Decode(r io.Reader) ([]Event, error) {
func (d *decoder) dispatchEvent(event Event, data string) {
dataLength := len(data)
if dataLength > 0 {
//If the data buffer's last character is a U+000A LINE FEED (LF) character, then remove the last character from the data buffer.
// If the data buffer's last character is a U+000A LINE FEED (LF) character,
// then remove the last character from the data buffer.
data = data[:dataLength-1]
dataLength--
}
@@ -37,7 +37,7 @@ func (d *decoder) dispatchEvent(event Event, data string) {
}
func (d *decoder) decode(r io.Reader) ([]Event, error) {
buf, err := ioutil.ReadAll(r)
buf, err := io.ReadAll(r)
if err != nil {
return nil, err
}
@@ -96,7 +96,8 @@ func (d *decoder) decode(r io.Reader) ([]Event, error) {
currentEvent.Id = string(value)
case "retry":
// If the field value consists of only characters in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9),
// then interpret the field value as an integer in base ten, and set the event stream's reconnection time to that integer.
// then interpret the field value as an integer in base ten, and set the event stream's
// reconnection time to that integer.
// Otherwise, ignore the field.
currentEvent.Id = string(value)
case "data":
@@ -105,7 +106,7 @@ func (d *decoder) decode(r io.Reader) ([]Event, error) {
// then append a single U+000A LINE FEED (LF) character to the data buffer.
dataBuffer.WriteString("\n")
default:
//Otherwise. The field is ignored.
// Otherwise. The field is ignored.
continue
}
}