| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package types |
| 16 |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "errors" |
| 20 | "fmt" |
| 21 | "strconv" |
| 22 | "strings" |
| 23 | ) |
| 24 |
| 25 | const ( |
| 26 | MB = 1024 * 1024 |
| 27 | KB = 1024 |
| 28 |
| 29 | BandwidthLimitModeClient = "client" |
| 30 | BandwidthLimitModeServer = "server" |
| 31 | ) |
| 32 |
| 33 | type BandwidthQuantity struct { |
| 34 | s string // MB or KB |
| 35 |
| 36 | i int64 // bytes |
| 37 | } |
| 38 |
| 39 | func NewBandwidthQuantity(s string) (BandwidthQuantity, error) { |
| 40 | q := BandwidthQuantity{} |
| 41 | err := q.UnmarshalString(s) |
| 42 | if err != nil { |
| 43 | return q, err |
| 44 | } |
| 45 | return q, nil |
| 46 | } |
| 47 |
| 48 | func (q *BandwidthQuantity) Equal(u *BandwidthQuantity) bool { |
| 49 | if q == nil && u == nil { |
| 50 | return true |
| 51 | } |
| 52 | if q != nil && u != nil { |
| 53 | return q.i == u.i |
| 54 | } |
| 55 | return false |
| 56 | } |
| 57 |
| 58 | func (q *BandwidthQuantity) String() string { |
| 59 | return q.s |
| 60 | } |
| 61 |
| 62 | func (q *BandwidthQuantity) UnmarshalString(s string) error { |
| 63 | s = strings.TrimSpace(s) |
| 64 | if s == "" { |
| 65 | return nil |
| 66 | } |
| 67 |
| 68 | var ( |
| 69 | base int64 |
| 70 | f float64 |
| 71 | err error |
| 72 | ) |
| 73 | if fstr, ok := strings.CutSuffix(s, "MB"); ok { |
| 74 | base = MB |
| 75 | f, err = strconv.ParseFloat(fstr, 64) |
| 76 | } else if fstr, ok := strings.CutSuffix(s, "KB"); ok { |
| 77 | base = KB |
| 78 | f, err = strconv.ParseFloat(fstr, 64) |
| 79 | } else { |
| 80 | return errors.New("unit not support") |
| 81 | } |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 |
| 86 | q.s = s |
| 87 | q.i = int64(f * float64(base)) |
| 88 | return nil |
| 89 | } |
| 90 |
| 91 | func (q *BandwidthQuantity) UnmarshalJSON(b []byte) error { |
| 92 | if len(b) == 4 && string(b) == "null" { |
| 93 | return nil |
| 94 | } |
| 95 |
| 96 | var str string |
| 97 | err := json.Unmarshal(b, &str) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 |
| 102 | return q.UnmarshalString(str) |
| 103 | } |
| 104 |
| 105 | func (q *BandwidthQuantity) MarshalJSON() ([]byte, error) { |
| 106 | return []byte("\"" + q.s + "\""), nil |
| 107 | } |
| 108 |
| 109 | func (q *BandwidthQuantity) Bytes() int64 { |
| 110 | return q.i |
| 111 | } |
| 112 |
| 113 | type PortsRange struct { |
| 114 | Start int `json:"start,omitempty"` |
| 115 | End int `json:"end,omitempty"` |
| 116 | Single int `json:"single,omitempty"` |
| 117 | } |
| 118 |
| 119 | type PortsRangeSlice []PortsRange |
| 120 |
| 121 | func (p PortsRangeSlice) String() string { |
| 122 | if len(p) == 0 { |
| 123 | return "" |
| 124 | } |
| 125 | strs := []string{} |
| 126 | for _, v := range p { |
| 127 | if v.Single > 0 { |
| 128 | strs = append(strs, strconv.Itoa(v.Single)) |
| 129 | } else { |
| 130 | strs = append(strs, strconv.Itoa(v.Start)+"-"+strconv.Itoa(v.End)) |
| 131 | } |
| 132 | } |
| 133 | return strings.Join(strs, ",") |
| 134 | } |
| 135 |
| 136 | // the format of str is like "1000-2000,3000,4000-5000" |
| 137 | func NewPortsRangeSliceFromString(str string) ([]PortsRange, error) { |
| 138 | str = strings.TrimSpace(str) |
| 139 | out := []PortsRange{} |
| 140 | numRanges := strings.SplitSeq(str, ",") |
| 141 | for numRangeStr := range numRanges { |
| 142 | // 1000-2000 or 2001 |
| 143 | numArray := strings.Split(numRangeStr, "-") |
| 144 | // length: only 1 or 2 is correct |
| 145 | rangeType := len(numArray) |
| 146 | switch rangeType { |
| 147 | case 1: |
| 148 | // single number |
| 149 | singleNum, err := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64) |
| 150 | if err != nil { |
| 151 | return nil, fmt.Errorf("range number is invalid, %v", err) |
| 152 | } |
| 153 | out = append(out, PortsRange{Single: int(singleNum)}) |
| 154 | case 2: |
| 155 | // range numbers |
| 156 | minNum, err := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64) |
| 157 | if err != nil { |
| 158 | return nil, fmt.Errorf("range number is invalid, %v", err) |
| 159 | } |
| 160 | maxNum, err := strconv.ParseInt(strings.TrimSpace(numArray[1]), 10, 64) |
| 161 | if err != nil { |
| 162 | return nil, fmt.Errorf("range number is invalid, %v", err) |
| 163 | } |
| 164 | if maxNum < minNum { |
| 165 | return nil, fmt.Errorf("range number is invalid") |
| 166 | } |
| 167 | out = append(out, PortsRange{Start: int(minNum), End: int(maxNum)}) |
| 168 | default: |
| 169 | return nil, fmt.Errorf("range number is invalid") |
| 170 | } |
| 171 | } |
| 172 | return out, nil |
| 173 | } |