-
Notifications
You must be signed in to change notification settings - Fork 220
/
opts.go
60 lines (53 loc) · 1.2 KB
/
opts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package miniredis
import (
"errors"
"math"
"strconv"
"time"
"github.com/alicebob/miniredis/v2/server"
)
// optInt parses an int option in a command.
// Writes "invalid integer" error to c if it's not a valid integer. Returns
// whether or not things were okay.
func optInt(c *server.Peer, src string, dest *int) bool {
return optIntErr(c, src, dest, msgInvalidInt)
}
func optIntErr(c *server.Peer, src string, dest *int, errMsg string) bool {
n, err := strconv.Atoi(src)
if err != nil {
setDirty(c)
c.WriteError(errMsg)
return false
}
*dest = n
return true
}
// optIntSimple sets dest or returns an error
func optIntSimple(src string, dest *int) error {
n, err := strconv.Atoi(src)
if err != nil {
return errors.New(msgInvalidInt)
}
*dest = n
return nil
}
func optDuration(c *server.Peer, src string, dest *time.Duration) bool {
n, err := strconv.ParseFloat(src, 64)
if err != nil {
setDirty(c)
c.WriteError(msgInvalidTimeout)
return false
}
if n < 0 {
setDirty(c)
c.WriteError(msgTimeoutNegative)
return false
}
if math.IsInf(n, 0) {
setDirty(c)
c.WriteError(msgTimeoutIsOutOfRange)
return false
}
*dest = time.Duration(n*1_000_000) * time.Microsecond
return true
}