diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 47cf1f5..0b14dbe 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -21,8 +21,9 @@ jobs: go-version: "^1" - name: Setup golangci-lint - uses: golangci/golangci-lint-action@v6 + uses: golangci/golangci-lint-action@v7 with: + version: v2.0 args: --verbose - name: Bearer diff --git a/.golangci.yml b/.golangci.yml index 4c44c5f..47094ac 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,3 +1,50 @@ +version: "2" linters: - disable: + default: none + enable: + - bodyclose + - dogsled + - dupl - errcheck + - exhaustive + - gochecknoinits + - goconst + - gocritic + - gocyclo + - goprintffuncname + - gosec + - govet + - ineffassign + - lll + - misspell + - nakedret + - noctx + - nolintlint + - rowserrcheck + - staticcheck + - unconvert + - unparam + - unused + - whitespace + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + paths: + - third_party$ + - builtin$ + - examples$ +formatters: + enable: + - gofmt + - gofumpt + - goimports + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/sse-decoder.go b/sse-decoder.go index fd49b9c..8ca065b 100644 --- a/sse-decoder.go +++ b/sse-decoder.go @@ -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 } } diff --git a/sse-encoder.go b/sse-encoder.go index 0d26c82..9ebb49f 100644 --- a/sse-encoder.go +++ b/sse-encoder.go @@ -20,8 +20,10 @@ import ( const ContentType = "text/event-stream;charset=utf-8" -var contentType = []string{ContentType} -var noCache = []string{"no-cache"} +var ( + contentType = []string{ContentType} + noCache = []string{"no-cache"} +) var fieldReplacer = strings.NewReplacer( "\n", "\\n", @@ -48,48 +50,48 @@ func Encode(writer io.Writer, event Event) error { func writeId(w stringWriter, id string) { if len(id) > 0 { - w.WriteString("id:") - fieldReplacer.WriteString(w, id) - w.WriteString("\n") + _, _ = w.WriteString("id:") + _, _ = fieldReplacer.WriteString(w, id) + _, _ = w.WriteString("\n") } } func writeEvent(w stringWriter, event string) { if len(event) > 0 { - w.WriteString("event:") - fieldReplacer.WriteString(w, event) - w.WriteString("\n") + _, _ = w.WriteString("event:") + _, _ = fieldReplacer.WriteString(w, event) + _, _ = w.WriteString("\n") } } func writeRetry(w stringWriter, retry uint) { if retry > 0 { - w.WriteString("retry:") - w.WriteString(strconv.FormatUint(uint64(retry), 10)) - w.WriteString("\n") + _, _ = w.WriteString("retry:") + _, _ = w.WriteString(strconv.FormatUint(uint64(retry), 10)) + _, _ = w.WriteString("\n") } } func writeData(w stringWriter, data interface{}) error { - w.WriteString("data:") + _, _ = w.WriteString("data:") bData, ok := data.([]byte) if ok { - dataReplacer.WriteString(w, string(bData)) - w.WriteString("\n\n") + _, _ = dataReplacer.WriteString(w, string(bData)) + _, _ = w.WriteString("\n\n") return nil } - switch kindOfData(data) { + switch kindOfData(data) { //nolint:exhaustive case reflect.Struct, reflect.Slice, reflect.Map: err := json.NewEncoder(w).Encode(data) if err != nil { return err } - w.WriteString("\n") + _, _ = w.WriteString("\n") default: - dataReplacer.WriteString(w, fmt.Sprint(data)) - w.WriteString("\n\n") + _, _ = dataReplacer.WriteString(w, fmt.Sprint(data)) + _, _ = w.WriteString("\n\n") } return nil } diff --git a/sse_test.go b/sse_test.go index 553ba29..6df102d 100644 --- a/sse_test.go +++ b/sse_test.go @@ -170,22 +170,25 @@ func TestEncodeFloat(t *testing.T) { func TestEncodeStream(t *testing.T) { w := new(bytes.Buffer) - Encode(w, Event{ + _ = Encode(w, Event{ Event: "float", Data: 1.5, }) - Encode(w, Event{ + _ = Encode(w, Event{ Id: "123", Data: map[string]interface{}{"foo": "bar", "bar": "foo"}, }) - Encode(w, Event{ + _ = Encode(w, Event{ Id: "124", Event: "chat", Data: "hi! dude", }) - assert.Equal(t, w.String(), "event:float\ndata:1.5\n\nid:123\ndata:{\"bar\":\"foo\",\"foo\":\"bar\"}\n\nid:124\nevent:chat\ndata:hi! dude\n\n") + assert.Equal(t, w.String(), + "event:float\ndata:1.5\n\n"+ + "id:123\ndata:{\"bar\":\"foo\",\"foo\":\"bar\"}\n\n"+ + "id:124\nevent:chat\ndata:hi! dude\n\n") } func TestRenderSSE(t *testing.T) { @@ -207,7 +210,7 @@ func BenchmarkResponseWriter(b *testing.B) { b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { - (Event{ + _ = (Event{ Event: "new_message", Data: "hi! how are you? I am fine. this is a long stupid message!!!", }).Render(w) @@ -219,7 +222,7 @@ func BenchmarkFullSSE(b *testing.B) { b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { - Encode(buf, Event{ + _ = Encode(buf, Event{ Event: "new_message", Id: "13435", Retry: 10, @@ -234,7 +237,7 @@ func BenchmarkNoRetrySSE(b *testing.B) { b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { - Encode(buf, Event{ + _ = Encode(buf, Event{ Event: "new_message", Id: "13435", Data: "hi! how are you? I am fine. this is a long stupid message!!!", @@ -248,7 +251,7 @@ func BenchmarkSimpleSSE(b *testing.B) { b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { - Encode(buf, Event{ + _ = Encode(buf, Event{ Event: "new_message", Data: "hi! how are you? I am fine. this is a long stupid message!!!", })