table_marshal.go 73 KB

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