Skip to content

Commit

Permalink
Fix lint warn
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Dec 13, 2024
1 parent 5569352 commit 1d9096c
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 17 deletions.
22 changes: 19 additions & 3 deletions capture/runbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 5 additions & 1 deletion grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions include_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 15 additions & 3 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
Expand All @@ -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))
Expand Down
18 changes: 15 additions & 3 deletions testutil/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,35 @@ 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 {
n, ok := r.Message["name"]
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 {
n, ok := r.Message["name"]
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 }).
Expand Down
6 changes: 5 additions & 1 deletion testutil/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 1d9096c

Please sign in to comment.