Debian Codegraph @

pkg/ffuf/optrange.go

raw symbols SCIP
1 package ffuf
2
3 import (
4 "encoding/json"
5 "fmt"
6 "strconv"
7 "strings"
8 )
9
10 //optRange stores either a single float, in which case the value is stored in min and IsRange is false,
11 //or a range of floats, in which case IsRange is true
12 type optRange struct {
13 Min float64
14 Max float64
15 IsRange bool
16 HasDelay bool
17 }
18
19 type optRangeJSON struct {
20 Value string `json:"value"`
21 }
22
23 func (o *optRange) MarshalJSON() ([]byte, error) {
24 value := ""
25 if o.Min == o.Max {
26 value = fmt.Sprintf("%.2f", o.Min)
27 } else {
28 value = fmt.Sprintf("%.2f-%.2f", o.Min, o.Max)
29 }
30 return json.Marshal(&optRangeJSON{
31 Value: value,
32 })
33 }
34
35 func (o *optRange) UnmarshalJSON(b []byte) error {
36 var inc optRangeJSON
37 err := json.Unmarshal(b, &inc)
38 if err != nil {
39 return err
40 }
41 return o.Initialize(inc.Value)
42 }
43
44 //Initialize sets up the optRange from string value
45 func (o *optRange) Initialize(value string) error {
46 var err, err2 error
47 d := strings.Split(value, "-")
48 if len(d) > 2 {
49 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\"")
50 } else if len(d) == 2 {
51 o.IsRange = true
52 o.HasDelay = true
53 o.Min, err = strconv.ParseFloat(d[0], 64)
54 o.Max, err2 = strconv.ParseFloat(d[1], 64)
55 if err != nil || err2 != nil {
56 return fmt.Errorf("Delay range min and max values need to be valid floats. For example: 0.1-0.5")
57 }
58 } else if len(value) > 0 {
59 o.IsRange = false
60 o.HasDelay = true
61 o.Min, err = strconv.ParseFloat(value, 64)
62 if err != nil {
63 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\"")
64 }
65 }
66 return nil
67 }