converter.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright 2012 The Gorilla Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package schema
  5. import (
  6. "reflect"
  7. "strconv"
  8. utils "github.com/gofiber/utils/v2"
  9. )
  10. type Converter func(string) reflect.Value
  11. var (
  12. invalidValue = reflect.Value{}
  13. boolType = reflect.Bool
  14. float32Type = reflect.Float32
  15. float64Type = reflect.Float64
  16. intType = reflect.Int
  17. int8Type = reflect.Int8
  18. int16Type = reflect.Int16
  19. int32Type = reflect.Int32
  20. int64Type = reflect.Int64
  21. stringType = reflect.String
  22. uintType = reflect.Uint
  23. uint8Type = reflect.Uint8
  24. uint16Type = reflect.Uint16
  25. uint32Type = reflect.Uint32
  26. uint64Type = reflect.Uint64
  27. )
  28. // Default converters for basic types.
  29. var builtinConverters = map[reflect.Kind]Converter{
  30. boolType: convertBool,
  31. float32Type: convertFloat32,
  32. float64Type: convertFloat64,
  33. intType: convertInt,
  34. int8Type: convertInt8,
  35. int16Type: convertInt16,
  36. int32Type: convertInt32,
  37. int64Type: convertInt64,
  38. stringType: convertString,
  39. uintType: convertUint,
  40. uint8Type: convertUint8,
  41. uint16Type: convertUint16,
  42. uint32Type: convertUint32,
  43. uint64Type: convertUint64,
  44. }
  45. func convertBool(value string) reflect.Value {
  46. if value == "on" {
  47. return reflect.ValueOf(true)
  48. } else if v, err := strconv.ParseBool(value); err == nil {
  49. return reflect.ValueOf(v)
  50. }
  51. return invalidValue
  52. }
  53. func convertFloat32(value string) reflect.Value {
  54. if v, err := utils.ParseFloat32(value); err == nil {
  55. return reflect.ValueOf(v)
  56. }
  57. return invalidValue
  58. }
  59. func convertFloat64(value string) reflect.Value {
  60. if v, err := utils.ParseFloat64(value); err == nil {
  61. return reflect.ValueOf(v)
  62. }
  63. return invalidValue
  64. }
  65. func convertInt(value string) reflect.Value {
  66. if v, err := utils.ParseInt(value); err == nil {
  67. return reflect.ValueOf(int(v))
  68. }
  69. return invalidValue
  70. }
  71. func convertInt8(value string) reflect.Value {
  72. if v, err := utils.ParseInt8(value); err == nil {
  73. return reflect.ValueOf(v)
  74. }
  75. return invalidValue
  76. }
  77. func convertInt16(value string) reflect.Value {
  78. if v, err := utils.ParseInt16(value); err == nil {
  79. return reflect.ValueOf(v)
  80. }
  81. return invalidValue
  82. }
  83. func convertInt32(value string) reflect.Value {
  84. if v, err := utils.ParseInt32(value); err == nil {
  85. return reflect.ValueOf(v)
  86. }
  87. return invalidValue
  88. }
  89. func convertInt64(value string) reflect.Value {
  90. if v, err := utils.ParseInt(value); err == nil {
  91. return reflect.ValueOf(v)
  92. }
  93. return invalidValue
  94. }
  95. func convertString(value string) reflect.Value {
  96. return reflect.ValueOf(value)
  97. }
  98. func convertUint(value string) reflect.Value {
  99. if v, err := utils.ParseUint(value); err == nil {
  100. return reflect.ValueOf(uint(v))
  101. }
  102. return invalidValue
  103. }
  104. func convertUint8(value string) reflect.Value {
  105. if v, err := utils.ParseUint8(value); err == nil {
  106. return reflect.ValueOf(v)
  107. }
  108. return invalidValue
  109. }
  110. func convertUint16(value string) reflect.Value {
  111. if v, err := utils.ParseUint16(value); err == nil {
  112. return reflect.ValueOf(v)
  113. }
  114. return invalidValue
  115. }
  116. func convertUint32(value string) reflect.Value {
  117. if v, err := utils.ParseUint32(value); err == nil {
  118. return reflect.ValueOf(v)
  119. }
  120. return invalidValue
  121. }
  122. func convertUint64(value string) reflect.Value {
  123. if v, err := utils.ParseUint(value); err == nil {
  124. return reflect.ValueOf(v)
  125. }
  126. return invalidValue
  127. }
  128. func convertPointer(k reflect.Kind, value string) reflect.Value {
  129. switch k {
  130. case boolType:
  131. if v := convertBool(value); v.IsValid() {
  132. converted := v.Bool()
  133. return reflect.ValueOf(&converted)
  134. }
  135. case float32Type:
  136. if v := convertFloat32(value); v.IsValid() {
  137. converted := float32(v.Float())
  138. return reflect.ValueOf(&converted)
  139. }
  140. case float64Type:
  141. if v := convertFloat64(value); v.IsValid() {
  142. converted := float64(v.Float())
  143. return reflect.ValueOf(&converted)
  144. }
  145. case intType:
  146. if v := convertInt(value); v.IsValid() {
  147. converted := int(v.Int())
  148. return reflect.ValueOf(&converted)
  149. }
  150. case int8Type:
  151. if v := convertInt8(value); v.IsValid() {
  152. converted := int8(v.Int())
  153. return reflect.ValueOf(&converted)
  154. }
  155. case int16Type:
  156. if v := convertInt16(value); v.IsValid() {
  157. converted := int16(v.Int())
  158. return reflect.ValueOf(&converted)
  159. }
  160. case int32Type:
  161. if v := convertInt32(value); v.IsValid() {
  162. converted := int32(v.Int())
  163. return reflect.ValueOf(&converted)
  164. }
  165. case int64Type:
  166. if v := convertInt64(value); v.IsValid() {
  167. converted := int64(v.Int())
  168. return reflect.ValueOf(&converted)
  169. }
  170. case stringType:
  171. if v := convertString(value); v.IsValid() {
  172. converted := v.String()
  173. return reflect.ValueOf(&converted)
  174. }
  175. case uintType:
  176. if v := convertUint(value); v.IsValid() {
  177. converted := uint(v.Uint())
  178. return reflect.ValueOf(&converted)
  179. }
  180. case uint8Type:
  181. if v := convertUint8(value); v.IsValid() {
  182. converted := uint8(v.Uint())
  183. return reflect.ValueOf(&converted)
  184. }
  185. case uint16Type:
  186. if v := convertUint16(value); v.IsValid() {
  187. converted := uint16(v.Uint())
  188. return reflect.ValueOf(&converted)
  189. }
  190. case uint32Type:
  191. if v := convertUint32(value); v.IsValid() {
  192. converted := uint32(v.Uint())
  193. return reflect.ValueOf(&converted)
  194. }
  195. case uint64Type:
  196. if v := convertUint64(value); v.IsValid() {
  197. converted := uint64(v.Uint())
  198. return reflect.ValueOf(&converted)
  199. }
  200. }
  201. return invalidValue
  202. }