value.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package value
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "reflect"
  7. "strconv"
  8. "strings"
  9. )
  10. func ValueOf(val interface{}) Value {
  11. return Value{
  12. val: val,
  13. }
  14. }
  15. type Value struct {
  16. val interface{}
  17. }
  18. func (s *Value) IsValid() bool {
  19. return s.val != nil
  20. }
  21. func (s *Value) Setup(val interface{}) (res bool) {
  22. rr := reflect.ValueOf(val)
  23. if rr.Kind() != reflect.Ptr {
  24. panic(fmt.Errorf("Expected ptr, given %v", rr.Type()))
  25. }
  26. rKind, rType := rr.Elem().Kind(), rr.Elem().Type()
  27. if rKind == reflect.Interface {
  28. rKind, rType = rr.Elem().Elem().Kind(), rr.Elem().Elem().Type()
  29. }
  30. if rKind == reflect.String {
  31. rls := strings.TrimSpace(fmt.Sprint(s.val))
  32. if len(rls) > 0 {
  33. rr.Elem().Set(reflect.ValueOf(rls))
  34. res = true
  35. }
  36. } else {
  37. rl := reflect.ValueOf(s.val)
  38. if rl.Kind() == reflect.String {
  39. switch rKind {
  40. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  41. {
  42. if tmp, err := strconv.ParseInt(rl.String(), 10, 64); err == nil {
  43. rr.Elem().Set(reflect.ValueOf(tmp).Convert(rType))
  44. res = true
  45. }
  46. }
  47. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  48. {
  49. if tmp, err := strconv.ParseUint(rl.String(), 10, 64); err == nil {
  50. rr.Elem().Set(reflect.ValueOf(tmp).Convert(rType))
  51. res = true
  52. }
  53. }
  54. case reflect.Float32, reflect.Float64:
  55. {
  56. if tmp, err := strconv.ParseFloat(rl.String(), 64); err == nil {
  57. rr.Elem().Set(reflect.ValueOf(tmp).Convert(rType))
  58. res = true
  59. }
  60. }
  61. case reflect.Slice:
  62. {
  63. log.Println("Slice")
  64. slType := rType.Elem()
  65. if rr.Kind() == reflect.Slice {
  66. slRes := reflect.MakeSlice(slType, rr.Len(), rr.Cap())
  67. for i := 0; i < rl.Len(); i++ {
  68. if val := rl.Index(i); val.CanConvert(slType) {
  69. slRes.Index(i).Set(val.Convert(slType))
  70. } else {
  71. return false
  72. }
  73. }
  74. rr.Elem().Set(slRes)
  75. res = true
  76. } else if rl.Kind() == reflect.String {
  77. }
  78. }
  79. default:
  80. // json
  81. i := reflect.New(rr.Elem().Type()).Interface()
  82. if err := json.Unmarshal([]byte(rl.String()), i); err == nil {
  83. rr.Elem().Set(reflect.ValueOf(i).Elem())
  84. }
  85. }
  86. } else {
  87. var rVal reflect.Value
  88. defer func() {
  89. if r := recover(); r == nil {
  90. rr.Elem().Set(rVal)
  91. res = true
  92. }
  93. }()
  94. rVal = rl.Convert(rType)
  95. }
  96. }
  97. return
  98. }
  99. func (s *Value) String() string {
  100. return fmt.Sprint(s.val)
  101. }
  102. func (s *Value) Int() int {
  103. var i int
  104. s.Setup(&i)
  105. return i
  106. }
  107. func (s *Value) Int8() int8 {
  108. var i int8
  109. s.Setup(&i)
  110. return i
  111. }
  112. func (s *Value) Int16() int16 {
  113. var i int16
  114. s.Setup(&i)
  115. return i
  116. }
  117. func (s *Value) Int32() int32 {
  118. return int32(s.Int())
  119. }
  120. func (s *Value) Float32() float32 {
  121. var i float32
  122. s.Setup(&i)
  123. return i
  124. }
  125. func (s *Value) Float64() float64 {
  126. var i float64
  127. s.Setup(&i)
  128. return i
  129. }