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