decode_gogo.go 5.9 KB

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