value.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package objx
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. // Value provides methods for extracting interface{} data in various
  7. // types.
  8. type Value struct {
  9. // data contains the raw data being managed by this Value
  10. data interface{}
  11. }
  12. // Data returns the raw data contained by this Value
  13. func (v *Value) Data() interface{} {
  14. return v.data
  15. }
  16. // String returns the value always as a string
  17. func (v *Value) String() string {
  18. switch {
  19. case v.IsNil():
  20. return ""
  21. case v.IsStr():
  22. return v.Str()
  23. case v.IsBool():
  24. return strconv.FormatBool(v.Bool())
  25. case v.IsFloat32():
  26. return strconv.FormatFloat(float64(v.Float32()), 'f', -1, 32)
  27. case v.IsFloat64():
  28. return strconv.FormatFloat(v.Float64(), 'f', -1, 64)
  29. case v.IsInt():
  30. return strconv.FormatInt(int64(v.Int()), 10)
  31. case v.IsInt8():
  32. return strconv.FormatInt(int64(v.Int8()), 10)
  33. case v.IsInt16():
  34. return strconv.FormatInt(int64(v.Int16()), 10)
  35. case v.IsInt32():
  36. return strconv.FormatInt(int64(v.Int32()), 10)
  37. case v.IsInt64():
  38. return strconv.FormatInt(v.Int64(), 10)
  39. case v.IsUint():
  40. return strconv.FormatUint(uint64(v.Uint()), 10)
  41. case v.IsUint8():
  42. return strconv.FormatUint(uint64(v.Uint8()), 10)
  43. case v.IsUint16():
  44. return strconv.FormatUint(uint64(v.Uint16()), 10)
  45. case v.IsUint32():
  46. return strconv.FormatUint(uint64(v.Uint32()), 10)
  47. case v.IsUint64():
  48. return strconv.FormatUint(v.Uint64(), 10)
  49. }
  50. return fmt.Sprintf("%#v", v.Data())
  51. }
  52. // StringSlice returns the value always as a []string
  53. func (v *Value) StringSlice(optionalDefault ...[]string) []string {
  54. switch {
  55. case v.IsStrSlice():
  56. return v.MustStrSlice()
  57. case v.IsBoolSlice():
  58. slice := v.MustBoolSlice()
  59. vals := make([]string, len(slice))
  60. for i, iv := range slice {
  61. vals[i] = strconv.FormatBool(iv)
  62. }
  63. return vals
  64. case v.IsFloat32Slice():
  65. slice := v.MustFloat32Slice()
  66. vals := make([]string, len(slice))
  67. for i, iv := range slice {
  68. vals[i] = strconv.FormatFloat(float64(iv), 'f', -1, 32)
  69. }
  70. return vals
  71. case v.IsFloat64Slice():
  72. slice := v.MustFloat64Slice()
  73. vals := make([]string, len(slice))
  74. for i, iv := range slice {
  75. vals[i] = strconv.FormatFloat(iv, 'f', -1, 64)
  76. }
  77. return vals
  78. case v.IsIntSlice():
  79. slice := v.MustIntSlice()
  80. vals := make([]string, len(slice))
  81. for i, iv := range slice {
  82. vals[i] = strconv.FormatInt(int64(iv), 10)
  83. }
  84. return vals
  85. case v.IsInt8Slice():
  86. slice := v.MustInt8Slice()
  87. vals := make([]string, len(slice))
  88. for i, iv := range slice {
  89. vals[i] = strconv.FormatInt(int64(iv), 10)
  90. }
  91. return vals
  92. case v.IsInt16Slice():
  93. slice := v.MustInt16Slice()
  94. vals := make([]string, len(slice))
  95. for i, iv := range slice {
  96. vals[i] = strconv.FormatInt(int64(iv), 10)
  97. }
  98. return vals
  99. case v.IsInt32Slice():
  100. slice := v.MustInt32Slice()
  101. vals := make([]string, len(slice))
  102. for i, iv := range slice {
  103. vals[i] = strconv.FormatInt(int64(iv), 10)
  104. }
  105. return vals
  106. case v.IsInt64Slice():
  107. slice := v.MustInt64Slice()
  108. vals := make([]string, len(slice))
  109. for i, iv := range slice {
  110. vals[i] = strconv.FormatInt(iv, 10)
  111. }
  112. return vals
  113. case v.IsUintSlice():
  114. slice := v.MustUintSlice()
  115. vals := make([]string, len(slice))
  116. for i, iv := range slice {
  117. vals[i] = strconv.FormatUint(uint64(iv), 10)
  118. }
  119. return vals
  120. case v.IsUint8Slice():
  121. slice := v.MustUint8Slice()
  122. vals := make([]string, len(slice))
  123. for i, iv := range slice {
  124. vals[i] = strconv.FormatUint(uint64(iv), 10)
  125. }
  126. return vals
  127. case v.IsUint16Slice():
  128. slice := v.MustUint16Slice()
  129. vals := make([]string, len(slice))
  130. for i, iv := range slice {
  131. vals[i] = strconv.FormatUint(uint64(iv), 10)
  132. }
  133. return vals
  134. case v.IsUint32Slice():
  135. slice := v.MustUint32Slice()
  136. vals := make([]string, len(slice))
  137. for i, iv := range slice {
  138. vals[i] = strconv.FormatUint(uint64(iv), 10)
  139. }
  140. return vals
  141. case v.IsUint64Slice():
  142. slice := v.MustUint64Slice()
  143. vals := make([]string, len(slice))
  144. for i, iv := range slice {
  145. vals[i] = strconv.FormatUint(iv, 10)
  146. }
  147. return vals
  148. }
  149. if len(optionalDefault) == 1 {
  150. return optionalDefault[0]
  151. }
  152. return []string{}
  153. }