quantity_proto.go 6.0 KB

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