/
optrange.go
67 lines (61 loc) · 1.63 KB
/
optrange.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
61
62
63
64
65
66
67
package ffuf
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
//optRange stores either a single float, in which case the value is stored in min and IsRange is false,
//or a range of floats, in which case IsRange is true
type optRange struct {
Min float64
Max float64
IsRange bool
HasDelay bool
}
type optRangeJSON struct {
Value string `json:"value"`
}
func (o *optRange) MarshalJSON() ([]byte, error) {
value := ""
if o.Min == o.Max {
value = fmt.Sprintf("%.2f", o.Min)
} else {
value = fmt.Sprintf("%.2f-%.2f", o.Min, o.Max)
}
return json.Marshal(&optRangeJSON{
Value: value,
})
}
func (o *optRange) UnmarshalJSON(b []byte) error {
var inc optRangeJSON
err := json.Unmarshal(b, &inc)
if err != nil {
return err
}
return o.Initialize(inc.Value)
}
//Initialize sets up the optRange from string value
func (o *optRange) Initialize(value string) error {
var err, err2 error
d := strings.Split(value, "-")
if len(d) > 2 {
return fmt.Errorf("Delay needs to be either a single float: \"0.1\" or a range of floats, delimited by dash: \"0.1-0.8\"")
} else if len(d) == 2 {
o.IsRange = true
o.HasDelay = true
o.Min, err = strconv.ParseFloat(d[0], 64)
o.Max, err2 = strconv.ParseFloat(d[1], 64)
if err != nil || err2 != nil {
return fmt.Errorf("Delay range min and max values need to be valid floats. For example: 0.1-0.5")
}
} else if len(value) > 0 {
o.IsRange = false
o.HasDelay = true
o.Min, err = strconv.ParseFloat(value, 64)
if err != nil {
return fmt.Errorf("Delay needs to be either a single float: \"0.1\" or a range of floats, delimited by dash: \"0.1-0.8\"")
}
}
return nil
}