From 854cdc8e82ebd968aff7f15ee0229b63a99a6631 Mon Sep 17 00:00:00 2001 From: Siyuan Miao Date: Wed, 10 Sep 2025 02:42:36 +0200 Subject: [PATCH] more proper type checking --- jsonrpc.go | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/jsonrpc.go b/jsonrpc.go index 47cc7960..e22aafd0 100644 --- a/jsonrpc.go +++ b/jsonrpc.go @@ -1111,28 +1111,38 @@ func rpcKeyboardReportMulti(ctx context.Context, macro []map[string]any) (usbgad } var modifier byte - if m, ok := step["modifier"].(float64); ok { + switch m := step["modifier"].(type) { + case uint8: + modifier = m + case int: + modifier = byte(m) + case float64: modifier = byte(int(m)) - } else if mi, ok := step["modifier"].(int); ok { - modifier = byte(mi) - } else if mb, ok := step["modifier"].(uint8); ok { - modifier = mb + default: + return last, fmt.Errorf("invalid modifier type: %T", m) } var keys []byte - if arr, ok := step["keys"].([]any); ok { + switch k := step["keys"].(type) { + case []byte: + keys = k + case []any: + arr := k keys = make([]byte, 0, len(arr)) for _, v := range arr { - if f, ok := v.(float64); ok { + switch f := v.(type) { + case uint8: + keys = append(keys, f) + case int: + keys = append(keys, byte(f)) + case float64: keys = append(keys, byte(int(f))) - } else if i, ok := v.(int); ok { - keys = append(keys, byte(i)) - } else if b, ok := v.(uint8); ok { - keys = append(keys, b) + default: + return last, fmt.Errorf("invalid key type: %T", f) } } - } else if bs, ok := step["keys"].([]byte); ok { - keys = bs + default: + return last, fmt.Errorf("invalid keys type: %T", k) } // Use context-aware sleep that can be cancelled