convert.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. )
  25. // IsFloat64AJSONInteger allow for integers [-2^53, 2^53-1] inclusive
  26. func IsFloat64AJSONInteger(f float64) bool {
  27. if math.IsNaN(f) || math.IsInf(f, 0) || f < minJSONFloat || f > maxJSONFloat {
  28. return false
  29. }
  30. return f == float64(int64(f)) || f == float64(uint64(f))
  31. }
  32. var evaluatesAsTrue = map[string]struct{}{
  33. "true": struct{}{},
  34. "1": struct{}{},
  35. "yes": struct{}{},
  36. "ok": struct{}{},
  37. "y": struct{}{},
  38. "on": struct{}{},
  39. "selected": struct{}{},
  40. "checked": struct{}{},
  41. "t": struct{}{},
  42. "enabled": struct{}{},
  43. }
  44. // ConvertBool turn a string into a boolean
  45. func ConvertBool(str string) (bool, error) {
  46. _, ok := evaluatesAsTrue[strings.ToLower(str)]
  47. return ok, nil
  48. }
  49. // ConvertFloat32 turn a string into a float32
  50. func ConvertFloat32(str string) (float32, error) {
  51. f, err := strconv.ParseFloat(str, 32)
  52. if err != nil {
  53. return 0, err
  54. }
  55. return float32(f), nil
  56. }
  57. // ConvertFloat64 turn a string into a float64
  58. func ConvertFloat64(str string) (float64, error) {
  59. return strconv.ParseFloat(str, 64)
  60. }
  61. // ConvertInt8 turn a string into int8 boolean
  62. func ConvertInt8(str string) (int8, error) {
  63. i, err := strconv.ParseInt(str, 10, 8)
  64. if err != nil {
  65. return 0, err
  66. }
  67. return int8(i), nil
  68. }
  69. // ConvertInt16 turn a string into a int16
  70. func ConvertInt16(str string) (int16, error) {
  71. i, err := strconv.ParseInt(str, 10, 16)
  72. if err != nil {
  73. return 0, err
  74. }
  75. return int16(i), nil
  76. }
  77. // ConvertInt32 turn a string into a int32
  78. func ConvertInt32(str string) (int32, error) {
  79. i, err := strconv.ParseInt(str, 10, 32)
  80. if err != nil {
  81. return 0, err
  82. }
  83. return int32(i), nil
  84. }
  85. // ConvertInt64 turn a string into a int64
  86. func ConvertInt64(str string) (int64, error) {
  87. return strconv.ParseInt(str, 10, 64)
  88. }
  89. // ConvertUint8 turn a string into a uint8
  90. func ConvertUint8(str string) (uint8, error) {
  91. i, err := strconv.ParseUint(str, 10, 8)
  92. if err != nil {
  93. return 0, err
  94. }
  95. return uint8(i), nil
  96. }
  97. // ConvertUint16 turn a string into a uint16
  98. func ConvertUint16(str string) (uint16, error) {
  99. i, err := strconv.ParseUint(str, 10, 16)
  100. if err != nil {
  101. return 0, err
  102. }
  103. return uint16(i), nil
  104. }
  105. // ConvertUint32 turn a string into a uint32
  106. func ConvertUint32(str string) (uint32, error) {
  107. i, err := strconv.ParseUint(str, 10, 32)
  108. if err != nil {
  109. return 0, err
  110. }
  111. return uint32(i), nil
  112. }
  113. // ConvertUint64 turn a string into a uint64
  114. func ConvertUint64(str string) (uint64, error) {
  115. return strconv.ParseUint(str, 10, 64)
  116. }
  117. // FormatBool turns a boolean into a string
  118. func FormatBool(value bool) string {
  119. return strconv.FormatBool(value)
  120. }
  121. // FormatFloat32 turns a float32 into a string
  122. func FormatFloat32(value float32) string {
  123. return strconv.FormatFloat(float64(value), 'f', -1, 32)
  124. }
  125. // FormatFloat64 turns a float64 into a string
  126. func FormatFloat64(value float64) string {
  127. return strconv.FormatFloat(value, 'f', -1, 64)
  128. }
  129. // FormatInt8 turns an int8 into a string
  130. func FormatInt8(value int8) string {
  131. return strconv.FormatInt(int64(value), 10)
  132. }
  133. // FormatInt16 turns an int16 into a string
  134. func FormatInt16(value int16) string {
  135. return strconv.FormatInt(int64(value), 10)
  136. }
  137. // FormatInt32 turns an int32 into a string
  138. func FormatInt32(value int32) string {
  139. return strconv.FormatInt(int64(value), 10)
  140. }
  141. // FormatInt64 turns an int64 into a string
  142. func FormatInt64(value int64) string {
  143. return strconv.FormatInt(value, 10)
  144. }
  145. // FormatUint8 turns an uint8 into a string
  146. func FormatUint8(value uint8) string {
  147. return strconv.FormatUint(uint64(value), 10)
  148. }
  149. // FormatUint16 turns an uint16 into a string
  150. func FormatUint16(value uint16) string {
  151. return strconv.FormatUint(uint64(value), 10)
  152. }
  153. // FormatUint32 turns an uint32 into a string
  154. func FormatUint32(value uint32) string {
  155. return strconv.FormatUint(uint64(value), 10)
  156. }
  157. // FormatUint64 turns an uint64 into a string
  158. func FormatUint64(value uint64) string {
  159. return strconv.FormatUint(value, 10)
  160. }