util.go 2.3 KB

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