decode_gogo.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Protocol Buffers for Go with Gadgets
  2. //
  3. // Copyright (c) 2013, The GoGo Authors. All rights reserved.
  4. // http://github.com/gogo/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. package proto
  29. import (
  30. "reflect"
  31. )
  32. // Decode a reference to a struct pointer.
  33. func (o *Buffer) dec_ref_struct_message(p *Properties, base structPointer) (err error) {
  34. raw, e := o.DecodeRawBytes(false)
  35. if e != nil {
  36. return e
  37. }
  38. // If the object can unmarshal itself, let it.
  39. if p.isUnmarshaler {
  40. panic("not supported, since this is a pointer receiver")
  41. }
  42. obuf := o.buf
  43. oi := o.index
  44. o.buf = raw
  45. o.index = 0
  46. bas := structPointer_FieldPointer(base, p.field)
  47. err = o.unmarshalType(p.stype, p.sprop, false, bas)
  48. o.buf = obuf
  49. o.index = oi
  50. return err
  51. }
  52. // Decode a slice of references to struct pointers ([]struct).
  53. func (o *Buffer) dec_slice_ref_struct(p *Properties, is_group bool, base structPointer) error {
  54. newBas := appendStructPointer(base, p.field, p.sstype)
  55. if is_group {
  56. panic("not supported, maybe in future, if requested.")
  57. }
  58. raw, err := o.DecodeRawBytes(false)
  59. if err != nil {
  60. return err
  61. }
  62. // If the object can unmarshal itself, let it.
  63. if p.isUnmarshaler {
  64. panic("not supported, since this is not a pointer receiver.")
  65. }
  66. obuf := o.buf
  67. oi := o.index
  68. o.buf = raw
  69. o.index = 0
  70. err = o.unmarshalType(p.stype, p.sprop, is_group, newBas)
  71. o.buf = obuf
  72. o.index = oi
  73. return err
  74. }
  75. // Decode a slice of references to struct pointers.
  76. func (o *Buffer) dec_slice_ref_struct_message(p *Properties, base structPointer) error {
  77. return o.dec_slice_ref_struct(p, false, base)
  78. }
  79. func setPtrCustomType(base structPointer, f field, v interface{}) {
  80. if v == nil {
  81. return
  82. }
  83. structPointer_SetStructPointer(base, f, toStructPointer(reflect.ValueOf(v)))
  84. }
  85. func setCustomType(base structPointer, f field, value interface{}) {
  86. if value == nil {
  87. return
  88. }
  89. v := reflect.ValueOf(value).Elem()
  90. t := reflect.TypeOf(value).Elem()
  91. kind := t.Kind()
  92. switch kind {
  93. case reflect.Slice:
  94. slice := reflect.MakeSlice(t, v.Len(), v.Cap())
  95. reflect.Copy(slice, v)
  96. oldHeader := structPointer_GetSliceHeader(base, f)
  97. oldHeader.Data = slice.Pointer()
  98. oldHeader.Len = v.Len()
  99. oldHeader.Cap = v.Cap()
  100. default:
  101. size := reflect.TypeOf(value).Elem().Size()
  102. structPointer_Copy(toStructPointer(reflect.ValueOf(value)), structPointer_Add(base, f), int(size))
  103. }
  104. }
  105. func (o *Buffer) dec_custom_bytes(p *Properties, base structPointer) error {
  106. b, err := o.DecodeRawBytes(true)
  107. if err != nil {
  108. return err
  109. }
  110. i := reflect.New(p.ctype.Elem()).Interface()
  111. custom := (i).(Unmarshaler)
  112. if err := custom.Unmarshal(b); err != nil {
  113. return err
  114. }
  115. setPtrCustomType(base, p.field, custom)
  116. return nil
  117. }
  118. func (o *Buffer) dec_custom_ref_bytes(p *Properties, base structPointer) error {
  119. b, err := o.DecodeRawBytes(true)
  120. if err != nil {
  121. return err
  122. }
  123. i := reflect.New(p.ctype).Interface()
  124. custom := (i).(Unmarshaler)
  125. if err := custom.Unmarshal(b); err != nil {
  126. return err
  127. }
  128. if custom != nil {
  129. setCustomType(base, p.field, custom)
  130. }
  131. return nil
  132. }
  133. // Decode a slice of bytes ([]byte) into a slice of custom types.
  134. func (o *Buffer) dec_custom_slice_bytes(p *Properties, base structPointer) error {
  135. b, err := o.DecodeRawBytes(true)
  136. if err != nil {
  137. return err
  138. }
  139. i := reflect.New(p.ctype.Elem()).Interface()
  140. custom := (i).(Unmarshaler)
  141. if err := custom.Unmarshal(b); err != nil {
  142. return err
  143. }
  144. newBas := appendStructPointer(base, p.field, p.ctype)
  145. var zero field
  146. setCustomType(newBas, zero, custom)
  147. return nil
  148. }