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