types.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. if strings.HasSuffix(s, "MB") {
  68. base = MB
  69. fstr := strings.TrimSuffix(s, "MB")
  70. f, err = strconv.ParseFloat(fstr, 64)
  71. if err != nil {
  72. return err
  73. }
  74. } else if strings.HasSuffix(s, "KB") {
  75. base = KB
  76. fstr := strings.TrimSuffix(s, "KB")
  77. f, err = strconv.ParseFloat(fstr, 64)
  78. if err != nil {
  79. return err
  80. }
  81. } else {
  82. return errors.New("unit not support")
  83. }
  84. q.s = s
  85. q.i = int64(f * float64(base))
  86. return nil
  87. }
  88. func (q *BandwidthQuantity) UnmarshalJSON(b []byte) error {
  89. if len(b) == 4 && string(b) == "null" {
  90. return nil
  91. }
  92. var str string
  93. err := json.Unmarshal(b, &str)
  94. if err != nil {
  95. return err
  96. }
  97. return q.UnmarshalString(str)
  98. }
  99. func (q *BandwidthQuantity) MarshalJSON() ([]byte, error) {
  100. return []byte("\"" + q.s + "\""), nil
  101. }
  102. func (q *BandwidthQuantity) Bytes() int64 {
  103. return q.i
  104. }