Browse Source

refactor: fix deprecated/unreachable codes (#1849)

* refactor: replace ioutil.ReadFile with os.ReadFile

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* refactor: remove rand.Seed

`rand.Seed` has been deprecated since Go 1.20 and the rand pkg is seeded randomly at program startup.

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* refactor: fix bad syntax for struct tag value

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* refactor: remove empty branch

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* refactor: remove unreachable code

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* refactor: remove TestEntry

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* refactor: remove *dynamic.Message case

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* refactor: replace grpc.WithInsecure with grpc.WithTransportCredentials

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

---------

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>
Jason Lyu 1 year atrás
parent
commit
d55bfade32

+ 4 - 3
docs/en_US/guide/ai/python_tensorflow_lite_tutorial.md

@@ -229,9 +229,10 @@ package main
 
 import (
 	"fmt"
-	mqtt "github.com/eclipse/paho.mqtt.golang"
-	"io/ioutil"
+	"os"
 	"time"
+
+	mqtt "github.com/eclipse/paho.mqtt.golang"
 )
 
 func main() {
@@ -249,7 +250,7 @@ func main() {
 	}
 	for _, image := range images {
 		fmt.Println("Publishing " + image)
-		payload, err := ioutil.ReadFile(image)
+		payload, err := os.ReadFile(image)
 		if err != nil {
 			fmt.Println(err)
 			continue

+ 4 - 3
docs/en_US/guide/ai/tensorflow_lite_tutorial.md

@@ -185,9 +185,10 @@ package main
 
 import (
 	"fmt"
-	mqtt "github.com/eclipse/paho.mqtt.golang"
-	"io/ioutil"
+	"os"
 	"time"
+
+	mqtt "github.com/eclipse/paho.mqtt.golang"
 )
 
 func main() {
@@ -205,7 +206,7 @@ func main() {
 	}
 	for _, image := range images {
 		fmt.Println("Publishing " + image)
-		payload, err := ioutil.ReadFile(image)
+		payload, err := os.ReadFile(image)
 		if err != nil {
 			fmt.Println(err)
 			continue

+ 4 - 3
docs/zh_CN/guide/ai/python_tensorflow_lite_tutorial.md

@@ -223,9 +223,10 @@ package main
 
 import (
 	"fmt"
-	mqtt "github.com/eclipse/paho.mqtt.golang"
-	"io/ioutil"
+	"os"
 	"time"
+
+	mqtt "github.com/eclipse/paho.mqtt.golang"
 )
 
 func main() {
@@ -243,7 +244,7 @@ func main() {
 	}
 	for _, image := range images {
 		fmt.Println("Publishing " + image)
-		payload, err := ioutil.ReadFile(image)
+		payload, err := os.ReadFile(image)
 		if err != nil {
 			fmt.Println(err)
 			continue

+ 4 - 3
docs/zh_CN/guide/ai/tensorflow_lite_tutorial.md

@@ -183,9 +183,10 @@ package main
 
 import (
 	"fmt"
-	mqtt "github.com/eclipse/paho.mqtt.golang"
-	"io/ioutil"
+	"os"
 	"time"
+
+	mqtt "github.com/eclipse/paho.mqtt.golang"
 )
 
 func main() {
@@ -203,7 +204,7 @@ func main() {
 	}
 	for _, image := range images {
 		fmt.Println("Publishing " + image)
-		payload, err := ioutil.ReadFile(image)
+		payload, err := os.ReadFile(image)
 		if err != nil {
 			fmt.Println(err)
 			continue

+ 7 - 6
extensions/functions/image/resize_test.go

@@ -1,4 +1,4 @@
-// Copyright 2022 EMQ Technologies Co., Ltd.
+// Copyright 2022-2023 EMQ Technologies Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -15,13 +15,14 @@
 package main
 
 import (
+	"os"
+	"reflect"
+	"testing"
+
 	"github.com/lf-edge/ekuiper/internal/conf"
 	kctx "github.com/lf-edge/ekuiper/internal/topo/context"
 	"github.com/lf-edge/ekuiper/internal/topo/state"
 	"github.com/lf-edge/ekuiper/pkg/api"
-	"io/ioutil"
-	"reflect"
-	"testing"
 )
 
 func TestResize(t *testing.T) {
@@ -39,12 +40,12 @@ func TestResize(t *testing.T) {
 		},
 	}
 	for i, tt := range tests {
-		payload, err := ioutil.ReadFile(tt.image)
+		payload, err := os.ReadFile(tt.image)
 		if err != nil {
 			t.Errorf("Failed to read image file %s", tt.image)
 			continue
 		}
-		resized, err := ioutil.ReadFile(tt.result)
+		resized, err := os.ReadFile(tt.result)
 		if err != nil {
 			t.Errorf("Failed to read result image file %s", tt.result)
 			continue

+ 2 - 12
internal/binder/factory_test.go

@@ -15,22 +15,12 @@
 package binder
 
 import (
-	"github.com/lf-edge/ekuiper/internal/binder/mock"
 	"reflect"
 	"sort"
 	"testing"
-)
 
-func TestEntry(t *testing.T) {
-	m := mock.NewMockFactory()
-	e := &FactoryEntry{
-		Name:    "mock",
-		Factory: m,
-	}
-	if e == nil {
-		t.Errorf("cannot instantiate FactoryEntry")
-	}
-}
+	"github.com/lf-edge/ekuiper/internal/binder/mock"
+)
 
 func TestEntriesSort(t *testing.T) {
 	m := mock.NewMockFactory()

+ 4 - 4
internal/compressor/compressor_test.go

@@ -17,14 +17,14 @@ package compressor
 import (
 	"bytes"
 	"fmt"
-	"io/ioutil"
+	"os"
 	"testing"
 )
 
 func BenchmarkCompressor(b *testing.B) {
 	compressors := []string{ZLIB, GZIP, FLATE, ZSTD}
 
-	data, err := ioutil.ReadFile("test.json")
+	data, err := os.ReadFile("test.json")
 	if err != nil {
 		b.Fatalf("failed to read test file: %v", err)
 	}
@@ -55,7 +55,7 @@ func BenchmarkCompressor(b *testing.B) {
 func BenchmarkDecompressor(b *testing.B) {
 	compressors := []string{ZLIB, GZIP, FLATE, ZSTD}
 
-	data, err := ioutil.ReadFile("test.json")
+	data, err := os.ReadFile("test.json")
 	if err != nil {
 		b.Fatalf("failed to read test file: %v", err)
 	}
@@ -92,7 +92,7 @@ func BenchmarkDecompressor(b *testing.B) {
 
 func TestCompressionRatio(t *testing.T) {
 	// Load JSON file
-	data, err := ioutil.ReadFile("test.json")
+	data, err := os.ReadFile("test.json")
 	if err != nil {
 		t.Fatalf("failed to read test file: %v", err)
 	}

+ 2 - 2
internal/converter/protobuf/fieldConverterSingleton.go

@@ -16,10 +16,12 @@ package protobuf
 
 import (
 	"fmt"
+
 	"github.com/golang/protobuf/proto"
 	dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
 	"github.com/jhump/protoreflect/desc"
 	"github.com/jhump/protoreflect/dynamic"
+
 	"github.com/lf-edge/ekuiper/pkg/cast"
 )
 
@@ -299,8 +301,6 @@ func (fc *FieldConverter) decodeSubMessage(input interface{}, ft *desc.MessageDe
 			return nil, err
 		}
 		return fc.DecodeMessage(message, ft), nil
-	case *dynamic.Message:
-		return fc.DecodeMessage(v, ft), nil
 	default:
 		return nil, fmt.Errorf("cannot decode %[1]T(%[1]v) to map", input)
 	}

+ 0 - 1
internal/pkg/httpx/http.go

@@ -129,7 +129,6 @@ func convertToMap(v interface{}, sendSingle bool) (map[string]interface{}, error
 	default:
 		return nil, fmt.Errorf("invalid content: %v", v)
 	}
-	return nil, fmt.Errorf("invalid content: %v", v)
 }
 
 func IsValidUrl(uri string) bool {

+ 5 - 3
internal/service/executors.go

@@ -31,12 +31,14 @@ import (
 	"github.com/golang/protobuf/proto"
 	"github.com/jhump/protoreflect/dynamic"
 	"github.com/jhump/protoreflect/dynamic/grpcdynamic"
+	"github.com/ugorji/go/codec"
+	"google.golang.org/grpc"
+	"google.golang.org/grpc/credentials/insecure"
+
 	"github.com/lf-edge/ekuiper/internal/pkg/httpx"
 	"github.com/lf-edge/ekuiper/pkg/api"
 	"github.com/lf-edge/ekuiper/pkg/cast"
 	"github.com/lf-edge/ekuiper/pkg/infra"
-	"github.com/ugorji/go/codec"
-	"google.golang.org/grpc"
 )
 
 // NewExecutor
@@ -122,7 +124,7 @@ func (d *grpcExecutor) InvokeFunction(_ api.FunctionContext, name string, params
 		)
 		go infra.SafeRun(func() error {
 			defer cancel()
-			conn, e = grpc.DialContext(dialCtx, d.addr.Host, grpc.WithInsecure(), grpc.WithBlock())
+			conn, e = grpc.DialContext(dialCtx, d.addr.Host, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
 			return e
 		})
 

+ 6 - 6
internal/xsql/parser.go

@@ -16,15 +16,17 @@ package xsql
 
 import (
 	"fmt"
-	"github.com/golang-collections/collections/stack"
-	"github.com/lf-edge/ekuiper/internal/binder/function"
-	"github.com/lf-edge/ekuiper/pkg/ast"
-	"github.com/lf-edge/ekuiper/pkg/message"
 	"io"
 	"math"
 	"reflect"
 	"strconv"
 	"strings"
+
+	"github.com/golang-collections/collections/stack"
+
+	"github.com/lf-edge/ekuiper/internal/binder/function"
+	"github.com/lf-edge/ekuiper/pkg/ast"
+	"github.com/lf-edge/ekuiper/pkg/message"
 )
 
 type Parser struct {
@@ -1391,8 +1393,6 @@ func (p *Parser) parseStreamArrayType() (ast.FieldType, error) {
 		} else {
 			return nil, fmt.Errorf("found %q, expect stream data types.", lit1)
 		}
-	} else {
-
 	}
 	return nil, nil
 }

+ 2 - 4
test/plugins/service/server.go

@@ -22,7 +22,6 @@ import (
 	"log"
 	"math/rand"
 	"net/http"
-	"time"
 )
 
 func alert(w http.ResponseWriter, req *http.Request) {
@@ -40,8 +39,8 @@ func alert(w http.ResponseWriter, req *http.Request) {
 var count = 0
 
 type Sensor struct {
-	Temperature int `json: "temperature""`
-	Humidity    int `json: "humidity"`
+	Temperature int `json:"temperature"`
+	Humidity    int `json:"humidity"`
 }
 
 var s = &Sensor{}
@@ -57,7 +56,6 @@ func pullSrv(w http.ResponseWriter, req *http.Request) {
 	}
 
 	if count%2 == 0 {
-		rand.Seed(time.Now().UnixNano())
 		s.Temperature = rand.Intn(100)
 		s.Humidity = rand.Intn(100)
 	}