timestamp_gogo.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Protocol Buffers for Go with Gadgets
  2. //
  3. // Copyright (c) 2016, 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. "time"
  32. )
  33. var timeType = reflect.TypeOf((*time.Time)(nil)).Elem()
  34. type timestamp struct {
  35. Seconds int64 `protobuf:"varint,1,opt,name=seconds,proto3" json:"seconds,omitempty"`
  36. Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"`
  37. }
  38. func (m *timestamp) Reset() { *m = timestamp{} }
  39. func (*timestamp) ProtoMessage() {}
  40. func (*timestamp) String() string { return "timestamp<string>" }
  41. func init() {
  42. RegisterType((*timestamp)(nil), "gogo.protobuf.proto.timestamp")
  43. }
  44. func (o *Buffer) decTimestamp() (time.Time, error) {
  45. b, err := o.DecodeRawBytes(true)
  46. if err != nil {
  47. return time.Time{}, err
  48. }
  49. tproto := &timestamp{}
  50. if err := Unmarshal(b, tproto); err != nil {
  51. return time.Time{}, err
  52. }
  53. return timestampFromProto(tproto)
  54. }
  55. func (o *Buffer) dec_time(p *Properties, base structPointer) error {
  56. t, err := o.decTimestamp()
  57. if err != nil {
  58. return err
  59. }
  60. setPtrCustomType(base, p.field, &t)
  61. return nil
  62. }
  63. func (o *Buffer) dec_ref_time(p *Properties, base structPointer) error {
  64. t, err := o.decTimestamp()
  65. if err != nil {
  66. return err
  67. }
  68. setCustomType(base, p.field, &t)
  69. return nil
  70. }
  71. func (o *Buffer) dec_slice_time(p *Properties, base structPointer) error {
  72. t, err := o.decTimestamp()
  73. if err != nil {
  74. return err
  75. }
  76. newBas := appendStructPointer(base, p.field, reflect.SliceOf(reflect.PtrTo(timeType)))
  77. setPtrCustomType(newBas, 0, &t)
  78. return nil
  79. }
  80. func (o *Buffer) dec_slice_ref_time(p *Properties, base structPointer) error {
  81. t, err := o.decTimestamp()
  82. if err != nil {
  83. return err
  84. }
  85. newBas := appendStructPointer(base, p.field, reflect.SliceOf(timeType))
  86. setCustomType(newBas, 0, &t)
  87. return nil
  88. }
  89. func size_time(p *Properties, base structPointer) (n int) {
  90. structp := structPointer_GetStructPointer(base, p.field)
  91. if structPointer_IsNil(structp) {
  92. return 0
  93. }
  94. tim := structPointer_Interface(structp, timeType).(*time.Time)
  95. t, err := timestampProto(*tim)
  96. if err != nil {
  97. return 0
  98. }
  99. size := Size(t)
  100. return size + sizeVarint(uint64(size)) + len(p.tagcode)
  101. }
  102. func (o *Buffer) enc_time(p *Properties, base structPointer) error {
  103. structp := structPointer_GetStructPointer(base, p.field)
  104. if structPointer_IsNil(structp) {
  105. return ErrNil
  106. }
  107. tim := structPointer_Interface(structp, timeType).(*time.Time)
  108. t, err := timestampProto(*tim)
  109. if err != nil {
  110. return err
  111. }
  112. data, err := Marshal(t)
  113. if err != nil {
  114. return err
  115. }
  116. o.buf = append(o.buf, p.tagcode...)
  117. o.EncodeRawBytes(data)
  118. return nil
  119. }
  120. func size_ref_time(p *Properties, base structPointer) (n int) {
  121. tim := structPointer_InterfaceAt(base, p.field, timeType).(*time.Time)
  122. t, err := timestampProto(*tim)
  123. if err != nil {
  124. return 0
  125. }
  126. size := Size(t)
  127. return size + sizeVarint(uint64(size)) + len(p.tagcode)
  128. }
  129. func (o *Buffer) enc_ref_time(p *Properties, base structPointer) error {
  130. tim := structPointer_InterfaceAt(base, p.field, timeType).(*time.Time)
  131. t, err := timestampProto(*tim)
  132. if err != nil {
  133. return err
  134. }
  135. data, err := Marshal(t)
  136. if err != nil {
  137. return err
  138. }
  139. o.buf = append(o.buf, p.tagcode...)
  140. o.EncodeRawBytes(data)
  141. return nil
  142. }
  143. func size_slice_time(p *Properties, base structPointer) (n int) {
  144. ptims := structPointer_InterfaceAt(base, p.field, reflect.SliceOf(reflect.PtrTo(timeType))).(*[]*time.Time)
  145. tims := *ptims
  146. for i := 0; i < len(tims); i++ {
  147. if tims[i] == nil {
  148. return 0
  149. }
  150. tproto, err := timestampProto(*tims[i])
  151. if err != nil {
  152. return 0
  153. }
  154. size := Size(tproto)
  155. n += len(p.tagcode) + size + sizeVarint(uint64(size))
  156. }
  157. return n
  158. }
  159. func (o *Buffer) enc_slice_time(p *Properties, base structPointer) error {
  160. ptims := structPointer_InterfaceAt(base, p.field, reflect.SliceOf(reflect.PtrTo(timeType))).(*[]*time.Time)
  161. tims := *ptims
  162. for i := 0; i < len(tims); i++ {
  163. if tims[i] == nil {
  164. return errRepeatedHasNil
  165. }
  166. tproto, err := timestampProto(*tims[i])
  167. if err != nil {
  168. return err
  169. }
  170. data, err := Marshal(tproto)
  171. if err != nil {
  172. return err
  173. }
  174. o.buf = append(o.buf, p.tagcode...)
  175. o.EncodeRawBytes(data)
  176. }
  177. return nil
  178. }
  179. func size_slice_ref_time(p *Properties, base structPointer) (n int) {
  180. ptims := structPointer_InterfaceAt(base, p.field, reflect.SliceOf(timeType)).(*[]time.Time)
  181. tims := *ptims
  182. for i := 0; i < len(tims); i++ {
  183. tproto, err := timestampProto(tims[i])
  184. if err != nil {
  185. return 0
  186. }
  187. size := Size(tproto)
  188. n += len(p.tagcode) + size + sizeVarint(uint64(size))
  189. }
  190. return n
  191. }
  192. func (o *Buffer) enc_slice_ref_time(p *Properties, base structPointer) error {
  193. ptims := structPointer_InterfaceAt(base, p.field, reflect.SliceOf(timeType)).(*[]time.Time)
  194. tims := *ptims
  195. for i := 0; i < len(tims); i++ {
  196. tproto, err := timestampProto(tims[i])
  197. if err != nil {
  198. return err
  199. }
  200. data, err := Marshal(tproto)
  201. if err != nil {
  202. return err
  203. }
  204. o.buf = append(o.buf, p.tagcode...)
  205. o.EncodeRawBytes(data)
  206. }
  207. return nil
  208. }