timestamp_gogo.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. var zero field
  78. setPtrCustomType(newBas, zero, &t)
  79. return nil
  80. }
  81. func (o *Buffer) dec_slice_ref_time(p *Properties, base structPointer) error {
  82. t, err := o.decTimestamp()
  83. if err != nil {
  84. return err
  85. }
  86. newBas := appendStructPointer(base, p.field, reflect.SliceOf(timeType))
  87. var zero field
  88. setCustomType(newBas, zero, &t)
  89. return nil
  90. }
  91. func size_time(p *Properties, base structPointer) (n int) {
  92. structp := structPointer_GetStructPointer(base, p.field)
  93. if structPointer_IsNil(structp) {
  94. return 0
  95. }
  96. tim := structPointer_Interface(structp, timeType).(*time.Time)
  97. t, err := timestampProto(*tim)
  98. if err != nil {
  99. return 0
  100. }
  101. size := Size(t)
  102. return size + sizeVarint(uint64(size)) + len(p.tagcode)
  103. }
  104. func (o *Buffer) enc_time(p *Properties, base structPointer) error {
  105. structp := structPointer_GetStructPointer(base, p.field)
  106. if structPointer_IsNil(structp) {
  107. return ErrNil
  108. }
  109. tim := structPointer_Interface(structp, timeType).(*time.Time)
  110. t, err := timestampProto(*tim)
  111. if err != nil {
  112. return err
  113. }
  114. data, err := Marshal(t)
  115. if err != nil {
  116. return err
  117. }
  118. o.buf = append(o.buf, p.tagcode...)
  119. o.EncodeRawBytes(data)
  120. return nil
  121. }
  122. func size_ref_time(p *Properties, base structPointer) (n int) {
  123. tim := structPointer_InterfaceAt(base, p.field, timeType).(*time.Time)
  124. t, err := timestampProto(*tim)
  125. if err != nil {
  126. return 0
  127. }
  128. size := Size(t)
  129. return size + sizeVarint(uint64(size)) + len(p.tagcode)
  130. }
  131. func (o *Buffer) enc_ref_time(p *Properties, base structPointer) error {
  132. tim := structPointer_InterfaceAt(base, p.field, timeType).(*time.Time)
  133. t, err := timestampProto(*tim)
  134. if err != nil {
  135. return err
  136. }
  137. data, err := Marshal(t)
  138. if err != nil {
  139. return err
  140. }
  141. o.buf = append(o.buf, p.tagcode...)
  142. o.EncodeRawBytes(data)
  143. return nil
  144. }
  145. func size_slice_time(p *Properties, base structPointer) (n int) {
  146. ptims := structPointer_InterfaceAt(base, p.field, reflect.SliceOf(reflect.PtrTo(timeType))).(*[]*time.Time)
  147. tims := *ptims
  148. for i := 0; i < len(tims); i++ {
  149. if tims[i] == nil {
  150. return 0
  151. }
  152. tproto, err := timestampProto(*tims[i])
  153. if err != nil {
  154. return 0
  155. }
  156. size := Size(tproto)
  157. n += len(p.tagcode) + size + sizeVarint(uint64(size))
  158. }
  159. return n
  160. }
  161. func (o *Buffer) enc_slice_time(p *Properties, base structPointer) error {
  162. ptims := structPointer_InterfaceAt(base, p.field, reflect.SliceOf(reflect.PtrTo(timeType))).(*[]*time.Time)
  163. tims := *ptims
  164. for i := 0; i < len(tims); i++ {
  165. if tims[i] == nil {
  166. return errRepeatedHasNil
  167. }
  168. tproto, err := timestampProto(*tims[i])
  169. if err != nil {
  170. return err
  171. }
  172. data, err := Marshal(tproto)
  173. if err != nil {
  174. return err
  175. }
  176. o.buf = append(o.buf, p.tagcode...)
  177. o.EncodeRawBytes(data)
  178. }
  179. return nil
  180. }
  181. func size_slice_ref_time(p *Properties, base structPointer) (n int) {
  182. ptims := structPointer_InterfaceAt(base, p.field, reflect.SliceOf(timeType)).(*[]time.Time)
  183. tims := *ptims
  184. for i := 0; i < len(tims); i++ {
  185. tproto, err := timestampProto(tims[i])
  186. if err != nil {
  187. return 0
  188. }
  189. size := Size(tproto)
  190. n += len(p.tagcode) + size + sizeVarint(uint64(size))
  191. }
  192. return n
  193. }
  194. func (o *Buffer) enc_slice_ref_time(p *Properties, base structPointer) error {
  195. ptims := structPointer_InterfaceAt(base, p.field, reflect.SliceOf(timeType)).(*[]time.Time)
  196. tims := *ptims
  197. for i := 0; i < len(tims); i++ {
  198. tproto, err := timestampProto(tims[i])
  199. if err != nil {
  200. return err
  201. }
  202. data, err := Marshal(tproto)
  203. if err != nil {
  204. return err
  205. }
  206. o.buf = append(o.buf, p.tagcode...)
  207. o.EncodeRawBytes(data)
  208. }
  209. return nil
  210. }