util.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package util
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "time"
  7. "github.com/pkg/errors"
  8. "kpt.xdmy/pkg/log"
  9. )
  10. func Bool() {
  11. }
  12. func Zero(b bool) int {
  13. if b {
  14. return 1
  15. }
  16. return 0
  17. }
  18. func ZeroStr(b bool, s string) string {
  19. if b {
  20. return s
  21. }
  22. return ""
  23. }
  24. func ParseDate(d string) (res time.Time, err error) {
  25. if len(d) == 8 {
  26. d = d[:4] + "-" + d[4:6] + "-" + d[6:]
  27. }
  28. res, err = time.ParseInLocation("2006-01-02", d, time.Local)
  29. if err != nil {
  30. err = errors.Wrapf(err, "util ParseDate %s", d)
  31. log.Print(err.Error())
  32. }
  33. return
  34. }
  35. func ParseDateAndTime(d, t string) (res time.Time, e error) {
  36. if d == "00000000" {
  37. return time.Now(), nil
  38. }
  39. if len(d) == 8 {
  40. d = d[:4] + "-" + d[4:6] + "-" + d[6:]
  41. }
  42. if len(t) == 6 {
  43. t = t[:2] + ":" + t[2:4] + ":" + t[4:]
  44. }
  45. if t != "" {
  46. d = d + " " + t
  47. }
  48. if res, e = time.ParseInLocation("2006-01-02 15:04:05", d, time.Local); e != nil {
  49. e = errors.Wrapf(e, "util ParseDateAndTime %s %s", d, t)
  50. }
  51. return
  52. }
  53. func Unmarshal(p interface{}, res interface{}) (e error) {
  54. b := []byte{}
  55. switch p.(type) {
  56. case string:
  57. b = []byte(p.(string))
  58. case []byte:
  59. b = p.([]byte)
  60. default:
  61. if b, e = json.Marshal(p); e != nil {
  62. return errors.Wrapf(e, "util Unmarshal")
  63. }
  64. }
  65. fmt.Println(string(b))
  66. e = json.Unmarshal(b, res)
  67. // defer func(e error) {
  68. // if e != nil {
  69. // log.Error("json UnMarshal ", e)
  70. // }
  71. // }(e)
  72. return errors.Wrapf(e, "util Unmarshal")
  73. }
  74. func UnmarshalNoflag(p interface{}, res interface{}) (err error) {
  75. b := make([]byte, 0)
  76. switch p.(type) {
  77. case string:
  78. b = []byte(p.(string))
  79. case []byte:
  80. b = p.([]byte)
  81. default:
  82. b, err = json.Marshal(p)
  83. if err != nil {
  84. return
  85. }
  86. }
  87. err = json.Unmarshal(b, res)
  88. return
  89. }
  90. func Marshal(p interface{}) (b []byte, err error) {
  91. // json.MarshalIndent(p, "", " ")
  92. if b, err = json.Marshal(p); err != nil {
  93. log.Error("json Marshal error:", err, p)
  94. }
  95. return
  96. }
  97. func JsonMarshal(p interface{}) (r io.Reader, err error) {
  98. if p == nil {
  99. return
  100. }
  101. var b []byte
  102. switch p.(type) {
  103. case string:
  104. b = []byte(p.(string))
  105. case []byte:
  106. b = p.([]byte)
  107. default:
  108. b, err = json.Marshal(p)
  109. if err != nil {
  110. err = log.Error("failed to marshal", err, p)
  111. return
  112. }
  113. }
  114. r = bytes.NewBuffer(b)
  115. return
  116. }
  117. func NewMap(name string, value interface{}) map[string]interface{} {
  118. return map[string]interface{}{
  119. name: value,
  120. }
  121. }