convert.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. func init() {
  48. evaluatesAsTrue = map[string]struct{}{
  49. "true": {},
  50. "1": {},
  51. "yes": {},
  52. "ok": {},
  53. "y": {},
  54. "on": {},
  55. "selected": {},
  56. "checked": {},
  57. "t": {},
  58. "enabled": {},
  59. }
  60. }
  61. // ConvertBool turn a string into a boolean
  62. func ConvertBool(str string) (bool, error) {
  63. _, ok := evaluatesAsTrue[strings.ToLower(str)]
  64. return ok, nil
  65. }
  66. // ConvertFloat32 turn a string into a float32
  67. func ConvertFloat32(str string) (float32, error) {
  68. f, err := strconv.ParseFloat(str, 32)
  69. if err != nil {
  70. return 0, err
  71. }
  72. return float32(f), nil
  73. }
  74. // ConvertFloat64 turn a string into a float64
  75. func ConvertFloat64(str string) (float64, error) {
  76. return strconv.ParseFloat(str, 64)
  77. }
  78. // ConvertInt8 turn a string into int8 boolean
  79. func ConvertInt8(str string) (int8, error) {
  80. i, err := strconv.ParseInt(str, 10, 8)
  81. if err != nil {
  82. return 0, err
  83. }
  84. return int8(i), nil
  85. }
  86. // ConvertInt16 turn a string into a int16
  87. func ConvertInt16(str string) (int16, error) {
  88. i, err := strconv.ParseInt(str, 10, 16)
  89. if err != nil {
  90. return 0, err
  91. }
  92. return int16(i), nil
  93. }
  94. // ConvertInt32 turn a string into a int32
  95. func ConvertInt32(str string) (int32, error) {
  96. i, err := strconv.ParseInt(str, 10, 32)
  97. if err != nil {
  98. return 0, err
  99. }
  100. return int32(i), nil
  101. }
  102. // ConvertInt64 turn a string into a int64
  103. func ConvertInt64(str string) (int64, error) {
  104. return strconv.ParseInt(str, 10, 64)
  105. }
  106. // ConvertUint8 turn a string into a uint8
  107. func ConvertUint8(str string) (uint8, error) {
  108. i, err := strconv.ParseUint(str, 10, 8)
  109. if err != nil {
  110. return 0, err
  111. }
  112. return uint8(i), nil
  113. }
  114. // ConvertUint16 turn a string into a uint16
  115. func ConvertUint16(str string) (uint16, error) {
  116. i, err := strconv.ParseUint(str, 10, 16)
  117. if err != nil {
  118. return 0, err
  119. }
  120. return uint16(i), nil
  121. }
  122. // ConvertUint32 turn a string into a uint32
  123. func ConvertUint32(str string) (uint32, error) {
  124. i, err := strconv.ParseUint(str, 10, 32)
  125. if err != nil {
  126. return 0, err
  127. }
  128. return uint32(i), nil
  129. }
  130. // ConvertUint64 turn a string into a uint64
  131. func ConvertUint64(str string) (uint64, error) {
  132. return strconv.ParseUint(str, 10, 64)
  133. }
  134. // FormatBool turns a boolean into a string
  135. func FormatBool(value bool) string {
  136. return strconv.FormatBool(value)
  137. }
  138. // FormatFloat32 turns a float32 into a string
  139. func FormatFloat32(value float32) string {
  140. return strconv.FormatFloat(float64(value), 'f', -1, 32)
  141. }
  142. // FormatFloat64 turns a float64 into a string
  143. func FormatFloat64(value float64) string {
  144. return strconv.FormatFloat(value, 'f', -1, 64)
  145. }
  146. // FormatInt8 turns an int8 into a string
  147. func FormatInt8(value int8) string {
  148. return strconv.FormatInt(int64(value), 10)
  149. }
  150. // FormatInt16 turns an int16 into a string
  151. func FormatInt16(value int16) string {
  152. return strconv.FormatInt(int64(value), 10)
  153. }
  154. // FormatInt32 turns an int32 into a string
  155. func FormatInt32(value int32) string {
  156. return strconv.Itoa(int(value))
  157. }
  158. // FormatInt64 turns an int64 into a string
  159. func FormatInt64(value int64) string {
  160. return strconv.FormatInt(value, 10)
  161. }
  162. // FormatUint8 turns an uint8 into a string
  163. func FormatUint8(value uint8) string {
  164. return strconv.FormatUint(uint64(value), 10)
  165. }
  166. // FormatUint16 turns an uint16 into a string
  167. func FormatUint16(value uint16) string {
  168. return strconv.FormatUint(uint64(value), 10)
  169. }
  170. // FormatUint32 turns an uint32 into a string
  171. func FormatUint32(value uint32) string {
  172. return strconv.FormatUint(uint64(value), 10)
  173. }
  174. // FormatUint64 turns an uint64 into a string
  175. func FormatUint64(value uint64) string {
  176. return strconv.FormatUint(value, 10)
  177. }