From 1d9096cc1cbe783a3344ce2f20682da9a130fdf9 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Fri, 13 Dec 2024 14:51:02 +0900 Subject: [PATCH] Fix lint warn --- capture/runbook.go | 22 +++++++++++++++++++--- grpc.go | 6 +++++- http.go | 15 ++++++++++++--- include_test.go | 12 ++++++++++-- operator.go | 6 +++++- parse.go | 18 +++++++++++++++--- testutil/grpc.go | 18 +++++++++++++++--- testutil/port.go | 6 +++++- 8 files changed, 86 insertions(+), 17 deletions(-) diff --git a/capture/runbook.go b/capture/runbook.go index b52e0249..a5cd8b51 100644 --- a/capture/runbook.go +++ b/capture/runbook.go @@ -476,7 +476,11 @@ func (c *cRunbook) setRunner(name string, value any) { } exist := false for _, rnr := range r.Runners { - if rnr.Key.(string) == name { + n, ok := rnr.Key.(string) + if !ok { + continue + } + if n == name { exist = true } } @@ -585,11 +589,23 @@ func (r *runbook) replaceLatestStep(rep yaml.MapSlice) { } func headersAndMessages(step yaml.MapSlice) yaml.MapSlice { - return step[0].Value.(yaml.MapSlice)[0].Value.(yaml.MapSlice) + s, ok := step[0].Value.(yaml.MapSlice) + if !ok { + return nil + } + ss, ok := s[0].Value.(yaml.MapSlice) + if !ok { + return nil + } + return ss } func replaceHeadersAndMessages(step, hb yaml.MapSlice) yaml.MapSlice { - step[0].Value.(yaml.MapSlice)[0].Value = hb + s, ok := step[0].Value.(yaml.MapSlice) + if !ok { + return nil + } + s[0].Value = hb return step } diff --git a/grpc.go b/grpc.go index bfed5e25..6b070a95 100644 --- a/grpc.go +++ b/grpc.go @@ -696,7 +696,11 @@ L: rnr.refc = nil d[grpcStoreMessagesKey] = messages - if h, err := stream.Header(); len(d[grpcStoreHeaderKey].(metadata.MD)) == 0 && err == nil { + mmd, ok := d[grpcStoreHeaderKey].(metadata.MD) + if !ok { + return fmt.Errorf("failed to cast headers: %s", d[grpcStoreHeaderKey]) + } + if h, err := stream.Header(); len(mmd) == 0 && err == nil { d[grpcStoreHeaderKey] = h } t, ok := dcopy(stream.Trailer()).(metadata.MD) diff --git a/http.go b/http.go index 732c497e..4bcaf74e 100644 --- a/http.go +++ b/http.go @@ -83,11 +83,16 @@ func newHTTPRunner(name, endpoint string) (*httpRunner, error) { if err != nil { return nil, err } + tp, ok := http.DefaultTransport.(*http.Transport) + if !ok { + return nil, fmt.Errorf("failed to cast: %v", http.DefaultTransport) + } + return &httpRunner{ name: name, endpoint: u, client: &http.Client{ - Transport: http.DefaultTransport.(*http.Transport).Clone(), + Transport: tp.Clone(), Timeout: time.Second * 30, }, validator: newNopValidator(), @@ -172,7 +177,7 @@ func (r *httpRequest) encodeBody() (io.Reader, error) { case string: return strings.NewReader(v), nil case []byte: - return bytes.NewBuffer(r.body.([]byte)), nil + return bytes.NewBuffer(v), nil case []any: arr, ok := r.body.([]any) if !ok { @@ -419,7 +424,11 @@ func (rnr *httpRunner) run(ctx context.Context, r *httpRequest, s *step) error { switch { case rnr.client != nil: if rnr.client.Transport == nil { - rnr.client.Transport = http.DefaultTransport.(*http.Transport).Clone() + tp, ok := http.DefaultTransport.(*http.Transport) + if !ok { + return fmt.Errorf("failed to cast: %v", http.DefaultTransport) + } + rnr.client.Transport = tp.Clone() } if ts, ok := rnr.client.Transport.(*http.Transport); ok { existingConfig := ts.TLSClientConfig diff --git a/include_test.go b/include_test.go index c3df701c..e351a4e3 100644 --- a/include_test.go +++ b/include_test.go @@ -45,7 +45,11 @@ func TestIncludeRunnerRun(t *testing.T) { } } { - got := len(o.store.steps[0]["steps"].([]map[string]any)) + steps, ok := o.store.steps[0]["steps"].([]map[string]any) + if !ok { + t.Errorf("failed to cast: %v", o.store.steps[0]["steps"]) + } + got := len(steps) if got != tt.want { t.Errorf("got %v\nwant %v", got, tt.want) } @@ -60,7 +64,11 @@ func TestIncludeRunnerRun(t *testing.T) { } } { - got := len(o.store.steps[0]["vars"].(map[string]any)) + vars, ok := o.store.steps[0]["vars"].(map[string]any) + if !ok { + t.Errorf("failed to cast: %v", o.store.steps[0]["vars"]) + } + got := len(vars) if want := len(tt.vars); got != want { t.Errorf("got %v\nwant %v", got, want) } diff --git a/operator.go b/operator.go index 8ab3ede6..9c7af32c 100644 --- a/operator.go +++ b/operator.go @@ -523,7 +523,11 @@ func New(opts ...Option) (*operator, error) { } } if len(hostRules) > 0 { - v.client.Transport.(*http.Transport).DialContext = hostRules.dialContextFunc() + tp, ok := v.client.Transport.(*http.Transport) + if !ok { + return nil, fmt.Errorf("failed to cast: %v", v.client.Transport) + } + tp.DialContext = hostRules.dialContextFunc() } op.httpRunners[k] = v } diff --git a/parse.go b/parse.go index 29209793..b2ec914c 100644 --- a/parse.go +++ b/parse.go @@ -51,7 +51,11 @@ func parseHTTPRequest(v map[string]any) (*httpRequest, error) { req.headers.Add(k, v) case []any: for _, vv := range v { - req.headers.Add(k, vv.(string)) + svv, ok := vv.(string) + if !ok { + return nil, fmt.Errorf("invalid request: %s", string(part)) + } + req.headers.Add(k, svv) } default: return nil, fmt.Errorf("invalid request: %s", string(part)) @@ -157,7 +161,11 @@ func parseGrpcRequest(v map[string]any, expand func(any) (any, error)) (*grpcReq if err != nil { return nil, err } - svc, mth, err := parseServiceAndMethod(pe.(string)) + pes, ok := pe.(string) + if !ok { + return nil, fmt.Errorf("invalid request: %s", string(part)) + } + svc, mth, err := parseServiceAndMethod(pes) if err != nil { return nil, err } @@ -183,7 +191,11 @@ func parseGrpcRequest(v map[string]any, expand func(any) (any, error)) (*grpcReq req.headers.Append(k, v) case []any: for _, vv := range v { - req.headers.Append(k, vv.(string)) + svv, ok := vv.(string) + if !ok { + return nil, fmt.Errorf("invalid request: %s", string(part)) + } + req.headers.Append(k, svv) } default: return nil, fmt.Errorf("invalid request: %s", string(part)) diff --git a/testutil/grpc.go b/testutil/grpc.go index 4fe80657..babb6a58 100644 --- a/testutil/grpc.go +++ b/testutil/grpc.go @@ -116,7 +116,11 @@ func GRPCServer(t *testing.T, useTLS bool, disableReflection bool) *grpcstub.Ser if !ok { return false } - return n.(string) == "alice" + ns, ok := n.(string) + if !ok { + return false + } + return ns == "alice" }).Header("hellochat", "header").Trailer("hellochat", "trailer"). ResponseString(`{"message":"hello", "num":34, "create_time":"2022-06-25T05:24:46.382783Z"}`) ts.Method("grpctest.GrpcTestService/HelloChat").Match(func(r *grpcstub.Request) bool { @@ -124,7 +128,11 @@ func GRPCServer(t *testing.T, useTLS bool, disableReflection bool) *grpcstub.Ser if !ok { return false } - return n.(string) == "bob" + ns, ok := n.(string) + if !ok { + return false + } + return ns == "bob" }).Header("hellochat-second", "header").Trailer("hellochat-second", "trailer"). ResponseString(`{"message":"hello", "num":35, "create_time":"2022-06-25T05:24:47.382783Z"}`) ts.Method("grpctest.GrpcTestService/HelloChat").Match(func(r *grpcstub.Request) bool { @@ -132,7 +140,11 @@ func GRPCServer(t *testing.T, useTLS bool, disableReflection bool) *grpcstub.Ser if !ok { return false } - return n.(string) == "charlie" + ns, ok := n.(string) + if !ok { + return false + } + return ns == "charlie" }).Header("hellochat-third", "header").Trailer("hellochat-second", "trailer"). ResponseString(`{"message":"hello", "num":36, "create_time":"2022-06-25T05:24:48.382783Z"}`) ts.Method("grpctest.GrpcTestService/HelloFields").Match(func(r *grpcstub.Request) bool { return true }). diff --git a/testutil/port.go b/testutil/port.go index 72bcc7b8..bd41cb5b 100644 --- a/testutil/port.go +++ b/testutil/port.go @@ -12,5 +12,9 @@ func NewPort(t testing.TB) int { t.Fatal(err) } defer l.Close() - return l.Addr().(*net.TCPAddr).Port + addr, ok := l.Addr().(*net.TCPAddr) + if !ok { + t.Fatal("invalid addr") + } + return addr.Port }