encode.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. package yu_proto_old
  2. import (
  3. "bytes"
  4. "encoding"
  5. "encoding/binary"
  6. "fmt"
  7. "math"
  8. "reflect"
  9. "time"
  10. )
  11. // Ufixed32 Message fields declared to have exactly this type
  12. // will be transmitted as fixed-size 32-bit unsigned integers.
  13. type Ufixed32 uint32
  14. // Ufixed64 Message fields declared to have exactly this type
  15. // will be transmitted as fixed-size 64-bit unsigned integers.
  16. type Ufixed64 uint64
  17. // Sfixed32 Message fields declared to have exactly this type
  18. // will be transmitted as fixed-size 32-bit signed integers.
  19. type Sfixed32 int32
  20. // Sfixed64 Message fields declared to have exactly this type
  21. // will be transmitted as fixed-size 64-bit signed integers.
  22. type Sfixed64 int64
  23. // Enum Protobufs enums are transmitted as unsigned varints;
  24. // using this type alias is optional but recommended
  25. // to ensure they get the correct type.
  26. type Enum uint32
  27. type encoder struct {
  28. bytes.Buffer
  29. }
  30. // Encode a Go struct into protocol buffer format.
  31. // The caller must pass a pointer to the struct to encode.
  32. func Encode(structPtr interface{}) (bytes []byte) {
  33. defer func() {
  34. if e := recover(); e != nil {
  35. bytes = nil
  36. }
  37. }()
  38. if structPtr == nil {
  39. return
  40. }
  41. if bu, ok := structPtr.(encoding.BinaryMarshaler); ok {
  42. bytes, _ = bu.MarshalBinary()
  43. return
  44. }
  45. en := encoder{}
  46. val := reflect.ValueOf(structPtr)
  47. if val.Kind() != reflect.Ptr {
  48. return nil
  49. }
  50. en.message(val.Elem())
  51. return en.Bytes()
  52. }
  53. /*
  54. func Encode(structPtr interface{}) (bytes []byte, err error) {
  55. defer func() {
  56. if e := recover(); e != nil {
  57. err = fmt.Errorf("%v", e)
  58. bytes = nil
  59. }
  60. }()
  61. if structPtr == nil {
  62. return nil, nil
  63. }
  64. if bu, ok := structPtr.(encoding.BinaryMarshaler); ok {
  65. return bu.MarshalBinary()
  66. }
  67. en := encoder{}
  68. val := reflect.ValueOf(structPtr)
  69. if val.Kind() != reflect.Ptr {
  70. return nil, errors.New("encode takes a pointer to struct")
  71. }
  72. en.message(val.Elem())
  73. return en.Bytes(), nil
  74. }
  75. */
  76. func (en *encoder) message(sval reflect.Value) {
  77. var index *ProtoField
  78. defer func() {
  79. if r := recover(); r != nil {
  80. if index != nil {
  81. panic(fmt.Sprintf("%s (field %s)", r, index.Field.Name))
  82. } else {
  83. panic(r)
  84. }
  85. }
  86. }()
  87. // Encode all fields in-order
  88. protoFields := ProtoFields(sval.Type())
  89. if len(protoFields) == 0 {
  90. return
  91. }
  92. noPublicFields := true
  93. for _, index = range protoFields {
  94. field := sval.FieldByIndex(index.Index)
  95. key := uint64(index.ID) << 3
  96. if field.CanSet() { // Skip blank/padding fields
  97. en.value(key, field, index.Prefix)
  98. noPublicFields = false
  99. }
  100. }
  101. if noPublicFields {
  102. panic("struct has no serializable fields")
  103. }
  104. }
  105. var timeType = reflect.TypeOf(time.Time{})
  106. var durationType = reflect.TypeOf(time.Duration(0))
  107. func (en *encoder) value(key uint64, val reflect.Value, prefix TagPrefix) {
  108. // Non-reflectively handle some of the fixed types
  109. switch v := val.Interface().(type) {
  110. case bool:
  111. en.uvarint(key | 0)
  112. vi := uint64(0)
  113. if v {
  114. vi = 1
  115. }
  116. en.uvarint(vi)
  117. return
  118. case int:
  119. en.uvarint(key | 0)
  120. en.svarint(int64(v))
  121. return
  122. case int32:
  123. en.uvarint(key | 0)
  124. en.svarint(int64(v))
  125. return
  126. case time.Time: // Encode time.Time as sfixed64
  127. t := v.UnixNano()
  128. en.uvarint(key | 1)
  129. en.u64(uint64(t))
  130. return
  131. case int64:
  132. en.uvarint(key | 0)
  133. en.svarint(v)
  134. return
  135. case uint32:
  136. en.uvarint(key | 0)
  137. en.uvarint(uint64(v))
  138. return
  139. case uint64:
  140. en.uvarint(key | 0)
  141. en.uvarint(v)
  142. return
  143. case Sfixed32:
  144. en.uvarint(key | 5)
  145. en.u32(uint32(v))
  146. return
  147. case Sfixed64:
  148. en.uvarint(key | 1)
  149. en.u64(uint64(v))
  150. return
  151. case Ufixed32:
  152. en.uvarint(key | 5)
  153. en.u32(uint32(v))
  154. return
  155. case Ufixed64:
  156. en.uvarint(key | 1)
  157. en.u64(uint64(v))
  158. return
  159. case float32:
  160. en.uvarint(key | 5)
  161. en.u32(math.Float32bits(v))
  162. return
  163. case float64:
  164. en.uvarint(key | 1)
  165. en.u64(math.Float64bits(v))
  166. return
  167. case string:
  168. en.uvarint(key | 2)
  169. b := []byte(v)
  170. en.uvarint(uint64(len(b)))
  171. en.Write(b)
  172. return
  173. }
  174. // Handle pointer or interface values (possibly within slices).
  175. // Note that this switch has to handle all the cases,
  176. // because custom type aliases will fail the above typeswitch.
  177. switch val.Kind() {
  178. case reflect.Bool:
  179. en.uvarint(key | 0)
  180. v := uint64(0)
  181. if val.Bool() {
  182. v = 1
  183. }
  184. en.uvarint(v)
  185. case reflect.Int, reflect.Int32, reflect.Int64:
  186. // Varint-encoded 32-bit and 64-bit signed integers.
  187. // Note that protobufs don't support 8- or 16-bit ints.
  188. en.uvarint(key | 0)
  189. en.svarint(val.Int())
  190. case reflect.Uint32, reflect.Uint64:
  191. // Varint-encoded 32-bit and 64-bit unsigned integers.
  192. en.uvarint(key | 0)
  193. en.uvarint(val.Uint())
  194. case reflect.Float32:
  195. // Fixed-length 32-bit floats.
  196. en.uvarint(key | 5)
  197. en.u32(math.Float32bits(float32(val.Float())))
  198. case reflect.Float64:
  199. // Fixed-length 64-bit floats.
  200. en.uvarint(key | 1)
  201. en.u64(math.Float64bits(val.Float()))
  202. case reflect.String:
  203. // Length-delimited string.
  204. en.uvarint(key | 2)
  205. b := []byte(val.String())
  206. en.uvarint(uint64(len(b)))
  207. en.Write(b)
  208. case reflect.Struct:
  209. var b []byte
  210. if enc, ok := val.Interface().(encoding.BinaryMarshaler); ok {
  211. en.uvarint(key | 2)
  212. var err error
  213. b, err = enc.MarshalBinary()
  214. if err != nil {
  215. panic(err.Error())
  216. }
  217. } else {
  218. // Embedded messages.
  219. en.uvarint(key | 2)
  220. emb := encoder{}
  221. emb.message(val)
  222. b = emb.Bytes()
  223. }
  224. en.uvarint(uint64(len(b)))
  225. en.Write(b)
  226. case reflect.Slice, reflect.Array:
  227. // Length-delimited slices or byte-vectors.
  228. en.slice(key, val)
  229. return
  230. case reflect.Ptr:
  231. // Optional field: encode only if pointer is non-nil.
  232. if val.IsNil() {
  233. if prefix == TagRequired {
  234. panic("required field is nil")
  235. }
  236. return
  237. }
  238. en.value(key, val.Elem(), prefix)
  239. case reflect.Interface:
  240. // Abstract interface field.
  241. if val.IsNil() {
  242. return
  243. }
  244. // If the object support self-encoding, use that.
  245. if enc, ok := val.Interface().(encoding.BinaryMarshaler); ok {
  246. en.uvarint(key | 2)
  247. bytes, err := enc.MarshalBinary()
  248. if err != nil {
  249. panic(err.Error())
  250. }
  251. size := len(bytes)
  252. var id GeneratorID
  253. im, ok := val.Interface().(InterfaceMarshaler)
  254. if ok {
  255. id = im.MarshalID()
  256. g := generators.get(id)
  257. ok = g != nil
  258. if ok {
  259. // add the length of the type tag
  260. size += len(id)
  261. }
  262. }
  263. en.uvarint(uint64(size))
  264. if ok {
  265. // Only write the tag if a generator exists
  266. en.Write(id[:])
  267. }
  268. en.Write(bytes)
  269. return
  270. }
  271. // Encode from the object the interface points to.
  272. en.value(key, val.Elem(), prefix)
  273. case reflect.Map:
  274. en.handleMap(key, val, prefix)
  275. return
  276. default:
  277. panic(fmt.Sprintf("unsupported field Kind %d", val.Kind()))
  278. }
  279. }
  280. func (en *encoder) slice(key uint64, slval reflect.Value) {
  281. // First handle common cases with a direct typeswitch
  282. sllen := slval.Len()
  283. packed := encoder{}
  284. switch slt := slval.Interface().(type) {
  285. case []bool:
  286. for i := 0; i < sllen; i++ {
  287. v := uint64(0)
  288. if slt[i] {
  289. v = 1
  290. }
  291. packed.uvarint(v)
  292. }
  293. case []int32:
  294. for i := 0; i < sllen; i++ {
  295. packed.svarint(int64(slt[i]))
  296. }
  297. case []int64:
  298. for i := 0; i < sllen; i++ {
  299. packed.svarint(slt[i])
  300. }
  301. case []uint32:
  302. for i := 0; i < sllen; i++ {
  303. packed.uvarint(uint64(slt[i]))
  304. }
  305. case []uint64:
  306. for i := 0; i < sllen; i++ {
  307. packed.uvarint(slt[i])
  308. }
  309. case []Sfixed32:
  310. for i := 0; i < sllen; i++ {
  311. packed.u32(uint32(slt[i]))
  312. }
  313. case []Sfixed64:
  314. for i := 0; i < sllen; i++ {
  315. packed.u64(uint64(slt[i]))
  316. }
  317. case []Ufixed32:
  318. for i := 0; i < sllen; i++ {
  319. packed.u32(uint32(slt[i]))
  320. }
  321. case []Ufixed64:
  322. for i := 0; i < sllen; i++ {
  323. packed.u64(uint64(slt[i]))
  324. }
  325. case []float32:
  326. for i := 0; i < sllen; i++ {
  327. packed.u32(math.Float32bits(slt[i]))
  328. }
  329. case []float64:
  330. for i := 0; i < sllen; i++ {
  331. packed.u64(math.Float64bits(slt[i]))
  332. }
  333. case []byte: // Write the whole byte-slice as one key,value pair
  334. en.uvarint(key | 2)
  335. en.uvarint(uint64(sllen))
  336. en.Write(slt)
  337. return
  338. case []string:
  339. for i := 0; i < sllen; i++ {
  340. subVal := slval.Index(i)
  341. subStr := subVal.Interface().(string)
  342. subSlice := []byte(subStr)
  343. en.uvarint(key | 2)
  344. en.uvarint(uint64(len(subSlice)))
  345. en.Write(subSlice)
  346. }
  347. return
  348. default: // We'll need to use the reflective path
  349. en.sliceReflect(key, slval)
  350. return
  351. }
  352. // Encode packed representation key/value pair
  353. en.uvarint(key | 2)
  354. b := packed.Bytes()
  355. en.uvarint(uint64(len(b)))
  356. en.Write(b)
  357. }
  358. // Handle the encoding of an arbritary map[K]V
  359. func (en *encoder) handleMap(key uint64, mpval reflect.Value, prefix TagPrefix) {
  360. /*
  361. A map defined as
  362. map<key_type, value_type> map_field = N;
  363. is encoded in the same way as
  364. message MapFieldEntry {
  365. key_type key = 1;
  366. value_type value = 2;
  367. }
  368. repeated MapFieldEntry map_field = N;
  369. */
  370. for _, mkey := range mpval.MapKeys() {
  371. mval := mpval.MapIndex(mkey)
  372. // illegal map entry values
  373. // - nil message pointers.
  374. switch kind := mval.Kind(); kind {
  375. case reflect.Ptr:
  376. if mval.IsNil() {
  377. panic("proto: map has nil element")
  378. }
  379. case reflect.Slice, reflect.Array:
  380. if mval.Type().Elem().Kind() != reflect.Uint8 {
  381. panic("protobuf: map only support []byte or string as repeated value")
  382. }
  383. }
  384. packed := encoder{}
  385. packed.value(1<<3, mkey, prefix)
  386. packed.value(2<<3, mval, prefix)
  387. en.uvarint(key | 2)
  388. b := packed.Bytes()
  389. en.uvarint((uint64(len(b))))
  390. en.Write(b)
  391. }
  392. }
  393. var bytesType = reflect.TypeOf([]byte{})
  394. func (en *encoder) sliceReflect(key uint64, slval reflect.Value) {
  395. kind := slval.Kind()
  396. if kind != reflect.Slice && kind != reflect.Array {
  397. panic("no slice passed")
  398. }
  399. sllen := slval.Len()
  400. slelt := slval.Type().Elem()
  401. packed := encoder{}
  402. switch slelt.Kind() {
  403. case reflect.Bool:
  404. for i := 0; i < sllen; i++ {
  405. v := uint64(0)
  406. if slval.Index(i).Bool() {
  407. v = 1
  408. }
  409. packed.uvarint(v)
  410. }
  411. case reflect.Int, reflect.Int32, reflect.Int64:
  412. for i := 0; i < sllen; i++ {
  413. packed.svarint(slval.Index(i).Int())
  414. }
  415. case reflect.Uint32, reflect.Uint64:
  416. for i := 0; i < sllen; i++ {
  417. packed.uvarint(slval.Index(i).Uint())
  418. }
  419. case reflect.Float32:
  420. for i := 0; i < sllen; i++ {
  421. packed.u32(math.Float32bits(
  422. float32(slval.Index(i).Float())))
  423. }
  424. case reflect.Float64:
  425. for i := 0; i < sllen; i++ {
  426. packed.u64(math.Float64bits(slval.Index(i).Float()))
  427. }
  428. case reflect.Uint8: // Write the byte-slice as one key,value pair
  429. en.uvarint(key | 2)
  430. en.uvarint(uint64(sllen))
  431. var b []byte
  432. if slval.Kind() == reflect.Array {
  433. if slval.CanAddr() {
  434. sliceVal := slval.Slice(0, sllen)
  435. b = sliceVal.Convert(bytesType).Interface().([]byte)
  436. } else {
  437. sliceVal := reflect.MakeSlice(bytesType, sllen, sllen)
  438. reflect.Copy(sliceVal, slval)
  439. b = sliceVal.Interface().([]byte)
  440. }
  441. } else {
  442. b = slval.Convert(bytesType).Interface().([]byte)
  443. }
  444. en.Write(b)
  445. return
  446. default: // Write each element as a separate key,value pair
  447. t := slval.Type().Elem()
  448. if t.Kind() == reflect.Slice || t.Kind() == reflect.Array {
  449. subSlice := t.Elem()
  450. if subSlice.Kind() != reflect.Uint8 {
  451. panic("protobuf: no support for 2-dimensional array except for [][]byte")
  452. }
  453. }
  454. for i := 0; i < sllen; i++ {
  455. en.value(key, slval.Index(i), TagNone)
  456. }
  457. return
  458. }
  459. // Encode packed representation key/value pair
  460. en.uvarint(key | 2)
  461. b := packed.Bytes()
  462. en.uvarint(uint64(len(b)))
  463. en.Write(b)
  464. }
  465. func (en *encoder) uvarint(v uint64) {
  466. var b [binary.MaxVarintLen64]byte
  467. n := binary.PutUvarint(b[:], v)
  468. en.Write(b[:n])
  469. }
  470. func (en *encoder) svarint(v int64) {
  471. if v >= 0 {
  472. en.uvarint(uint64(v) << 1)
  473. } else {
  474. en.uvarint(^uint64(v << 1))
  475. }
  476. }
  477. func (en *encoder) u32(v uint32) {
  478. var b [4]byte
  479. b[0] = byte(v)
  480. b[1] = byte(v >> 8)
  481. b[2] = byte(v >> 16)
  482. b[3] = byte(v >> 24)
  483. en.Write(b[:])
  484. }
  485. func (en *encoder) u64(v uint64) {
  486. var b [8]byte
  487. b[0] = byte(v)
  488. b[1] = byte(v >> 8)
  489. b[2] = byte(v >> 16)
  490. b[3] = byte(v >> 24)
  491. b[4] = byte(v >> 32)
  492. b[5] = byte(v >> 40)
  493. b[6] = byte(v >> 48)
  494. b[7] = byte(v >> 56)
  495. en.Write(b[:])
  496. }