quantity_proto.go 5.9 KB

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