value.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package value
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "reflect"
  6. "strconv"
  7. "strings"
  8. )
  9. func ValueOf(val interface{}) Value {
  10. return Value{
  11. val: val,
  12. }
  13. }
  14. type Value struct {
  15. val interface{}
  16. }
  17. func (s *Value) IsValid() bool {
  18. return s.val != nil
  19. }
  20. func (s *Value) Setup(val interface{}) (res bool) {
  21. rr := reflect.ValueOf(val)
  22. if rr.Kind() != reflect.Ptr {
  23. panic(fmt.Errorf("Expected ptr, given %v", rr.Type()))
  24. }
  25. rKind, rType := rr.Elem().Kind(), rr.Elem().Type()
  26. if rKind == reflect.Interface {
  27. rKind, rType = rr.Elem().Elem().Kind(), rr.Elem().Elem().Type()
  28. }
  29. if rKind == reflect.String {
  30. rls := strings.TrimSpace(fmt.Sprint(s.val))
  31. if len(rls) > 0 {
  32. rr.Elem().Set(reflect.ValueOf(rls))
  33. res = true
  34. }
  35. } else {
  36. rl := reflect.ValueOf(s.val)
  37. if rl.Kind() == reflect.String {
  38. switch rKind {
  39. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  40. {
  41. if tmp, err := strconv.ParseInt(rl.String(), 10, 64); err == nil {
  42. rr.Elem().Set(reflect.ValueOf(tmp).Convert(rType))
  43. res = true
  44. }
  45. }
  46. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  47. {
  48. if tmp, err := strconv.ParseUint(rl.String(), 10, 64); err == nil {
  49. rr.Elem().Set(reflect.ValueOf(tmp).Convert(rType))
  50. res = true
  51. }
  52. }
  53. case reflect.Float32, reflect.Float64:
  54. {
  55. if tmp, err := strconv.ParseFloat(rl.String(), 64); err == nil {
  56. rr.Elem().Set(reflect.ValueOf(tmp).Convert(rType))
  57. res = true
  58. }
  59. }
  60. case reflect.Slice:
  61. {
  62. slType := rType.Elem()
  63. if rr.Kind() == reflect.Slice {
  64. slRes := reflect.MakeSlice(rType, rr.Len(), rr.Cap())
  65. for i := 0; i < rl.Len(); i++ {
  66. if val := rl.Index(i); val.CanConvert(slType) {
  67. slRes.Index(i).Set(val.Convert(slType))
  68. } else {
  69. return false
  70. }
  71. }
  72. rr.Elem().Set(slRes)
  73. res = true
  74. } else if rl.Kind() == reflect.String {
  75. slType := rType.Elem()
  76. sls := strings.Split(rl.String(), ",")
  77. slRes := reflect.MakeSlice(rType, len(sls), cap(sls))
  78. for i, sVal := range sls {
  79. sItem := reflect.ValueOf(strings.TrimSpace(sVal))
  80. switch {
  81. case isKindNumber(slType.Kind()):
  82. {
  83. numVal, err := strconv.ParseFloat(sItem.String(), 64)
  84. if err != nil {
  85. return false
  86. }
  87. sItem = reflect.ValueOf(numVal)
  88. slRes.Index(i).Set(sItem.Convert(slType))
  89. }
  90. default:
  91. {
  92. if !sItem.CanConvert(slType) {
  93. return false
  94. }
  95. slRes.Index(i).Set(sItem.Convert(slType))
  96. }
  97. }
  98. }
  99. rr.Elem().Set(slRes)
  100. res = true
  101. }
  102. /*
  103. log.Println("Slice")
  104. slType := rType.Elem()
  105. if rr.Kind() == reflect.Slice {
  106. slRes := reflect.MakeSlice(slType, rr.Len(), rr.Cap())
  107. for i := 0; i < rl.Len(); i++ {
  108. if val := rl.Index(i); val.CanConvert(slType) {
  109. slRes.Index(i).Set(val.Convert(slType))
  110. } else {
  111. return false
  112. }
  113. }
  114. rr.Elem().Set(slRes)
  115. res = true
  116. } else if rl.Kind() == reflect.String {
  117. log.Println("sl <===> str")
  118. sls := strings.Split(rl.String(), ",")
  119. log.Println(slType, rr.Type())
  120. slRes := reflect.MakeSlice(slType, len(sls), cap(sls))
  121. switch {
  122. case isKindNumber(slType.Kind()):
  123. log.Println("AAAAAAAAAAAAAAAAA")
  124. for i, sVal := range sls {
  125. numVal, err := strconv.ParseFloat(sVal, 64)
  126. if err != nil {
  127. return false
  128. }
  129. rl := reflect.ValueOf(numVal)
  130. if val := rl.Index(i); val.CanConvert(slType) {
  131. slRes.Index(i).Set(val.Convert(slType))
  132. } else {
  133. return false
  134. }
  135. }
  136. rr.Elem().Set(slRes)
  137. res = true
  138. }
  139. }
  140. */
  141. }
  142. default:
  143. // json
  144. i := reflect.New(rr.Elem().Type()).Interface()
  145. if err := json.Unmarshal([]byte(rl.String()), i); err == nil {
  146. rr.Elem().Set(reflect.ValueOf(i).Elem())
  147. }
  148. }
  149. } else {
  150. var rVal reflect.Value
  151. defer func() {
  152. if r := recover(); r == nil {
  153. rr.Elem().Set(rVal)
  154. res = true
  155. }
  156. }()
  157. rVal = rl.Convert(rType)
  158. }
  159. }
  160. return
  161. }
  162. func (s *Value) String() string {
  163. return fmt.Sprint(s.val)
  164. }
  165. func (s *Value) Int() int {
  166. var i int
  167. s.Setup(&i)
  168. return i
  169. }
  170. func (s *Value) Int8() int8 {
  171. var i int8
  172. s.Setup(&i)
  173. return i
  174. }
  175. func (s *Value) Int16() int16 {
  176. var i int16
  177. s.Setup(&i)
  178. return i
  179. }
  180. func (s *Value) Int32() int32 {
  181. return int32(s.Int())
  182. }
  183. func (s *Value) Float32() float32 {
  184. var i float32
  185. s.Setup(&i)
  186. return i
  187. }
  188. func (s *Value) Float64() float64 {
  189. var i float64
  190. s.Setup(&i)
  191. return i
  192. }