http urls monitor.

convert.go 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2015 go-swagger maintainers
  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 swag
  15. import (
  16. "math"
  17. "strconv"
  18. "strings"
  19. )
  20. // same as ECMA Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER
  21. const (
  22. maxJSONFloat = float64(1<<53 - 1) // 9007199254740991.0 2^53 - 1
  23. minJSONFloat = -float64(1<<53 - 1) //-9007199254740991.0 -2^53 - 1
  24. epsilon float64 = 1e-9
  25. )
  26. // IsFloat64AJSONInteger allow for integers [-2^53, 2^53-1] inclusive
  27. func IsFloat64AJSONInteger(f float64) bool {
  28. if math.IsNaN(f) || math.IsInf(f, 0) || f < minJSONFloat || f > maxJSONFloat {
  29. return false
  30. }
  31. fa := math.Abs(f)
  32. g := float64(uint64(f))
  33. ga := math.Abs(g)
  34. diff := math.Abs(f - g)
  35. // more info: https://floating-point-gui.de/errors/comparison/#look-out-for-edge-cases
  36. if f == g { // best case
  37. return true
  38. } else if f == float64(int64(f)) || f == float64(uint64(f)) { // optimistic case
  39. return true
  40. } else if f == 0 || g == 0 || diff < math.SmallestNonzeroFloat64 { // very close to 0 values
  41. return diff < (epsilon * math.SmallestNonzeroFloat64)
  42. }
  43. // check the relative error
  44. return diff/math.Min(fa+ga, math.MaxFloat64) < epsilon
  45. }
  46. var evaluatesAsTrue = map[string]struct{}{
  47. "true": {},
  48. "1": {},
  49. "yes": {},
  50. "ok": {},
  51. "y": {},
  52. "on": {},
  53. "selected": {},
  54. "checked": {},
  55. "t": {},
  56. "enabled": {},
  57. }
  58. // ConvertBool turn a string into a boolean
  59. func ConvertBool(str string) (bool, error) {
  60. _, ok := evaluatesAsTrue[strings.ToLower(str)]
  61. return ok, nil
  62. }
  63. // ConvertFloat32 turn a string into a float32
  64. func ConvertFloat32(str string) (float32, error) {
  65. f, err := strconv.ParseFloat(str, 32)
  66. if err != nil {
  67. return 0, err
  68. }
  69. return float32(f), nil
  70. }
  71. // ConvertFloat64 turn a string into a float64
  72. func ConvertFloat64(str string) (float64, error) {
  73. return strconv.ParseFloat(str, 64)
  74. }
  75. // ConvertInt8 turn a string into int8 boolean
  76. func ConvertInt8(str string) (int8, error) {
  77. i, err := strconv.ParseInt(str, 10, 8)
  78. if err != nil {
  79. return 0, err
  80. }
  81. return int8(i), nil
  82. }
  83. // ConvertInt16 turn a string into a int16
  84. func ConvertInt16(str string) (int16, error) {
  85. i, err := strconv.ParseInt(str, 10, 16)
  86. if err != nil {
  87. return 0, err
  88. }
  89. return int16(i), nil
  90. }
  91. // ConvertInt32 turn a string into a int32
  92. func ConvertInt32(str string) (int32, error) {
  93. i, err := strconv.ParseInt(str, 10, 32)
  94. if err != nil {
  95. return 0, err
  96. }
  97. return int32(i), nil
  98. }
  99. // ConvertInt64 turn a string into a int64
  100. func ConvertInt64(str string) (int64, error) {
  101. return strconv.ParseInt(str, 10, 64)
  102. }
  103. // ConvertUint8 turn a string into a uint8
  104. func ConvertUint8(str string) (uint8, error) {
  105. i, err := strconv.ParseUint(str, 10, 8)
  106. if err != nil {
  107. return 0, err
  108. }
  109. return uint8(i), nil
  110. }
  111. // ConvertUint16 turn a string into a uint16
  112. func ConvertUint16(str string) (uint16, error) {
  113. i, err := strconv.ParseUint(str, 10, 16)
  114. if err != nil {
  115. return 0, err
  116. }
  117. return uint16(i), nil
  118. }
  119. // ConvertUint32 turn a string into a uint32
  120. func ConvertUint32(str string) (uint32, error) {
  121. i, err := strconv.ParseUint(str, 10, 32)
  122. if err != nil {
  123. return 0, err
  124. }
  125. return uint32(i), nil
  126. }
  127. // ConvertUint64 turn a string into a uint64
  128. func ConvertUint64(str string) (uint64, error) {
  129. return strconv.ParseUint(str, 10, 64)
  130. }
  131. // FormatBool turns a boolean into a string
  132. func FormatBool(value bool) string {
  133. return strconv.FormatBool(value)
  134. }
  135. // FormatFloat32 turns a float32 into a string
  136. func FormatFloat32(value float32) string {
  137. return strconv.FormatFloat(float64(value), 'f', -1, 32)
  138. }
  139. // FormatFloat64 turns a float64 into a string
  140. func FormatFloat64(value float64) string {
  141. return strconv.FormatFloat(value, 'f', -1, 64)
  142. }
  143. // FormatInt8 turns an int8 into a string
  144. func FormatInt8(value int8) string {
  145. return strconv.FormatInt(int64(value), 10)
  146. }
  147. // FormatInt16 turns an int16 into a string
  148. func FormatInt16(value int16) string {
  149. return strconv.FormatInt(int64(value), 10)
  150. }
  151. // FormatInt32 turns an int32 into a string
  152. func FormatInt32(value int32) string {
  153. return strconv.Itoa(int(value))
  154. }
  155. // FormatInt64 turns an int64 into a string
  156. func FormatInt64(value int64) string {
  157. return strconv.FormatInt(value, 10)
  158. }
  159. // FormatUint8 turns an uint8 into a string
  160. func FormatUint8(value uint8) string {
  161. return strconv.FormatUint(uint64(value), 10)
  162. }
  163. // FormatUint16 turns an uint16 into a string
  164. func FormatUint16(value uint16) string {
  165. return strconv.FormatUint(uint64(value), 10)
  166. }
  167. // FormatUint32 turns an uint32 into a string
  168. func FormatUint32(value uint32) string {
  169. return strconv.FormatUint(uint64(value), 10)
  170. }
  171. // FormatUint64 turns an uint64 into a string
  172. func FormatUint64(value uint64) string {
  173. return strconv.FormatUint(value, 10)
  174. }