duration_gogo.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 durationType = reflect.TypeOf((*time.Duration)(nil)).Elem()
  34. type duration 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 *duration) Reset() { *m = duration{} }
  39. func (*duration) ProtoMessage() {}
  40. func (*duration) String() string { return "duration<string>" }
  41. func init() {
  42. RegisterType((*duration)(nil), "gogo.protobuf.proto.duration")
  43. }
  44. func (o *Buffer) decDuration() (time.Duration, error) {
  45. b, err := o.DecodeRawBytes(true)
  46. if err != nil {
  47. return 0, err
  48. }
  49. dproto := &duration{}
  50. if err := Unmarshal(b, dproto); err != nil {
  51. return 0, err
  52. }
  53. return durationFromProto(dproto)
  54. }
  55. func (o *Buffer) dec_duration(p *Properties, base structPointer) error {
  56. d, err := o.decDuration()
  57. if err != nil {
  58. return err
  59. }
  60. word64_Set(structPointer_Word64(base, p.field), o, uint64(d))
  61. return nil
  62. }
  63. func (o *Buffer) dec_ref_duration(p *Properties, base structPointer) error {
  64. d, err := o.decDuration()
  65. if err != nil {
  66. return err
  67. }
  68. word64Val_Set(structPointer_Word64Val(base, p.field), o, uint64(d))
  69. return nil
  70. }
  71. func (o *Buffer) dec_slice_duration(p *Properties, base structPointer) error {
  72. d, err := o.decDuration()
  73. if err != nil {
  74. return err
  75. }
  76. newBas := appendStructPointer(base, p.field, reflect.SliceOf(reflect.PtrTo(durationType)))
  77. var zero field
  78. setPtrCustomType(newBas, zero, &d)
  79. return nil
  80. }
  81. func (o *Buffer) dec_slice_ref_duration(p *Properties, base structPointer) error {
  82. d, err := o.decDuration()
  83. if err != nil {
  84. return err
  85. }
  86. structPointer_Word64Slice(base, p.field).Append(uint64(d))
  87. return nil
  88. }
  89. func size_duration(p *Properties, base structPointer) (n int) {
  90. structp := structPointer_GetStructPointer(base, p.field)
  91. if structPointer_IsNil(structp) {
  92. return 0
  93. }
  94. dur := structPointer_Interface(structp, durationType).(*time.Duration)
  95. d := durationProto(*dur)
  96. size := Size(d)
  97. return size + sizeVarint(uint64(size)) + len(p.tagcode)
  98. }
  99. func (o *Buffer) enc_duration(p *Properties, base structPointer) error {
  100. structp := structPointer_GetStructPointer(base, p.field)
  101. if structPointer_IsNil(structp) {
  102. return ErrNil
  103. }
  104. dur := structPointer_Interface(structp, durationType).(*time.Duration)
  105. d := durationProto(*dur)
  106. data, err := Marshal(d)
  107. if err != nil {
  108. return err
  109. }
  110. o.buf = append(o.buf, p.tagcode...)
  111. o.EncodeRawBytes(data)
  112. return nil
  113. }
  114. func size_ref_duration(p *Properties, base structPointer) (n int) {
  115. dur := structPointer_InterfaceAt(base, p.field, durationType).(*time.Duration)
  116. d := durationProto(*dur)
  117. size := Size(d)
  118. return size + sizeVarint(uint64(size)) + len(p.tagcode)
  119. }
  120. func (o *Buffer) enc_ref_duration(p *Properties, base structPointer) error {
  121. dur := structPointer_InterfaceAt(base, p.field, durationType).(*time.Duration)
  122. d := durationProto(*dur)
  123. data, err := Marshal(d)
  124. if err != nil {
  125. return err
  126. }
  127. o.buf = append(o.buf, p.tagcode...)
  128. o.EncodeRawBytes(data)
  129. return nil
  130. }
  131. func size_slice_duration(p *Properties, base structPointer) (n int) {
  132. pdurs := structPointer_InterfaceAt(base, p.field, reflect.SliceOf(reflect.PtrTo(durationType))).(*[]*time.Duration)
  133. durs := *pdurs
  134. for i := 0; i < len(durs); i++ {
  135. if durs[i] == nil {
  136. return 0
  137. }
  138. dproto := durationProto(*durs[i])
  139. size := Size(dproto)
  140. n += len(p.tagcode) + size + sizeVarint(uint64(size))
  141. }
  142. return n
  143. }
  144. func (o *Buffer) enc_slice_duration(p *Properties, base structPointer) error {
  145. pdurs := structPointer_InterfaceAt(base, p.field, reflect.SliceOf(reflect.PtrTo(durationType))).(*[]*time.Duration)
  146. durs := *pdurs
  147. for i := 0; i < len(durs); i++ {
  148. if durs[i] == nil {
  149. return errRepeatedHasNil
  150. }
  151. dproto := durationProto(*durs[i])
  152. data, err := Marshal(dproto)
  153. if err != nil {
  154. return err
  155. }
  156. o.buf = append(o.buf, p.tagcode...)
  157. o.EncodeRawBytes(data)
  158. }
  159. return nil
  160. }
  161. func size_slice_ref_duration(p *Properties, base structPointer) (n int) {
  162. pdurs := structPointer_InterfaceAt(base, p.field, reflect.SliceOf(durationType)).(*[]time.Duration)
  163. durs := *pdurs
  164. for i := 0; i < len(durs); i++ {
  165. dproto := durationProto(durs[i])
  166. size := Size(dproto)
  167. n += len(p.tagcode) + size + sizeVarint(uint64(size))
  168. }
  169. return n
  170. }
  171. func (o *Buffer) enc_slice_ref_duration(p *Properties, base structPointer) error {
  172. pdurs := structPointer_InterfaceAt(base, p.field, reflect.SliceOf(durationType)).(*[]time.Duration)
  173. durs := *pdurs
  174. for i := 0; i < len(durs); i++ {
  175. dproto := durationProto(durs[i])
  176. data, err := Marshal(dproto)
  177. if err != nil {
  178. return err
  179. }
  180. o.buf = append(o.buf, p.tagcode...)
  181. o.EncodeRawBytes(data)
  182. }
  183. return nil
  184. }