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