bytes.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package yu_proto
  2. func (t *Protobuf) Bytes() []byte {
  3. t.tmp_buf = nil
  4. if t.list.id == 0 {
  5. return nil
  6. }
  7. return t.bytes_obj(t.list)
  8. }
  9. func (t *Protobuf) bytes_obj(list *node) []byte {
  10. var j_buf []byte
  11. for {
  12. if list.id != 0 {
  13. j_buf = append(j_buf, t.bytes_value(list.id, list.value)...)
  14. }
  15. if list._next == nil {
  16. break
  17. }
  18. list = list._next
  19. }
  20. return j_buf
  21. }
  22. func (t *Protobuf) bytes_value(id uint64, value any) []byte {
  23. var j_buf []byte
  24. switch j_value := value.(type) {
  25. case bool:
  26. j_buf = make([]byte, 0, maxVarintLen64+1)
  27. j_buf = appendUint64(j_buf, (id<<3)|0)
  28. if j_value {
  29. j_buf = appendUint64(j_buf, 1)
  30. } else {
  31. j_buf = appendUint64(j_buf, 0)
  32. }
  33. case byte:
  34. j_buf = make([]byte, 0, maxVarintLen64+1)
  35. j_buf = appendUint64(j_buf, (id<<3)|0)
  36. j_buf = appendUint64(j_buf, uint64(j_value))
  37. case int8:
  38. j_buf = make([]byte, 0, maxVarintLen64+1)
  39. j_buf = appendUint64(j_buf, (id<<3)|0)
  40. j_buf = appendInt64(j_buf, int64(j_value))
  41. case int16:
  42. j_buf = make([]byte, 0, maxVarintLen64+maxVarintLen16)
  43. j_buf = appendUint64(j_buf, (id<<3)|0)
  44. j_buf = appendInt64(j_buf, int64(j_value))
  45. case uint16:
  46. j_buf = make([]byte, 0, maxVarintLen64+maxVarintLen16)
  47. j_buf = appendUint64(j_buf, (id<<3)|0)
  48. j_buf = appendUint64(j_buf, uint64(j_value))
  49. case int32:
  50. j_buf = make([]byte, 0, maxVarintLen64+maxVarintLen32)
  51. j_buf = appendUint64(j_buf, (id<<3)|0)
  52. j_buf = appendInt64(j_buf, int64(j_value))
  53. case uint32:
  54. j_buf = make([]byte, 0, maxVarintLen64+maxVarintLen32)
  55. j_buf = appendUint64(j_buf, (id<<3)|0)
  56. j_buf = appendUint64(j_buf, uint64(j_value))
  57. case int64:
  58. j_buf = make([]byte, 0, maxVarintLen64+maxVarintLen64)
  59. j_buf = appendUint64(j_buf, (id<<3)|0)
  60. j_buf = appendInt64(j_buf, j_value)
  61. case uint64:
  62. j_buf = make([]byte, 0, maxVarintLen64+maxVarintLen64)
  63. j_buf = appendUint64(j_buf, (id<<3)|0)
  64. j_buf = appendUint64(j_buf, j_value)
  65. case float32:
  66. j_buf = make([]byte, 0, maxVarintLen64+maxVarintLen32)
  67. j_buf = appendUint64(j_buf, (id<<3)|5)
  68. j_buf = appendFloat32(j_buf, j_value)
  69. case float64:
  70. j_buf = make([]byte, 0, maxVarintLen64+maxVarintLen64)
  71. j_buf = appendUint64(j_buf, (id<<3)|1)
  72. j_buf = appendFloat64(j_buf, j_value)
  73. case []byte:
  74. j_buf = make([]byte, 0, maxVarintLen64+maxVarintLen64)
  75. j_buf = appendUint64(j_buf, (id<<3)|2)
  76. j_buf = appendUint64(j_buf, uint64(len(j_value)))
  77. j_buf = append(j_buf, j_value...)
  78. case *node:
  79. j_node := t.bytes_obj(j_value)
  80. j_buf = make([]byte, 0, maxVarintLen64+maxVarintLen64)
  81. j_buf = appendUint64(j_buf, (id<<3)|2)
  82. j_buf = appendUint64(j_buf, uint64(len(j_node)))
  83. j_buf = append(j_buf, j_node...)
  84. case *[]*node:
  85. for _, j_item := range *j_value {
  86. if j_item == nil {
  87. j_buf = appendUint64(j_buf, (id<<3)|2)
  88. j_buf = appendUint64(j_buf, 0)
  89. continue
  90. }
  91. j_buf = append(j_buf, t.bytes_value(id, j_item.value)...)
  92. }
  93. default:
  94. panic("不支持的数据类型")
  95. }
  96. return j_buf
  97. }