value.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.IsStr():
  20. return v.Str()
  21. case v.IsBool():
  22. return strconv.FormatBool(v.Bool())
  23. case v.IsFloat32():
  24. return strconv.FormatFloat(float64(v.Float32()), 'f', -1, 32)
  25. case v.IsFloat64():
  26. return strconv.FormatFloat(v.Float64(), 'f', -1, 64)
  27. case v.IsInt():
  28. return strconv.FormatInt(int64(v.Int()), 10)
  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. }