golang 的 rabbitmq 消费项目
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

json.go 2.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package tool
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "log"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "unicode"
  10. )
  11. /*************************************** 下划线json ***************************************/
  12. type JsonSnakeCase struct {
  13. Value interface{}
  14. }
  15. func MarshalJSON(marshalJson []byte) []byte {
  16. // Regexp definitions
  17. var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`)
  18. var wordBarrierRegex = regexp.MustCompile(`(\w)([A-Z])`)
  19. converted := keyMatchRegex.ReplaceAllFunc(
  20. marshalJson,
  21. func(match []byte) []byte {
  22. return bytes.ToLower(wordBarrierRegex.ReplaceAll(
  23. match,
  24. []byte(`${1}_${2}`),
  25. ))
  26. },
  27. )
  28. return converted
  29. }
  30. /*************************************** 驼峰json ***************************************/
  31. type JsonCamelCase struct {
  32. Value interface{}
  33. }
  34. func (c JsonCamelCase) MarshalJSON() ([]byte, error) {
  35. var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`)
  36. marshalled, err := json.Marshal(c.Value)
  37. converted := keyMatchRegex.ReplaceAllFunc(
  38. marshalled,
  39. func(match []byte) []byte {
  40. matchStr := string(match)
  41. key := matchStr[1 : len(matchStr)-2]
  42. resKey := Lcfirst(Case2Camel(key))
  43. return []byte(`"` + resKey + `":`)
  44. },
  45. )
  46. return converted, err
  47. }
  48. /*************************************** 其他方法 ***************************************/
  49. // 驼峰式写法转为下划线写法
  50. func Camel2Case(name string) string {
  51. buffer := NewBuffer()
  52. for i, r := range name {
  53. if unicode.IsUpper(r) {
  54. if i != 0 {
  55. buffer.Append('_')
  56. }
  57. buffer.Append(unicode.ToLower(r))
  58. } else {
  59. buffer.Append(r)
  60. }
  61. }
  62. return buffer.String()
  63. }
  64. // 下划线写法转为驼峰写法
  65. func Case2Camel(name string) string {
  66. name = strings.Replace(name, "_", " ", -1)
  67. name = strings.Title(name)
  68. return strings.Replace(name, " ", "", -1)
  69. }
  70. // 首字母大写
  71. func Ucfirst(str string) string {
  72. for i, v := range str {
  73. return string(unicode.ToUpper(v)) + str[i+1:]
  74. }
  75. return ""
  76. }
  77. // 首字母小写
  78. func Lcfirst(str string) string {
  79. for i, v := range str {
  80. return string(unicode.ToLower(v)) + str[i+1:]
  81. }
  82. return ""
  83. }
  84. // 内嵌bytes.Buffer,支持连写
  85. type Buffer struct {
  86. *bytes.Buffer
  87. }
  88. func NewBuffer() *Buffer {
  89. return &Buffer{Buffer: new(bytes.Buffer)}
  90. }
  91. func (b *Buffer) Append(i interface{}) *Buffer {
  92. switch val := i.(type) {
  93. case int:
  94. b.append(strconv.Itoa(val))
  95. case int64:
  96. b.append(strconv.FormatInt(val, 10))
  97. case uint:
  98. b.append(strconv.FormatUint(uint64(val), 10))
  99. case uint64:
  100. b.append(strconv.FormatUint(val, 10))
  101. case string:
  102. b.append(val)
  103. case []byte:
  104. b.Write(val)
  105. case rune:
  106. b.WriteRune(val)
  107. }
  108. return b
  109. }
  110. func (b *Buffer) append(s string) *Buffer {
  111. defer func() {
  112. if err := recover(); err != nil {
  113. log.Println("*****内存不够了!******")
  114. }
  115. }()
  116. b.WriteString(s)
  117. return b
  118. }