vmToX.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package vm
  2. import (
  3. "errors"
  4. "fmt"
  5. "reflect"
  6. "strconv"
  7. "strings"
  8. )
  9. // toString converts all reflect.Value-s into string.
  10. func toString(v reflect.Value) string {
  11. if v.Kind() == reflect.Interface && !v.IsNil() {
  12. v = v.Elem()
  13. }
  14. if v.Kind() == reflect.Ptr {
  15. v = v.Elem()
  16. }
  17. if v.Kind() == reflect.String {
  18. return v.String()
  19. }
  20. return fmt.Sprint(v.Interface())
  21. }
  22. // toBool converts all reflect.Value-s into bool.
  23. func toBool(v reflect.Value) bool {
  24. b, _ := tryToBool(v)
  25. return b
  26. }
  27. // tryToBool attempts to convert the value 'v' to a boolean, returning
  28. // an error if it cannot. When converting a string, the function returns
  29. // true if the string nonempty and does not satisfy the condition for false
  30. // with parseBool https://golang.org/pkg/strconv/#ParseBool
  31. // and is not 0.0
  32. func tryToBool(v reflect.Value) (bool, error) {
  33. if v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
  34. v = v.Elem()
  35. }
  36. switch v.Kind() {
  37. case reflect.Float64, reflect.Float32:
  38. return v.Float() != 0, nil
  39. case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
  40. return v.Int() != 0, nil
  41. case reflect.Bool:
  42. return v.Bool(), nil
  43. case reflect.String:
  44. if v.Len() == 0 {
  45. return false, nil
  46. }
  47. s := v.String()
  48. if b, err := strconv.ParseBool(s); err == nil && !b {
  49. return false, nil
  50. }
  51. if f, err := tryToFloat64(v); err == nil && f == 0 {
  52. return false, nil
  53. }
  54. return true, nil
  55. case reflect.Slice, reflect.Map:
  56. if v.Len() > 0 {
  57. return true, nil
  58. }
  59. return false, nil
  60. }
  61. return false, errors.New("unknown type")
  62. }
  63. // toFloat64 converts all reflect.Value-s into float64.
  64. func toFloat64(v reflect.Value) float64 {
  65. f, _ := tryToFloat64(v)
  66. return f
  67. }
  68. // tryToFloat64 attempts to convert a value to a float64.
  69. // If it cannot (in the case of a non-numeric string, a struct, etc.)
  70. // it returns 0.0 and an error.
  71. func tryToFloat64(v reflect.Value) (float64, error) {
  72. if v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
  73. v = v.Elem()
  74. }
  75. switch v.Kind() {
  76. case reflect.Float64, reflect.Float32:
  77. return v.Float(), nil
  78. case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
  79. return float64(v.Int()), nil
  80. case reflect.Bool:
  81. if v.Bool() {
  82. return 1, nil
  83. }
  84. return 0, nil
  85. case reflect.String:
  86. f, err := strconv.ParseFloat(v.String(), 64)
  87. if err == nil {
  88. return f, nil
  89. }
  90. }
  91. return 0.0, errors.New("couldn't convert to a float64")
  92. }
  93. // toInt64 converts all reflect.Value-s into int64.
  94. func toInt64(v reflect.Value) int64 {
  95. i, _ := tryToInt64(v)
  96. return i
  97. }
  98. // tryToInt64 attempts to convert a value to an int64.
  99. // If it cannot (in the case of a non-numeric string, a struct, etc.)
  100. // it returns 0 and an error.
  101. func tryToInt64(v reflect.Value) (int64, error) {
  102. if v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
  103. v = v.Elem()
  104. }
  105. switch v.Kind() {
  106. case reflect.Float64, reflect.Float32:
  107. return int64(v.Float()), nil
  108. case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
  109. return v.Int(), nil
  110. case reflect.Bool:
  111. if v.Bool() {
  112. return 1, nil
  113. }
  114. return 0, nil
  115. case reflect.String:
  116. s := v.String()
  117. var i int64
  118. var err error
  119. if strings.HasPrefix(s, "0x") {
  120. i, err = strconv.ParseInt(s, 16, 64)
  121. } else {
  122. i, err = strconv.ParseInt(s, 10, 64)
  123. }
  124. if err == nil {
  125. return i, nil
  126. }
  127. }
  128. return 0, errors.New("couldn't convert to integer")
  129. }
  130. // toInt converts all reflect.Value-s into int.
  131. func toInt(v reflect.Value) int {
  132. i, _ := tryToInt(v)
  133. return i
  134. }
  135. // tryToInt attempts to convert a value to an int.
  136. // If it cannot (in the case of a non-numeric string, a struct, etc.)
  137. // it returns 0 and an error.
  138. func tryToInt(v reflect.Value) (int, error) {
  139. if v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
  140. v = v.Elem()
  141. }
  142. switch v.Kind() {
  143. case reflect.Float64, reflect.Float32:
  144. return int(v.Float()), nil
  145. case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
  146. return int(v.Int()), nil
  147. case reflect.Bool:
  148. if v.Bool() {
  149. return 1, nil
  150. }
  151. return 0, nil
  152. case reflect.String:
  153. s := v.String()
  154. var i int64
  155. var err error
  156. if strings.HasPrefix(s, "0x") {
  157. i, err = strconv.ParseInt(s, 16, 64)
  158. } else {
  159. i, err = strconv.ParseInt(s, 10, 64)
  160. }
  161. if err == nil {
  162. return int(i), nil
  163. }
  164. }
  165. return 0, errors.New("couldn't convert to integer")
  166. }