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