blob: f89ca163cd39a2a98844693eb154b3696f021395 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package resource
18
19import (
20 "fmt"
21 "io"
22 "math/bits"
23
24 "github.com/gogo/protobuf/proto"
25)
26
27var _ proto.Sizer = &Quantity{}
28
29func (m *Quantity) Marshal() (data []byte, err error) {
30 size := m.Size()
31 data = make([]byte, size)
32 n, err := m.MarshalToSizedBuffer(data[:size])
33 if err != nil {
34 return nil, err
35 }
36 return data[:n], nil
37}
38
39// MarshalTo is a customized version of the generated Protobuf unmarshaler for a struct
40// with a single string field.
41func (m *Quantity) MarshalTo(data []byte) (int, error) {
42 size := m.Size()
43 return m.MarshalToSizedBuffer(data[:size])
44}
45
46// MarshalToSizedBuffer is a customized version of the generated
47// Protobuf unmarshaler for a struct with a single string field.
48func (m *Quantity) MarshalToSizedBuffer(data []byte) (int, error) {
49 i := len(data)
50 _ = i
51 var l int
52 _ = l
53
54 // BEGIN CUSTOM MARSHAL
55 out := m.String()
56 i -= len(out)
57 copy(data[i:], out)
58 i = encodeVarintGenerated(data, i, uint64(len(out)))
59 // END CUSTOM MARSHAL
60 i--
61 data[i] = 0xa
62
63 return len(data) - i, nil
64}
65
66func encodeVarintGenerated(data []byte, offset int, v uint64) int {
67 offset -= sovGenerated(v)
68 base := offset
69 for v >= 1<<7 {
70 data[offset] = uint8(v&0x7f | 0x80)
71 v >>= 7
72 offset++
73 }
74 data[offset] = uint8(v)
75 return base
76}
77
78func (m *Quantity) Size() (n int) {
79 var l int
80 _ = l
81
82 // BEGIN CUSTOM SIZE
83 l = len(m.String())
84 // END CUSTOM SIZE
85
86 n += 1 + l + sovGenerated(uint64(l))
87 return n
88}
89
90func sovGenerated(x uint64) (n int) {
91 return (bits.Len64(x|1) + 6) / 7
92}
93
94// Unmarshal is a customized version of the generated Protobuf unmarshaler for a struct
95// with a single string field.
96func (m *Quantity) Unmarshal(data []byte) error {
97 l := len(data)
98 iNdEx := 0
99 for iNdEx < l {
100 preIndex := iNdEx
101 var wire uint64
102 for shift := uint(0); ; shift += 7 {
103 if shift >= 64 {
104 return ErrIntOverflowGenerated
105 }
106 if iNdEx >= l {
107 return io.ErrUnexpectedEOF
108 }
109 b := data[iNdEx]
110 iNdEx++
111 wire |= (uint64(b) & 0x7F) << shift
112 if b < 0x80 {
113 break
114 }
115 }
116 fieldNum := int32(wire >> 3)
117 wireType := int(wire & 0x7)
118 if wireType == 4 {
119 return fmt.Errorf("proto: Quantity: wiretype end group for non-group")
120 }
121 if fieldNum <= 0 {
122 return fmt.Errorf("proto: Quantity: illegal tag %d (wire type %d)", fieldNum, wire)
123 }
124 switch fieldNum {
125 case 1:
126 if wireType != 2 {
127 return fmt.Errorf("proto: wrong wireType = %d for field String_", wireType)
128 }
129 var stringLen uint64
130 for shift := uint(0); ; shift += 7 {
131 if shift >= 64 {
132 return ErrIntOverflowGenerated
133 }
134 if iNdEx >= l {
135 return io.ErrUnexpectedEOF
136 }
137 b := data[iNdEx]
138 iNdEx++
139 stringLen |= (uint64(b) & 0x7F) << shift
140 if b < 0x80 {
141 break
142 }
143 }
144 intStringLen := int(stringLen)
145 if intStringLen < 0 {
146 return ErrInvalidLengthGenerated
147 }
148 postIndex := iNdEx + intStringLen
149 if postIndex > l {
150 return io.ErrUnexpectedEOF
151 }
152 s := string(data[iNdEx:postIndex])
153
154 // BEGIN CUSTOM DECODE
155 p, err := ParseQuantity(s)
156 if err != nil {
157 return err
158 }
159 *m = p
160 // END CUSTOM DECODE
161
162 iNdEx = postIndex
163 default:
164 iNdEx = preIndex
165 skippy, err := skipGenerated(data[iNdEx:])
166 if err != nil {
167 return err
168 }
169 if skippy < 0 {
170 return ErrInvalidLengthGenerated
171 }
172 if (iNdEx + skippy) > l {
173 return io.ErrUnexpectedEOF
174 }
175 iNdEx += skippy
176 }
177 }
178
179 if iNdEx > l {
180 return io.ErrUnexpectedEOF
181 }
182 return nil
183}
184
185func skipGenerated(data []byte) (n int, err error) {
186 l := len(data)
187 iNdEx := 0
188 for iNdEx < l {
189 var wire uint64
190 for shift := uint(0); ; shift += 7 {
191 if shift >= 64 {
192 return 0, ErrIntOverflowGenerated
193 }
194 if iNdEx >= l {
195 return 0, io.ErrUnexpectedEOF
196 }
197 b := data[iNdEx]
198 iNdEx++
199 wire |= (uint64(b) & 0x7F) << shift
200 if b < 0x80 {
201 break
202 }
203 }
204 wireType := int(wire & 0x7)
205 switch wireType {
206 case 0:
207 for shift := uint(0); ; shift += 7 {
208 if shift >= 64 {
209 return 0, ErrIntOverflowGenerated
210 }
211 if iNdEx >= l {
212 return 0, io.ErrUnexpectedEOF
213 }
214 iNdEx++
215 if data[iNdEx-1] < 0x80 {
216 break
217 }
218 }
219 return iNdEx, nil
220 case 1:
221 iNdEx += 8
222 return iNdEx, nil
223 case 2:
224 var length int
225 for shift := uint(0); ; shift += 7 {
226 if shift >= 64 {
227 return 0, ErrIntOverflowGenerated
228 }
229 if iNdEx >= l {
230 return 0, io.ErrUnexpectedEOF
231 }
232 b := data[iNdEx]
233 iNdEx++
234 length |= (int(b) & 0x7F) << shift
235 if b < 0x80 {
236 break
237 }
238 }
239 iNdEx += length
240 if length < 0 {
241 return 0, ErrInvalidLengthGenerated
242 }
243 return iNdEx, nil
244 case 3:
245 for {
246 var innerWire uint64
247 var start int = iNdEx
248 for shift := uint(0); ; shift += 7 {
249 if shift >= 64 {
250 return 0, ErrIntOverflowGenerated
251 }
252 if iNdEx >= l {
253 return 0, io.ErrUnexpectedEOF
254 }
255 b := data[iNdEx]
256 iNdEx++
257 innerWire |= (uint64(b) & 0x7F) << shift
258 if b < 0x80 {
259 break
260 }
261 }
262 innerWireType := int(innerWire & 0x7)
263 if innerWireType == 4 {
264 break
265 }
266 next, err := skipGenerated(data[start:])
267 if err != nil {
268 return 0, err
269 }
270 iNdEx = start + next
271 }
272 return iNdEx, nil
273 case 4:
274 return iNdEx, nil
275 case 5:
276 iNdEx += 4
277 return iNdEx, nil
278 default:
279 return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
280 }
281 }
282 panic("unreachable")
283}
284
285var (
286 ErrInvalidLengthGenerated = fmt.Errorf("proto: negative length found during unmarshaling")
287 ErrIntOverflowGenerated = fmt.Errorf("proto: integer overflow")
288)