raw.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package helper
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "reflect"
  6. "strconv"
  7. )
  8. type Raw struct {
  9. val interface{}
  10. }
  11. type argInt []int
  12. func NewRaw(val interface{}) *Raw {
  13. return &Raw{val: val}
  14. }
  15. //json解码
  16. func (v *Raw) JSONDecode(val interface{}) error {
  17. return json.Unmarshal(v.val.([]byte), val)
  18. }
  19. //json加密
  20. func (v *Raw) JSONEncode() ([]byte, error) {
  21. j, err := json.Marshal(v.val)
  22. if err == nil {
  23. return j, nil
  24. }
  25. return nil, err
  26. }
  27. func (a argInt) Get(i int, args ...int) (r int) {
  28. if i >= 0 && i < len(a) {
  29. r = a[i]
  30. }
  31. if len(args) > 0 {
  32. r = args[0]
  33. }
  34. return r
  35. }
  36. func (v *Raw) Result(args ...int) *Result {
  37. return NewResult(v.ToStr(args...))
  38. }
  39. func (v *Raw) String(args ...int) (s string) {
  40. return v.ToStr(args...)
  41. }
  42. func (v *Raw) Str(args ...int) *Str {
  43. return NewStr(v.ToStr(args...))
  44. }
  45. //是否为空
  46. func (r *Raw) Empty() bool {
  47. if r.val == nil {
  48. return true
  49. }
  50. v := reflect.ValueOf(r.val)
  51. switch v.Kind() {
  52. case reflect.String, reflect.Array:
  53. return v.Len() == 0
  54. case reflect.Map, reflect.Slice:
  55. return v.Len() == 0 || v.IsNil()
  56. case reflect.Bool:
  57. return !v.Bool()
  58. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  59. return v.Int() == 0
  60. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  61. return v.Uint() == 0
  62. case reflect.Float32, reflect.Float64:
  63. return v.Float() == 0
  64. case reflect.Interface, reflect.Ptr:
  65. return v.IsNil()
  66. }
  67. return reflect.DeepEqual(r.val, reflect.Zero(v.Type()).Interface())
  68. }
  69. func (v *Raw) ToStr(args ...int) (s string) {
  70. switch v := v.val.(type) {
  71. case bool:
  72. s = strconv.FormatBool(v)
  73. case float32:
  74. s = strconv.FormatFloat(float64(v), 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 32))
  75. case float64:
  76. s = strconv.FormatFloat(v, 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 64))
  77. case int:
  78. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  79. case int8:
  80. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  81. case int16:
  82. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  83. case int32:
  84. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  85. case int64:
  86. s = strconv.FormatInt(v, argInt(args).Get(0, 10))
  87. case uint:
  88. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  89. case uint8:
  90. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  91. case uint16:
  92. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  93. case uint32:
  94. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  95. case uint64:
  96. s = strconv.FormatUint(v, argInt(args).Get(0, 10))
  97. case string:
  98. s = v
  99. case []byte:
  100. s = string(v)
  101. default:
  102. s = fmt.Sprintf("%v", v)
  103. }
  104. return s
  105. }