types.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2019 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  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. package config
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "strconv"
  19. "strings"
  20. )
  21. const (
  22. MB = 1024 * 1024
  23. KB = 1024
  24. )
  25. type BandwidthQuantity struct {
  26. s string // MB or KB
  27. i int64 // bytes
  28. }
  29. func NewBandwidthQuantity(s string) (BandwidthQuantity, error) {
  30. q := BandwidthQuantity{}
  31. err := q.UnmarshalString(s)
  32. if err != nil {
  33. return q, err
  34. }
  35. return q, nil
  36. }
  37. func MustBandwidthQuantity(s string) BandwidthQuantity {
  38. q := BandwidthQuantity{}
  39. err := q.UnmarshalString(s)
  40. if err != nil {
  41. panic(err)
  42. }
  43. return q
  44. }
  45. func (q *BandwidthQuantity) Equal(u *BandwidthQuantity) bool {
  46. if q == nil && u == nil {
  47. return true
  48. }
  49. if q != nil && u != nil {
  50. return q.i == u.i
  51. }
  52. return false
  53. }
  54. func (q *BandwidthQuantity) String() string {
  55. return q.s
  56. }
  57. func (q *BandwidthQuantity) UnmarshalString(s string) error {
  58. s = strings.TrimSpace(s)
  59. if s == "" {
  60. return nil
  61. }
  62. var (
  63. base int64
  64. f float64
  65. err error
  66. )
  67. switch {
  68. case strings.HasSuffix(s, "MB"):
  69. base = MB
  70. fstr := strings.TrimSuffix(s, "MB")
  71. f, err = strconv.ParseFloat(fstr, 64)
  72. if err != nil {
  73. return err
  74. }
  75. case strings.HasSuffix(s, "KB"):
  76. base = KB
  77. fstr := strings.TrimSuffix(s, "KB")
  78. f, err = strconv.ParseFloat(fstr, 64)
  79. if err != nil {
  80. return err
  81. }
  82. default:
  83. return errors.New("unit not support")
  84. }
  85. q.s = s
  86. q.i = int64(f * float64(base))
  87. return nil
  88. }
  89. func (q *BandwidthQuantity) UnmarshalJSON(b []byte) error {
  90. if len(b) == 4 && string(b) == "null" {
  91. return nil
  92. }
  93. var str string
  94. err := json.Unmarshal(b, &str)
  95. if err != nil {
  96. return err
  97. }
  98. return q.UnmarshalString(str)
  99. }
  100. func (q *BandwidthQuantity) MarshalJSON() ([]byte, error) {
  101. return []byte("\"" + q.s + "\""), nil
  102. }
  103. func (q *BandwidthQuantity) Bytes() int64 {
  104. return q.i
  105. }