Skip to content

Commit

Permalink
chore(deps): replace deprecated io/ioutil
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Nov 3, 2023
1 parent e7581a2 commit b186107
Show file tree
Hide file tree
Showing 9 changed files with 2,432 additions and 2,437 deletions.
8 changes: 4 additions & 4 deletions api/http/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
//
// Service can be obtained from client using HTTPService() method.
// It can be also created directly. To instantiate a Service use NewService(). Remember, the authorization param is in form "Token your-auth-token". e.g. "Token DXnd7annkGteV5Wqx9G3YjO9Ezkw87nHk8OabcyHCxF5451kdBV0Ag2cG7OmZZgCUTHroagUPdxbuoyen6TSPw==".
// srv := http.NewService("http://localhost:8086", "Token my-token", http.DefaultOptions())
//
// srv := http.NewService("http://localhost:8086", "Token my-token", http.DefaultOptions())
package http

import (
"context"
"encoding/json"
"io"
"io/ioutil"
"mime"
"net/http"
"net/url"
Expand Down Expand Up @@ -145,7 +145,7 @@ func (s *service) parseHTTPError(r *http.Response) *Error {
}
defer func() {
// discard body so connection can be reused
_, _ = io.Copy(ioutil.Discard, r.Body)
_, _ = io.Copy(io.Discard, r.Body)
_ = r.Body.Close()
}()

Expand All @@ -164,7 +164,7 @@ func (s *service) parseHTTPError(r *http.Response) *Error {
if ctype == "application/json" {
perror.Err = json.NewDecoder(r.Body).Decode(perror)
} else {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
perror.Err = err
return perror
Expand Down
28 changes: 13 additions & 15 deletions api/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -50,20 +49,19 @@ const (
// The name of a struct field or a map key (must be a string) will be a param name.
// The name of the parameter represented by a struct field can be specified by JSON annotation:
//
// type Condition struct {
// Start time.Time `json:"start"`
// Field string `json:"field"`
// Value float64 `json:"value"`
// }
// type Condition struct {
// Start time.Time `json:"start"`
// Field string `json:"field"`
// Value float64 `json:"value"`
// }
//
// Parameters are then accessed via the Flux params object:
//
// query:= `from(bucket: "environment")
// |> range(start: time(v: params.start))
// |> filter(fn: (r) => r._measurement == "air")
// |> filter(fn: (r) => r._field == params.field)
// |> filter(fn: (r) => r._value > params.value)`
// Parameters are then accessed via the Flux params object:
//
// query:= `from(bucket: "environment")
// |> range(start: time(v: params.start))
// |> filter(fn: (r) => r._measurement == "air")
// |> filter(fn: (r) => r._field == params.field)
// |> filter(fn: (r) => r._value > params.value)`
type QueryAPI interface {
// QueryRaw executes flux query on the InfluxDB server and returns complete query result as a string with table annotations according to dialect
QueryRaw(ctx context.Context, query string, dialect *domain.Dialect) (string, error)
Expand Down Expand Up @@ -113,7 +111,7 @@ type queryAPI struct {
lock sync.Mutex
}

// queryBody holds the body for an HTTP query request.
// queryBody holds the body for an HTTP query request.
type queryBody struct {
Dialect *domain.Dialect `json:"dialect,omitempty"`
Query string `json:"query"`
Expand Down Expand Up @@ -158,7 +156,7 @@ func (q *queryAPI) QueryRawWithParams(ctx context.Context, query string, dialect
return err
}
}
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
61 changes: 31 additions & 30 deletions api/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package api
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestQueryCVSResultSingleTable(t *testing.T) {
)

reader := strings.NewReader(csvTable)
queryResult := NewQueryTableResult(ioutil.NopCloser(reader))
queryResult := NewQueryTableResult(io.NopCloser(reader))
require.True(t, queryResult.Next(), queryResult.Err())
require.Nil(t, queryResult.Err())

Expand Down Expand Up @@ -307,7 +307,7 @@ func TestQueryCVSResultMultiTables(t *testing.T) {
)

reader := strings.NewReader(csvTable)
queryResult := NewQueryTableResult(ioutil.NopCloser(reader))
queryResult := NewQueryTableResult(io.NopCloser(reader))
assert.Equal(t, -1, queryResult.TablePosition())
require.True(t, queryResult.Next(), queryResult.Err())
require.Nil(t, queryResult.Err())
Expand Down Expand Up @@ -439,7 +439,7 @@ func TestQueryCVSResultSingleTableMultiColumnsNoValue(t *testing.T) {
)

reader := strings.NewReader(csvTable)
queryResult := NewQueryTableResult(ioutil.NopCloser(reader))
queryResult := NewQueryTableResult(io.NopCloser(reader))
require.True(t, queryResult.Next(), queryResult.Err())
require.Nil(t, queryResult.Err())

Expand All @@ -462,7 +462,8 @@ func TestQueryCVSResultSingleTableMultiColumnsNoValue(t *testing.T) {
}

func TestQueryRawResult(t *testing.T) {
csvRows := []string{`#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string,string,string,string`,
csvRows := []string{
`#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string,string,string,string`,
`#group,false,false,true,true,false,false,true,true,true,true`,
`#default,_result,,,,,,,,,`,
`,result,table,_start,_stop,_time,_value,_field,_measurement,a,b`,
Expand All @@ -482,12 +483,12 @@ func TestQueryRawResult(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-time.After(100 * time.Millisecond)
if r.Method == http.MethodPost {
rbody, _ := ioutil.ReadAll(r.Body)
rbody, _ := io.ReadAll(r.Body)
fmt.Printf("Req: %s\n", string(rbody))
body, err := gzip.CompressWithGzip(strings.NewReader(csvTable))
if err == nil {
var bytes []byte
bytes, err = ioutil.ReadAll(body)
bytes, err = io.ReadAll(body)
if err == nil {
w.Header().Set("Content-Type", "text/csv")
w.Header().Set("Content-Encoding", "gzip")
Expand All @@ -510,7 +511,6 @@ func TestQueryRawResult(t *testing.T) {
require.Nil(t, err)
require.NotNil(t, result)
assert.Equal(t, csvTable, result)

}

func TestErrorInRow(t *testing.T) {
Expand All @@ -519,10 +519,11 @@ func TestErrorInRow(t *testing.T) {
`#group,true,true`,
`#default,,`,
`,error,reference`,
`,failed to create physical plan: invalid time bounds from procedure from: bounds contain zero time,897`}
`,failed to create physical plan: invalid time bounds from procedure from: bounds contain zero time,897`,
}
csvTable := makeCSVstring(csvRowsError)
reader := strings.NewReader(csvTable)
queryResult := NewQueryTableResult(ioutil.NopCloser(reader))
queryResult := NewQueryTableResult(io.NopCloser(reader))

require.False(t, queryResult.Next())
require.NotNil(t, queryResult.Err())
Expand All @@ -533,10 +534,11 @@ func TestErrorInRow(t *testing.T) {
`#group,true,true`,
`#default,,`,
`,error,reference`,
`,failed to create physical plan: invalid time bounds from procedure from: bounds contain zero time,`}
`,failed to create physical plan: invalid time bounds from procedure from: bounds contain zero time,`,
}
csvTable = makeCSVstring(csvRowsErrorNoReference)
reader = strings.NewReader(csvTable)
queryResult = NewQueryTableResult(ioutil.NopCloser(reader))
queryResult = NewQueryTableResult(io.NopCloser(reader))

require.False(t, queryResult.Next())
require.NotNil(t, queryResult.Err())
Expand All @@ -547,10 +549,11 @@ func TestErrorInRow(t *testing.T) {
`#group,true,true`,
`#default,,`,
`,error,reference`,
`,,`}
`,,`,
}
csvTable = makeCSVstring(csvRowsErrorNoMessage)
reader = strings.NewReader(csvTable)
queryResult = NewQueryTableResult(ioutil.NopCloser(reader))
queryResult = NewQueryTableResult(io.NopCloser(reader))

require.False(t, queryResult.Next())
require.NotNil(t, queryResult.Err())
Expand All @@ -567,7 +570,7 @@ func TestInvalidDataType(t *testing.T) {
`

reader := strings.NewReader(csvTable)
queryResult := NewQueryTableResult(ioutil.NopCloser(reader))
queryResult := NewQueryTableResult(io.NopCloser(reader))
require.False(t, queryResult.Next())
require.NotNil(t, queryResult.Err())
assert.Equal(t, "deviceId has unknown data type int", queryResult.Err().Error())
Expand Down Expand Up @@ -627,7 +630,7 @@ func TestReorderedAnnotations(t *testing.T) {
`
reader := strings.NewReader(csvTable1)
queryResult := NewQueryTableResult(ioutil.NopCloser(reader))
queryResult := NewQueryTableResult(io.NopCloser(reader))
require.True(t, queryResult.Next(), queryResult.Err())
require.Nil(t, queryResult.Err())

Expand All @@ -654,7 +657,7 @@ func TestReorderedAnnotations(t *testing.T) {
`
reader = strings.NewReader(csvTable2)
queryResult = NewQueryTableResult(ioutil.NopCloser(reader))
queryResult = NewQueryTableResult(io.NopCloser(reader))
require.True(t, queryResult.Next(), queryResult.Err())
require.Nil(t, queryResult.Err())

Expand Down Expand Up @@ -725,7 +728,7 @@ func TestDatatypeOnlyAnnotation(t *testing.T) {
`
reader := strings.NewReader(csvTable1)
queryResult := NewQueryTableResult(ioutil.NopCloser(reader))
queryResult := NewQueryTableResult(io.NopCloser(reader))
require.True(t, queryResult.Next(), queryResult.Err())
require.Nil(t, queryResult.Err())

Expand Down Expand Up @@ -754,7 +757,7 @@ func TestMissingDatatypeAnnotation(t *testing.T) {
`

reader := strings.NewReader(csvTable1)
queryResult := NewQueryTableResult(ioutil.NopCloser(reader))
queryResult := NewQueryTableResult(io.NopCloser(reader))
require.False(t, queryResult.Next())
require.NotNil(t, queryResult.Err())
assert.Equal(t, "parsing error, datatype annotation not found", queryResult.Err().Error())
Expand All @@ -768,7 +771,7 @@ func TestMissingDatatypeAnnotation(t *testing.T) {
`

reader = strings.NewReader(csvTable2)
queryResult = NewQueryTableResult(ioutil.NopCloser(reader))
queryResult = NewQueryTableResult(io.NopCloser(reader))
require.False(t, queryResult.Next())
require.NotNil(t, queryResult.Err())
assert.Equal(t, "parsing error, datatype annotation not found", queryResult.Err().Error())
Expand All @@ -782,7 +785,7 @@ func TestMissingAnnotations(t *testing.T) {
`
reader := strings.NewReader(csvTable3)
queryResult := NewQueryTableResult(ioutil.NopCloser(reader))
queryResult := NewQueryTableResult(io.NopCloser(reader))
require.False(t, queryResult.Next())
require.NotNil(t, queryResult.Err())
assert.Equal(t, "parsing error, annotations not found", queryResult.Err().Error())
Expand All @@ -797,7 +800,7 @@ func TestDifferentNumberOfColumns(t *testing.T) {
`

reader := strings.NewReader(csvTable)
queryResult := NewQueryTableResult(ioutil.NopCloser(reader))
queryResult := NewQueryTableResult(io.NopCloser(reader))
require.False(t, queryResult.Next())
require.NotNil(t, queryResult.Err())
assert.Equal(t, "parsing error, row has different number of columns than the table: 11 vs 10", queryResult.Err().Error())
Expand All @@ -810,7 +813,7 @@ func TestDifferentNumberOfColumns(t *testing.T) {
`

reader = strings.NewReader(csvTable2)
queryResult = NewQueryTableResult(ioutil.NopCloser(reader))
queryResult = NewQueryTableResult(io.NopCloser(reader))
require.False(t, queryResult.Next())
require.NotNil(t, queryResult.Err())
assert.Equal(t, "parsing error, row has different number of columns than the table: 8 vs 10", queryResult.Err().Error())
Expand All @@ -823,7 +826,7 @@ func TestDifferentNumberOfColumns(t *testing.T) {
`

reader = strings.NewReader(csvTable3)
queryResult = NewQueryTableResult(ioutil.NopCloser(reader))
queryResult = NewQueryTableResult(io.NopCloser(reader))
require.False(t, queryResult.Next())
require.NotNil(t, queryResult.Err())
assert.Equal(t, "parsing error, row has different number of columns than the table: 10 vs 8", queryResult.Err().Error())
Expand All @@ -840,7 +843,7 @@ func TestEmptyValue(t *testing.T) {
`

reader := strings.NewReader(csvTable)
queryResult := NewQueryTableResult(ioutil.NopCloser(reader))
queryResult := NewQueryTableResult(io.NopCloser(reader))

require.True(t, queryResult.Next(), queryResult.Err())
require.Nil(t, queryResult.Err())
Expand All @@ -864,7 +867,7 @@ func TestFluxError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-time.After(100 * time.Millisecond)
if r.Method == http.MethodPost {
_, _ = ioutil.ReadAll(r.Body)
_, _ = io.ReadAll(r.Body)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"code":"invalid","message":"compilation failed: loc 4:17-4:86: expected an operator between two expressions"}`))
Expand All @@ -882,12 +885,11 @@ func TestFluxError(t *testing.T) {
assert.Nil(t, tableRes)
require.NotNil(t, err)
assert.Equal(t, "invalid: compilation failed: loc 4:17-4:86: expected an operator between two expressions", err.Error())

}

func TestQueryParamsTypes(t *testing.T) {
var i int8 = 1
var paramsTypeTests = []struct {
paramsTypeTests := []struct {
testName string
params interface{}
expectError string
Expand Down Expand Up @@ -1020,7 +1022,7 @@ func TestQueryParamsSerialized(t *testing.T) {
expectedBody := `{"dialect":{"annotations":["datatype","group","default"],"delimiter":",","header":true},"query":"from(bucket: \"environment\") |\u003e range(start: time(v: params.start)) |\u003e filter(fn: (r) =\u003e r._measurement == \"air\") |\u003e filter(fn: (r) =\u003e r._field == params.field) |\u003e filter(fn: (r) =\u003e r._value \u003e params.value)","type":"flux","params":{"start":"2022-02-17T11:27:23+01:00","field":"field","value":24.4}}`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -1058,7 +1060,6 @@ func TestQueryParamsSerialized(t *testing.T) {

_, err = queryAPI.QueryWithParams(context.Background(), query, condition)
require.NoError(t, err, err)

}

func makeCSVstring(rows []string) string {
Expand Down
Loading

0 comments on commit b186107

Please sign in to comment.