From e425b2f5fefa38c022c65fabd091c237e755bf84 Mon Sep 17 00:00:00 2001 From: 180909 <734461790@qq.com> Date: Tue, 11 Jun 2024 02:01:06 +0800 Subject: [PATCH] Remove esutil ioutil import --- esutil/bulk_indexer_benchmark_test.go | 4 ++-- esutil/bulk_indexer_internal_test.go | 23 +++++++++++------------ esutil/json_reader.go | 4 +--- esutil/json_reader_benchmark_test.go | 5 ++--- esutil/json_reader_internal_test.go | 5 ++--- 5 files changed, 18 insertions(+), 23 deletions(-) diff --git a/esutil/bulk_indexer_benchmark_test.go b/esutil/bulk_indexer_benchmark_test.go index 8ec356cedf..3158340cb8 100644 --- a/esutil/bulk_indexer_benchmark_test.go +++ b/esutil/bulk_indexer_benchmark_test.go @@ -23,7 +23,7 @@ package esutil_test import ( "bytes" "context" - "io/ioutil" + "io" "net/http" "strconv" "strings" @@ -55,7 +55,7 @@ var mockResponseBody = `{ type mockTransp struct{} func (t *mockTransp) RoundTrip(req *http.Request) (*http.Response, error) { - return &http.Response{Body: ioutil.NopCloser(strings.NewReader(mockResponseBody))}, nil // 1x alloc + return &http.Response{Body: io.NopCloser(strings.NewReader(mockResponseBody))}, nil // 1x alloc } func BenchmarkBulkIndexer(b *testing.B) { diff --git a/esutil/bulk_indexer_internal_test.go b/esutil/bulk_indexer_internal_test.go index e78621e39d..12acddf0df 100644 --- a/esutil/bulk_indexer_internal_test.go +++ b/esutil/bulk_indexer_internal_test.go @@ -26,7 +26,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "log" "net/http" "os" @@ -46,7 +45,7 @@ import ( ) var defaultRoundTripFunc = func(*http.Request) (*http.Response, error) { - return &http.Response{Body: ioutil.NopCloser(strings.NewReader(`{}`))}, nil + return &http.Response{Body: io.NopCloser(strings.NewReader(`{}`))}, nil } type mockTransport struct { @@ -81,9 +80,9 @@ func TestBulkIndexer(t *testing.T) { case 3: testfile = "testdata/bulk_response_1c.json" } - bodyContent, _ := ioutil.ReadFile(testfile) + bodyContent, _ := os.ReadFile(testfile) return &http.Response{ - Body: ioutil.NopCloser(bytes.NewBuffer(bodyContent)), + Body: io.NopCloser(bytes.NewBuffer(bodyContent)), Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}}, }, nil }, @@ -261,13 +260,13 @@ func TestBulkIndexer(t *testing.T) { numItems = 4 numFailed = 2 - bodyContent, _ = ioutil.ReadFile("testdata/bulk_response_2.json") + bodyContent, _ = os.ReadFile("testdata/bulk_response_2.json") ) es, _ := elasticsearch.NewClient(elasticsearch.Config{Transport: &mockTransport{ RoundTripFunc: func(*http.Request) (*http.Response, error) { return &http.Response{ - Body: ioutil.NopCloser(bytes.NewBuffer(bodyContent)), + Body: io.NopCloser(bytes.NewBuffer(bodyContent)), Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}}, }, nil }, @@ -283,7 +282,7 @@ func TestBulkIndexer(t *testing.T) { successFunc := func(ctx context.Context, item BulkIndexerItem, res BulkIndexerResponseItem) { atomic.AddUint64(&countSuccessful, 1) - buf, err := ioutil.ReadAll(item.Body) + buf, err := io.ReadAll(item.Body) if err != nil { t.Fatalf("Unexpected error: %s", err) } @@ -293,7 +292,7 @@ func TestBulkIndexer(t *testing.T) { atomic.AddUint64(&countFailed, 1) failedIDs = append(failedIDs, item.DocumentID) - buf, err := ioutil.ReadAll(item.Body) + buf, err := io.ReadAll(item.Body) if err != nil { t.Fatalf("Unexpected error: %s", err) } @@ -436,7 +435,7 @@ func TestBulkIndexer(t *testing.T) { return &http.Response{ StatusCode: http.StatusOK, Status: "200 OK", - Body: ioutil.NopCloser(strings.NewReader(`{"items":[{"index": {}}]}`)), + Body: io.NopCloser(strings.NewReader(`{"items":[{"index": {}}]}`)), Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}}, }, nil }, @@ -499,14 +498,14 @@ func TestBulkIndexer(t *testing.T) { return &http.Response{ StatusCode: http.StatusTooManyRequests, Status: "429 TooManyRequests", - Body: ioutil.NopCloser(strings.NewReader(`{"took":1}`)), + Body: io.NopCloser(strings.NewReader(`{"took":1}`)), }, nil } - bodyContent, _ := ioutil.ReadFile("testdata/bulk_response_1c.json") + bodyContent, _ := os.ReadFile("testdata/bulk_response_1c.json") return &http.Response{ StatusCode: http.StatusOK, Status: "200 OK", - Body: ioutil.NopCloser(bytes.NewBuffer(bodyContent)), + Body: io.NopCloser(bytes.NewBuffer(bodyContent)), Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}}, }, nil }, diff --git a/esutil/json_reader.go b/esutil/json_reader.go index 36673a4ca4..0051fe1216 100644 --- a/esutil/json_reader.go +++ b/esutil/json_reader.go @@ -67,9 +67,7 @@ func (r *JSONReader) WriteTo(w io.Writer) (int64, error) { return int64(cw.n), err } -func (r *JSONReader) encode(w io.Writer) error { - var err error - +func (r *JSONReader) encode(w io.Writer) (err error) { if e, ok := r.val.(JSONEncoder); ok { err = e.EncodeJSON(w) if err != nil { diff --git a/esutil/json_reader_benchmark_test.go b/esutil/json_reader_benchmark_test.go index f9caa9e927..8794815827 100644 --- a/esutil/json_reader_benchmark_test.go +++ b/esutil/json_reader_benchmark_test.go @@ -25,7 +25,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "strings" "testing" @@ -71,7 +70,7 @@ func BenchmarkJSONReader(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - out, _ := ioutil.ReadAll(esutil.NewJSONReader(map[string]string{"foo": "bar"})) + out, _ := io.ReadAll(esutil.NewJSONReader(map[string]string{"foo": "bar"})) if string(out) != `{"foo":"bar"}`+"\n" { b.Fatalf("Unexpected output: %q", out) } @@ -95,7 +94,7 @@ func BenchmarkJSONReader(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - out, _ := ioutil.ReadAll(esutil.NewJSONReader(Foo{Bar: "baz"})) + out, _ := io.ReadAll(esutil.NewJSONReader(Foo{Bar: "baz"})) if string(out) != `{"bar":"BAZ"}`+"\n" { b.Fatalf("Unexpected output: %q", out) } diff --git a/esutil/json_reader_internal_test.go b/esutil/json_reader_internal_test.go index 6b3841f0c5..ca6f9d0031 100644 --- a/esutil/json_reader_internal_test.go +++ b/esutil/json_reader_internal_test.go @@ -24,7 +24,6 @@ import ( "bytes" "errors" "io" - "io/ioutil" "strings" "testing" ) @@ -49,14 +48,14 @@ func (f Foo) EncodeJSON(w io.Writer) error { func TestJSONReader(t *testing.T) { t.Run("Default", func(t *testing.T) { - out, _ := ioutil.ReadAll(NewJSONReader(map[string]string{"foo": "bar"})) + out, _ := io.ReadAll(NewJSONReader(map[string]string{"foo": "bar"})) if string(out) != `{"foo":"bar"}`+"\n" { t.Fatalf("Unexpected output: %s", out) } }) t.Run("Custom", func(t *testing.T) { - out, _ := ioutil.ReadAll(NewJSONReader(Foo{Bar: "baz"})) + out, _ := io.ReadAll(NewJSONReader(Foo{Bar: "baz"})) if string(out) != `{"bar":"BAZ"}`+"\n" { t.Fatalf("Unexpected output: %s", out) }