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