Skip to content

Commit 854cdc8

Browse files
committed
more proper type checking
1 parent f58e547 commit 854cdc8

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

jsonrpc.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,28 +1111,38 @@ func rpcKeyboardReportMulti(ctx context.Context, macro []map[string]any) (usbgad
11111111
}
11121112

11131113
var modifier byte
1114-
if m, ok := step["modifier"].(float64); ok {
1114+
switch m := step["modifier"].(type) {
1115+
case uint8:
1116+
modifier = m
1117+
case int:
1118+
modifier = byte(m)
1119+
case float64:
11151120
modifier = byte(int(m))
1116-
} else if mi, ok := step["modifier"].(int); ok {
1117-
modifier = byte(mi)
1118-
} else if mb, ok := step["modifier"].(uint8); ok {
1119-
modifier = mb
1121+
default:
1122+
return last, fmt.Errorf("invalid modifier type: %T", m)
11201123
}
11211124

11221125
var keys []byte
1123-
if arr, ok := step["keys"].([]any); ok {
1126+
switch k := step["keys"].(type) {
1127+
case []byte:
1128+
keys = k
1129+
case []any:
1130+
arr := k
11241131
keys = make([]byte, 0, len(arr))
11251132
for _, v := range arr {
1126-
if f, ok := v.(float64); ok {
1133+
switch f := v.(type) {
1134+
case uint8:
1135+
keys = append(keys, f)
1136+
case int:
1137+
keys = append(keys, byte(f))
1138+
case float64:
11271139
keys = append(keys, byte(int(f)))
1128-
} else if i, ok := v.(int); ok {
1129-
keys = append(keys, byte(i))
1130-
} else if b, ok := v.(uint8); ok {
1131-
keys = append(keys, b)
1140+
default:
1141+
return last, fmt.Errorf("invalid key type: %T", f)
11321142
}
11331143
}
1134-
} else if bs, ok := step["keys"].([]byte); ok {
1135-
keys = bs
1144+
default:
1145+
return last, fmt.Errorf("invalid keys type: %T", k)
11361146
}
11371147

11381148
// Use context-aware sleep that can be cancelled

0 commit comments

Comments
 (0)