table_marshal.go 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2016 The Go Authors. All rights reserved.
  4. // https://github.com/golang/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. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package proto
  32. import (
  33. "errors"
  34. "fmt"
  35. "math"
  36. "reflect"
  37. "sort"
  38. "strconv"
  39. "strings"
  40. "sync"
  41. "sync/atomic"
  42. "unicode/utf8"
  43. )
  44. // a sizer takes a pointer to a field and the size of its tag, computes the size of
  45. // the encoded data.
  46. type sizer func(pointer, int) int
  47. // a marshaler takes a byte slice, a pointer to a field, and its tag (in wire format),
  48. // marshals the field to the end of the slice, returns the slice and error (if any).
  49. type marshaler func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error)
  50. // marshalInfo is the information used for marshaling a message.
  51. type marshalInfo struct {
  52. typ reflect.Type
  53. fields []*marshalFieldInfo
  54. unrecognized field // offset of XXX_unrecognized
  55. extensions field // offset of XXX_InternalExtensions
  56. v1extensions field // offset of XXX_extensions
  57. sizecache field // offset of XXX_sizecache
  58. initialized int32 // 0 -- only typ is set, 1 -- fully initialized
  59. messageset bool // uses message set wire format
  60. hasmarshaler bool // has custom marshaler
  61. sync.RWMutex // protect extElems map, also for initialization
  62. extElems map[int32]*marshalElemInfo // info of extension elements
  63. hassizer bool // has custom sizer
  64. hasprotosizer bool // has custom protosizer
  65. bytesExtensions field // offset of XXX_extensions where the field type is []byte
  66. }
  67. // marshalFieldInfo is the information used for marshaling a field of a message.
  68. type marshalFieldInfo struct {
  69. field field
  70. wiretag uint64 // tag in wire format
  71. tagsize int // size of tag in wire format
  72. sizer sizer
  73. marshaler marshaler
  74. isPointer bool
  75. required bool // field is required
  76. name string // name of the field, for error reporting
  77. oneofElems map[reflect.Type]*marshalElemInfo // info of oneof elements
  78. }
  79. // marshalElemInfo is the information used for marshaling an extension or oneof element.
  80. type marshalElemInfo struct {
  81. wiretag uint64 // tag in wire format
  82. tagsize int // size of tag in wire format
  83. sizer sizer
  84. marshaler marshaler
  85. isptr bool // elem is pointer typed, thus interface of this type is a direct interface (extension only)
  86. }
  87. var (
  88. marshalInfoMap = map[reflect.Type]*marshalInfo{}
  89. marshalInfoLock sync.Mutex
  90. uint8SliceType = reflect.TypeOf(([]uint8)(nil)).Kind()
  91. )
  92. // getMarshalInfo returns the information to marshal a given type of message.
  93. // The info it returns may not necessarily initialized.
  94. // t is the type of the message (NOT the pointer to it).
  95. func getMarshalInfo(t reflect.Type) *marshalInfo {
  96. marshalInfoLock.Lock()
  97. u, ok := marshalInfoMap[t]
  98. if !ok {
  99. u = &marshalInfo{typ: t}
  100. marshalInfoMap[t] = u
  101. }
  102. marshalInfoLock.Unlock()
  103. return u
  104. }
  105. // Size is the entry point from generated code,
  106. // and should be ONLY called by generated code.
  107. // It computes the size of encoded data of msg.
  108. // a is a pointer to a place to store cached marshal info.
  109. func (a *InternalMessageInfo) Size(msg Message) int {
  110. u := getMessageMarshalInfo(msg, a)
  111. ptr := toPointer(&msg)
  112. if ptr.isNil() {
  113. // We get here if msg is a typed nil ((*SomeMessage)(nil)),
  114. // so it satisfies the interface, and msg == nil wouldn't
  115. // catch it. We don't want crash in this case.
  116. return 0
  117. }
  118. return u.size(ptr)
  119. }
  120. // Marshal is the entry point from generated code,
  121. // and should be ONLY called by generated code.
  122. // It marshals msg to the end of b.
  123. // a is a pointer to a place to store cached marshal info.
  124. func (a *InternalMessageInfo) Marshal(b []byte, msg Message, deterministic bool) ([]byte, error) {
  125. u := getMessageMarshalInfo(msg, a)
  126. ptr := toPointer(&msg)
  127. if ptr.isNil() {
  128. // We get here if msg is a typed nil ((*SomeMessage)(nil)),
  129. // so it satisfies the interface, and msg == nil wouldn't
  130. // catch it. We don't want crash in this case.
  131. return b, ErrNil
  132. }
  133. return u.marshal(b, ptr, deterministic)
  134. }
  135. func getMessageMarshalInfo(msg interface{}, a *InternalMessageInfo) *marshalInfo {
  136. // u := a.marshal, but atomically.
  137. // We use an atomic here to ensure memory consistency.
  138. u := atomicLoadMarshalInfo(&a.marshal)
  139. if u == nil {
  140. // Get marshal information from type of message.
  141. t := reflect.ValueOf(msg).Type()
  142. if t.Kind() != reflect.Ptr {
  143. panic(fmt.Sprintf("cannot handle non-pointer message type %v", t))
  144. }
  145. u = getMarshalInfo(t.Elem())
  146. // Store it in the cache for later users.
  147. // a.marshal = u, but atomically.
  148. atomicStoreMarshalInfo(&a.marshal, u)
  149. }
  150. return u
  151. }
  152. // size is the main function to compute the size of the encoded data of a message.
  153. // ptr is the pointer to the message.
  154. func (u *marshalInfo) size(ptr pointer) int {
  155. if atomic.LoadInt32(&u.initialized) == 0 {
  156. u.computeMarshalInfo()
  157. }
  158. // If the message can marshal itself, let it do it, for compatibility.
  159. // NOTE: This is not efficient.
  160. if u.hasmarshaler {
  161. // Uses the message's Size method if available
  162. if u.hassizer {
  163. s := ptr.asPointerTo(u.typ).Interface().(Sizer)
  164. return s.Size()
  165. }
  166. // Uses the message's ProtoSize method if available
  167. if u.hasprotosizer {
  168. s := ptr.asPointerTo(u.typ).Interface().(ProtoSizer)
  169. return s.ProtoSize()
  170. }
  171. m := ptr.asPointerTo(u.typ).Interface().(Marshaler)
  172. b, _ := m.Marshal()
  173. return len(b)
  174. }
  175. n := 0
  176. for _, f := range u.fields {
  177. if f.isPointer && ptr.offset(f.field).getPointer().isNil() {
  178. // nil pointer always marshals to nothing
  179. continue
  180. }
  181. n += f.sizer(ptr.offset(f.field), f.tagsize)
  182. }
  183. if u.extensions.IsValid() {
  184. e := ptr.offset(u.extensions).toExtensions()
  185. if u.messageset {
  186. n += u.sizeMessageSet(e)
  187. } else {
  188. n += u.sizeExtensions(e)
  189. }
  190. }
  191. if u.v1extensions.IsValid() {
  192. m := *ptr.offset(u.v1extensions).toOldExtensions()
  193. n += u.sizeV1Extensions(m)
  194. }
  195. if u.bytesExtensions.IsValid() {
  196. s := *ptr.offset(u.bytesExtensions).toBytes()
  197. n += len(s)
  198. }
  199. if u.unrecognized.IsValid() {
  200. s := *ptr.offset(u.unrecognized).toBytes()
  201. n += len(s)
  202. }
  203. // cache the result for use in marshal
  204. if u.sizecache.IsValid() {
  205. atomic.StoreInt32(ptr.offset(u.sizecache).toInt32(), int32(n))
  206. }
  207. return n
  208. }
  209. // cachedsize gets the size from cache. If there is no cache (i.e. message is not generated),
  210. // fall back to compute the size.
  211. func (u *marshalInfo) cachedsize(ptr pointer) int {
  212. if u.sizecache.IsValid() {
  213. return int(atomic.LoadInt32(ptr.offset(u.sizecache).toInt32()))
  214. }
  215. return u.size(ptr)
  216. }
  217. // marshal is the main function to marshal a message. It takes a byte slice and appends
  218. // the encoded data to the end of the slice, returns the slice and error (if any).
  219. // ptr is the pointer to the message.
  220. // If deterministic is true, map is marshaled in deterministic order.
  221. func (u *marshalInfo) marshal(b []byte, ptr pointer, deterministic bool) ([]byte, error) {
  222. if atomic.LoadInt32(&u.initialized) == 0 {
  223. u.computeMarshalInfo()
  224. }
  225. // If the message can marshal itself, let it do it, for compatibility.
  226. // NOTE: This is not efficient.
  227. if u.hasmarshaler {
  228. m := ptr.asPointerTo(u.typ).Interface().(Marshaler)
  229. b1, err := m.Marshal()
  230. b = append(b, b1...)
  231. return b, err
  232. }
  233. var err, errLater error
  234. // The old marshaler encodes extensions at beginning.
  235. if u.extensions.IsValid() {
  236. e := ptr.offset(u.extensions).toExtensions()
  237. if u.messageset {
  238. b, err = u.appendMessageSet(b, e, deterministic)
  239. } else {
  240. b, err = u.appendExtensions(b, e, deterministic)
  241. }
  242. if err != nil {
  243. return b, err
  244. }
  245. }
  246. if u.v1extensions.IsValid() {
  247. m := *ptr.offset(u.v1extensions).toOldExtensions()
  248. b, err = u.appendV1Extensions(b, m, deterministic)
  249. if err != nil {
  250. return b, err
  251. }
  252. }
  253. if u.bytesExtensions.IsValid() {
  254. s := *ptr.offset(u.bytesExtensions).toBytes()
  255. b = append(b, s...)
  256. }
  257. for _, f := range u.fields {
  258. if f.required {
  259. if f.isPointer && ptr.offset(f.field).getPointer().isNil() {
  260. // Required field is not set.
  261. // We record the error but keep going, to give a complete marshaling.
  262. if errLater == nil {
  263. errLater = &RequiredNotSetError{f.name}
  264. }
  265. continue
  266. }
  267. }
  268. if f.isPointer && ptr.offset(f.field).getPointer().isNil() {
  269. // nil pointer always marshals to nothing
  270. continue
  271. }
  272. b, err = f.marshaler(b, ptr.offset(f.field), f.wiretag, deterministic)
  273. if err != nil {
  274. if err1, ok := err.(*RequiredNotSetError); ok {
  275. // Required field in submessage is not set.
  276. // We record the error but keep going, to give a complete marshaling.
  277. if errLater == nil {
  278. errLater = &RequiredNotSetError{f.name + "." + err1.field}
  279. }
  280. continue
  281. }
  282. if err == errRepeatedHasNil {
  283. err = errors.New("proto: repeated field " + f.name + " has nil element")
  284. }
  285. if err == errInvalidUTF8 {
  286. if errLater == nil {
  287. fullName := revProtoTypes[reflect.PtrTo(u.typ)] + "." + f.name
  288. errLater = &invalidUTF8Error{fullName}
  289. }
  290. continue
  291. }
  292. return b, err
  293. }
  294. }
  295. if u.unrecognized.IsValid() {
  296. s := *ptr.offset(u.unrecognized).toBytes()
  297. b = append(b, s...)
  298. }
  299. return b, errLater
  300. }
  301. // computeMarshalInfo initializes the marshal info.
  302. func (u *marshalInfo) computeMarshalInfo() {
  303. u.Lock()
  304. defer u.Unlock()
  305. if u.initialized != 0 { // non-atomic read is ok as it is protected by the lock
  306. return
  307. }
  308. t := u.typ
  309. u.unrecognized = invalidField
  310. u.extensions = invalidField
  311. u.v1extensions = invalidField
  312. u.bytesExtensions = invalidField
  313. u.sizecache = invalidField
  314. isOneofMessage := false
  315. if reflect.PtrTo(t).Implements(sizerType) {
  316. u.hassizer = true
  317. }
  318. if reflect.PtrTo(t).Implements(protosizerType) {
  319. u.hasprotosizer = true
  320. }
  321. // If the message can marshal itself, let it do it, for compatibility.
  322. // NOTE: This is not efficient.
  323. if reflect.PtrTo(t).Implements(marshalerType) {
  324. u.hasmarshaler = true
  325. atomic.StoreInt32(&u.initialized, 1)
  326. return
  327. }
  328. n := t.NumField()
  329. // deal with XXX fields first
  330. for i := 0; i < t.NumField(); i++ {
  331. f := t.Field(i)
  332. if f.Tag.Get("protobuf_oneof") != "" {
  333. isOneofMessage = true
  334. }
  335. if !strings.HasPrefix(f.Name, "XXX_") {
  336. continue
  337. }
  338. switch f.Name {
  339. case "XXX_sizecache":
  340. u.sizecache = toField(&f)
  341. case "XXX_unrecognized":
  342. u.unrecognized = toField(&f)
  343. case "XXX_InternalExtensions":
  344. u.extensions = toField(&f)
  345. u.messageset = f.Tag.Get("protobuf_messageset") == "1"
  346. case "XXX_extensions":
  347. if f.Type.Kind() == reflect.Map {
  348. u.v1extensions = toField(&f)
  349. } else {
  350. u.bytesExtensions = toField(&f)
  351. }
  352. case "XXX_NoUnkeyedLiteral":
  353. // nothing to do
  354. default:
  355. panic("unknown XXX field: " + f.Name)
  356. }
  357. n--
  358. }
  359. // get oneof implementers
  360. var oneofImplementers []interface{}
  361. // gogo: isOneofMessage is needed for embedded oneof messages, without a marshaler and unmarshaler
  362. if m, ok := reflect.Zero(reflect.PtrTo(t)).Interface().(oneofMessage); ok && isOneofMessage {
  363. _, _, _, oneofImplementers = m.XXX_OneofFuncs()
  364. }
  365. // normal fields
  366. fields := make([]marshalFieldInfo, n) // batch allocation
  367. u.fields = make([]*marshalFieldInfo, 0, n)
  368. for i, j := 0, 0; i < t.NumField(); i++ {
  369. f := t.Field(i)
  370. if strings.HasPrefix(f.Name, "XXX_") {
  371. continue
  372. }
  373. field := &fields[j]
  374. j++
  375. field.name = f.Name
  376. u.fields = append(u.fields, field)
  377. if f.Tag.Get("protobuf_oneof") != "" {
  378. field.computeOneofFieldInfo(&f, oneofImplementers)
  379. continue
  380. }
  381. if f.Tag.Get("protobuf") == "" {
  382. // field has no tag (not in generated message), ignore it
  383. u.fields = u.fields[:len(u.fields)-1]
  384. j--
  385. continue
  386. }
  387. field.computeMarshalFieldInfo(&f)
  388. }
  389. // fields are marshaled in tag order on the wire.
  390. sort.Sort(byTag(u.fields))
  391. atomic.StoreInt32(&u.initialized, 1)
  392. }
  393. // helper for sorting fields by tag
  394. type byTag []*marshalFieldInfo
  395. func (a byTag) Len() int { return len(a) }
  396. func (a byTag) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  397. func (a byTag) Less(i, j int) bool { return a[i].wiretag < a[j].wiretag }
  398. // getExtElemInfo returns the information to marshal an extension element.
  399. // The info it returns is initialized.
  400. func (u *marshalInfo) getExtElemInfo(desc *ExtensionDesc) *marshalElemInfo {
  401. // get from cache first
  402. u.RLock()
  403. e, ok := u.extElems[desc.Field]
  404. u.RUnlock()
  405. if ok {
  406. return e
  407. }
  408. t := reflect.TypeOf(desc.ExtensionType) // pointer or slice to basic type or struct
  409. tags := strings.Split(desc.Tag, ",")
  410. tag, err := strconv.Atoi(tags[1])
  411. if err != nil {
  412. panic("tag is not an integer")
  413. }
  414. wt := wiretype(tags[0])
  415. sizr, marshalr := typeMarshaler(t, tags, false, false)
  416. e = &marshalElemInfo{
  417. wiretag: uint64(tag)<<3 | wt,
  418. tagsize: SizeVarint(uint64(tag) << 3),
  419. sizer: sizr,
  420. marshaler: marshalr,
  421. isptr: t.Kind() == reflect.Ptr,
  422. }
  423. // update cache
  424. u.Lock()
  425. if u.extElems == nil {
  426. u.extElems = make(map[int32]*marshalElemInfo)
  427. }
  428. u.extElems[desc.Field] = e
  429. u.Unlock()
  430. return e
  431. }
  432. // computeMarshalFieldInfo fills up the information to marshal a field.
  433. func (fi *marshalFieldInfo) computeMarshalFieldInfo(f *reflect.StructField) {
  434. // parse protobuf tag of the field.
  435. // tag has format of "bytes,49,opt,name=foo,def=hello!"
  436. tags := strings.Split(f.Tag.Get("protobuf"), ",")
  437. if tags[0] == "" {
  438. return
  439. }
  440. tag, err := strconv.Atoi(tags[1])
  441. if err != nil {
  442. panic("tag is not an integer")
  443. }
  444. wt := wiretype(tags[0])
  445. if tags[2] == "req" {
  446. fi.required = true
  447. }
  448. fi.setTag(f, tag, wt)
  449. fi.setMarshaler(f, tags)
  450. }
  451. func (fi *marshalFieldInfo) computeOneofFieldInfo(f *reflect.StructField, oneofImplementers []interface{}) {
  452. fi.field = toField(f)
  453. fi.wiretag = math.MaxInt32 // Use a large tag number, make oneofs sorted at the end. This tag will not appear on the wire.
  454. fi.isPointer = true
  455. fi.sizer, fi.marshaler = makeOneOfMarshaler(fi, f)
  456. fi.oneofElems = make(map[reflect.Type]*marshalElemInfo)
  457. ityp := f.Type // interface type
  458. for _, o := range oneofImplementers {
  459. t := reflect.TypeOf(o)
  460. if !t.Implements(ityp) {
  461. continue
  462. }
  463. sf := t.Elem().Field(0) // oneof implementer is a struct with a single field
  464. tags := strings.Split(sf.Tag.Get("protobuf"), ",")
  465. tag, err := strconv.Atoi(tags[1])
  466. if err != nil {
  467. panic("tag is not an integer")
  468. }
  469. wt := wiretype(tags[0])
  470. sizr, marshalr := typeMarshaler(sf.Type, tags, false, true) // oneof should not omit any zero value
  471. fi.oneofElems[t.Elem()] = &marshalElemInfo{
  472. wiretag: uint64(tag)<<3 | wt,
  473. tagsize: SizeVarint(uint64(tag) << 3),
  474. sizer: sizr,
  475. marshaler: marshalr,
  476. }
  477. }
  478. }
  479. type oneofMessage interface {
  480. XXX_OneofFuncs() (func(Message, *Buffer) error, func(Message, int, int, *Buffer) (bool, error), func(Message) int, []interface{})
  481. }
  482. // wiretype returns the wire encoding of the type.
  483. func wiretype(encoding string) uint64 {
  484. switch encoding {
  485. case "fixed32":
  486. return WireFixed32
  487. case "fixed64":
  488. return WireFixed64
  489. case "varint", "zigzag32", "zigzag64":
  490. return WireVarint
  491. case "bytes":
  492. return WireBytes
  493. case "group":
  494. return WireStartGroup
  495. }
  496. panic("unknown wire type " + encoding)
  497. }
  498. // setTag fills up the tag (in wire format) and its size in the info of a field.
  499. func (fi *marshalFieldInfo) setTag(f *reflect.StructField, tag int, wt uint64) {
  500. fi.field = toField(f)
  501. fi.wiretag = uint64(tag)<<3 | wt
  502. fi.tagsize = SizeVarint(uint64(tag) << 3)
  503. }
  504. // setMarshaler fills up the sizer and marshaler in the info of a field.
  505. func (fi *marshalFieldInfo) setMarshaler(f *reflect.StructField, tags []string) {
  506. switch f.Type.Kind() {
  507. case reflect.Map:
  508. // map field
  509. fi.isPointer = true
  510. fi.sizer, fi.marshaler = makeMapMarshaler(f)
  511. return
  512. case reflect.Ptr, reflect.Slice:
  513. fi.isPointer = true
  514. }
  515. fi.sizer, fi.marshaler = typeMarshaler(f.Type, tags, true, false)
  516. }
  517. // typeMarshaler returns the sizer and marshaler of a given field.
  518. // t is the type of the field.
  519. // tags is the generated "protobuf" tag of the field.
  520. // If nozero is true, zero value is not marshaled to the wire.
  521. // If oneof is true, it is a oneof field.
  522. func typeMarshaler(t reflect.Type, tags []string, nozero, oneof bool) (sizer, marshaler) {
  523. encoding := tags[0]
  524. pointer := false
  525. slice := false
  526. if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
  527. slice = true
  528. t = t.Elem()
  529. }
  530. if t.Kind() == reflect.Ptr {
  531. pointer = true
  532. t = t.Elem()
  533. }
  534. packed := false
  535. proto3 := false
  536. ctype := false
  537. isTime := false
  538. isDuration := false
  539. isWktPointer := false
  540. validateUTF8 := true
  541. for i := 2; i < len(tags); i++ {
  542. if tags[i] == "packed" {
  543. packed = true
  544. }
  545. if tags[i] == "proto3" {
  546. proto3 = true
  547. }
  548. if strings.HasPrefix(tags[i], "customtype=") {
  549. ctype = true
  550. }
  551. if tags[i] == "stdtime" {
  552. isTime = true
  553. }
  554. if tags[i] == "stdduration" {
  555. isDuration = true
  556. }
  557. if tags[i] == "wktptr" {
  558. isWktPointer = true
  559. }
  560. }
  561. validateUTF8 = validateUTF8 && proto3
  562. if !proto3 && !pointer && !slice {
  563. nozero = false
  564. }
  565. if ctype {
  566. if reflect.PtrTo(t).Implements(customType) {
  567. if slice {
  568. return makeMessageRefSliceMarshaler(getMarshalInfo(t))
  569. }
  570. if pointer {
  571. return makeCustomPtrMarshaler(getMarshalInfo(t))
  572. }
  573. return makeCustomMarshaler(getMarshalInfo(t))
  574. } else {
  575. panic(fmt.Sprintf("custom type: type: %v, does not implement the proto.custom interface", t))
  576. }
  577. }
  578. if isTime {
  579. if pointer {
  580. if slice {
  581. return makeTimePtrSliceMarshaler(getMarshalInfo(t))
  582. }
  583. return makeTimePtrMarshaler(getMarshalInfo(t))
  584. }
  585. if slice {
  586. return makeTimeSliceMarshaler(getMarshalInfo(t))
  587. }
  588. return makeTimeMarshaler(getMarshalInfo(t))
  589. }
  590. if isDuration {
  591. if pointer {
  592. if slice {
  593. return makeDurationPtrSliceMarshaler(getMarshalInfo(t))
  594. }
  595. return makeDurationPtrMarshaler(getMarshalInfo(t))
  596. }
  597. if slice {
  598. return makeDurationSliceMarshaler(getMarshalInfo(t))
  599. }
  600. return makeDurationMarshaler(getMarshalInfo(t))
  601. }
  602. if isWktPointer {
  603. switch t.Kind() {
  604. case reflect.Float64:
  605. if pointer {
  606. if slice {
  607. return makeStdDoubleValuePtrSliceMarshaler(getMarshalInfo(t))
  608. }
  609. return makeStdDoubleValuePtrMarshaler(getMarshalInfo(t))
  610. }
  611. if slice {
  612. return makeStdDoubleValueSliceMarshaler(getMarshalInfo(t))
  613. }
  614. return makeStdDoubleValueMarshaler(getMarshalInfo(t))
  615. case reflect.Float32:
  616. if pointer {
  617. if slice {
  618. return makeStdFloatValuePtrSliceMarshaler(getMarshalInfo(t))
  619. }
  620. return makeStdFloatValuePtrMarshaler(getMarshalInfo(t))
  621. }
  622. if slice {
  623. return makeStdFloatValueSliceMarshaler(getMarshalInfo(t))
  624. }
  625. return makeStdFloatValueMarshaler(getMarshalInfo(t))
  626. case reflect.Int64:
  627. if pointer {
  628. if slice {
  629. return makeStdInt64ValuePtrSliceMarshaler(getMarshalInfo(t))
  630. }
  631. return makeStdInt64ValuePtrMarshaler(getMarshalInfo(t))
  632. }
  633. if slice {
  634. return makeStdInt64ValueSliceMarshaler(getMarshalInfo(t))
  635. }
  636. return makeStdInt64ValueMarshaler(getMarshalInfo(t))
  637. case reflect.Uint64:
  638. if pointer {
  639. if slice {
  640. return makeStdUInt64ValuePtrSliceMarshaler(getMarshalInfo(t))
  641. }
  642. return makeStdUInt64ValuePtrMarshaler(getMarshalInfo(t))
  643. }
  644. if slice {
  645. return makeStdUInt64ValueSliceMarshaler(getMarshalInfo(t))
  646. }
  647. return makeStdUInt64ValueMarshaler(getMarshalInfo(t))
  648. case reflect.Int32:
  649. if pointer {
  650. if slice {
  651. return makeStdInt32ValuePtrSliceMarshaler(getMarshalInfo(t))
  652. }
  653. return makeStdInt32ValuePtrMarshaler(getMarshalInfo(t))
  654. }
  655. if slice {
  656. return makeStdInt32ValueSliceMarshaler(getMarshalInfo(t))
  657. }
  658. return makeStdInt32ValueMarshaler(getMarshalInfo(t))
  659. case reflect.Uint32:
  660. if pointer {
  661. if slice {
  662. return makeStdUInt32ValuePtrSliceMarshaler(getMarshalInfo(t))
  663. }
  664. return makeStdUInt32ValuePtrMarshaler(getMarshalInfo(t))
  665. }
  666. if slice {
  667. return makeStdUInt32ValueSliceMarshaler(getMarshalInfo(t))
  668. }
  669. return makeStdUInt32ValueMarshaler(getMarshalInfo(t))
  670. case reflect.Bool:
  671. if pointer {
  672. if slice {
  673. return makeStdBoolValuePtrSliceMarshaler(getMarshalInfo(t))
  674. }
  675. return makeStdBoolValuePtrMarshaler(getMarshalInfo(t))
  676. }
  677. if slice {
  678. return makeStdBoolValueSliceMarshaler(getMarshalInfo(t))
  679. }
  680. return makeStdBoolValueMarshaler(getMarshalInfo(t))
  681. case reflect.String:
  682. if pointer {
  683. if slice {
  684. return makeStdStringValuePtrSliceMarshaler(getMarshalInfo(t))
  685. }
  686. return makeStdStringValuePtrMarshaler(getMarshalInfo(t))
  687. }
  688. if slice {
  689. return makeStdStringValueSliceMarshaler(getMarshalInfo(t))
  690. }
  691. return makeStdStringValueMarshaler(getMarshalInfo(t))
  692. case uint8SliceType:
  693. if pointer {
  694. if slice {
  695. return makeStdBytesValuePtrSliceMarshaler(getMarshalInfo(t))
  696. }
  697. return makeStdBytesValuePtrMarshaler(getMarshalInfo(t))
  698. }
  699. if slice {
  700. return makeStdBytesValueSliceMarshaler(getMarshalInfo(t))
  701. }
  702. return makeStdBytesValueMarshaler(getMarshalInfo(t))
  703. default:
  704. panic(fmt.Sprintf("unknown wktpointer type %#v", t))
  705. }
  706. }
  707. switch t.Kind() {
  708. case reflect.Bool:
  709. if pointer {
  710. return sizeBoolPtr, appendBoolPtr
  711. }
  712. if slice {
  713. if packed {
  714. return sizeBoolPackedSlice, appendBoolPackedSlice
  715. }
  716. return sizeBoolSlice, appendBoolSlice
  717. }
  718. if nozero {
  719. return sizeBoolValueNoZero, appendBoolValueNoZero
  720. }
  721. return sizeBoolValue, appendBoolValue
  722. case reflect.Uint32:
  723. switch encoding {
  724. case "fixed32":
  725. if pointer {
  726. return sizeFixed32Ptr, appendFixed32Ptr
  727. }
  728. if slice {
  729. if packed {
  730. return sizeFixed32PackedSlice, appendFixed32PackedSlice
  731. }
  732. return sizeFixed32Slice, appendFixed32Slice
  733. }
  734. if nozero {
  735. return sizeFixed32ValueNoZero, appendFixed32ValueNoZero
  736. }
  737. return sizeFixed32Value, appendFixed32Value
  738. case "varint":
  739. if pointer {
  740. return sizeVarint32Ptr, appendVarint32Ptr
  741. }
  742. if slice {
  743. if packed {
  744. return sizeVarint32PackedSlice, appendVarint32PackedSlice
  745. }
  746. return sizeVarint32Slice, appendVarint32Slice
  747. }
  748. if nozero {
  749. return sizeVarint32ValueNoZero, appendVarint32ValueNoZero
  750. }
  751. return sizeVarint32Value, appendVarint32Value
  752. }
  753. case reflect.Int32:
  754. switch encoding {
  755. case "fixed32":
  756. if pointer {
  757. return sizeFixedS32Ptr, appendFixedS32Ptr
  758. }
  759. if slice {
  760. if packed {
  761. return sizeFixedS32PackedSlice, appendFixedS32PackedSlice
  762. }
  763. return sizeFixedS32Slice, appendFixedS32Slice
  764. }
  765. if nozero {
  766. return sizeFixedS32ValueNoZero, appendFixedS32ValueNoZero
  767. }
  768. return sizeFixedS32Value, appendFixedS32Value
  769. case "varint":
  770. if pointer {
  771. return sizeVarintS32Ptr, appendVarintS32Ptr
  772. }
  773. if slice {
  774. if packed {
  775. return sizeVarintS32PackedSlice, appendVarintS32PackedSlice
  776. }
  777. return sizeVarintS32Slice, appendVarintS32Slice
  778. }
  779. if nozero {
  780. return sizeVarintS32ValueNoZero, appendVarintS32ValueNoZero
  781. }
  782. return sizeVarintS32Value, appendVarintS32Value
  783. case "zigzag32":
  784. if pointer {
  785. return sizeZigzag32Ptr, appendZigzag32Ptr
  786. }
  787. if slice {
  788. if packed {
  789. return sizeZigzag32PackedSlice, appendZigzag32PackedSlice
  790. }
  791. return sizeZigzag32Slice, appendZigzag32Slice
  792. }
  793. if nozero {
  794. return sizeZigzag32ValueNoZero, appendZigzag32ValueNoZero
  795. }
  796. return sizeZigzag32Value, appendZigzag32Value
  797. }
  798. case reflect.Uint64:
  799. switch encoding {
  800. case "fixed64":
  801. if pointer {
  802. return sizeFixed64Ptr, appendFixed64Ptr
  803. }
  804. if slice {
  805. if packed {
  806. return sizeFixed64PackedSlice, appendFixed64PackedSlice
  807. }
  808. return sizeFixed64Slice, appendFixed64Slice
  809. }
  810. if nozero {
  811. return sizeFixed64ValueNoZero, appendFixed64ValueNoZero
  812. }
  813. return sizeFixed64Value, appendFixed64Value
  814. case "varint":
  815. if pointer {
  816. return sizeVarint64Ptr, appendVarint64Ptr
  817. }
  818. if slice {
  819. if packed {
  820. return sizeVarint64PackedSlice, appendVarint64PackedSlice
  821. }
  822. return sizeVarint64Slice, appendVarint64Slice
  823. }
  824. if nozero {
  825. return sizeVarint64ValueNoZero, appendVarint64ValueNoZero
  826. }
  827. return sizeVarint64Value, appendVarint64Value
  828. }
  829. case reflect.Int64:
  830. switch encoding {
  831. case "fixed64":
  832. if pointer {
  833. return sizeFixedS64Ptr, appendFixedS64Ptr
  834. }
  835. if slice {
  836. if packed {
  837. return sizeFixedS64PackedSlice, appendFixedS64PackedSlice
  838. }
  839. return sizeFixedS64Slice, appendFixedS64Slice
  840. }
  841. if nozero {
  842. return sizeFixedS64ValueNoZero, appendFixedS64ValueNoZero
  843. }
  844. return sizeFixedS64Value, appendFixedS64Value
  845. case "varint":
  846. if pointer {
  847. return sizeVarintS64Ptr, appendVarintS64Ptr
  848. }
  849. if slice {
  850. if packed {
  851. return sizeVarintS64PackedSlice, appendVarintS64PackedSlice
  852. }
  853. return sizeVarintS64Slice, appendVarintS64Slice
  854. }
  855. if nozero {
  856. return sizeVarintS64ValueNoZero, appendVarintS64ValueNoZero
  857. }
  858. return sizeVarintS64Value, appendVarintS64Value
  859. case "zigzag64":
  860. if pointer {
  861. return sizeZigzag64Ptr, appendZigzag64Ptr
  862. }
  863. if slice {
  864. if packed {
  865. return sizeZigzag64PackedSlice, appendZigzag64PackedSlice
  866. }
  867. return sizeZigzag64Slice, appendZigzag64Slice
  868. }
  869. if nozero {
  870. return sizeZigzag64ValueNoZero, appendZigzag64ValueNoZero
  871. }
  872. return sizeZigzag64Value, appendZigzag64Value
  873. }
  874. case reflect.Float32:
  875. if pointer {
  876. return sizeFloat32Ptr, appendFloat32Ptr
  877. }
  878. if slice {
  879. if packed {
  880. return sizeFloat32PackedSlice, appendFloat32PackedSlice
  881. }
  882. return sizeFloat32Slice, appendFloat32Slice
  883. }
  884. if nozero {
  885. return sizeFloat32ValueNoZero, appendFloat32ValueNoZero
  886. }
  887. return sizeFloat32Value, appendFloat32Value
  888. case reflect.Float64:
  889. if pointer {
  890. return sizeFloat64Ptr, appendFloat64Ptr
  891. }
  892. if slice {
  893. if packed {
  894. return sizeFloat64PackedSlice, appendFloat64PackedSlice
  895. }
  896. return sizeFloat64Slice, appendFloat64Slice
  897. }
  898. if nozero {
  899. return sizeFloat64ValueNoZero, appendFloat64ValueNoZero
  900. }
  901. return sizeFloat64Value, appendFloat64Value
  902. case reflect.String:
  903. if validateUTF8 {
  904. if pointer {
  905. return sizeStringPtr, appendUTF8StringPtr
  906. }
  907. if slice {
  908. return sizeStringSlice, appendUTF8StringSlice
  909. }
  910. if nozero {
  911. return sizeStringValueNoZero, appendUTF8StringValueNoZero
  912. }
  913. return sizeStringValue, appendUTF8StringValue
  914. }
  915. if pointer {
  916. return sizeStringPtr, appendStringPtr
  917. }
  918. if slice {
  919. return sizeStringSlice, appendStringSlice
  920. }
  921. if nozero {
  922. return sizeStringValueNoZero, appendStringValueNoZero
  923. }
  924. return sizeStringValue, appendStringValue
  925. case reflect.Slice:
  926. if slice {
  927. return sizeBytesSlice, appendBytesSlice
  928. }
  929. if oneof {
  930. // Oneof bytes field may also have "proto3" tag.
  931. // We want to marshal it as a oneof field. Do this
  932. // check before the proto3 check.
  933. return sizeBytesOneof, appendBytesOneof
  934. }
  935. if proto3 {
  936. return sizeBytes3, appendBytes3
  937. }
  938. return sizeBytes, appendBytes
  939. case reflect.Struct:
  940. switch encoding {
  941. case "group":
  942. if slice {
  943. return makeGroupSliceMarshaler(getMarshalInfo(t))
  944. }
  945. return makeGroupMarshaler(getMarshalInfo(t))
  946. case "bytes":
  947. if pointer {
  948. if slice {
  949. return makeMessageSliceMarshaler(getMarshalInfo(t))
  950. }
  951. return makeMessageMarshaler(getMarshalInfo(t))
  952. } else {
  953. if slice {
  954. return makeMessageRefSliceMarshaler(getMarshalInfo(t))
  955. }
  956. return makeMessageRefMarshaler(getMarshalInfo(t))
  957. }
  958. }
  959. }
  960. panic(fmt.Sprintf("unknown or mismatched type: type: %v, wire type: %v", t, encoding))
  961. }
  962. // Below are functions to size/marshal a specific type of a field.
  963. // They are stored in the field's info, and called by function pointers.
  964. // They have type sizer or marshaler.
  965. func sizeFixed32Value(_ pointer, tagsize int) int {
  966. return 4 + tagsize
  967. }
  968. func sizeFixed32ValueNoZero(ptr pointer, tagsize int) int {
  969. v := *ptr.toUint32()
  970. if v == 0 {
  971. return 0
  972. }
  973. return 4 + tagsize
  974. }
  975. func sizeFixed32Ptr(ptr pointer, tagsize int) int {
  976. p := *ptr.toUint32Ptr()
  977. if p == nil {
  978. return 0
  979. }
  980. return 4 + tagsize
  981. }
  982. func sizeFixed32Slice(ptr pointer, tagsize int) int {
  983. s := *ptr.toUint32Slice()
  984. return (4 + tagsize) * len(s)
  985. }
  986. func sizeFixed32PackedSlice(ptr pointer, tagsize int) int {
  987. s := *ptr.toUint32Slice()
  988. if len(s) == 0 {
  989. return 0
  990. }
  991. return 4*len(s) + SizeVarint(uint64(4*len(s))) + tagsize
  992. }
  993. func sizeFixedS32Value(_ pointer, tagsize int) int {
  994. return 4 + tagsize
  995. }
  996. func sizeFixedS32ValueNoZero(ptr pointer, tagsize int) int {
  997. v := *ptr.toInt32()
  998. if v == 0 {
  999. return 0
  1000. }
  1001. return 4 + tagsize
  1002. }
  1003. func sizeFixedS32Ptr(ptr pointer, tagsize int) int {
  1004. p := ptr.getInt32Ptr()
  1005. if p == nil {
  1006. return 0
  1007. }
  1008. return 4 + tagsize
  1009. }
  1010. func sizeFixedS32Slice(ptr pointer, tagsize int) int {
  1011. s := ptr.getInt32Slice()
  1012. return (4 + tagsize) * len(s)
  1013. }
  1014. func sizeFixedS32PackedSlice(ptr pointer, tagsize int) int {
  1015. s := ptr.getInt32Slice()
  1016. if len(s) == 0 {
  1017. return 0
  1018. }
  1019. return 4*len(s) + SizeVarint(uint64(4*len(s))) + tagsize
  1020. }
  1021. func sizeFloat32Value(_ pointer, tagsize int) int {
  1022. return 4 + tagsize
  1023. }
  1024. func sizeFloat32ValueNoZero(ptr pointer, tagsize int) int {
  1025. v := math.Float32bits(*ptr.toFloat32())
  1026. if v == 0 {
  1027. return 0
  1028. }
  1029. return 4 + tagsize
  1030. }
  1031. func sizeFloat32Ptr(ptr pointer, tagsize int) int {
  1032. p := *ptr.toFloat32Ptr()
  1033. if p == nil {
  1034. return 0
  1035. }
  1036. return 4 + tagsize
  1037. }
  1038. func sizeFloat32Slice(ptr pointer, tagsize int) int {
  1039. s := *ptr.toFloat32Slice()
  1040. return (4 + tagsize) * len(s)
  1041. }
  1042. func sizeFloat32PackedSlice(ptr pointer, tagsize int) int {
  1043. s := *ptr.toFloat32Slice()
  1044. if len(s) == 0 {
  1045. return 0
  1046. }
  1047. return 4*len(s) + SizeVarint(uint64(4*len(s))) + tagsize
  1048. }
  1049. func sizeFixed64Value(_ pointer, tagsize int) int {
  1050. return 8 + tagsize
  1051. }
  1052. func sizeFixed64ValueNoZero(ptr pointer, tagsize int) int {
  1053. v := *ptr.toUint64()
  1054. if v == 0 {
  1055. return 0
  1056. }
  1057. return 8 + tagsize
  1058. }
  1059. func sizeFixed64Ptr(ptr pointer, tagsize int) int {
  1060. p := *ptr.toUint64Ptr()
  1061. if p == nil {
  1062. return 0
  1063. }
  1064. return 8 + tagsize
  1065. }
  1066. func sizeFixed64Slice(ptr pointer, tagsize int) int {
  1067. s := *ptr.toUint64Slice()
  1068. return (8 + tagsize) * len(s)
  1069. }
  1070. func sizeFixed64PackedSlice(ptr pointer, tagsize int) int {
  1071. s := *ptr.toUint64Slice()
  1072. if len(s) == 0 {
  1073. return 0
  1074. }
  1075. return 8*len(s) + SizeVarint(uint64(8*len(s))) + tagsize
  1076. }
  1077. func sizeFixedS64Value(_ pointer, tagsize int) int {
  1078. return 8 + tagsize
  1079. }
  1080. func sizeFixedS64ValueNoZero(ptr pointer, tagsize int) int {
  1081. v := *ptr.toInt64()
  1082. if v == 0 {
  1083. return 0
  1084. }
  1085. return 8 + tagsize
  1086. }
  1087. func sizeFixedS64Ptr(ptr pointer, tagsize int) int {
  1088. p := *ptr.toInt64Ptr()
  1089. if p == nil {
  1090. return 0
  1091. }
  1092. return 8 + tagsize
  1093. }
  1094. func sizeFixedS64Slice(ptr pointer, tagsize int) int {
  1095. s := *ptr.toInt64Slice()
  1096. return (8 + tagsize) * len(s)
  1097. }
  1098. func sizeFixedS64PackedSlice(ptr pointer, tagsize int) int {
  1099. s := *ptr.toInt64Slice()
  1100. if len(s) == 0 {
  1101. return 0
  1102. }
  1103. return 8*len(s) + SizeVarint(uint64(8*len(s))) + tagsize
  1104. }
  1105. func sizeFloat64Value(_ pointer, tagsize int) int {
  1106. return 8 + tagsize
  1107. }
  1108. func sizeFloat64ValueNoZero(ptr pointer, tagsize int) int {
  1109. v := math.Float64bits(*ptr.toFloat64())
  1110. if v == 0 {
  1111. return 0
  1112. }
  1113. return 8 + tagsize
  1114. }
  1115. func sizeFloat64Ptr(ptr pointer, tagsize int) int {
  1116. p := *ptr.toFloat64Ptr()
  1117. if p == nil {
  1118. return 0
  1119. }
  1120. return 8 + tagsize
  1121. }
  1122. func sizeFloat64Slice(ptr pointer, tagsize int) int {
  1123. s := *ptr.toFloat64Slice()
  1124. return (8 + tagsize) * len(s)
  1125. }
  1126. func sizeFloat64PackedSlice(ptr pointer, tagsize int) int {
  1127. s := *ptr.toFloat64Slice()
  1128. if len(s) == 0 {
  1129. return 0
  1130. }
  1131. return 8*len(s) + SizeVarint(uint64(8*len(s))) + tagsize
  1132. }
  1133. func sizeVarint32Value(ptr pointer, tagsize int) int {
  1134. v := *ptr.toUint32()
  1135. return SizeVarint(uint64(v)) + tagsize
  1136. }
  1137. func sizeVarint32ValueNoZero(ptr pointer, tagsize int) int {
  1138. v := *ptr.toUint32()
  1139. if v == 0 {
  1140. return 0
  1141. }
  1142. return SizeVarint(uint64(v)) + tagsize
  1143. }
  1144. func sizeVarint32Ptr(ptr pointer, tagsize int) int {
  1145. p := *ptr.toUint32Ptr()
  1146. if p == nil {
  1147. return 0
  1148. }
  1149. return SizeVarint(uint64(*p)) + tagsize
  1150. }
  1151. func sizeVarint32Slice(ptr pointer, tagsize int) int {
  1152. s := *ptr.toUint32Slice()
  1153. n := 0
  1154. for _, v := range s {
  1155. n += SizeVarint(uint64(v)) + tagsize
  1156. }
  1157. return n
  1158. }
  1159. func sizeVarint32PackedSlice(ptr pointer, tagsize int) int {
  1160. s := *ptr.toUint32Slice()
  1161. if len(s) == 0 {
  1162. return 0
  1163. }
  1164. n := 0
  1165. for _, v := range s {
  1166. n += SizeVarint(uint64(v))
  1167. }
  1168. return n + SizeVarint(uint64(n)) + tagsize
  1169. }
  1170. func sizeVarintS32Value(ptr pointer, tagsize int) int {
  1171. v := *ptr.toInt32()
  1172. return SizeVarint(uint64(v)) + tagsize
  1173. }
  1174. func sizeVarintS32ValueNoZero(ptr pointer, tagsize int) int {
  1175. v := *ptr.toInt32()
  1176. if v == 0 {
  1177. return 0
  1178. }
  1179. return SizeVarint(uint64(v)) + tagsize
  1180. }
  1181. func sizeVarintS32Ptr(ptr pointer, tagsize int) int {
  1182. p := ptr.getInt32Ptr()
  1183. if p == nil {
  1184. return 0
  1185. }
  1186. return SizeVarint(uint64(*p)) + tagsize
  1187. }
  1188. func sizeVarintS32Slice(ptr pointer, tagsize int) int {
  1189. s := ptr.getInt32Slice()
  1190. n := 0
  1191. for _, v := range s {
  1192. n += SizeVarint(uint64(v)) + tagsize
  1193. }
  1194. return n
  1195. }
  1196. func sizeVarintS32PackedSlice(ptr pointer, tagsize int) int {
  1197. s := ptr.getInt32Slice()
  1198. if len(s) == 0 {
  1199. return 0
  1200. }
  1201. n := 0
  1202. for _, v := range s {
  1203. n += SizeVarint(uint64(v))
  1204. }
  1205. return n + SizeVarint(uint64(n)) + tagsize
  1206. }
  1207. func sizeVarint64Value(ptr pointer, tagsize int) int {
  1208. v := *ptr.toUint64()
  1209. return SizeVarint(v) + tagsize
  1210. }
  1211. func sizeVarint64ValueNoZero(ptr pointer, tagsize int) int {
  1212. v := *ptr.toUint64()
  1213. if v == 0 {
  1214. return 0
  1215. }
  1216. return SizeVarint(v) + tagsize
  1217. }
  1218. func sizeVarint64Ptr(ptr pointer, tagsize int) int {
  1219. p := *ptr.toUint64Ptr()
  1220. if p == nil {
  1221. return 0
  1222. }
  1223. return SizeVarint(*p) + tagsize
  1224. }
  1225. func sizeVarint64Slice(ptr pointer, tagsize int) int {
  1226. s := *ptr.toUint64Slice()
  1227. n := 0
  1228. for _, v := range s {
  1229. n += SizeVarint(v) + tagsize
  1230. }
  1231. return n
  1232. }
  1233. func sizeVarint64PackedSlice(ptr pointer, tagsize int) int {
  1234. s := *ptr.toUint64Slice()
  1235. if len(s) == 0 {
  1236. return 0
  1237. }
  1238. n := 0
  1239. for _, v := range s {
  1240. n += SizeVarint(v)
  1241. }
  1242. return n + SizeVarint(uint64(n)) + tagsize
  1243. }
  1244. func sizeVarintS64Value(ptr pointer, tagsize int) int {
  1245. v := *ptr.toInt64()
  1246. return SizeVarint(uint64(v)) + tagsize
  1247. }
  1248. func sizeVarintS64ValueNoZero(ptr pointer, tagsize int) int {
  1249. v := *ptr.toInt64()
  1250. if v == 0 {
  1251. return 0
  1252. }
  1253. return SizeVarint(uint64(v)) + tagsize
  1254. }
  1255. func sizeVarintS64Ptr(ptr pointer, tagsize int) int {
  1256. p := *ptr.toInt64Ptr()
  1257. if p == nil {
  1258. return 0
  1259. }
  1260. return SizeVarint(uint64(*p)) + tagsize
  1261. }
  1262. func sizeVarintS64Slice(ptr pointer, tagsize int) int {
  1263. s := *ptr.toInt64Slice()
  1264. n := 0
  1265. for _, v := range s {
  1266. n += SizeVarint(uint64(v)) + tagsize
  1267. }
  1268. return n
  1269. }
  1270. func sizeVarintS64PackedSlice(ptr pointer, tagsize int) int {
  1271. s := *ptr.toInt64Slice()
  1272. if len(s) == 0 {
  1273. return 0
  1274. }
  1275. n := 0
  1276. for _, v := range s {
  1277. n += SizeVarint(uint64(v))
  1278. }
  1279. return n + SizeVarint(uint64(n)) + tagsize
  1280. }
  1281. func sizeZigzag32Value(ptr pointer, tagsize int) int {
  1282. v := *ptr.toInt32()
  1283. return SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
  1284. }
  1285. func sizeZigzag32ValueNoZero(ptr pointer, tagsize int) int {
  1286. v := *ptr.toInt32()
  1287. if v == 0 {
  1288. return 0
  1289. }
  1290. return SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
  1291. }
  1292. func sizeZigzag32Ptr(ptr pointer, tagsize int) int {
  1293. p := ptr.getInt32Ptr()
  1294. if p == nil {
  1295. return 0
  1296. }
  1297. v := *p
  1298. return SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
  1299. }
  1300. func sizeZigzag32Slice(ptr pointer, tagsize int) int {
  1301. s := ptr.getInt32Slice()
  1302. n := 0
  1303. for _, v := range s {
  1304. n += SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
  1305. }
  1306. return n
  1307. }
  1308. func sizeZigzag32PackedSlice(ptr pointer, tagsize int) int {
  1309. s := ptr.getInt32Slice()
  1310. if len(s) == 0 {
  1311. return 0
  1312. }
  1313. n := 0
  1314. for _, v := range s {
  1315. n += SizeVarint(uint64((uint32(v) << 1) ^ uint32((int32(v) >> 31))))
  1316. }
  1317. return n + SizeVarint(uint64(n)) + tagsize
  1318. }
  1319. func sizeZigzag64Value(ptr pointer, tagsize int) int {
  1320. v := *ptr.toInt64()
  1321. return SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
  1322. }
  1323. func sizeZigzag64ValueNoZero(ptr pointer, tagsize int) int {
  1324. v := *ptr.toInt64()
  1325. if v == 0 {
  1326. return 0
  1327. }
  1328. return SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
  1329. }
  1330. func sizeZigzag64Ptr(ptr pointer, tagsize int) int {
  1331. p := *ptr.toInt64Ptr()
  1332. if p == nil {
  1333. return 0
  1334. }
  1335. v := *p
  1336. return SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
  1337. }
  1338. func sizeZigzag64Slice(ptr pointer, tagsize int) int {
  1339. s := *ptr.toInt64Slice()
  1340. n := 0
  1341. for _, v := range s {
  1342. n += SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
  1343. }
  1344. return n
  1345. }
  1346. func sizeZigzag64PackedSlice(ptr pointer, tagsize int) int {
  1347. s := *ptr.toInt64Slice()
  1348. if len(s) == 0 {
  1349. return 0
  1350. }
  1351. n := 0
  1352. for _, v := range s {
  1353. n += SizeVarint(uint64(v<<1) ^ uint64((int64(v) >> 63)))
  1354. }
  1355. return n + SizeVarint(uint64(n)) + tagsize
  1356. }
  1357. func sizeBoolValue(_ pointer, tagsize int) int {
  1358. return 1 + tagsize
  1359. }
  1360. func sizeBoolValueNoZero(ptr pointer, tagsize int) int {
  1361. v := *ptr.toBool()
  1362. if !v {
  1363. return 0
  1364. }
  1365. return 1 + tagsize
  1366. }
  1367. func sizeBoolPtr(ptr pointer, tagsize int) int {
  1368. p := *ptr.toBoolPtr()
  1369. if p == nil {
  1370. return 0
  1371. }
  1372. return 1 + tagsize
  1373. }
  1374. func sizeBoolSlice(ptr pointer, tagsize int) int {
  1375. s := *ptr.toBoolSlice()
  1376. return (1 + tagsize) * len(s)
  1377. }
  1378. func sizeBoolPackedSlice(ptr pointer, tagsize int) int {
  1379. s := *ptr.toBoolSlice()
  1380. if len(s) == 0 {
  1381. return 0
  1382. }
  1383. return len(s) + SizeVarint(uint64(len(s))) + tagsize
  1384. }
  1385. func sizeStringValue(ptr pointer, tagsize int) int {
  1386. v := *ptr.toString()
  1387. return len(v) + SizeVarint(uint64(len(v))) + tagsize
  1388. }
  1389. func sizeStringValueNoZero(ptr pointer, tagsize int) int {
  1390. v := *ptr.toString()
  1391. if v == "" {
  1392. return 0
  1393. }
  1394. return len(v) + SizeVarint(uint64(len(v))) + tagsize
  1395. }
  1396. func sizeStringPtr(ptr pointer, tagsize int) int {
  1397. p := *ptr.toStringPtr()
  1398. if p == nil {
  1399. return 0
  1400. }
  1401. v := *p
  1402. return len(v) + SizeVarint(uint64(len(v))) + tagsize
  1403. }
  1404. func sizeStringSlice(ptr pointer, tagsize int) int {
  1405. s := *ptr.toStringSlice()
  1406. n := 0
  1407. for _, v := range s {
  1408. n += len(v) + SizeVarint(uint64(len(v))) + tagsize
  1409. }
  1410. return n
  1411. }
  1412. func sizeBytes(ptr pointer, tagsize int) int {
  1413. v := *ptr.toBytes()
  1414. if v == nil {
  1415. return 0
  1416. }
  1417. return len(v) + SizeVarint(uint64(len(v))) + tagsize
  1418. }
  1419. func sizeBytes3(ptr pointer, tagsize int) int {
  1420. v := *ptr.toBytes()
  1421. if len(v) == 0 {
  1422. return 0
  1423. }
  1424. return len(v) + SizeVarint(uint64(len(v))) + tagsize
  1425. }
  1426. func sizeBytesOneof(ptr pointer, tagsize int) int {
  1427. v := *ptr.toBytes()
  1428. return len(v) + SizeVarint(uint64(len(v))) + tagsize
  1429. }
  1430. func sizeBytesSlice(ptr pointer, tagsize int) int {
  1431. s := *ptr.toBytesSlice()
  1432. n := 0
  1433. for _, v := range s {
  1434. n += len(v) + SizeVarint(uint64(len(v))) + tagsize
  1435. }
  1436. return n
  1437. }
  1438. // appendFixed32 appends an encoded fixed32 to b.
  1439. func appendFixed32(b []byte, v uint32) []byte {
  1440. b = append(b,
  1441. byte(v),
  1442. byte(v>>8),
  1443. byte(v>>16),
  1444. byte(v>>24))
  1445. return b
  1446. }
  1447. // appendFixed64 appends an encoded fixed64 to b.
  1448. func appendFixed64(b []byte, v uint64) []byte {
  1449. b = append(b,
  1450. byte(v),
  1451. byte(v>>8),
  1452. byte(v>>16),
  1453. byte(v>>24),
  1454. byte(v>>32),
  1455. byte(v>>40),
  1456. byte(v>>48),
  1457. byte(v>>56))
  1458. return b
  1459. }
  1460. // appendVarint appends an encoded varint to b.
  1461. func appendVarint(b []byte, v uint64) []byte {
  1462. // TODO: make 1-byte (maybe 2-byte) case inline-able, once we
  1463. // have non-leaf inliner.
  1464. switch {
  1465. case v < 1<<7:
  1466. b = append(b, byte(v))
  1467. case v < 1<<14:
  1468. b = append(b,
  1469. byte(v&0x7f|0x80),
  1470. byte(v>>7))
  1471. case v < 1<<21:
  1472. b = append(b,
  1473. byte(v&0x7f|0x80),
  1474. byte((v>>7)&0x7f|0x80),
  1475. byte(v>>14))
  1476. case v < 1<<28:
  1477. b = append(b,
  1478. byte(v&0x7f|0x80),
  1479. byte((v>>7)&0x7f|0x80),
  1480. byte((v>>14)&0x7f|0x80),
  1481. byte(v>>21))
  1482. case v < 1<<35:
  1483. b = append(b,
  1484. byte(v&0x7f|0x80),
  1485. byte((v>>7)&0x7f|0x80),
  1486. byte((v>>14)&0x7f|0x80),
  1487. byte((v>>21)&0x7f|0x80),
  1488. byte(v>>28))
  1489. case v < 1<<42:
  1490. b = append(b,
  1491. byte(v&0x7f|0x80),
  1492. byte((v>>7)&0x7f|0x80),
  1493. byte((v>>14)&0x7f|0x80),
  1494. byte((v>>21)&0x7f|0x80),
  1495. byte((v>>28)&0x7f|0x80),
  1496. byte(v>>35))
  1497. case v < 1<<49:
  1498. b = append(b,
  1499. byte(v&0x7f|0x80),
  1500. byte((v>>7)&0x7f|0x80),
  1501. byte((v>>14)&0x7f|0x80),
  1502. byte((v>>21)&0x7f|0x80),
  1503. byte((v>>28)&0x7f|0x80),
  1504. byte((v>>35)&0x7f|0x80),
  1505. byte(v>>42))
  1506. case v < 1<<56:
  1507. b = append(b,
  1508. byte(v&0x7f|0x80),
  1509. byte((v>>7)&0x7f|0x80),
  1510. byte((v>>14)&0x7f|0x80),
  1511. byte((v>>21)&0x7f|0x80),
  1512. byte((v>>28)&0x7f|0x80),
  1513. byte((v>>35)&0x7f|0x80),
  1514. byte((v>>42)&0x7f|0x80),
  1515. byte(v>>49))
  1516. case v < 1<<63:
  1517. b = append(b,
  1518. byte(v&0x7f|0x80),
  1519. byte((v>>7)&0x7f|0x80),
  1520. byte((v>>14)&0x7f|0x80),
  1521. byte((v>>21)&0x7f|0x80),
  1522. byte((v>>28)&0x7f|0x80),
  1523. byte((v>>35)&0x7f|0x80),
  1524. byte((v>>42)&0x7f|0x80),
  1525. byte((v>>49)&0x7f|0x80),
  1526. byte(v>>56))
  1527. default:
  1528. b = append(b,
  1529. byte(v&0x7f|0x80),
  1530. byte((v>>7)&0x7f|0x80),
  1531. byte((v>>14)&0x7f|0x80),
  1532. byte((v>>21)&0x7f|0x80),
  1533. byte((v>>28)&0x7f|0x80),
  1534. byte((v>>35)&0x7f|0x80),
  1535. byte((v>>42)&0x7f|0x80),
  1536. byte((v>>49)&0x7f|0x80),
  1537. byte((v>>56)&0x7f|0x80),
  1538. 1)
  1539. }
  1540. return b
  1541. }
  1542. func appendFixed32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1543. v := *ptr.toUint32()
  1544. b = appendVarint(b, wiretag)
  1545. b = appendFixed32(b, v)
  1546. return b, nil
  1547. }
  1548. func appendFixed32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1549. v := *ptr.toUint32()
  1550. if v == 0 {
  1551. return b, nil
  1552. }
  1553. b = appendVarint(b, wiretag)
  1554. b = appendFixed32(b, v)
  1555. return b, nil
  1556. }
  1557. func appendFixed32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1558. p := *ptr.toUint32Ptr()
  1559. if p == nil {
  1560. return b, nil
  1561. }
  1562. b = appendVarint(b, wiretag)
  1563. b = appendFixed32(b, *p)
  1564. return b, nil
  1565. }
  1566. func appendFixed32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1567. s := *ptr.toUint32Slice()
  1568. for _, v := range s {
  1569. b = appendVarint(b, wiretag)
  1570. b = appendFixed32(b, v)
  1571. }
  1572. return b, nil
  1573. }
  1574. func appendFixed32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1575. s := *ptr.toUint32Slice()
  1576. if len(s) == 0 {
  1577. return b, nil
  1578. }
  1579. b = appendVarint(b, wiretag&^7|WireBytes)
  1580. b = appendVarint(b, uint64(4*len(s)))
  1581. for _, v := range s {
  1582. b = appendFixed32(b, v)
  1583. }
  1584. return b, nil
  1585. }
  1586. func appendFixedS32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1587. v := *ptr.toInt32()
  1588. b = appendVarint(b, wiretag)
  1589. b = appendFixed32(b, uint32(v))
  1590. return b, nil
  1591. }
  1592. func appendFixedS32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1593. v := *ptr.toInt32()
  1594. if v == 0 {
  1595. return b, nil
  1596. }
  1597. b = appendVarint(b, wiretag)
  1598. b = appendFixed32(b, uint32(v))
  1599. return b, nil
  1600. }
  1601. func appendFixedS32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1602. p := ptr.getInt32Ptr()
  1603. if p == nil {
  1604. return b, nil
  1605. }
  1606. b = appendVarint(b, wiretag)
  1607. b = appendFixed32(b, uint32(*p))
  1608. return b, nil
  1609. }
  1610. func appendFixedS32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1611. s := ptr.getInt32Slice()
  1612. for _, v := range s {
  1613. b = appendVarint(b, wiretag)
  1614. b = appendFixed32(b, uint32(v))
  1615. }
  1616. return b, nil
  1617. }
  1618. func appendFixedS32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1619. s := ptr.getInt32Slice()
  1620. if len(s) == 0 {
  1621. return b, nil
  1622. }
  1623. b = appendVarint(b, wiretag&^7|WireBytes)
  1624. b = appendVarint(b, uint64(4*len(s)))
  1625. for _, v := range s {
  1626. b = appendFixed32(b, uint32(v))
  1627. }
  1628. return b, nil
  1629. }
  1630. func appendFloat32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1631. v := math.Float32bits(*ptr.toFloat32())
  1632. b = appendVarint(b, wiretag)
  1633. b = appendFixed32(b, v)
  1634. return b, nil
  1635. }
  1636. func appendFloat32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1637. v := math.Float32bits(*ptr.toFloat32())
  1638. if v == 0 {
  1639. return b, nil
  1640. }
  1641. b = appendVarint(b, wiretag)
  1642. b = appendFixed32(b, v)
  1643. return b, nil
  1644. }
  1645. func appendFloat32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1646. p := *ptr.toFloat32Ptr()
  1647. if p == nil {
  1648. return b, nil
  1649. }
  1650. b = appendVarint(b, wiretag)
  1651. b = appendFixed32(b, math.Float32bits(*p))
  1652. return b, nil
  1653. }
  1654. func appendFloat32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1655. s := *ptr.toFloat32Slice()
  1656. for _, v := range s {
  1657. b = appendVarint(b, wiretag)
  1658. b = appendFixed32(b, math.Float32bits(v))
  1659. }
  1660. return b, nil
  1661. }
  1662. func appendFloat32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1663. s := *ptr.toFloat32Slice()
  1664. if len(s) == 0 {
  1665. return b, nil
  1666. }
  1667. b = appendVarint(b, wiretag&^7|WireBytes)
  1668. b = appendVarint(b, uint64(4*len(s)))
  1669. for _, v := range s {
  1670. b = appendFixed32(b, math.Float32bits(v))
  1671. }
  1672. return b, nil
  1673. }
  1674. func appendFixed64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1675. v := *ptr.toUint64()
  1676. b = appendVarint(b, wiretag)
  1677. b = appendFixed64(b, v)
  1678. return b, nil
  1679. }
  1680. func appendFixed64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1681. v := *ptr.toUint64()
  1682. if v == 0 {
  1683. return b, nil
  1684. }
  1685. b = appendVarint(b, wiretag)
  1686. b = appendFixed64(b, v)
  1687. return b, nil
  1688. }
  1689. func appendFixed64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1690. p := *ptr.toUint64Ptr()
  1691. if p == nil {
  1692. return b, nil
  1693. }
  1694. b = appendVarint(b, wiretag)
  1695. b = appendFixed64(b, *p)
  1696. return b, nil
  1697. }
  1698. func appendFixed64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1699. s := *ptr.toUint64Slice()
  1700. for _, v := range s {
  1701. b = appendVarint(b, wiretag)
  1702. b = appendFixed64(b, v)
  1703. }
  1704. return b, nil
  1705. }
  1706. func appendFixed64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1707. s := *ptr.toUint64Slice()
  1708. if len(s) == 0 {
  1709. return b, nil
  1710. }
  1711. b = appendVarint(b, wiretag&^7|WireBytes)
  1712. b = appendVarint(b, uint64(8*len(s)))
  1713. for _, v := range s {
  1714. b = appendFixed64(b, v)
  1715. }
  1716. return b, nil
  1717. }
  1718. func appendFixedS64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1719. v := *ptr.toInt64()
  1720. b = appendVarint(b, wiretag)
  1721. b = appendFixed64(b, uint64(v))
  1722. return b, nil
  1723. }
  1724. func appendFixedS64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1725. v := *ptr.toInt64()
  1726. if v == 0 {
  1727. return b, nil
  1728. }
  1729. b = appendVarint(b, wiretag)
  1730. b = appendFixed64(b, uint64(v))
  1731. return b, nil
  1732. }
  1733. func appendFixedS64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1734. p := *ptr.toInt64Ptr()
  1735. if p == nil {
  1736. return b, nil
  1737. }
  1738. b = appendVarint(b, wiretag)
  1739. b = appendFixed64(b, uint64(*p))
  1740. return b, nil
  1741. }
  1742. func appendFixedS64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1743. s := *ptr.toInt64Slice()
  1744. for _, v := range s {
  1745. b = appendVarint(b, wiretag)
  1746. b = appendFixed64(b, uint64(v))
  1747. }
  1748. return b, nil
  1749. }
  1750. func appendFixedS64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1751. s := *ptr.toInt64Slice()
  1752. if len(s) == 0 {
  1753. return b, nil
  1754. }
  1755. b = appendVarint(b, wiretag&^7|WireBytes)
  1756. b = appendVarint(b, uint64(8*len(s)))
  1757. for _, v := range s {
  1758. b = appendFixed64(b, uint64(v))
  1759. }
  1760. return b, nil
  1761. }
  1762. func appendFloat64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1763. v := math.Float64bits(*ptr.toFloat64())
  1764. b = appendVarint(b, wiretag)
  1765. b = appendFixed64(b, v)
  1766. return b, nil
  1767. }
  1768. func appendFloat64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1769. v := math.Float64bits(*ptr.toFloat64())
  1770. if v == 0 {
  1771. return b, nil
  1772. }
  1773. b = appendVarint(b, wiretag)
  1774. b = appendFixed64(b, v)
  1775. return b, nil
  1776. }
  1777. func appendFloat64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1778. p := *ptr.toFloat64Ptr()
  1779. if p == nil {
  1780. return b, nil
  1781. }
  1782. b = appendVarint(b, wiretag)
  1783. b = appendFixed64(b, math.Float64bits(*p))
  1784. return b, nil
  1785. }
  1786. func appendFloat64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1787. s := *ptr.toFloat64Slice()
  1788. for _, v := range s {
  1789. b = appendVarint(b, wiretag)
  1790. b = appendFixed64(b, math.Float64bits(v))
  1791. }
  1792. return b, nil
  1793. }
  1794. func appendFloat64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1795. s := *ptr.toFloat64Slice()
  1796. if len(s) == 0 {
  1797. return b, nil
  1798. }
  1799. b = appendVarint(b, wiretag&^7|WireBytes)
  1800. b = appendVarint(b, uint64(8*len(s)))
  1801. for _, v := range s {
  1802. b = appendFixed64(b, math.Float64bits(v))
  1803. }
  1804. return b, nil
  1805. }
  1806. func appendVarint32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1807. v := *ptr.toUint32()
  1808. b = appendVarint(b, wiretag)
  1809. b = appendVarint(b, uint64(v))
  1810. return b, nil
  1811. }
  1812. func appendVarint32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1813. v := *ptr.toUint32()
  1814. if v == 0 {
  1815. return b, nil
  1816. }
  1817. b = appendVarint(b, wiretag)
  1818. b = appendVarint(b, uint64(v))
  1819. return b, nil
  1820. }
  1821. func appendVarint32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1822. p := *ptr.toUint32Ptr()
  1823. if p == nil {
  1824. return b, nil
  1825. }
  1826. b = appendVarint(b, wiretag)
  1827. b = appendVarint(b, uint64(*p))
  1828. return b, nil
  1829. }
  1830. func appendVarint32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1831. s := *ptr.toUint32Slice()
  1832. for _, v := range s {
  1833. b = appendVarint(b, wiretag)
  1834. b = appendVarint(b, uint64(v))
  1835. }
  1836. return b, nil
  1837. }
  1838. func appendVarint32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1839. s := *ptr.toUint32Slice()
  1840. if len(s) == 0 {
  1841. return b, nil
  1842. }
  1843. b = appendVarint(b, wiretag&^7|WireBytes)
  1844. // compute size
  1845. n := 0
  1846. for _, v := range s {
  1847. n += SizeVarint(uint64(v))
  1848. }
  1849. b = appendVarint(b, uint64(n))
  1850. for _, v := range s {
  1851. b = appendVarint(b, uint64(v))
  1852. }
  1853. return b, nil
  1854. }
  1855. func appendVarintS32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1856. v := *ptr.toInt32()
  1857. b = appendVarint(b, wiretag)
  1858. b = appendVarint(b, uint64(v))
  1859. return b, nil
  1860. }
  1861. func appendVarintS32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1862. v := *ptr.toInt32()
  1863. if v == 0 {
  1864. return b, nil
  1865. }
  1866. b = appendVarint(b, wiretag)
  1867. b = appendVarint(b, uint64(v))
  1868. return b, nil
  1869. }
  1870. func appendVarintS32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1871. p := ptr.getInt32Ptr()
  1872. if p == nil {
  1873. return b, nil
  1874. }
  1875. b = appendVarint(b, wiretag)
  1876. b = appendVarint(b, uint64(*p))
  1877. return b, nil
  1878. }
  1879. func appendVarintS32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1880. s := ptr.getInt32Slice()
  1881. for _, v := range s {
  1882. b = appendVarint(b, wiretag)
  1883. b = appendVarint(b, uint64(v))
  1884. }
  1885. return b, nil
  1886. }
  1887. func appendVarintS32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1888. s := ptr.getInt32Slice()
  1889. if len(s) == 0 {
  1890. return b, nil
  1891. }
  1892. b = appendVarint(b, wiretag&^7|WireBytes)
  1893. // compute size
  1894. n := 0
  1895. for _, v := range s {
  1896. n += SizeVarint(uint64(v))
  1897. }
  1898. b = appendVarint(b, uint64(n))
  1899. for _, v := range s {
  1900. b = appendVarint(b, uint64(v))
  1901. }
  1902. return b, nil
  1903. }
  1904. func appendVarint64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1905. v := *ptr.toUint64()
  1906. b = appendVarint(b, wiretag)
  1907. b = appendVarint(b, v)
  1908. return b, nil
  1909. }
  1910. func appendVarint64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1911. v := *ptr.toUint64()
  1912. if v == 0 {
  1913. return b, nil
  1914. }
  1915. b = appendVarint(b, wiretag)
  1916. b = appendVarint(b, v)
  1917. return b, nil
  1918. }
  1919. func appendVarint64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1920. p := *ptr.toUint64Ptr()
  1921. if p == nil {
  1922. return b, nil
  1923. }
  1924. b = appendVarint(b, wiretag)
  1925. b = appendVarint(b, *p)
  1926. return b, nil
  1927. }
  1928. func appendVarint64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1929. s := *ptr.toUint64Slice()
  1930. for _, v := range s {
  1931. b = appendVarint(b, wiretag)
  1932. b = appendVarint(b, v)
  1933. }
  1934. return b, nil
  1935. }
  1936. func appendVarint64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1937. s := *ptr.toUint64Slice()
  1938. if len(s) == 0 {
  1939. return b, nil
  1940. }
  1941. b = appendVarint(b, wiretag&^7|WireBytes)
  1942. // compute size
  1943. n := 0
  1944. for _, v := range s {
  1945. n += SizeVarint(v)
  1946. }
  1947. b = appendVarint(b, uint64(n))
  1948. for _, v := range s {
  1949. b = appendVarint(b, v)
  1950. }
  1951. return b, nil
  1952. }
  1953. func appendVarintS64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1954. v := *ptr.toInt64()
  1955. b = appendVarint(b, wiretag)
  1956. b = appendVarint(b, uint64(v))
  1957. return b, nil
  1958. }
  1959. func appendVarintS64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1960. v := *ptr.toInt64()
  1961. if v == 0 {
  1962. return b, nil
  1963. }
  1964. b = appendVarint(b, wiretag)
  1965. b = appendVarint(b, uint64(v))
  1966. return b, nil
  1967. }
  1968. func appendVarintS64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1969. p := *ptr.toInt64Ptr()
  1970. if p == nil {
  1971. return b, nil
  1972. }
  1973. b = appendVarint(b, wiretag)
  1974. b = appendVarint(b, uint64(*p))
  1975. return b, nil
  1976. }
  1977. func appendVarintS64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1978. s := *ptr.toInt64Slice()
  1979. for _, v := range s {
  1980. b = appendVarint(b, wiretag)
  1981. b = appendVarint(b, uint64(v))
  1982. }
  1983. return b, nil
  1984. }
  1985. func appendVarintS64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1986. s := *ptr.toInt64Slice()
  1987. if len(s) == 0 {
  1988. return b, nil
  1989. }
  1990. b = appendVarint(b, wiretag&^7|WireBytes)
  1991. // compute size
  1992. n := 0
  1993. for _, v := range s {
  1994. n += SizeVarint(uint64(v))
  1995. }
  1996. b = appendVarint(b, uint64(n))
  1997. for _, v := range s {
  1998. b = appendVarint(b, uint64(v))
  1999. }
  2000. return b, nil
  2001. }
  2002. func appendZigzag32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2003. v := *ptr.toInt32()
  2004. b = appendVarint(b, wiretag)
  2005. b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
  2006. return b, nil
  2007. }
  2008. func appendZigzag32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2009. v := *ptr.toInt32()
  2010. if v == 0 {
  2011. return b, nil
  2012. }
  2013. b = appendVarint(b, wiretag)
  2014. b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
  2015. return b, nil
  2016. }
  2017. func appendZigzag32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2018. p := ptr.getInt32Ptr()
  2019. if p == nil {
  2020. return b, nil
  2021. }
  2022. b = appendVarint(b, wiretag)
  2023. v := *p
  2024. b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
  2025. return b, nil
  2026. }
  2027. func appendZigzag32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2028. s := ptr.getInt32Slice()
  2029. for _, v := range s {
  2030. b = appendVarint(b, wiretag)
  2031. b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
  2032. }
  2033. return b, nil
  2034. }
  2035. func appendZigzag32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2036. s := ptr.getInt32Slice()
  2037. if len(s) == 0 {
  2038. return b, nil
  2039. }
  2040. b = appendVarint(b, wiretag&^7|WireBytes)
  2041. // compute size
  2042. n := 0
  2043. for _, v := range s {
  2044. n += SizeVarint(uint64((uint32(v) << 1) ^ uint32((int32(v) >> 31))))
  2045. }
  2046. b = appendVarint(b, uint64(n))
  2047. for _, v := range s {
  2048. b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
  2049. }
  2050. return b, nil
  2051. }
  2052. func appendZigzag64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2053. v := *ptr.toInt64()
  2054. b = appendVarint(b, wiretag)
  2055. b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
  2056. return b, nil
  2057. }
  2058. func appendZigzag64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2059. v := *ptr.toInt64()
  2060. if v == 0 {
  2061. return b, nil
  2062. }
  2063. b = appendVarint(b, wiretag)
  2064. b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
  2065. return b, nil
  2066. }
  2067. func appendZigzag64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2068. p := *ptr.toInt64Ptr()
  2069. if p == nil {
  2070. return b, nil
  2071. }
  2072. b = appendVarint(b, wiretag)
  2073. v := *p
  2074. b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
  2075. return b, nil
  2076. }
  2077. func appendZigzag64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2078. s := *ptr.toInt64Slice()
  2079. for _, v := range s {
  2080. b = appendVarint(b, wiretag)
  2081. b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
  2082. }
  2083. return b, nil
  2084. }
  2085. func appendZigzag64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2086. s := *ptr.toInt64Slice()
  2087. if len(s) == 0 {
  2088. return b, nil
  2089. }
  2090. b = appendVarint(b, wiretag&^7|WireBytes)
  2091. // compute size
  2092. n := 0
  2093. for _, v := range s {
  2094. n += SizeVarint(uint64(v<<1) ^ uint64((int64(v) >> 63)))
  2095. }
  2096. b = appendVarint(b, uint64(n))
  2097. for _, v := range s {
  2098. b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
  2099. }
  2100. return b, nil
  2101. }
  2102. func appendBoolValue(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2103. v := *ptr.toBool()
  2104. b = appendVarint(b, wiretag)
  2105. if v {
  2106. b = append(b, 1)
  2107. } else {
  2108. b = append(b, 0)
  2109. }
  2110. return b, nil
  2111. }
  2112. func appendBoolValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2113. v := *ptr.toBool()
  2114. if !v {
  2115. return b, nil
  2116. }
  2117. b = appendVarint(b, wiretag)
  2118. b = append(b, 1)
  2119. return b, nil
  2120. }
  2121. func appendBoolPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2122. p := *ptr.toBoolPtr()
  2123. if p == nil {
  2124. return b, nil
  2125. }
  2126. b = appendVarint(b, wiretag)
  2127. if *p {
  2128. b = append(b, 1)
  2129. } else {
  2130. b = append(b, 0)
  2131. }
  2132. return b, nil
  2133. }
  2134. func appendBoolSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2135. s := *ptr.toBoolSlice()
  2136. for _, v := range s {
  2137. b = appendVarint(b, wiretag)
  2138. if v {
  2139. b = append(b, 1)
  2140. } else {
  2141. b = append(b, 0)
  2142. }
  2143. }
  2144. return b, nil
  2145. }
  2146. func appendBoolPackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2147. s := *ptr.toBoolSlice()
  2148. if len(s) == 0 {
  2149. return b, nil
  2150. }
  2151. b = appendVarint(b, wiretag&^7|WireBytes)
  2152. b = appendVarint(b, uint64(len(s)))
  2153. for _, v := range s {
  2154. if v {
  2155. b = append(b, 1)
  2156. } else {
  2157. b = append(b, 0)
  2158. }
  2159. }
  2160. return b, nil
  2161. }
  2162. func appendStringValue(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2163. v := *ptr.toString()
  2164. b = appendVarint(b, wiretag)
  2165. b = appendVarint(b, uint64(len(v)))
  2166. b = append(b, v...)
  2167. return b, nil
  2168. }
  2169. func appendStringValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2170. v := *ptr.toString()
  2171. if v == "" {
  2172. return b, nil
  2173. }
  2174. b = appendVarint(b, wiretag)
  2175. b = appendVarint(b, uint64(len(v)))
  2176. b = append(b, v...)
  2177. return b, nil
  2178. }
  2179. func appendStringPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2180. p := *ptr.toStringPtr()
  2181. if p == nil {
  2182. return b, nil
  2183. }
  2184. v := *p
  2185. b = appendVarint(b, wiretag)
  2186. b = appendVarint(b, uint64(len(v)))
  2187. b = append(b, v...)
  2188. return b, nil
  2189. }
  2190. func appendStringSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2191. s := *ptr.toStringSlice()
  2192. for _, v := range s {
  2193. b = appendVarint(b, wiretag)
  2194. b = appendVarint(b, uint64(len(v)))
  2195. b = append(b, v...)
  2196. }
  2197. return b, nil
  2198. }
  2199. func appendUTF8StringValue(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2200. var invalidUTF8 bool
  2201. v := *ptr.toString()
  2202. if !utf8.ValidString(v) {
  2203. invalidUTF8 = true
  2204. }
  2205. b = appendVarint(b, wiretag)
  2206. b = appendVarint(b, uint64(len(v)))
  2207. b = append(b, v...)
  2208. if invalidUTF8 {
  2209. return b, errInvalidUTF8
  2210. }
  2211. return b, nil
  2212. }
  2213. func appendUTF8StringValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2214. var invalidUTF8 bool
  2215. v := *ptr.toString()
  2216. if v == "" {
  2217. return b, nil
  2218. }
  2219. if !utf8.ValidString(v) {
  2220. invalidUTF8 = true
  2221. }
  2222. b = appendVarint(b, wiretag)
  2223. b = appendVarint(b, uint64(len(v)))
  2224. b = append(b, v...)
  2225. if invalidUTF8 {
  2226. return b, errInvalidUTF8
  2227. }
  2228. return b, nil
  2229. }
  2230. func appendUTF8StringPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2231. var invalidUTF8 bool
  2232. p := *ptr.toStringPtr()
  2233. if p == nil {
  2234. return b, nil
  2235. }
  2236. v := *p
  2237. if !utf8.ValidString(v) {
  2238. invalidUTF8 = true
  2239. }
  2240. b = appendVarint(b, wiretag)
  2241. b = appendVarint(b, uint64(len(v)))
  2242. b = append(b, v...)
  2243. if invalidUTF8 {
  2244. return b, errInvalidUTF8
  2245. }
  2246. return b, nil
  2247. }
  2248. func appendUTF8StringSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2249. var invalidUTF8 bool
  2250. s := *ptr.toStringSlice()
  2251. for _, v := range s {
  2252. if !utf8.ValidString(v) {
  2253. invalidUTF8 = true
  2254. }
  2255. b = appendVarint(b, wiretag)
  2256. b = appendVarint(b, uint64(len(v)))
  2257. b = append(b, v...)
  2258. }
  2259. if invalidUTF8 {
  2260. return b, errInvalidUTF8
  2261. }
  2262. return b, nil
  2263. }
  2264. func appendBytes(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2265. v := *ptr.toBytes()
  2266. if v == nil {
  2267. return b, nil
  2268. }
  2269. b = appendVarint(b, wiretag)
  2270. b = appendVarint(b, uint64(len(v)))
  2271. b = append(b, v...)
  2272. return b, nil
  2273. }
  2274. func appendBytes3(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2275. v := *ptr.toBytes()
  2276. if len(v) == 0 {
  2277. return b, nil
  2278. }
  2279. b = appendVarint(b, wiretag)
  2280. b = appendVarint(b, uint64(len(v)))
  2281. b = append(b, v...)
  2282. return b, nil
  2283. }
  2284. func appendBytesOneof(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2285. v := *ptr.toBytes()
  2286. b = appendVarint(b, wiretag)
  2287. b = appendVarint(b, uint64(len(v)))
  2288. b = append(b, v...)
  2289. return b, nil
  2290. }
  2291. func appendBytesSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2292. s := *ptr.toBytesSlice()
  2293. for _, v := range s {
  2294. b = appendVarint(b, wiretag)
  2295. b = appendVarint(b, uint64(len(v)))
  2296. b = append(b, v...)
  2297. }
  2298. return b, nil
  2299. }
  2300. // makeGroupMarshaler returns the sizer and marshaler for a group.
  2301. // u is the marshal info of the underlying message.
  2302. func makeGroupMarshaler(u *marshalInfo) (sizer, marshaler) {
  2303. return func(ptr pointer, tagsize int) int {
  2304. p := ptr.getPointer()
  2305. if p.isNil() {
  2306. return 0
  2307. }
  2308. return u.size(p) + 2*tagsize
  2309. },
  2310. func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  2311. p := ptr.getPointer()
  2312. if p.isNil() {
  2313. return b, nil
  2314. }
  2315. var err error
  2316. b = appendVarint(b, wiretag) // start group
  2317. b, err = u.marshal(b, p, deterministic)
  2318. b = appendVarint(b, wiretag+(WireEndGroup-WireStartGroup)) // end group
  2319. return b, err
  2320. }
  2321. }
  2322. // makeGroupSliceMarshaler returns the sizer and marshaler for a group slice.
  2323. // u is the marshal info of the underlying message.
  2324. func makeGroupSliceMarshaler(u *marshalInfo) (sizer, marshaler) {
  2325. return func(ptr pointer, tagsize int) int {
  2326. s := ptr.getPointerSlice()
  2327. n := 0
  2328. for _, v := range s {
  2329. if v.isNil() {
  2330. continue
  2331. }
  2332. n += u.size(v) + 2*tagsize
  2333. }
  2334. return n
  2335. },
  2336. func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  2337. s := ptr.getPointerSlice()
  2338. var err error
  2339. var nerr nonFatal
  2340. for _, v := range s {
  2341. if v.isNil() {
  2342. return b, errRepeatedHasNil
  2343. }
  2344. b = appendVarint(b, wiretag) // start group
  2345. b, err = u.marshal(b, v, deterministic)
  2346. b = appendVarint(b, wiretag+(WireEndGroup-WireStartGroup)) // end group
  2347. if !nerr.Merge(err) {
  2348. if err == ErrNil {
  2349. err = errRepeatedHasNil
  2350. }
  2351. return b, err
  2352. }
  2353. }
  2354. return b, nerr.E
  2355. }
  2356. }
  2357. // makeMessageMarshaler returns the sizer and marshaler for a message field.
  2358. // u is the marshal info of the message.
  2359. func makeMessageMarshaler(u *marshalInfo) (sizer, marshaler) {
  2360. return func(ptr pointer, tagsize int) int {
  2361. p := ptr.getPointer()
  2362. if p.isNil() {
  2363. return 0
  2364. }
  2365. siz := u.size(p)
  2366. return siz + SizeVarint(uint64(siz)) + tagsize
  2367. },
  2368. func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  2369. p := ptr.getPointer()
  2370. if p.isNil() {
  2371. return b, nil
  2372. }
  2373. b = appendVarint(b, wiretag)
  2374. siz := u.cachedsize(p)
  2375. b = appendVarint(b, uint64(siz))
  2376. return u.marshal(b, p, deterministic)
  2377. }
  2378. }
  2379. // makeMessageSliceMarshaler returns the sizer and marshaler for a message slice.
  2380. // u is the marshal info of the message.
  2381. func makeMessageSliceMarshaler(u *marshalInfo) (sizer, marshaler) {
  2382. return func(ptr pointer, tagsize int) int {
  2383. s := ptr.getPointerSlice()
  2384. n := 0
  2385. for _, v := range s {
  2386. if v.isNil() {
  2387. continue
  2388. }
  2389. siz := u.size(v)
  2390. n += siz + SizeVarint(uint64(siz)) + tagsize
  2391. }
  2392. return n
  2393. },
  2394. func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  2395. s := ptr.getPointerSlice()
  2396. var err error
  2397. var nerr nonFatal
  2398. for _, v := range s {
  2399. if v.isNil() {
  2400. return b, errRepeatedHasNil
  2401. }
  2402. b = appendVarint(b, wiretag)
  2403. siz := u.cachedsize(v)
  2404. b = appendVarint(b, uint64(siz))
  2405. b, err = u.marshal(b, v, deterministic)
  2406. if !nerr.Merge(err) {
  2407. if err == ErrNil {
  2408. err = errRepeatedHasNil
  2409. }
  2410. return b, err
  2411. }
  2412. }
  2413. return b, nerr.E
  2414. }
  2415. }
  2416. // makeMapMarshaler returns the sizer and marshaler for a map field.
  2417. // f is the pointer to the reflect data structure of the field.
  2418. func makeMapMarshaler(f *reflect.StructField) (sizer, marshaler) {
  2419. // figure out key and value type
  2420. t := f.Type
  2421. keyType := t.Key()
  2422. valType := t.Elem()
  2423. tags := strings.Split(f.Tag.Get("protobuf"), ",")
  2424. keyTags := strings.Split(f.Tag.Get("protobuf_key"), ",")
  2425. valTags := strings.Split(f.Tag.Get("protobuf_val"), ",")
  2426. stdOptions := false
  2427. for _, t := range tags {
  2428. if strings.HasPrefix(t, "customtype=") {
  2429. valTags = append(valTags, t)
  2430. }
  2431. if t == "stdtime" {
  2432. valTags = append(valTags, t)
  2433. stdOptions = true
  2434. }
  2435. if t == "stdduration" {
  2436. valTags = append(valTags, t)
  2437. stdOptions = true
  2438. }
  2439. if t == "wktptr" {
  2440. valTags = append(valTags, t)
  2441. }
  2442. }
  2443. keySizer, keyMarshaler := typeMarshaler(keyType, keyTags, false, false) // don't omit zero value in map
  2444. valSizer, valMarshaler := typeMarshaler(valType, valTags, false, false) // don't omit zero value in map
  2445. keyWireTag := 1<<3 | wiretype(keyTags[0])
  2446. valWireTag := 2<<3 | wiretype(valTags[0])
  2447. // We create an interface to get the addresses of the map key and value.
  2448. // If value is pointer-typed, the interface is a direct interface, the
  2449. // idata itself is the value. Otherwise, the idata is the pointer to the
  2450. // value.
  2451. // Key cannot be pointer-typed.
  2452. valIsPtr := valType.Kind() == reflect.Ptr
  2453. // If value is a message with nested maps, calling
  2454. // valSizer in marshal may be quadratic. We should use
  2455. // cached version in marshal (but not in size).
  2456. // If value is not message type, we don't have size cache,
  2457. // but it cannot be nested either. Just use valSizer.
  2458. valCachedSizer := valSizer
  2459. if valIsPtr && !stdOptions && valType.Elem().Kind() == reflect.Struct {
  2460. u := getMarshalInfo(valType.Elem())
  2461. valCachedSizer = func(ptr pointer, tagsize int) int {
  2462. // Same as message sizer, but use cache.
  2463. p := ptr.getPointer()
  2464. if p.isNil() {
  2465. return 0
  2466. }
  2467. siz := u.cachedsize(p)
  2468. return siz + SizeVarint(uint64(siz)) + tagsize
  2469. }
  2470. }
  2471. return func(ptr pointer, tagsize int) int {
  2472. m := ptr.asPointerTo(t).Elem() // the map
  2473. n := 0
  2474. for _, k := range m.MapKeys() {
  2475. ki := k.Interface()
  2476. vi := m.MapIndex(k).Interface()
  2477. kaddr := toAddrPointer(&ki, false) // pointer to key
  2478. vaddr := toAddrPointer(&vi, valIsPtr) // pointer to value
  2479. siz := keySizer(kaddr, 1) + valSizer(vaddr, 1) // tag of key = 1 (size=1), tag of val = 2 (size=1)
  2480. n += siz + SizeVarint(uint64(siz)) + tagsize
  2481. }
  2482. return n
  2483. },
  2484. func(b []byte, ptr pointer, tag uint64, deterministic bool) ([]byte, error) {
  2485. m := ptr.asPointerTo(t).Elem() // the map
  2486. var err error
  2487. keys := m.MapKeys()
  2488. if len(keys) > 1 && deterministic {
  2489. sort.Sort(mapKeys(keys))
  2490. }
  2491. var nerr nonFatal
  2492. for _, k := range keys {
  2493. ki := k.Interface()
  2494. vi := m.MapIndex(k).Interface()
  2495. kaddr := toAddrPointer(&ki, false) // pointer to key
  2496. vaddr := toAddrPointer(&vi, valIsPtr) // pointer to value
  2497. b = appendVarint(b, tag)
  2498. siz := keySizer(kaddr, 1) + valCachedSizer(vaddr, 1) // tag of key = 1 (size=1), tag of val = 2 (size=1)
  2499. b = appendVarint(b, uint64(siz))
  2500. b, err = keyMarshaler(b, kaddr, keyWireTag, deterministic)
  2501. if !nerr.Merge(err) {
  2502. return b, err
  2503. }
  2504. b, err = valMarshaler(b, vaddr, valWireTag, deterministic)
  2505. if err != ErrNil && !nerr.Merge(err) { // allow nil value in map
  2506. return b, err
  2507. }
  2508. }
  2509. return b, nerr.E
  2510. }
  2511. }
  2512. // makeOneOfMarshaler returns the sizer and marshaler for a oneof field.
  2513. // fi is the marshal info of the field.
  2514. // f is the pointer to the reflect data structure of the field.
  2515. func makeOneOfMarshaler(fi *marshalFieldInfo, f *reflect.StructField) (sizer, marshaler) {
  2516. // Oneof field is an interface. We need to get the actual data type on the fly.
  2517. t := f.Type
  2518. return func(ptr pointer, _ int) int {
  2519. p := ptr.getInterfacePointer()
  2520. if p.isNil() {
  2521. return 0
  2522. }
  2523. v := ptr.asPointerTo(t).Elem().Elem().Elem() // *interface -> interface -> *struct -> struct
  2524. telem := v.Type()
  2525. e := fi.oneofElems[telem]
  2526. return e.sizer(p, e.tagsize)
  2527. },
  2528. func(b []byte, ptr pointer, _ uint64, deterministic bool) ([]byte, error) {
  2529. p := ptr.getInterfacePointer()
  2530. if p.isNil() {
  2531. return b, nil
  2532. }
  2533. v := ptr.asPointerTo(t).Elem().Elem().Elem() // *interface -> interface -> *struct -> struct
  2534. telem := v.Type()
  2535. if telem.Field(0).Type.Kind() == reflect.Ptr && p.getPointer().isNil() {
  2536. return b, errOneofHasNil
  2537. }
  2538. e := fi.oneofElems[telem]
  2539. return e.marshaler(b, p, e.wiretag, deterministic)
  2540. }
  2541. }
  2542. // sizeExtensions computes the size of encoded data for a XXX_InternalExtensions field.
  2543. func (u *marshalInfo) sizeExtensions(ext *XXX_InternalExtensions) int {
  2544. m, mu := ext.extensionsRead()
  2545. if m == nil {
  2546. return 0
  2547. }
  2548. mu.Lock()
  2549. n := 0
  2550. for _, e := range m {
  2551. if e.value == nil || e.desc == nil {
  2552. // Extension is only in its encoded form.
  2553. n += len(e.enc)
  2554. continue
  2555. }
  2556. // We don't skip extensions that have an encoded form set,
  2557. // because the extension value may have been mutated after
  2558. // the last time this function was called.
  2559. ei := u.getExtElemInfo(e.desc)
  2560. v := e.value
  2561. p := toAddrPointer(&v, ei.isptr)
  2562. n += ei.sizer(p, ei.tagsize)
  2563. }
  2564. mu.Unlock()
  2565. return n
  2566. }
  2567. // appendExtensions marshals a XXX_InternalExtensions field to the end of byte slice b.
  2568. func (u *marshalInfo) appendExtensions(b []byte, ext *XXX_InternalExtensions, deterministic bool) ([]byte, error) {
  2569. m, mu := ext.extensionsRead()
  2570. if m == nil {
  2571. return b, nil
  2572. }
  2573. mu.Lock()
  2574. defer mu.Unlock()
  2575. var err error
  2576. var nerr nonFatal
  2577. // Fast-path for common cases: zero or one extensions.
  2578. // Don't bother sorting the keys.
  2579. if len(m) <= 1 {
  2580. for _, e := range m {
  2581. if e.value == nil || e.desc == nil {
  2582. // Extension is only in its encoded form.
  2583. b = append(b, e.enc...)
  2584. continue
  2585. }
  2586. // We don't skip extensions that have an encoded form set,
  2587. // because the extension value may have been mutated after
  2588. // the last time this function was called.
  2589. ei := u.getExtElemInfo(e.desc)
  2590. v := e.value
  2591. p := toAddrPointer(&v, ei.isptr)
  2592. b, err = ei.marshaler(b, p, ei.wiretag, deterministic)
  2593. if !nerr.Merge(err) {
  2594. return b, err
  2595. }
  2596. }
  2597. return b, nerr.E
  2598. }
  2599. // Sort the keys to provide a deterministic encoding.
  2600. // Not sure this is required, but the old code does it.
  2601. keys := make([]int, 0, len(m))
  2602. for k := range m {
  2603. keys = append(keys, int(k))
  2604. }
  2605. sort.Ints(keys)
  2606. for _, k := range keys {
  2607. e := m[int32(k)]
  2608. if e.value == nil || e.desc == nil {
  2609. // Extension is only in its encoded form.
  2610. b = append(b, e.enc...)
  2611. continue
  2612. }
  2613. // We don't skip extensions that have an encoded form set,
  2614. // because the extension value may have been mutated after
  2615. // the last time this function was called.
  2616. ei := u.getExtElemInfo(e.desc)
  2617. v := e.value
  2618. p := toAddrPointer(&v, ei.isptr)
  2619. b, err = ei.marshaler(b, p, ei.wiretag, deterministic)
  2620. if !nerr.Merge(err) {
  2621. return b, err
  2622. }
  2623. }
  2624. return b, nerr.E
  2625. }
  2626. // message set format is:
  2627. // message MessageSet {
  2628. // repeated group Item = 1 {
  2629. // required int32 type_id = 2;
  2630. // required string message = 3;
  2631. // };
  2632. // }
  2633. // sizeMessageSet computes the size of encoded data for a XXX_InternalExtensions field
  2634. // in message set format (above).
  2635. func (u *marshalInfo) sizeMessageSet(ext *XXX_InternalExtensions) int {
  2636. m, mu := ext.extensionsRead()
  2637. if m == nil {
  2638. return 0
  2639. }
  2640. mu.Lock()
  2641. n := 0
  2642. for id, e := range m {
  2643. n += 2 // start group, end group. tag = 1 (size=1)
  2644. n += SizeVarint(uint64(id)) + 1 // type_id, tag = 2 (size=1)
  2645. if e.value == nil || e.desc == nil {
  2646. // Extension is only in its encoded form.
  2647. msgWithLen := skipVarint(e.enc) // skip old tag, but leave the length varint
  2648. siz := len(msgWithLen)
  2649. n += siz + 1 // message, tag = 3 (size=1)
  2650. continue
  2651. }
  2652. // We don't skip extensions that have an encoded form set,
  2653. // because the extension value may have been mutated after
  2654. // the last time this function was called.
  2655. ei := u.getExtElemInfo(e.desc)
  2656. v := e.value
  2657. p := toAddrPointer(&v, ei.isptr)
  2658. n += ei.sizer(p, 1) // message, tag = 3 (size=1)
  2659. }
  2660. mu.Unlock()
  2661. return n
  2662. }
  2663. // appendMessageSet marshals a XXX_InternalExtensions field in message set format (above)
  2664. // to the end of byte slice b.
  2665. func (u *marshalInfo) appendMessageSet(b []byte, ext *XXX_InternalExtensions, deterministic bool) ([]byte, error) {
  2666. m, mu := ext.extensionsRead()
  2667. if m == nil {
  2668. return b, nil
  2669. }
  2670. mu.Lock()
  2671. defer mu.Unlock()
  2672. var err error
  2673. var nerr nonFatal
  2674. // Fast-path for common cases: zero or one extensions.
  2675. // Don't bother sorting the keys.
  2676. if len(m) <= 1 {
  2677. for id, e := range m {
  2678. b = append(b, 1<<3|WireStartGroup)
  2679. b = append(b, 2<<3|WireVarint)
  2680. b = appendVarint(b, uint64(id))
  2681. if e.value == nil || e.desc == nil {
  2682. // Extension is only in its encoded form.
  2683. msgWithLen := skipVarint(e.enc) // skip old tag, but leave the length varint
  2684. b = append(b, 3<<3|WireBytes)
  2685. b = append(b, msgWithLen...)
  2686. b = append(b, 1<<3|WireEndGroup)
  2687. continue
  2688. }
  2689. // We don't skip extensions that have an encoded form set,
  2690. // because the extension value may have been mutated after
  2691. // the last time this function was called.
  2692. ei := u.getExtElemInfo(e.desc)
  2693. v := e.value
  2694. p := toAddrPointer(&v, ei.isptr)
  2695. b, err = ei.marshaler(b, p, 3<<3|WireBytes, deterministic)
  2696. if !nerr.Merge(err) {
  2697. return b, err
  2698. }
  2699. b = append(b, 1<<3|WireEndGroup)
  2700. }
  2701. return b, nerr.E
  2702. }
  2703. // Sort the keys to provide a deterministic encoding.
  2704. keys := make([]int, 0, len(m))
  2705. for k := range m {
  2706. keys = append(keys, int(k))
  2707. }
  2708. sort.Ints(keys)
  2709. for _, id := range keys {
  2710. e := m[int32(id)]
  2711. b = append(b, 1<<3|WireStartGroup)
  2712. b = append(b, 2<<3|WireVarint)
  2713. b = appendVarint(b, uint64(id))
  2714. if e.value == nil || e.desc == nil {
  2715. // Extension is only in its encoded form.
  2716. msgWithLen := skipVarint(e.enc) // skip old tag, but leave the length varint
  2717. b = append(b, 3<<3|WireBytes)
  2718. b = append(b, msgWithLen...)
  2719. b = append(b, 1<<3|WireEndGroup)
  2720. continue
  2721. }
  2722. // We don't skip extensions that have an encoded form set,
  2723. // because the extension value may have been mutated after
  2724. // the last time this function was called.
  2725. ei := u.getExtElemInfo(e.desc)
  2726. v := e.value
  2727. p := toAddrPointer(&v, ei.isptr)
  2728. b, err = ei.marshaler(b, p, 3<<3|WireBytes, deterministic)
  2729. b = append(b, 1<<3|WireEndGroup)
  2730. if !nerr.Merge(err) {
  2731. return b, err
  2732. }
  2733. }
  2734. return b, nerr.E
  2735. }
  2736. // sizeV1Extensions computes the size of encoded data for a V1-API extension field.
  2737. func (u *marshalInfo) sizeV1Extensions(m map[int32]Extension) int {
  2738. if m == nil {
  2739. return 0
  2740. }
  2741. n := 0
  2742. for _, e := range m {
  2743. if e.value == nil || e.desc == nil {
  2744. // Extension is only in its encoded form.
  2745. n += len(e.enc)
  2746. continue
  2747. }
  2748. // We don't skip extensions that have an encoded form set,
  2749. // because the extension value may have been mutated after
  2750. // the last time this function was called.
  2751. ei := u.getExtElemInfo(e.desc)
  2752. v := e.value
  2753. p := toAddrPointer(&v, ei.isptr)
  2754. n += ei.sizer(p, ei.tagsize)
  2755. }
  2756. return n
  2757. }
  2758. // appendV1Extensions marshals a V1-API extension field to the end of byte slice b.
  2759. func (u *marshalInfo) appendV1Extensions(b []byte, m map[int32]Extension, deterministic bool) ([]byte, error) {
  2760. if m == nil {
  2761. return b, nil
  2762. }
  2763. // Sort the keys to provide a deterministic encoding.
  2764. keys := make([]int, 0, len(m))
  2765. for k := range m {
  2766. keys = append(keys, int(k))
  2767. }
  2768. sort.Ints(keys)
  2769. var err error
  2770. var nerr nonFatal
  2771. for _, k := range keys {
  2772. e := m[int32(k)]
  2773. if e.value == nil || e.desc == nil {
  2774. // Extension is only in its encoded form.
  2775. b = append(b, e.enc...)
  2776. continue
  2777. }
  2778. // We don't skip extensions that have an encoded form set,
  2779. // because the extension value may have been mutated after
  2780. // the last time this function was called.
  2781. ei := u.getExtElemInfo(e.desc)
  2782. v := e.value
  2783. p := toAddrPointer(&v, ei.isptr)
  2784. b, err = ei.marshaler(b, p, ei.wiretag, deterministic)
  2785. if !nerr.Merge(err) {
  2786. return b, err
  2787. }
  2788. }
  2789. return b, nerr.E
  2790. }
  2791. // newMarshaler is the interface representing objects that can marshal themselves.
  2792. //
  2793. // This exists to support protoc-gen-go generated messages.
  2794. // The proto package will stop type-asserting to this interface in the future.
  2795. //
  2796. // DO NOT DEPEND ON THIS.
  2797. type newMarshaler interface {
  2798. XXX_Size() int
  2799. XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
  2800. }
  2801. // Size returns the encoded size of a protocol buffer message.
  2802. // This is the main entry point.
  2803. func Size(pb Message) int {
  2804. if m, ok := pb.(newMarshaler); ok {
  2805. return m.XXX_Size()
  2806. }
  2807. if m, ok := pb.(Marshaler); ok {
  2808. // If the message can marshal itself, let it do it, for compatibility.
  2809. // NOTE: This is not efficient.
  2810. b, _ := m.Marshal()
  2811. return len(b)
  2812. }
  2813. // in case somehow we didn't generate the wrapper
  2814. if pb == nil {
  2815. return 0
  2816. }
  2817. var info InternalMessageInfo
  2818. return info.Size(pb)
  2819. }
  2820. // Marshal takes a protocol buffer message
  2821. // and encodes it into the wire format, returning the data.
  2822. // This is the main entry point.
  2823. func Marshal(pb Message) ([]byte, error) {
  2824. if m, ok := pb.(newMarshaler); ok {
  2825. siz := m.XXX_Size()
  2826. b := make([]byte, 0, siz)
  2827. return m.XXX_Marshal(b, false)
  2828. }
  2829. if m, ok := pb.(Marshaler); ok {
  2830. // If the message can marshal itself, let it do it, for compatibility.
  2831. // NOTE: This is not efficient.
  2832. return m.Marshal()
  2833. }
  2834. // in case somehow we didn't generate the wrapper
  2835. if pb == nil {
  2836. return nil, ErrNil
  2837. }
  2838. var info InternalMessageInfo
  2839. siz := info.Size(pb)
  2840. b := make([]byte, 0, siz)
  2841. return info.Marshal(b, pb, false)
  2842. }
  2843. // Marshal takes a protocol buffer message
  2844. // and encodes it into the wire format, writing the result to the
  2845. // Buffer.
  2846. // This is an alternative entry point. It is not necessary to use
  2847. // a Buffer for most applications.
  2848. func (p *Buffer) Marshal(pb Message) error {
  2849. var err error
  2850. if p.deterministic {
  2851. if _, ok := pb.(Marshaler); ok {
  2852. return fmt.Errorf("proto: deterministic not supported by the Marshal method of %T", pb)
  2853. }
  2854. }
  2855. if m, ok := pb.(newMarshaler); ok {
  2856. siz := m.XXX_Size()
  2857. p.grow(siz) // make sure buf has enough capacity
  2858. p.buf, err = m.XXX_Marshal(p.buf, p.deterministic)
  2859. return err
  2860. }
  2861. if m, ok := pb.(Marshaler); ok {
  2862. // If the message can marshal itself, let it do it, for compatibility.
  2863. // NOTE: This is not efficient.
  2864. var b []byte
  2865. b, err = m.Marshal()
  2866. p.buf = append(p.buf, b...)
  2867. return err
  2868. }
  2869. // in case somehow we didn't generate the wrapper
  2870. if pb == nil {
  2871. return ErrNil
  2872. }
  2873. var info InternalMessageInfo
  2874. siz := info.Size(pb)
  2875. p.grow(siz) // make sure buf has enough capacity
  2876. p.buf, err = info.Marshal(p.buf, pb, p.deterministic)
  2877. return err
  2878. }
  2879. // grow grows the buffer's capacity, if necessary, to guarantee space for
  2880. // another n bytes. After grow(n), at least n bytes can be written to the
  2881. // buffer without another allocation.
  2882. func (p *Buffer) grow(n int) {
  2883. need := len(p.buf) + n
  2884. if need <= cap(p.buf) {
  2885. return
  2886. }
  2887. newCap := len(p.buf) * 2
  2888. if newCap < need {
  2889. newCap = need
  2890. }
  2891. p.buf = append(make([]byte, 0, newCap), p.buf...)
  2892. }