converter.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. )
  9. type Converter func(string) reflect.Value
  10. var (
  11. invalidValue = reflect.Value{}
  12. boolType = reflect.Bool
  13. float32Type = reflect.Float32
  14. float64Type = reflect.Float64
  15. intType = reflect.Int
  16. int8Type = reflect.Int8
  17. int16Type = reflect.Int16
  18. int32Type = reflect.Int32
  19. int64Type = reflect.Int64
  20. stringType = reflect.String
  21. uintType = reflect.Uint
  22. uint8Type = reflect.Uint8
  23. uint16Type = reflect.Uint16
  24. uint32Type = reflect.Uint32
  25. uint64Type = reflect.Uint64
  26. )
  27. // Default converters for basic types.
  28. var builtinConverters = map[reflect.Kind]Converter{
  29. boolType: convertBool,
  30. float32Type: convertFloat32,
  31. float64Type: convertFloat64,
  32. intType: convertInt,
  33. int8Type: convertInt8,
  34. int16Type: convertInt16,
  35. int32Type: convertInt32,
  36. int64Type: convertInt64,
  37. stringType: convertString,
  38. uintType: convertUint,
  39. uint8Type: convertUint8,
  40. uint16Type: convertUint16,
  41. uint32Type: convertUint32,
  42. uint64Type: convertUint64,
  43. }
  44. func convertBool(value string) reflect.Value {
  45. if value == "on" {
  46. return reflect.ValueOf(true)
  47. } else if v, err := strconv.ParseBool(value); err == nil {
  48. return reflect.ValueOf(v)
  49. }
  50. return invalidValue
  51. }
  52. func convertFloat32(value string) reflect.Value {
  53. if v, err := strconv.ParseFloat(value, 32); err == nil {
  54. return reflect.ValueOf(float32(v))
  55. }
  56. return invalidValue
  57. }
  58. func convertFloat64(value string) reflect.Value {
  59. if v, err := strconv.ParseFloat(value, 64); err == nil {
  60. return reflect.ValueOf(v)
  61. }
  62. return invalidValue
  63. }
  64. func convertInt(value string) reflect.Value {
  65. if v, err := strconv.ParseInt(value, 10, 0); err == nil {
  66. return reflect.ValueOf(int(v))
  67. }
  68. return invalidValue
  69. }
  70. func convertInt8(value string) reflect.Value {
  71. if v, err := strconv.ParseInt(value, 10, 8); err == nil {
  72. return reflect.ValueOf(int8(v))
  73. }
  74. return invalidValue
  75. }
  76. func convertInt16(value string) reflect.Value {
  77. if v, err := strconv.ParseInt(value, 10, 16); err == nil {
  78. return reflect.ValueOf(int16(v))
  79. }
  80. return invalidValue
  81. }
  82. func convertInt32(value string) reflect.Value {
  83. if v, err := strconv.ParseInt(value, 10, 32); err == nil {
  84. return reflect.ValueOf(int32(v))
  85. }
  86. return invalidValue
  87. }
  88. func convertInt64(value string) reflect.Value {
  89. if v, err := strconv.ParseInt(value, 10, 64); err == nil {
  90. return reflect.ValueOf(v)
  91. }
  92. return invalidValue
  93. }
  94. func convertString(value string) reflect.Value {
  95. return reflect.ValueOf(value)
  96. }
  97. func convertUint(value string) reflect.Value {
  98. if v, err := strconv.ParseUint(value, 10, 0); err == nil {
  99. return reflect.ValueOf(uint(v))
  100. }
  101. return invalidValue
  102. }
  103. func convertUint8(value string) reflect.Value {
  104. if v, err := strconv.ParseUint(value, 10, 8); err == nil {
  105. return reflect.ValueOf(uint8(v))
  106. }
  107. return invalidValue
  108. }
  109. func convertUint16(value string) reflect.Value {
  110. if v, err := strconv.ParseUint(value, 10, 16); err == nil {
  111. return reflect.ValueOf(uint16(v))
  112. }
  113. return invalidValue
  114. }
  115. func convertUint32(value string) reflect.Value {
  116. if v, err := strconv.ParseUint(value, 10, 32); err == nil {
  117. return reflect.ValueOf(uint32(v))
  118. }
  119. return invalidValue
  120. }
  121. func convertUint64(value string) reflect.Value {
  122. if v, err := strconv.ParseUint(value, 10, 64); err == nil {
  123. return reflect.ValueOf(v)
  124. }
  125. return invalidValue
  126. }