Browse Source

update vendor dir

Stephen McQuay 9 months ago
parent
commit
1f93adcb25
Signed by: Stephen McQuay (smcquay) <stephen@mcquay.me> GPG Key ID: C383C74875475AC8
26 changed files with 6420 additions and 3053 deletions
  1. 29
    5
      vendor/github.com/beorn7/perks/quantile/stream.go
  2. 0
    43
      vendor/github.com/golang/protobuf/proto/Makefile
  3. 35
    11
      vendor/github.com/golang/protobuf/proto/clone.go
  4. 63
    605
      vendor/github.com/golang/protobuf/proto/decode.go
  5. 200
    1
      vendor/github.com/golang/protobuf/proto/discard.go
  6. 25
    1166
      vendor/github.com/golang/protobuf/proto/encode.go
  7. 15
    15
      vendor/github.com/golang/protobuf/proto/equal.go
  8. 80
    124
      vendor/github.com/golang/protobuf/proto/extensions.go
  9. 47
    23
      vendor/github.com/golang/protobuf/proto/lib.go
  10. 42
    39
      vendor/github.com/golang/protobuf/proto/message_set.go
  11. 234
    361
      vendor/github.com/golang/protobuf/proto/pointer_reflect.go
  12. 202
    164
      vendor/github.com/golang/protobuf/proto/pointer_unsafe.go
  13. 48
    376
      vendor/github.com/golang/protobuf/proto/properties.go
  14. 2681
    0
      vendor/github.com/golang/protobuf/proto/table_marshal.go
  15. 654
    0
      vendor/github.com/golang/protobuf/proto/table_merge.go
  16. 1967
    0
      vendor/github.com/golang/protobuf/proto/table_unmarshal.go
  17. 25
    36
      vendor/github.com/golang/protobuf/proto/text.go
  18. 31
    46
      vendor/github.com/golang/protobuf/proto/text_parser.go
  19. 2
    2
      vendor/github.com/prometheus/common/expfmt/decode.go
  20. 2
    2
      vendor/github.com/prometheus/common/expfmt/text_parse.go
  21. 2
    2
      vendor/github.com/prometheus/common/model/silence.go
  22. 2
    2
      vendor/github.com/prometheus/common/model/value.go
  23. 1
    1
      vendor/github.com/prometheus/procfs/net_dev.go
  24. 10
    10
      vendor/github.com/prometheus/procfs/nfs/nfs.go
  25. 8
    8
      vendor/github.com/prometheus/procfs/nfs/parse.go
  26. 15
    11
      vendor/github.com/prometheus/procfs/ttar

+ 29
- 5
vendor/github.com/beorn7/perks/quantile/stream.go View File

@@ -77,15 +77,20 @@ func NewHighBiased(epsilon float64) *Stream {
77 77
 // is guaranteed to be within (Quantile±Epsilon).
78 78
 //
79 79
 // See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error properties.
80
-func NewTargeted(targets map[float64]float64) *Stream {
80
+func NewTargeted(targetMap map[float64]float64) *Stream {
81
+	// Convert map to slice to avoid slow iterations on a map.
82
+	// ƒ is called on the hot path, so converting the map to a slice
83
+	// beforehand results in significant CPU savings.
84
+	targets := targetMapToSlice(targetMap)
85
+
81 86
 	ƒ := func(s *stream, r float64) float64 {
82 87
 		var m = math.MaxFloat64
83 88
 		var f float64
84
-		for quantile, epsilon := range targets {
85
-			if quantile*s.n <= r {
86
-				f = (2 * epsilon * r) / quantile
89
+		for _, t := range targets {
90
+			if t.quantile*s.n <= r {
91
+				f = (2 * t.epsilon * r) / t.quantile
87 92
 			} else {
88
-				f = (2 * epsilon * (s.n - r)) / (1 - quantile)
93
+				f = (2 * t.epsilon * (s.n - r)) / (1 - t.quantile)
89 94
 			}
90 95
 			if f < m {
91 96
 				m = f
@@ -96,6 +101,25 @@ func NewTargeted(targets map[float64]float64) *Stream {
96 101
 	return newStream(ƒ)
97 102
 }
98 103
 
104
+type target struct {
105
+	quantile float64
106
+	epsilon  float64
107
+}
108
+
109
+func targetMapToSlice(targetMap map[float64]float64) []target {
110
+	targets := make([]target, 0, len(targetMap))
111
+
112
+	for quantile, epsilon := range targetMap {
113
+		t := target{
114
+			quantile: quantile,
115
+			epsilon:  epsilon,
116
+		}
117
+		targets = append(targets, t)
118
+	}
119
+
120
+	return targets
121
+}
122
+
99 123
 // Stream computes quantiles for a stream of float64s. It is not thread-safe by
100 124
 // design. Take care when using across multiple goroutines.
101 125
 type Stream struct {

+ 0
- 43
vendor/github.com/golang/protobuf/proto/Makefile View File

@@ -1,43 +0,0 @@
1
-# Go support for Protocol Buffers - Google's data interchange format
2
-#
3
-# Copyright 2010 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
-install:
33
-	go install
34
-
35
-test: install generate-test-pbs
36
-	go test
37
-
38
-
39
-generate-test-pbs:
40
-	make install
41
-	make -C testdata
42
-	protoc --go_out=Mtestdata/test.proto=github.com/golang/protobuf/proto/testdata,Mgoogle/protobuf/any.proto=github.com/golang/protobuf/ptypes/any:. proto3_proto/proto3.proto
43
-	make

+ 35
- 11
vendor/github.com/golang/protobuf/proto/clone.go View File

@@ -35,22 +35,39 @@
35 35
 package proto
36 36
 
37 37
 import (
38
+	"fmt"
38 39
 	"log"
39 40
 	"reflect"
40 41
 	"strings"
41 42
 )
42 43
 
43 44
 // Clone returns a deep copy of a protocol buffer.
44
-func Clone(pb Message) Message {
45
-	in := reflect.ValueOf(pb)
45
+func Clone(src Message) Message {
46
+	in := reflect.ValueOf(src)
46 47
 	if in.IsNil() {
47
-		return pb
48
+		return src
48 49
 	}
49
-
50 50
 	out := reflect.New(in.Type().Elem())
51
-	// out is empty so a merge is a deep copy.
52
-	mergeStruct(out.Elem(), in.Elem())
53
-	return out.Interface().(Message)
51
+	dst := out.Interface().(Message)
52
+	Merge(dst, src)
53
+	return dst
54
+}
55
+
56
+// Merger is the interface representing objects that can merge messages of the same type.
57
+type Merger interface {
58
+	// Merge merges src into this message.
59
+	// Required and optional fields that are set in src will be set to that value in dst.
60
+	// Elements of repeated fields will be appended.
61
+	//
62
+	// Merge may panic if called with a different argument type than the receiver.
63
+	Merge(src Message)
64
+}
65
+
66
+// generatedMerger is the custom merge method that generated protos will have.
67
+// We must add this method since a generate Merge method will conflict with
68
+// many existing protos that have a Merge data field already defined.
69
+type generatedMerger interface {
70
+	XXX_Merge(src Message)
54 71
 }
55 72
 
56 73
 // Merge merges src into dst.
@@ -58,17 +75,24 @@ func Clone(pb Message) Message {
58 75
 // Elements of repeated fields will be appended.
59 76
 // Merge panics if src and dst are not the same type, or if dst is nil.
60 77
 func Merge(dst, src Message) {
78
+	if m, ok := dst.(Merger); ok {
79
+		m.Merge(src)
80
+		return
81
+	}
82
+
61 83
 	in := reflect.ValueOf(src)
62 84
 	out := reflect.ValueOf(dst)
63 85
 	if out.IsNil() {
64 86
 		panic("proto: nil destination")
65 87
 	}
66 88
 	if in.Type() != out.Type() {
67
-		// Explicit test prior to mergeStruct so that mistyped nils will fail
68
-		panic("proto: type mismatch")
89
+		panic(fmt.Sprintf("proto.Merge(%T, %T) type mismatch", dst, src))
69 90
 	}
70 91
 	if in.IsNil() {
71
-		// Merging nil into non-nil is a quiet no-op
92
+		return // Merge from nil src is a noop
93
+	}
94
+	if m, ok := dst.(generatedMerger); ok {
95
+		m.XXX_Merge(src)
72 96
 		return
73 97
 	}
74 98
 	mergeStruct(out.Elem(), in.Elem())
@@ -84,7 +108,7 @@ func mergeStruct(out, in reflect.Value) {
84 108
 		mergeAny(out.Field(i), in.Field(i), false, sprop.Prop[i])
85 109
 	}
86 110
 
87
-	if emIn, ok := extendable(in.Addr().Interface()); ok {
111
+	if emIn, err := extendable(in.Addr().Interface()); err == nil {
88 112
 		emOut, _ := extendable(out.Addr().Interface())
89 113
 		mIn, muIn := emIn.extensionsRead()
90 114
 		if mIn != nil {

+ 63
- 605
vendor/github.com/golang/protobuf/proto/decode.go View File

@@ -39,8 +39,6 @@ import (
39 39
 	"errors"
40 40
 	"fmt"
41 41
 	"io"
42
-	"os"
43
-	"reflect"
44 42
 )
45 43
 
46 44
 // errOverflow is returned when an integer is too large to be represented.
@@ -50,10 +48,6 @@ var errOverflow = errors.New("proto: integer overflow")
50 48
 // wire type is encountered. It does not get returned to user code.
51 49
 var ErrInternalBadWireType = errors.New("proto: internal error: bad wiretype for oneof")
52 50
 
53
-// The fundamental decoders that interpret bytes on the wire.
54
-// Those that take integer types all return uint64 and are
55
-// therefore of type valueDecoder.
56
-
57 51
 // DecodeVarint reads a varint-encoded integer from the slice.
58 52
 // It returns the integer and the number of bytes consumed, or
59 53
 // zero if there is not enough.
@@ -267,9 +261,6 @@ func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
267 261
 	return
268 262
 }
269 263
 
270
-// These are not ValueDecoders: they produce an array of bytes or a string.
271
-// bytes, embedded messages
272
-
273 264
 // DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
274 265
 // This is the format used for the bytes protocol buffer
275 266
 // type and for embedded messages.
@@ -311,81 +302,29 @@ func (p *Buffer) DecodeStringBytes() (s string, err error) {
311 302
 	return string(buf), nil
312 303
 }
313 304
 
314
-// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
315
-// If the protocol buffer has extensions, and the field matches, add it as an extension.
316
-// Otherwise, if the XXX_unrecognized field exists, append the skipped data there.
317
-func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base structPointer, unrecField field) error {
318
-	oi := o.index
319
-
320
-	err := o.skip(t, tag, wire)
321
-	if err != nil {
322
-		return err
323
-	}
324
-
325
-	if !unrecField.IsValid() {
326
-		return nil
327
-	}
328
-
329
-	ptr := structPointer_Bytes(base, unrecField)
330
-
331
-	// Add the skipped field to struct field
332
-	obuf := o.buf
333
-
334
-	o.buf = *ptr
335
-	o.EncodeVarint(uint64(tag<<3 | wire))
336
-	*ptr = append(o.buf, obuf[oi:o.index]...)
337
-
338
-	o.buf = obuf
339
-
340
-	return nil
341
-}
342
-
343
-// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
344
-func (o *Buffer) skip(t reflect.Type, tag, wire int) error {
345
-
346
-	var u uint64
347
-	var err error
348
-
349
-	switch wire {
350
-	case WireVarint:
351
-		_, err = o.DecodeVarint()
352
-	case WireFixed64:
353
-		_, err = o.DecodeFixed64()
354
-	case WireBytes:
355
-		_, err = o.DecodeRawBytes(false)
356
-	case WireFixed32:
357
-		_, err = o.DecodeFixed32()
358
-	case WireStartGroup:
359
-		for {
360
-			u, err = o.DecodeVarint()
361
-			if err != nil {
362
-				break
363
-			}
364
-			fwire := int(u & 0x7)
365
-			if fwire == WireEndGroup {
366
-				break
367
-			}
368
-			ftag := int(u >> 3)
369
-			err = o.skip(t, ftag, fwire)
370
-			if err != nil {
371
-				break
372
-			}
373
-		}
374
-	default:
375
-		err = fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, t)
376
-	}
377
-	return err
378
-}
379
-
380 305
 // Unmarshaler is the interface representing objects that can
381
-// unmarshal themselves.  The method should reset the receiver before
382
-// decoding starts.  The argument points to data that may be
306
+// unmarshal themselves.  The argument points to data that may be
383 307
 // overwritten, so implementations should not keep references to the
384 308
 // buffer.
309
+// Unmarshal implementations should not clear the receiver.
310
+// Any unmarshaled data should be merged into the receiver.
311
+// Callers of Unmarshal that do not want to retain existing data
312
+// should Reset the receiver before calling Unmarshal.
385 313
 type Unmarshaler interface {
386 314
 	Unmarshal([]byte) error
387 315
 }
388 316
 
317
+// newUnmarshaler is the interface representing objects that can
318
+// unmarshal themselves. The semantics are identical to Unmarshaler.
319
+//
320
+// This exists to support protoc-gen-go generated messages.
321
+// The proto package will stop type-asserting to this interface in the future.
322
+//
323
+// DO NOT DEPEND ON THIS.
324
+type newUnmarshaler interface {
325
+	XXX_Unmarshal([]byte) error
326
+}
327
+
389 328
 // Unmarshal parses the protocol buffer representation in buf and places the
390 329
 // decoded result in pb.  If the struct underlying pb does not match
391 330
 // the data in buf, the results can be unpredictable.
@@ -395,7 +334,13 @@ type Unmarshaler interface {
395 334
 // to preserve and append to existing data.
396 335
 func Unmarshal(buf []byte, pb Message) error {
397 336
 	pb.Reset()
398
-	return UnmarshalMerge(buf, pb)
337
+	if u, ok := pb.(newUnmarshaler); ok {
338
+		return u.XXX_Unmarshal(buf)
339
+	}
340
+	if u, ok := pb.(Unmarshaler); ok {
341
+		return u.Unmarshal(buf)
342
+	}
343
+	return NewBuffer(buf).Unmarshal(pb)
399 344
 }
400 345
 
401 346
 // UnmarshalMerge parses the protocol buffer representation in buf and
@@ -405,8 +350,16 @@ func Unmarshal(buf []byte, pb Message) error {
405 350
 // UnmarshalMerge merges into existing data in pb.
406 351
 // Most code should use Unmarshal instead.
407 352
 func UnmarshalMerge(buf []byte, pb Message) error {
408
-	// If the object can unmarshal itself, let it.
353
+	if u, ok := pb.(newUnmarshaler); ok {
354
+		return u.XXX_Unmarshal(buf)
355
+	}
409 356
 	if u, ok := pb.(Unmarshaler); ok {
357
+		// NOTE: The history of proto have unfortunately been inconsistent
358
+		// whether Unmarshaler should or should not implicitly clear itself.
359
+		// Some implementations do, most do not.
360
+		// Thus, calling this here may or may not do what people want.
361
+		//
362
+		// See https://github.com/golang/protobuf/issues/424
410 363
 		return u.Unmarshal(buf)
411 364
 	}
412 365
 	return NewBuffer(buf).Unmarshal(pb)
@@ -422,12 +375,17 @@ func (p *Buffer) DecodeMessage(pb Message) error {
422 375
 }
423 376
 
424 377
 // DecodeGroup reads a tag-delimited group from the Buffer.
378
+// StartGroup tag is already consumed. This function consumes
379
+// EndGroup tag.
425 380
 func (p *Buffer) DecodeGroup(pb Message) error {
426
-	typ, base, err := getbase(pb)
427
-	if err != nil {
428
-		return err
381
+	b := p.buf[p.index:]
382
+	x, y := findEndGroup(b)
383
+	if x < 0 {
384
+		return io.ErrUnexpectedEOF
429 385
 	}
430
-	return p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), true, base)
386
+	err := Unmarshal(b[:x], pb)
387
+	p.index += y
388
+	return err
431 389
 }
432 390
 
433 391
 // Unmarshal parses the protocol buffer representation in the
@@ -438,533 +396,33 @@ func (p *Buffer) DecodeGroup(pb Message) error {
438 396
 // Unlike proto.Unmarshal, this does not reset pb before starting to unmarshal.
439 397
 func (p *Buffer) Unmarshal(pb Message) error {
440 398
 	// If the object can unmarshal itself, let it.
441
-	if u, ok := pb.(Unmarshaler); ok {
442
-		err := u.Unmarshal(p.buf[p.index:])
399
+	if u, ok := pb.(newUnmarshaler); ok {
400
+		err := u.XXX_Unmarshal(p.buf[p.index:])
443 401
 		p.index = len(p.buf)
444 402
 		return err
445 403
 	}
446
-
447
-	typ, base, err := getbase(pb)
448
-	if err != nil {
449
-		return err
450
-	}
451
-
452
-	err = p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), false, base)
453
-
454
-	if collectStats {
455
-		stats.Decode++
456
-	}
457
-
458
-	return err
459
-}
460
-
461
-// unmarshalType does the work of unmarshaling a structure.
462
-func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group bool, base structPointer) error {
463
-	var state errorState
464
-	required, reqFields := prop.reqCount, uint64(0)
465
-
466
-	var err error
467
-	for err == nil && o.index < len(o.buf) {
468
-		oi := o.index
469
-		var u uint64
470
-		u, err = o.DecodeVarint()
471
-		if err != nil {
472
-			break
473
-		}
474
-		wire := int(u & 0x7)
475
-		if wire == WireEndGroup {
476
-			if is_group {
477
-				if required > 0 {
478
-					// Not enough information to determine the exact field.
479
-					// (See below.)
480
-					return &RequiredNotSetError{"{Unknown}"}
481
-				}
482
-				return nil // input is satisfied
483
-			}
484
-			return fmt.Errorf("proto: %s: wiretype end group for non-group", st)
485
-		}
486
-		tag := int(u >> 3)
487
-		if tag <= 0 {
488
-			return fmt.Errorf("proto: %s: illegal tag %d (wire type %d)", st, tag, wire)
489
-		}
490
-		fieldnum, ok := prop.decoderTags.get(tag)
491
-		if !ok {
492
-			// Maybe it's an extension?
493
-			if prop.extendable {
494
-				if e, _ := extendable(structPointer_Interface(base, st)); isExtensionField(e, int32(tag)) {
495
-					if err = o.skip(st, tag, wire); err == nil {
496
-						extmap := e.extensionsWrite()
497
-						ext := extmap[int32(tag)] // may be missing
498
-						ext.enc = append(ext.enc, o.buf[oi:o.index]...)
499
-						extmap[int32(tag)] = ext
500
-					}
501
-					continue
502
-				}
503
-			}
504
-			// Maybe it's a oneof?
505
-			if prop.oneofUnmarshaler != nil {
506
-				m := structPointer_Interface(base, st).(Message)
507
-				// First return value indicates whether tag is a oneof field.
508
-				ok, err = prop.oneofUnmarshaler(m, tag, wire, o)
509
-				if err == ErrInternalBadWireType {
510
-					// Map the error to something more descriptive.
511
-					// Do the formatting here to save generated code space.
512
-					err = fmt.Errorf("bad wiretype for oneof field in %T", m)
513
-				}
514
-				if ok {
515
-					continue
516
-				}
517
-			}
518
-			err = o.skipAndSave(st, tag, wire, base, prop.unrecField)
519
-			continue
520
-		}
521
-		p := prop.Prop[fieldnum]
522
-
523
-		if p.dec == nil {
524
-			fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", st, st.Field(fieldnum).Name)
525
-			continue
526
-		}
527
-		dec := p.dec
528
-		if wire != WireStartGroup && wire != p.WireType {
529
-			if wire == WireBytes && p.packedDec != nil {
530
-				// a packable field
531
-				dec = p.packedDec
532
-			} else {
533
-				err = fmt.Errorf("proto: bad wiretype for field %s.%s: got wiretype %d, want %d", st, st.Field(fieldnum).Name, wire, p.WireType)
534
-				continue
535
-			}
536
-		}
537
-		decErr := dec(o, p, base)
538
-		if decErr != nil && !state.shouldContinue(decErr, p) {
539
-			err = decErr
540
-		}
541
-		if err == nil && p.Required {
542
-			// Successfully decoded a required field.
543
-			if tag <= 64 {
544
-				// use bitmap for fields 1-64 to catch field reuse.
545
-				var mask uint64 = 1 << uint64(tag-1)
546
-				if reqFields&mask == 0 {
547
-					// new required field
548
-					reqFields |= mask
549
-					required--
550
-				}
551
-			} else {
552
-				// This is imprecise. It can be fooled by a required field
553
-				// with a tag > 64 that is encoded twice; that's very rare.
554
-				// A fully correct implementation would require allocating
555
-				// a data structure, which we would like to avoid.
556
-				required--
557
-			}
558
-		}
559
-	}
560
-	if err == nil {
561
-		if is_group {
562
-			return io.ErrUnexpectedEOF
563
-		}
564
-		if state.err != nil {
565
-			return state.err
566
-		}
567
-		if required > 0 {
568
-			// Not enough information to determine the exact field. If we use extra
569
-			// CPU, we could determine the field only if the missing required field
570
-			// has a tag <= 64 and we check reqFields.
571
-			return &RequiredNotSetError{"{Unknown}"}
572
-		}
573
-	}
574
-	return err
575
-}
576
-
577
-// Individual type decoders
578
-// For each,
579
-//	u is the decoded value,
580
-//	v is a pointer to the field (pointer) in the struct
581
-
582
-// Sizes of the pools to allocate inside the Buffer.
583
-// The goal is modest amortization and allocation
584
-// on at least 16-byte boundaries.
585
-const (
586
-	boolPoolSize   = 16
587
-	uint32PoolSize = 8
588
-	uint64PoolSize = 4
589
-)
590
-
591
-// Decode a bool.
592
-func (o *Buffer) dec_bool(p *Properties, base structPointer) error {
593
-	u, err := p.valDec(o)
594
-	if err != nil {
595
-		return err
596
-	}
597
-	if len(o.bools) == 0 {
598
-		o.bools = make([]bool, boolPoolSize)
599
-	}
600
-	o.bools[0] = u != 0
601
-	*structPointer_Bool(base, p.field) = &o.bools[0]
602
-	o.bools = o.bools[1:]
603
-	return nil
604
-}
605
-
606
-func (o *Buffer) dec_proto3_bool(p *Properties, base structPointer) error {
607
-	u, err := p.valDec(o)
608
-	if err != nil {
609
-		return err
610
-	}
611
-	*structPointer_BoolVal(base, p.field) = u != 0
612
-	return nil
613
-}
614
-
615
-// Decode an int32.
616
-func (o *Buffer) dec_int32(p *Properties, base structPointer) error {
617
-	u, err := p.valDec(o)
618
-	if err != nil {
619
-		return err
620
-	}
621
-	word32_Set(structPointer_Word32(base, p.field), o, uint32(u))
622
-	return nil
623
-}
624
-
625
-func (o *Buffer) dec_proto3_int32(p *Properties, base structPointer) error {
626
-	u, err := p.valDec(o)
627
-	if err != nil {
628
-		return err
629
-	}
630
-	word32Val_Set(structPointer_Word32Val(base, p.field), uint32(u))
631
-	return nil
632
-}
633
-
634
-// Decode an int64.
635
-func (o *Buffer) dec_int64(p *Properties, base structPointer) error {
636
-	u, err := p.valDec(o)
637
-	if err != nil {
638
-		return err
639
-	}
640
-	word64_Set(structPointer_Word64(base, p.field), o, u)
641
-	return nil
642
-}
643
-
644
-func (o *Buffer) dec_proto3_int64(p *Properties, base structPointer) error {
645
-	u, err := p.valDec(o)
646
-	if err != nil {
647
-		return err
648
-	}
649
-	word64Val_Set(structPointer_Word64Val(base, p.field), o, u)
650
-	return nil
651
-}
652
-
653
-// Decode a string.
654
-func (o *Buffer) dec_string(p *Properties, base structPointer) error {
655
-	s, err := o.DecodeStringBytes()
656
-	if err != nil {
657
-		return err
658
-	}
659
-	*structPointer_String(base, p.field) = &s
660
-	return nil
661
-}
662
-
663
-func (o *Buffer) dec_proto3_string(p *Properties, base structPointer) error {
664
-	s, err := o.DecodeStringBytes()
665
-	if err != nil {
666
-		return err
667
-	}
668
-	*structPointer_StringVal(base, p.field) = s
669
-	return nil
670
-}
671
-
672
-// Decode a slice of bytes ([]byte).
673
-func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error {
674
-	b, err := o.DecodeRawBytes(true)
675
-	if err != nil {
676
-		return err
677
-	}
678
-	*structPointer_Bytes(base, p.field) = b
679
-	return nil
680
-}
681
-
682
-// Decode a slice of bools ([]bool).
683
-func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error {
684
-	u, err := p.valDec(o)
685
-	if err != nil {
686
-		return err
687
-	}
688
-	v := structPointer_BoolSlice(base, p.field)
689
-	*v = append(*v, u != 0)
690
-	return nil
691
-}
692
-
693
-// Decode a slice of bools ([]bool) in packed format.
694
-func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error {
695
-	v := structPointer_BoolSlice(base, p.field)
696
-
697
-	nn, err := o.DecodeVarint()
698
-	if err != nil {
699
-		return err
700
-	}
701
-	nb := int(nn) // number of bytes of encoded bools
702
-	fin := o.index + nb
703
-	if fin < o.index {
704
-		return errOverflow
705
-	}
706
-
707
-	y := *v
708
-	for o.index < fin {
709
-		u, err := p.valDec(o)
710
-		if err != nil {
711
-			return err
712
-		}
713
-		y = append(y, u != 0)
714
-	}
715
-
716
-	*v = y
717
-	return nil
718
-}
719
-
720
-// Decode a slice of int32s ([]int32).
721
-func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error {
722
-	u, err := p.valDec(o)
723
-	if err != nil {
724
-		return err
725
-	}
726
-	structPointer_Word32Slice(base, p.field).Append(uint32(u))
727
-	return nil
728
-}
729
-
730
-// Decode a slice of int32s ([]int32) in packed format.
731
-func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error {
732
-	v := structPointer_Word32Slice(base, p.field)
733
-
734
-	nn, err := o.DecodeVarint()
735
-	if err != nil {
736
-		return err
737
-	}
738
-	nb := int(nn) // number of bytes of encoded int32s
739
-
740
-	fin := o.index + nb
741
-	if fin < o.index {
742
-		return errOverflow
743
-	}
744
-	for o.index < fin {
745
-		u, err := p.valDec(o)
746
-		if err != nil {
747
-			return err
748
-		}
749
-		v.Append(uint32(u))
750
-	}
751
-	return nil
752
-}
753
-
754
-// Decode a slice of int64s ([]int64).
755
-func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error {
756
-	u, err := p.valDec(o)
757
-	if err != nil {
758
-		return err
759
-	}
760
-
761
-	structPointer_Word64Slice(base, p.field).Append(u)
762
-	return nil
763
-}
764
-
765
-// Decode a slice of int64s ([]int64) in packed format.
766
-func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error {
767
-	v := structPointer_Word64Slice(base, p.field)
768
-
769
-	nn, err := o.DecodeVarint()
770
-	if err != nil {
771
-		return err
772
-	}
773
-	nb := int(nn) // number of bytes of encoded int64s
774
-
775
-	fin := o.index + nb
776
-	if fin < o.index {
777
-		return errOverflow
778
-	}
779
-	for o.index < fin {
780
-		u, err := p.valDec(o)
781
-		if err != nil {
782
-			return err
783
-		}
784
-		v.Append(u)
785
-	}
786
-	return nil
787
-}
788
-
789
-// Decode a slice of strings ([]string).
790
-func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error {
791
-	s, err := o.DecodeStringBytes()
792
-	if err != nil {
793
-		return err
794
-	}
795
-	v := structPointer_StringSlice(base, p.field)
796
-	*v = append(*v, s)
797
-	return nil
798
-}
799
-
800
-// Decode a slice of slice of bytes ([][]byte).
801
-func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error {
802
-	b, err := o.DecodeRawBytes(true)
803
-	if err != nil {
804
-		return err
805
-	}
806
-	v := structPointer_BytesSlice(base, p.field)
807
-	*v = append(*v, b)
808
-	return nil
809
-}
810
-
811
-// Decode a map field.
812
-func (o *Buffer) dec_new_map(p *Properties, base structPointer) error {
813
-	raw, err := o.DecodeRawBytes(false)
814
-	if err != nil {
815
-		return err
816
-	}
817
-	oi := o.index       // index at the end of this map entry
818
-	o.index -= len(raw) // move buffer back to start of map entry
819
-
820
-	mptr := structPointer_NewAt(base, p.field, p.mtype) // *map[K]V
821
-	if mptr.Elem().IsNil() {
822
-		mptr.Elem().Set(reflect.MakeMap(mptr.Type().Elem()))
823
-	}
824
-	v := mptr.Elem() // map[K]V
825
-
826
-	// Prepare addressable doubly-indirect placeholders for the key and value types.
827
-	// See enc_new_map for why.
828
-	keyptr := reflect.New(reflect.PtrTo(p.mtype.Key())).Elem() // addressable *K
829
-	keybase := toStructPointer(keyptr.Addr())                  // **K
830
-
831
-	var valbase structPointer
832
-	var valptr reflect.Value
833
-	switch p.mtype.Elem().Kind() {
834
-	case reflect.Slice:
835
-		// []byte
836
-		var dummy []byte
837
-		valptr = reflect.ValueOf(&dummy)  // *[]byte
838
-		valbase = toStructPointer(valptr) // *[]byte
839
-	case reflect.Ptr:
840
-		// message; valptr is **Msg; need to allocate the intermediate pointer
841
-		valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V
842
-		valptr.Set(reflect.New(valptr.Type().Elem()))
843
-		valbase = toStructPointer(valptr)
844
-	default:
845
-		// everything else
846
-		valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V
847
-		valbase = toStructPointer(valptr.Addr())                   // **V
848
-	}
849
-
850
-	// Decode.
851
-	// This parses a restricted wire format, namely the encoding of a message
852
-	// with two fields. See enc_new_map for the format.
853
-	for o.index < oi {
854
-		// tagcode for key and value properties are always a single byte
855
-		// because they have tags 1 and 2.
856
-		tagcode := o.buf[o.index]
857
-		o.index++
858
-		switch tagcode {
859
-		case p.mkeyprop.tagcode[0]:
860
-			if err := p.mkeyprop.dec(o, p.mkeyprop, keybase); err != nil {
861
-				return err
862
-			}
863
-		case p.mvalprop.tagcode[0]:
864
-			if err := p.mvalprop.dec(o, p.mvalprop, valbase); err != nil {
865
-				return err
866
-			}
867
-		default:
868
-			// TODO: Should we silently skip this instead?
869
-			return fmt.Errorf("proto: bad map data tag %d", raw[0])
870
-		}
871
-	}
872
-	keyelem, valelem := keyptr.Elem(), valptr.Elem()
873
-	if !keyelem.IsValid() {
874
-		keyelem = reflect.Zero(p.mtype.Key())
875
-	}
876
-	if !valelem.IsValid() {
877
-		valelem = reflect.Zero(p.mtype.Elem())
878
-	}
879
-
880
-	v.SetMapIndex(keyelem, valelem)
881
-	return nil
882
-}
883
-
884
-// Decode a group.
885
-func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error {
886
-	bas := structPointer_GetStructPointer(base, p.field)
887
-	if structPointer_IsNil(bas) {
888
-		// allocate new nested message
889
-		bas = toStructPointer(reflect.New(p.stype))
890
-		structPointer_SetStructPointer(base, p.field, bas)
891
-	}
892
-	return o.unmarshalType(p.stype, p.sprop, true, bas)
893
-}
894
-
895
-// Decode an embedded message.
896
-func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) {
897
-	raw, e := o.DecodeRawBytes(false)
898
-	if e != nil {
899
-		return e
900
-	}
901
-
902
-	bas := structPointer_GetStructPointer(base, p.field)
903
-	if structPointer_IsNil(bas) {
904
-		// allocate new nested message
905
-		bas = toStructPointer(reflect.New(p.stype))
906
-		structPointer_SetStructPointer(base, p.field, bas)
907
-	}
908
-
909
-	// If the object can unmarshal itself, let it.
910
-	if p.isUnmarshaler {
911
-		iv := structPointer_Interface(bas, p.stype)
912
-		return iv.(Unmarshaler).Unmarshal(raw)
913
-	}
914
-
915
-	obuf := o.buf
916
-	oi := o.index
917
-	o.buf = raw
918
-	o.index = 0
919
-
920
-	err = o.unmarshalType(p.stype, p.sprop, false, bas)
921
-	o.buf = obuf
922
-	o.index = oi
923
-
924
-	return err
925
-}
926
-
927
-// Decode a slice of embedded messages.
928
-func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error {
929
-	return o.dec_slice_struct(p, false, base)
930
-}
931
-
932
-// Decode a slice of embedded groups.
933
-func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error {
934
-	return o.dec_slice_struct(p, true, base)
935
-}
936
-
937
-// Decode a slice of structs ([]*struct).
938
-func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error {
939
-	v := reflect.New(p.stype)
940
-	bas := toStructPointer(v)
941
-	structPointer_StructPointerSlice(base, p.field).Append(bas)
942
-
943
-	if is_group {
944
-		err := o.unmarshalType(p.stype, p.sprop, is_group, bas)
945
-		return err
946
-	}
947
-
948
-	raw, err := o.DecodeRawBytes(false)
949
-	if err != nil {
404
+	if u, ok := pb.(Unmarshaler); ok {
405
+		// NOTE: The history of proto have unfortunately been inconsistent
406
+		// whether Unmarshaler should or should not implicitly clear itself.
407
+		// Some implementations do, most do not.
408
+		// Thus, calling this here may or may not do what people want.
409
+		//
410
+		// See https://github.com/golang/protobuf/issues/424
411
+		err := u.Unmarshal(p.buf[p.index:])
412
+		p.index = len(p.buf)
950 413
 		return err
951 414
 	}
952 415
 
953
-	// If the object can unmarshal itself, let it.
954
-	if p.isUnmarshaler {
955
-		iv := v.Interface()
956
-		return iv.(Unmarshaler).Unmarshal(raw)
957
-	}
958
-
959
-	obuf := o.buf
960
-	oi := o.index
961
-	o.buf = raw
962
-	o.index = 0
963
-
964
-	err = o.unmarshalType(p.stype, p.sprop, is_group, bas)
965
-
966
-	o.buf = obuf
967
-	o.index = oi
968
-
416
+	// Slow workaround for messages that aren't Unmarshalers.
417
+	// This includes some hand-coded .pb.go files and
418
+	// bootstrap protos.
419
+	// TODO: fix all of those and then add Unmarshal to
420
+	// the Message interface. Then:
421
+	// The cast above and code below can be deleted.
422
+	// The old unmarshaler can be deleted.
423
+	// Clients can call Unmarshal directly (can already do that, actually).
424
+	var info InternalMessageInfo
425
+	err := info.Unmarshal(pb, p.buf[p.index:])
426
+	p.index = len(p.buf)
969 427
 	return err
970 428
 }

+ 200
- 1
vendor/github.com/golang/protobuf/proto/discard.go View File

@@ -35,8 +35,14 @@ import (
35 35
 	"fmt"
36 36
 	"reflect"
37 37
 	"strings"
38
+	"sync"
39
+	"sync/atomic"
38 40
 )
39 41
 
42
+type generatedDiscarder interface {
43
+	XXX_DiscardUnknown()
44
+}
45
+
40 46
 // DiscardUnknown recursively discards all unknown fields from this message
41 47
 // and all embedded messages.
42 48
 //
@@ -49,9 +55,202 @@ import (
49 55
 // For proto2 messages, the unknown fields of message extensions are only
50 56
 // discarded from messages that have been accessed via GetExtension.
51 57
 func DiscardUnknown(m Message) {
58
+	if m, ok := m.(generatedDiscarder); ok {
59
+		m.XXX_DiscardUnknown()
60
+		return
61
+	}
62
+	// TODO: Dynamically populate a InternalMessageInfo for legacy messages,
63
+	// but the master branch has no implementation for InternalMessageInfo,
64
+	// so it would be more work to replicate that approach.
52 65
 	discardLegacy(m)
53 66
 }
54 67
 
68
+// DiscardUnknown recursively discards all unknown fields.
69
+func (a *InternalMessageInfo) DiscardUnknown(m Message) {
70
+	di := atomicLoadDiscardInfo(&a.discard)
71
+	if di == nil {
72
+		di = getDiscardInfo(reflect.TypeOf(m).Elem())
73
+		atomicStoreDiscardInfo(&a.discard, di)
74
+	}
75
+	di.discard(toPointer(&m))
76
+}
77
+
78
+type discardInfo struct {
79
+	typ reflect.Type
80
+
81
+	initialized int32 // 0: only typ is valid, 1: everything is valid
82
+	lock        sync.Mutex
83
+
84
+	fields       []discardFieldInfo
85
+	unrecognized field
86
+}
87
+
88
+type discardFieldInfo struct {
89
+	field   field // Offset of field, guaranteed to be valid
90
+	discard func(src pointer)
91
+}
92
+
93
+var (
94
+	discardInfoMap  = map[reflect.Type]*discardInfo{}
95
+	discardInfoLock sync.Mutex
96
+)
97
+
98
+func getDiscardInfo(t reflect.Type) *discardInfo {
99
+	discardInfoLock.Lock()
100
+	defer discardInfoLock.Unlock()
101
+	di := discardInfoMap[t]
102
+	if di == nil {
103
+		di = &discardInfo{typ: t}
104
+		discardInfoMap[t] = di
105
+	}
106
+	return di
107
+}
108
+
109
+func (di *discardInfo) discard(src pointer) {
110
+	if src.isNil() {
111
+		return // Nothing to do.
112
+	}
113
+
114
+	if atomic.LoadInt32(&di.initialized) == 0 {
115
+		di.computeDiscardInfo()
116
+	}
117
+
118
+	for _, fi := range di.fields {
119
+		sfp := src.offset(fi.field)
120
+		fi.discard(sfp)
121
+	}
122
+
123
+	// For proto2 messages, only discard unknown fields in message extensions
124
+	// that have been accessed via GetExtension.
125
+	if em, err := extendable(src.asPointerTo(di.typ).Interface()); err == nil {
126
+		// Ignore lock since DiscardUnknown is not concurrency safe.
127
+		emm, _ := em.extensionsRead()
128
+		for _, mx := range emm {
129
+			if m, ok := mx.value.(Message); ok {
130
+				DiscardUnknown(m)
131
+			}
132
+		}
133
+	}
134
+
135
+	if di.unrecognized.IsValid() {
136
+		*src.offset(di.unrecognized).toBytes() = nil
137
+	}
138
+}
139
+
140
+func (di *discardInfo) computeDiscardInfo() {
141
+	di.lock.Lock()
142
+	defer di.lock.Unlock()
143
+	if di.initialized != 0 {
144
+		return
145
+	}
146
+	t := di.typ
147
+	n := t.NumField()
148
+
149
+	for i := 0; i < n; i++ {
150
+		f := t.Field(i)
151
+		if strings.HasPrefix(f.Name, "XXX_") {
152
+			continue
153
+		}
154
+
155
+		dfi := discardFieldInfo{field: toField(&f)}
156
+		tf := f.Type
157
+
158
+		// Unwrap tf to get its most basic type.
159
+		var isPointer, isSlice bool
160
+		if tf.Kind() == reflect.Slice && tf.Elem().Kind() != reflect.Uint8 {
161
+			isSlice = true
162
+			tf = tf.Elem()
163
+		}
164
+		if tf.Kind() == reflect.Ptr {
165
+			isPointer = true
166
+			tf = tf.Elem()
167
+		}
168
+		if isPointer && isSlice && tf.Kind() != reflect.Struct {
169
+			panic(fmt.Sprintf("%v.%s cannot be a slice of pointers to primitive types", t, f.Name))
170
+		}
171
+
172
+		switch tf.Kind() {
173
+		case reflect.Struct:
174
+			switch {
175
+			case !isPointer:
176
+				panic(fmt.Sprintf("%v.%s cannot be a direct struct value", t, f.Name))
177
+			case isSlice: // E.g., []*pb.T
178
+				di := getDiscardInfo(tf)
179
+				dfi.discard = func(src pointer) {
180
+					sps := src.getPointerSlice()
181
+					for _, sp := range sps {
182
+						if !sp.isNil() {
183
+							di.discard(sp)
184
+						}
185
+					}
186
+				}
187
+			default: // E.g., *pb.T
188
+				di := getDiscardInfo(tf)
189
+				dfi.discard = func(src pointer) {
190
+					sp := src.getPointer()
191
+					if !sp.isNil() {
192
+						di.discard(sp)
193
+					}
194
+				}
195
+			}
196
+		case reflect.Map:
197
+			switch {
198
+			case isPointer || isSlice:
199
+				panic(fmt.Sprintf("%v.%s cannot be a pointer to a map or a slice of map values", t, f.Name))
200
+			default: // E.g., map[K]V
201
+				if tf.Elem().Kind() == reflect.Ptr { // Proto struct (e.g., *T)
202
+					dfi.discard = func(src pointer) {
203
+						sm := src.asPointerTo(tf).Elem()
204
+						if sm.Len() == 0 {
205
+							return
206
+						}
207
+						for _, key := range sm.MapKeys() {
208
+							val := sm.MapIndex(key)
209
+							DiscardUnknown(val.Interface().(Message))
210
+						}
211
+					}
212
+				} else {
213
+					dfi.discard = func(pointer) {} // Noop
214
+				}
215
+			}
216
+		case reflect.Interface:
217
+			// Must be oneof field.
218
+			switch {
219
+			case isPointer || isSlice:
220
+				panic(fmt.Sprintf("%v.%s cannot be a pointer to a interface or a slice of interface values", t, f.Name))
221
+			default: // E.g., interface{}
222
+				// TODO: Make this faster?
223
+				dfi.discard = func(src pointer) {
224
+					su := src.asPointerTo(tf).Elem()
225
+					if !su.IsNil() {
226
+						sv := su.Elem().Elem().Field(0)
227
+						if sv.Kind() == reflect.Ptr && sv.IsNil() {
228
+							return
229
+						}
230
+						switch sv.Type().Kind() {
231
+						case reflect.Ptr: // Proto struct (e.g., *T)
232
+							DiscardUnknown(sv.Interface().(Message))
233
+						}
234
+					}
235
+				}
236
+			}
237
+		default:
238
+			continue
239
+		}
240
+		di.fields = append(di.fields, dfi)
241
+	}
242
+
243
+	di.unrecognized = invalidField
244
+	if f, ok := t.FieldByName("XXX_unrecognized"); ok {
245
+		if f.Type != reflect.TypeOf([]byte{}) {
246
+			panic("expected XXX_unrecognized to be of type []byte")
247
+		}
248
+		di.unrecognized = toField(&f)
249
+	}
250
+
251
+	atomic.StoreInt32(&di.initialized, 1)
252
+}
253
+
55 254
 func discardLegacy(m Message) {
56 255
 	v := reflect.ValueOf(m)
57 256
 	if v.Kind() != reflect.Ptr || v.IsNil() {
@@ -139,7 +338,7 @@ func discardLegacy(m Message) {
139 338
 
140 339
 	// For proto2 messages, only discard unknown fields in message extensions
141 340
 	// that have been accessed via GetExtension.
142
-	if em, ok := extendable(m); ok {
341
+	if em, err := extendable(m); err == nil {
143 342
 		// Ignore lock since discardLegacy is not concurrency safe.
144 343
 		emm, _ := em.extensionsRead()
145 344
 		for _, mx := range emm {

+ 25
- 1166
vendor/github.com/golang/protobuf/proto/encode.go
File diff suppressed because it is too large
View File


+ 15
- 15
vendor/github.com/golang/protobuf/proto/equal.go View File

@@ -109,15 +109,6 @@ func equalStruct(v1, v2 reflect.Value) bool {
109 109
 				// set/unset mismatch
110 110
 				return false
111 111
 			}
112
-			b1, ok := f1.Interface().(raw)
113
-			if ok {
114
-				b2 := f2.Interface().(raw)
115
-				// RawMessage
116
-				if !bytes.Equal(b1.Bytes(), b2.Bytes()) {
117
-					return false
118
-				}
119
-				continue
120
-			}
121 112
 			f1, f2 = f1.Elem(), f2.Elem()
122 113
 		}
123 114
 		if !equalAny(f1, f2, sprop.Prop[i]) {
@@ -146,11 +137,7 @@ func equalStruct(v1, v2 reflect.Value) bool {
146 137
 
147 138
 	u1 := uf.Bytes()
148 139
 	u2 := v2.FieldByName("XXX_unrecognized").Bytes()
149
-	if !bytes.Equal(u1, u2) {
150
-		return false
151
-	}
152
-
153
-	return true
140
+	return bytes.Equal(u1, u2)
154 141
 }
155 142
 
156 143
 // v1 and v2 are known to have the same type.
@@ -261,6 +248,15 @@ func equalExtMap(base reflect.Type, em1, em2 map[int32]Extension) bool {
261 248
 
262 249
 		m1, m2 := e1.value, e2.value
263 250
 
251
+		if m1 == nil && m2 == nil {
252
+			// Both have only encoded form.
253
+			if bytes.Equal(e1.enc, e2.enc) {
254
+				continue
255
+			}
256
+			// The bytes are different, but the extensions might still be
257
+			// equal. We need to decode them to compare.
258
+		}
259
+
264 260
 		if m1 != nil && m2 != nil {
265 261
 			// Both are unencoded.
266 262
 			if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2), nil) {
@@ -276,8 +272,12 @@ func equalExtMap(base reflect.Type, em1, em2 map[int32]Extension) bool {
276 272
 			desc = m[extNum]
277 273
 		}
278 274
 		if desc == nil {
275
+			// If both have only encoded form and the bytes are the same,
276
+			// it is handled above. We get here when the bytes are different.
277
+			// We don't know how to decode it, so just compare them as byte
278
+			// slices.
279 279
 			log.Printf("proto: don't know how to compare extension %d of %v", extNum, base)
280
-			continue
280
+			return false
281 281
 		}
282 282
 		var err error
283 283
 		if m1 == nil {

+ 80
- 124
vendor/github.com/golang/protobuf/proto/extensions.go View File

@@ -38,6 +38,7 @@ package proto
38 38
 import (
39 39
 	"errors"
40 40
 	"fmt"
41
+	"io"
41 42
 	"reflect"
42 43
 	"strconv"
43 44
 	"sync"
@@ -91,14 +92,29 @@ func (n notLocker) Unlock() {}
91 92
 // extendable returns the extendableProto interface for the given generated proto message.
92 93
 // If the proto message has the old extension format, it returns a wrapper that implements
93 94
 // the extendableProto interface.
94
-func extendable(p interface{}) (extendableProto, bool) {
95
-	if ep, ok := p.(extendableProto); ok {
96
-		return ep, ok
97
-	}
98
-	if ep, ok := p.(extendableProtoV1); ok {
99
-		return extensionAdapter{ep}, ok
95
+func extendable(p interface{}) (extendableProto, error) {
96
+	switch p := p.(type) {
97
+	case extendableProto:
98
+		if isNilPtr(p) {
99
+			return nil, fmt.Errorf("proto: nil %T is not extendable", p)
100
+		}
101
+		return p, nil
102
+	case extendableProtoV1:
103
+		if isNilPtr(p) {
104
+			return nil, fmt.Errorf("proto: nil %T is not extendable", p)
105
+		}
106
+		return extensionAdapter{p}, nil
100 107
 	}
101
-	return nil, false
108
+	// Don't allocate a specific error containing %T:
109
+	// this is the hot path for Clone and MarshalText.
110
+	return nil, errNotExtendable
111
+}
112
+
113
+var errNotExtendable = errors.New("proto: not an extendable proto.Message")
114
+
115
+func isNilPtr(x interface{}) bool {
116
+	v := reflect.ValueOf(x)
117
+	return v.Kind() == reflect.Ptr && v.IsNil()
102 118
 }
103 119
 
104 120
 // XXX_InternalExtensions is an internal representation of proto extensions.
@@ -143,9 +159,6 @@ func (e *XXX_InternalExtensions) extensionsRead() (map[int32]Extension, sync.Loc
143 159
 	return e.p.extensionMap, &e.p.mu
144 160
 }
145 161
 
146
-var extendableProtoType = reflect.TypeOf((*extendableProto)(nil)).Elem()
147
-var extendableProtoV1Type = reflect.TypeOf((*extendableProtoV1)(nil)).Elem()
148
-
149 162
 // ExtensionDesc represents an extension specification.
150 163
 // Used in generated code from the protocol compiler.
151 164
 type ExtensionDesc struct {
@@ -179,8 +192,8 @@ type Extension struct {
179 192
 
180 193
 // SetRawExtension is for testing only.
181 194
 func SetRawExtension(base Message, id int32, b []byte) {
182
-	epb, ok := extendable(base)
183
-	if !ok {
195
+	epb, err := extendable(base)
196
+	if err != nil {
184 197
 		return
185 198
 	}
186 199
 	extmap := epb.extensionsWrite()
@@ -205,7 +218,7 @@ func checkExtensionTypes(pb extendableProto, extension *ExtensionDesc) error {
205 218
 		pbi = ea.extendableProtoV1
206 219
 	}
207 220
 	if a, b := reflect.TypeOf(pbi), reflect.TypeOf(extension.ExtendedType); a != b {
208
-		return errors.New("proto: bad extended type; " + b.String() + " does not extend " + a.String())
221
+		return fmt.Errorf("proto: bad extended type; %v does not extend %v", b, a)
209 222
 	}
210 223
 	// Check the range.
211 224
 	if !isExtensionField(pb, extension.Field) {
@@ -250,85 +263,11 @@ func extensionProperties(ed *ExtensionDesc) *Properties {
250 263
 	return prop
251 264
 }
252 265
 
253
-// encode encodes any unmarshaled (unencoded) extensions in e.
254
-func encodeExtensions(e *XXX_InternalExtensions) error {
255
-	m, mu := e.extensionsRead()
256
-	if m == nil {
257
-		return nil // fast path
258
-	}
259
-	mu.Lock()
260
-	defer mu.Unlock()
261
-	return encodeExtensionsMap(m)
262
-}
263
-
264
-// encode encodes any unmarshaled (unencoded) extensions in e.
265
-func encodeExtensionsMap(m map[int32]Extension) error {
266
-	for k, e := range m {
267
-		if e.value == nil || e.desc == nil {
268
-			// Extension is only in its encoded form.
269
-			continue
270
-		}
271
-
272
-		// We don't skip extensions that have an encoded form set,
273
-		// because the extension value may have been mutated after
274
-		// the last time this function was called.
275
-
276
-		et := reflect.TypeOf(e.desc.ExtensionType)
277
-		props := extensionProperties(e.desc)
278
-
279
-		p := NewBuffer(nil)
280
-		// If e.value has type T, the encoder expects a *struct{ X T }.
281
-		// Pass a *T with a zero field and hope it all works out.
282
-		x := reflect.New(et)
283
-		x.Elem().Set(reflect.ValueOf(e.value))
284
-		if err := props.enc(p, props, toStructPointer(x)); err != nil {
285
-			return err
286
-		}
287
-		e.enc = p.buf
288
-		m[k] = e
289
-	}
290
-	return nil
291
-}
292
-
293
-func extensionsSize(e *XXX_InternalExtensions) (n int) {
294
-	m, mu := e.extensionsRead()
295
-	if m == nil {
296
-		return 0
297
-	}
298
-	mu.Lock()
299
-	defer mu.Unlock()
300
-	return extensionsMapSize(m)
301
-}
302
-
303
-func extensionsMapSize(m map[int32]Extension) (n int) {
304
-	for _, e := range m {
305
-		if e.value == nil || e.desc == nil {
306
-			// Extension is only in its encoded form.
307
-			n += len(e.enc)
308
-			continue
309
-		}
310
-
311
-		// We don't skip extensions that have an encoded form set,
312
-		// because the extension value may have been mutated after
313
-		// the last time this function was called.
314
-
315
-		et := reflect.TypeOf(e.desc.ExtensionType)
316
-		props := extensionProperties(e.desc)
317
-
318
-		// If e.value has type T, the encoder expects a *struct{ X T }.
319
-		// Pass a *T with a zero field and hope it all works out.
320
-		x := reflect.New(et)
321
-		x.Elem().Set(reflect.ValueOf(e.value))
322
-		n += props.size(props, toStructPointer(x))
323
-	}
324
-	return
325
-}
326
-
327 266
 // HasExtension returns whether the given extension is present in pb.
328 267
 func HasExtension(pb Message, extension *ExtensionDesc) bool {
329 268
 	// TODO: Check types, field numbers, etc.?
330
-	epb, ok := extendable(pb)
331
-	if !ok {
269
+	epb, err := extendable(pb)
270
+	if err != nil {
332 271
 		return false
333 272
 	}
334 273
 	extmap, mu := epb.extensionsRead()
@@ -336,15 +275,15 @@ func HasExtension(pb Message, extension *ExtensionDesc) bool {
336 275
 		return false
337 276
 	}
338 277
 	mu.Lock()
339
-	_, ok = extmap[extension.Field]
278
+	_, ok := extmap[extension.Field]
340 279
 	mu.Unlock()
341 280
 	return ok
342 281
 }
343 282
 
344 283
 // ClearExtension removes the given extension from pb.
345 284
 func ClearExtension(pb Message, extension *ExtensionDesc) {
346
-	epb, ok := extendable(pb)
347
-	if !ok {
285
+	epb, err := extendable(pb)
286
+	if err != nil {
348 287
 		return
349 288
 	}
350 289
 	// TODO: Check types, field numbers, etc.?
@@ -352,16 +291,26 @@ func ClearExtension(pb Message, extension *ExtensionDesc) {
352 291
 	delete(extmap, extension.Field)
353 292
 }
354 293
 
355
-// GetExtension parses and returns the given extension of pb.
356
-// If the extension is not present and has no default value it returns ErrMissingExtension.
294
+// GetExtension retrieves a proto2 extended field from pb.
295
+//
296
+// If the descriptor is type complete (i.e., ExtensionDesc.ExtensionType is non-nil),
297
+// then GetExtension parses the encoded field and returns a Go value of the specified type.
298
+// If the field is not present, then the default value is returned (if one is specified),
299
+// otherwise ErrMissingExtension is reported.
300
+//
301
+// If the descriptor is not type complete (i.e., ExtensionDesc.ExtensionType is nil),
302
+// then GetExtension returns the raw encoded bytes of the field extension.
357 303
 func GetExtension(pb Message, extension *ExtensionDesc) (interface{}, error) {
358
-	epb, ok := extendable(pb)
359
-	if !ok {
360
-		return nil, errors.New("proto: not an extendable proto")
304
+	epb, err := extendable(pb)
305
+	if err != nil {
306
+		return nil, err
361 307
 	}
362 308
 
363
-	if err := checkExtensionTypes(epb, extension); err != nil {
364
-		return nil, err
309
+	if extension.ExtendedType != nil {
310
+		// can only check type if this is a complete descriptor
311
+		if err := checkExtensionTypes(epb, extension); err != nil {
312
+			return nil, err
313
+		}
365 314
 	}
366 315
 
367 316
 	emap, mu := epb.extensionsRead()
@@ -388,6 +337,11 @@ func GetExtension(pb Message, extension *ExtensionDesc) (interface{}, error) {
388 337
 		return e.value, nil
389 338
 	}
390 339
 
340
+	if extension.ExtensionType == nil {
341
+		// incomplete descriptor
342
+		return e.enc, nil
343
+	}
344
+
391 345
 	v, err := decodeExtension(e.enc, extension)
392 346
 	if err != nil {
393 347
 		return nil, err
@@ -405,6 +359,11 @@ func GetExtension(pb Message, extension *ExtensionDesc) (interface{}, error) {
405 359
 // defaultExtensionValue returns the default value for extension.
406 360
 // If no default for an extension is defined ErrMissingExtension is returned.
407 361
 func defaultExtensionValue(extension *ExtensionDesc) (interface{}, error) {
362
+	if extension.ExtensionType == nil {
363
+		// incomplete descriptor, so no default
364
+		return nil, ErrMissingExtension
365
+	}
366
+
408 367
 	t := reflect.TypeOf(extension.ExtensionType)
409 368
 	props := extensionProperties(extension)
410 369
 
@@ -439,31 +398,28 @@ func defaultExtensionValue(extension *ExtensionDesc) (interface{}, error) {
439 398
 
440 399
 // decodeExtension decodes an extension encoded in b.
441 400
 func decodeExtension(b []byte, extension *ExtensionDesc) (interface{}, error) {
442
-	o := NewBuffer(b)
443
-
444 401
 	t := reflect.TypeOf(extension.ExtensionType)
445
-
446
-	props := extensionProperties(extension)
402
+	unmarshal := typeUnmarshaler(t, extension.Tag)
447 403
 
448 404
 	// t is a pointer to a struct, pointer to basic type or a slice.
449
-	// Allocate a "field" to store the pointer/slice itself; the
450
-	// pointer/slice will be stored here. We pass
451
-	// the address of this field to props.dec.
452
-	// This passes a zero field and a *t and lets props.dec
453
-	// interpret it as a *struct{ x t }.
405
+	// Allocate space to store the pointer/slice.
454 406
 	value := reflect.New(t).Elem()
455 407
 
408
+	var err error
456 409
 	for {
457
-		// Discard wire type and field number varint. It isn't needed.
458
-		if _, err := o.DecodeVarint(); err != nil {
459
-			return nil, err
410
+		x, n := decodeVarint(b)
411
+		if n == 0 {
412
+			return nil, io.ErrUnexpectedEOF
460 413
 		}
414
+		b = b[n:]
415
+		wire := int(x) & 7
461 416
 
462
-		if err := props.dec(o, props, toStructPointer(value.Addr())); err != nil {
417
+		b, err = unmarshal(b, valToPointer(value.Addr()), wire)
418
+		if err != nil {
463 419
 			return nil, err
464 420
 		}
465 421
 
466
-		if o.index >= len(o.buf) {
422
+		if len(b) == 0 {
467 423
 			break
468 424
 		}
469 425
 	}
@@ -473,9 +429,9 @@ func decodeExtension(b []byte, extension *ExtensionDesc) (interface{}, error) {
473 429
 // GetExtensions returns a slice of the extensions present in pb that are also listed in es.
474 430
 // The returned slice has the same length as es; missing extensions will appear as nil elements.
475 431
 func GetExtensions(pb Message, es []*ExtensionDesc) (extensions []interface{}, err error) {
476
-	epb, ok := extendable(pb)
477
-	if !ok {
478
-		return nil, errors.New("proto: not an extendable proto")
432
+	epb, err := extendable(pb)
433
+	if err != nil {
434
+		return nil, err
479 435
 	}
480 436
 	extensions = make([]interface{}, len(es))
481 437
 	for i, e := range es {
@@ -494,9 +450,9 @@ func GetExtensions(pb Message, es []*ExtensionDesc) (extensions []interface{}, e
494 450
 // For non-registered extensions, ExtensionDescs returns an incomplete descriptor containing
495 451
 // just the Field field, which defines the extension's field number.
496 452
 func ExtensionDescs(pb Message) ([]*ExtensionDesc, error) {
497
-	epb, ok := extendable(pb)
498
-	if !ok {
499
-		return nil, fmt.Errorf("proto: %T is not an extendable proto.Message", pb)
453
+	epb, err := extendable(pb)
454
+	if err != nil {
455
+		return nil, err
500 456
 	}
501 457
 	registeredExtensions := RegisteredExtensions(pb)
502 458
 
@@ -523,9 +479,9 @@ func ExtensionDescs(pb Message) ([]*ExtensionDesc, error) {
523 479
 
524 480
 // SetExtension sets the specified extension of pb to the specified value.
525 481
 func SetExtension(pb Message, extension *ExtensionDesc, value interface{}) error {
526
-	epb, ok := extendable(pb)
527
-	if !ok {
528
-		return errors.New("proto: not an extendable proto")
482
+	epb, err := extendable(pb)
483
+	if err != nil {
484
+		return err
529 485
 	}
530 486
 	if err := checkExtensionTypes(epb, extension); err != nil {
531 487
 		return err
@@ -550,8 +506,8 @@ func SetExtension(pb Message, extension *ExtensionDesc, value interface{}) error
550 506
 
551 507
 // ClearAllExtensions clears all extensions from pb.
552 508
 func ClearAllExtensions(pb Message) {
553
-	epb, ok := extendable(pb)
554
-	if !ok {
509
+	epb, err := extendable(pb)
510
+	if err != nil {
555 511
 		return
556 512
 	}
557 513
 	m := epb.extensionsWrite()

+ 47
- 23
vendor/github.com/golang/protobuf/proto/lib.go View File

@@ -265,6 +265,7 @@ package proto
265 265
 
266 266
 import (
267 267
 	"encoding/json"
268
+	"errors"
268 269
 	"fmt"
269 270
 	"log"
270 271
 	"reflect"
@@ -273,6 +274,8 @@ import (
273 274
 	"sync"
274 275
 )
275 276
 
277
+var errInvalidUTF8 = errors.New("proto: invalid UTF-8 string")
278
+
276 279
 // Message is implemented by generated protocol buffer messages.
277 280
 type Message interface {
278 281
 	Reset()
@@ -309,16 +312,7 @@ type Buffer struct {
309 312
 	buf   []byte // encode/decode byte stream
310 313
 	index int    // read point
311 314
 
312
-	// pools of basic types to amortize allocation.
313
-	bools   []bool
314
-	uint32s []uint32
315
-	uint64s []uint64
316
-
317
-	// extra pools, only used with pointer_reflect.go
318
-	int32s   []int32
319
-	int64s   []int64
320
-	float32s []float32
321
-	float64s []float64
315
+	deterministic bool
322 316
 }
323 317
 
324 318
 // NewBuffer allocates a new Buffer and initializes its internal data to
@@ -343,6 +337,30 @@ func (p *Buffer) SetBuf(s []byte) {
343 337
 // Bytes returns the contents of the Buffer.
344 338
 func (p *Buffer) Bytes() []byte { return p.buf }
345 339
 
340
+// SetDeterministic sets whether to use deterministic serialization.
341
+//
342
+// Deterministic serialization guarantees that for a given binary, equal
343
+// messages will always be serialized to the same bytes. This implies:
344
+//
345
+//   - Repeated serialization of a message will return the same bytes.
346
+//   - Different processes of the same binary (which may be executing on
347
+//     different machines) will serialize equal messages to the same bytes.
348
+//
349
+// Note that the deterministic serialization is NOT canonical across
350
+// languages. It is not guaranteed to remain stable over time. It is unstable
351
+// across different builds with schema changes due to unknown fields.
352
+// Users who need canonical serialization (e.g., persistent storage in a
353
+// canonical form, fingerprinting, etc.) should define their own
354
+// canonicalization specification and implement their own serializer rather
355
+// than relying on this API.
356
+//
357
+// If deterministic serialization is requested, map entries will be sorted
358
+// by keys in lexographical order. This is an implementation detail and
359
+// subject to change.
360
+func (p *Buffer) SetDeterministic(deterministic bool) {
361
+	p.deterministic = deterministic
362
+}
363
+
346 364
 /*
347 365
  * Helper routines for simplifying the creation of optional fields of basic type.
348 366
  */
@@ -831,22 +849,12 @@ func fieldDefault(ft reflect.Type, prop *Properties) (sf *scalarField, nestedMes
831 849
 	return sf, false, nil
832 850
 }
833 851
 
852
+// mapKeys returns a sort.Interface to be used for sorting the map keys.
834 853
 // Map fields may have key types of non-float scalars, strings and enums.
835
-// The easiest way to sort them in some deterministic order is to use fmt.
836
-// If this turns out to be inefficient we can always consider other options,
837
-// such as doing a Schwartzian transform.
838
-
839 854
 func mapKeys(vs []reflect.Value) sort.Interface {
840
-	s := mapKeySorter{
841
-		vs: vs,
842
-		// default Less function: textual comparison
843
-		less: func(a, b reflect.Value) bool {
844
-			return fmt.Sprint(a.Interface()) < fmt.Sprint(b.Interface())
845
-		},
846
-	}
855
+	s := mapKeySorter{vs: vs}
847 856
 
848
-	// Type specialization per https://developers.google.com/protocol-buffers/docs/proto#maps;
849
-	// numeric keys are sorted numerically.
857
+	// Type specialization per https://developers.google.com/protocol-buffers/docs/proto#maps.
850 858
 	if len(vs) == 0 {
851 859
 		return s
852 860
 	}
@@ -855,6 +863,12 @@ func mapKeys(vs []reflect.Value) sort.Interface {
855 863
 		s.less = func(a, b reflect.Value) bool { return a.Int() < b.Int() }
856 864
 	case reflect.Uint32, reflect.Uint64:
857 865
 		s.less = func(a, b reflect.Value) bool { return a.Uint() < b.Uint() }
866
+	case reflect.Bool:
867
+		s.less = func(a, b reflect.Value) bool { return !a.Bool() && b.Bool() } // false < true
868
+	case reflect.String:
869
+		s.less = func(a, b reflect.Value) bool { return a.String() < b.String() }
870
+	default:
871
+		panic(fmt.Sprintf("unsupported map key type: %v", vs[0].Kind()))
858 872
 	}
859 873
 
860 874
 	return s
@@ -895,3 +909,13 @@ const ProtoPackageIsVersion2 = true
895 909
 // ProtoPackageIsVersion1 is referenced from generated protocol buffer files
896 910
 // to assert that that code is compatible with this version of the proto package.
897 911
 const ProtoPackageIsVersion1 = true
912
+
913
+// InternalMessageInfo is a type used internally by generated .pb.go files.
914
+// This type is not intended to be used by non-generated code.
915
+// This type is not subject to any compatibility guarantee.
916
+type InternalMessageInfo struct {
917
+	marshal   *marshalInfo
918
+	unmarshal *unmarshalInfo
919
+	merge     *mergeInfo
920
+	discard   *discardInfo
921
+}

+ 42
- 39
vendor/github.com/golang/protobuf/proto/message_set.go View File

@@ -42,6 +42,7 @@ import (
42 42
 	"fmt"
43 43
 	"reflect"
44 44
 	"sort"
45
+	"sync"
45 46
 )
46 47
 
47 48
 // errNoMessageTypeID occurs when a protocol buffer does not have a message type ID.
@@ -94,10 +95,7 @@ func (ms *messageSet) find(pb Message) *_MessageSet_Item {
94 95
 }
95 96
 
96 97
 func (ms *messageSet) Has(pb Message) bool {
97
-	if ms.find(pb) != nil {
98
-		return true
99
-	}
100
-	return false
98
+	return ms.find(pb) != nil
101 99
 }
102 100
 
103 101
 func (ms *messageSet) Unmarshal(pb Message) error {
@@ -150,46 +148,42 @@ func skipVarint(buf []byte) []byte {
150 148
 // MarshalMessageSet encodes the extension map represented by m in the message set wire format.
151 149
 // It is called by generated Marshal methods on protocol buffer messages with the message_set_wire_format option.
152 150
 func MarshalMessageSet(exts interface{}) ([]byte, error) {
153
-	var m map[int32]Extension
151
+	return marshalMessageSet(exts, false)
152
+}
153
+
154
+// marshaMessageSet implements above function, with the opt to turn on / off deterministic during Marshal.
155
+func marshalMessageSet(exts interface{}, deterministic bool) ([]byte, error) {
154 156
 	switch exts := exts.(type) {
155 157
 	case *XXX_InternalExtensions:
156
-		if err := encodeExtensions(exts); err != nil {
157
-			return nil, err
158
-		}
159
-		m, _ = exts.extensionsRead()
158
+		var u marshalInfo
159
+		siz := u.sizeMessageSet(exts)
160
+		b := make([]byte, 0, siz)
161
+		return u.appendMessageSet(b, exts, deterministic)
162
+
160 163
 	case map[int32]Extension:
161
-		if err := encodeExtensionsMap(exts); err != nil {
162
-			return nil, err
164
+		// This is an old-style extension map.
165
+		// Wrap it in a new-style XXX_InternalExtensions.
166
+		ie := XXX_InternalExtensions{
167
+			p: &struct {
168
+				mu           sync.Mutex
169
+				extensionMap map[int32]Extension
170
+			}{
171
+				extensionMap: exts,
172
+			},
163 173
 		}
164
-		m = exts
174
+
175
+		var u marshalInfo
176
+		siz := u.sizeMessageSet(&ie)
177
+		b := make([]byte, 0, siz)
178
+		return u.appendMessageSet(b, &ie, deterministic)
179
+
165 180
 	default:
166 181
 		return nil, errors.New("proto: not an extension map")
167 182
 	}
168
-
169
-	// Sort extension IDs to provide a deterministic encoding.
170
-	// See also enc_map in encode.go.
171
-	ids := make([]int, 0, len(m))
172
-	for id := range m {
173
-		ids = append(ids, int(id))
174
-	}
175
-	sort.Ints(ids)
176
-
177
-	ms := &messageSet{Item: make([]*_MessageSet_Item, 0, len(m))}
178
-	for _, id := range ids {
179
-		e := m[int32(id)]
180
-		// Remove the wire type and field number varint, as well as the length varint.
181
-		msg := skipVarint(skipVarint(e.enc))
182
-
183
-		ms.Item = append(ms.Item, &_MessageSet_Item{
184
-			TypeId:  Int32(int32(id)),
185
-			Message: msg,
186
-		})
187
-	}
188
-	return Marshal(ms)
189 183
 }
190 184
 
191 185
 // UnmarshalMessageSet decodes the extension map encoded in buf in the message set wire format.
192
-// It is called by generated Unmarshal methods on protocol buffer messages with the message_set_wire_format option.
186
+// It is called by Unmarshal methods on protocol buffer messages with the message_set_wire_format option.
193 187
 func UnmarshalMessageSet(buf []byte, exts interface{}) error {
194 188
 	var m map[int32]Extension
195 189
 	switch exts := exts.(type) {
@@ -235,7 +229,15 @@ func MarshalMessageSetJSON(exts interface{}) ([]byte, error) {
235 229
 	var m map[int32]Extension
236 230
 	switch exts := exts.(type) {
237 231
 	case *XXX_InternalExtensions:
238
-		m, _ = exts.extensionsRead()
232
+		var mu sync.Locker
233
+		m, mu = exts.extensionsRead()
234
+		if m != nil {
235
+			// Keep the extensions map locked until we're done marshaling to prevent
236
+			// races between marshaling and unmarshaling the lazily-{en,de}coded
237
+			// values.
238
+			mu.Lock()
239
+			defer mu.Unlock()
240
+		}
239 241
 	case map[int32]Extension:
240 242
 		m = exts
241 243
 	default:
@@ -253,15 +255,16 @@ func MarshalMessageSetJSON(exts interface{}) ([]byte, error) {
253 255
 
254 256
 	for i, id := range ids {
255 257
 		ext := m[id]
256
-		if i > 0 {
257
-			b.WriteByte(',')
258
-		}
259
-
260 258
 		msd, ok := messageSetMap[id]
261 259
 		if !ok {
262 260
 			// Unknown type; we can't render it, so skip it.
263 261
 			continue
264 262
 		}
263
+
264
+		if i > 0 && b.Len() > 1 {
265
+			b.WriteByte(',')
266
+		}
267
+
265 268
 		fmt.Fprintf(&b, `"[%s]":`, msd.name)
266 269
 
267 270
 		x := ext.value

+ 234
- 361
vendor/github.com/golang/protobuf/proto/pointer_reflect.go View File

@@ -29,7 +29,7 @@
29 29
 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 30
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 31
 
32
-// +build appengine js
32
+// +build purego appengine js
33 33
 
34 34
 // This file contains an implementation of proto field accesses using package reflect.
35 35
 // It is slower than the code in pointer_unsafe.go but it avoids package unsafe and can
@@ -38,32 +38,13 @@
38 38
 package proto
39 39
 
40 40
 import (
41
-	"math"
42 41
 	"reflect"
42
+	"sync"
43 43
 )
44 44
 
45
-// A structPointer is a pointer to a struct.
46
-type structPointer struct {
47
-	v reflect.Value
48
-}
49
-
50
-// toStructPointer returns a structPointer equivalent to the given reflect value.
51
-// The reflect value must itself be a pointer to a struct.
52
-func toStructPointer(v reflect.Value) structPointer {
53
-	return structPointer{v}
54
-}
55
-
56
-// IsNil reports whether p is nil.
57
-func structPointer_IsNil(p structPointer) bool {
58
-	return p.v.IsNil()
59
-}
45
+const unsafeAllowed = false
60 46
 
61
-// Interface returns the struct pointer as an interface value.
62
-func structPointer_Interface(p structPointer, _ reflect.Type) interface{} {
63
-	return p.v.Interface()
64
-}
65
-
66
-// A field identifies a field in a struct, accessible from a structPointer.
47
+// A field identifies a field in a struct, accessible from a pointer.
67 48
 // In this implementation, a field is identified by the sequence of field indices
68 49
 // passed to reflect's FieldByIndex.
69 50
 type field []int
@@ -76,409 +57,301 @@ func toField(f *reflect.StructField) field {
76 57
 // invalidField is an invalid field identifier.
77 58
 var invalidField = field(nil)
78 59
 
60
+// zeroField is a noop when calling pointer.offset.
61
+var zeroField = field([]int{})
62
+
79 63
 // IsValid reports whether the field identifier is valid.
80 64
 func (f field) IsValid() bool { return f != nil }
81 65
 
82
-// field returns the given field in the struct as a reflect value.
83
-func structPointer_field(p structPointer, f field) reflect.Value {
84
-	// Special case: an extension map entry with a value of type T
85
-	// passes a *T to the struct-handling code with a zero field,
86
-	// expecting that it will be treated as equivalent to *struct{ X T },
87
-	// which has the same memory layout. We have to handle that case
88
-	// specially, because reflect will panic if we call FieldByIndex on a
89
-	// non-struct.
90
-	if f == nil {
91
-		return p.v.Elem()
92
-	}
93
-
94
-	return p.v.Elem().FieldByIndex(f)
66
+// The pointer type is for the table-driven decoder.
67
+// The implementation here uses a reflect.Value of pointer type to
68
+// create a generic pointer. In pointer_unsafe.go we use unsafe
69
+// instead of reflect to implement the same (but faster) interface.
70
+type pointer struct {
71
+	v reflect.Value
95 72
 }
96 73
 
97
-// ifield returns the given field in the struct as an interface value.
98
-func structPointer_ifield(p structPointer, f field) interface{} {
99
-	return structPointer_field(p, f).Addr().Interface()
74
+// toPointer converts an interface of pointer type to a pointer
75
+// that points to the same target.
76
+func toPointer(i *Message) pointer {
77
+	return pointer{v: reflect.ValueOf(*i)}
100 78
 }
101 79
 
102
-// Bytes returns the address of a []byte field in the struct.
103
-func structPointer_Bytes(p structPointer, f field) *[]byte {
104
-	return structPointer_ifield(p, f).(*[]byte)
80
+// toAddrPointer converts an interface to a pointer that points to
81
+// the interface data.
82
+func toAddrPointer(i *interface{}, isptr bool) pointer {
83
+	v := reflect.ValueOf(*i)
84
+	u := reflect.New(v.Type())
85
+	u.Elem().Set(v)
86
+	return pointer{v: u}
105 87
 }
106 88
 
107
-// BytesSlice returns the address of a [][]byte field in the struct.
108
-func structPointer_BytesSlice(p structPointer, f field) *[][]byte {
109
-	return structPointer_ifield(p, f).(*[][]byte)
89
+// valToPointer converts v to a pointer.  v must be of pointer type.
90
+func valToPointer(v reflect.Value) pointer {
91
+	return pointer{v: v}
110 92
 }
111 93
 
112
-// Bool returns the address of a *bool field in the struct.
113
-func structPointer_Bool(p structPointer, f field) **bool {
114
-	return structPointer_ifield(p, f).(**bool)
94
+// offset converts from a pointer to a structure to a pointer to
95
+// one of its fields.
96
+func (p pointer) offset(f field) pointer {
97
+	return pointer{v: p.v.Elem().FieldByIndex(f).Addr()}
115 98
 }
116 99
 
117
-// BoolVal returns the address of a bool field in the struct.
118
-func structPointer_BoolVal(p structPointer, f field) *bool {
119
-	return structPointer_ifield(p, f).(*bool)
100
+func (p pointer) isNil() bool {
101
+	return p.v.IsNil()
120 102
 }
121 103
 
122
-// BoolSlice returns the address of a []bool field in the struct.
123
-func structPointer_BoolSlice(p structPointer, f field) *[]bool {
124
-	return structPointer_ifield(p, f).(*[]bool)
104
+// grow updates the slice s in place to make it one element longer.
105
+// s must be addressable.
106
+// Returns the (addressable) new element.
107
+func grow(s reflect.Value) reflect.Value {
108
+	n, m := s.Len(), s.Cap()
109
+	if n < m {
110
+		s.SetLen(n + 1)
111
+	} else {
112
+		s.Set(reflect.Append(s, reflect.Zero(s.Type().Elem())))
113
+	}
114
+	return s.Index(n)
125 115
 }
126 116
 
127
-// String returns the address of a *string field in the struct.
128
-func structPointer_String(p structPointer, f field) **string {
129
-	return structPointer_ifield(p, f).(**string)
117
+func (p pointer) toInt64() *int64 {
118
+	return p.v.Interface().(*int64)
130 119
 }
131
-
132
-// StringVal returns the address of a string field in the struct.
133
-func structPointer_StringVal(p structPointer, f field) *string {
134
-	return structPointer_ifield(p, f).(*string)
120
+func (p pointer) toInt64Ptr() **int64 {
121
+	return p.v.Interface().(**int64)
135 122
 }
136
-
137
-// StringSlice returns the address of a []string field in the struct.
138
-func structPointer_StringSlice(p structPointer, f field) *[]string {
139
-	return structPointer_ifield(p, f).(*[]string)
123
+func (p pointer) toInt64Slice() *[]int64 {
124
+	return p.v.Interface().(*[]int64)
140 125
 }
141 126
 
142
-// Extensions returns the address of an extension map field in the struct.
143
-func structPointer_Extensions(p structPointer, f field) *XXX_InternalExtensions {
144
-	return structPointer_ifield(p, f).(*XXX_InternalExtensions)
145
-}
127
+var int32ptr = reflect.TypeOf((*int32)(nil))
146 128
 
147
-// ExtMap returns the address of an extension map field in the struct.
148
-func structPointer_ExtMap(p structPointer, f field) *map[int32]Extension {
149
-	return structPointer_ifield(p, f).(*map[int32]Extension)
129
+func (p pointer) toInt32() *int32 {
130
+	return p.v.Convert(int32ptr).Interface().(*int32)
150 131
 }
151 132
 
152
-// NewAt returns the reflect.Value for a pointer to a field in the struct.
153
-func structPointer_NewAt(p structPointer, f field, typ reflect.Type) reflect.Value {
154
-	return structPointer_field(p, f).Addr()
133
+// The toInt32Ptr/Slice methods don't work because of enums.
134
+// Instead, we must use set/get methods for the int32ptr/slice case.
135
+/*
136
+	func (p pointer) toInt32Ptr() **int32 {
137
+		return p.v.Interface().(**int32)
155 138
 }
156
-
157
-// SetStructPointer writes a *struct field in the struct.
158
-func structPointer_SetStructPointer(p structPointer, f field, q structPointer) {
159
-	structPointer_field(p, f).Set(q.v)
139
+	func (p pointer) toInt32Slice() *[]int32 {
140
+		return p.v.Interface().(*[]int32)
160 141
 }
161
-
162
-// GetStructPointer reads a *struct field in the struct.
163
-func structPointer_GetStructPointer(p structPointer, f field) structPointer {
164
-	return structPointer{structPointer_field(p, f)}
142
+*/
143
+func (p pointer) getInt32Ptr() *int32 {
144
+	if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {
145
+		// raw int32 type
146
+		return p.v.Elem().Interface().(*int32)
147
+	}
148
+	// an enum
149
+	return p.v.Elem().Convert(int32PtrType).Interface().(*int32)
150
+}
151
+func (p pointer) setInt32Ptr(v int32) {
152
+	// Allocate value in a *int32. Possibly convert that to a *enum.
153
+	// Then assign it to a **int32 or **enum.
154
+	// Note: we can convert *int32 to *enum, but we can't convert
155
+	// **int32 to **enum!
156
+	p.v.Elem().Set(reflect.ValueOf(&v).Convert(p.v.Type().Elem()))
157
+}
158
+
159
+// getInt32Slice copies []int32 from p as a new slice.
160
+// This behavior differs from the implementation in pointer_unsafe.go.
161
+func (p pointer) getInt32Slice() []int32 {
162
+	if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {
163
+		// raw int32 type
164
+		return p.v.Elem().Interface().([]int32)
165
+	}
166
+	// an enum
167
+	// Allocate a []int32, then assign []enum's values into it.
168
+	// Note: we can't convert []enum to []int32.
169
+	slice := p.v.Elem()
170
+	s := make([]int32, slice.Len())
171
+	for i := 0; i < slice.Len(); i++ {
172
+		s[i] = int32(slice.Index(i).Int())
173
+	}
174
+	return s
165 175
 }
166 176
 
167
-// StructPointerSlice the address of a []*struct field in the struct.
168
-func structPointer_StructPointerSlice(p structPointer, f field) structPointerSlice {
169
-	return structPointerSlice{structPointer_field(p, f)}
177
+// setInt32Slice copies []int32 into p as a new slice.
178
+// This behavior differs from the implementation in pointer_unsafe.go.
179
+func (p pointer) setInt32Slice(v []int32) {
180
+	if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {
181
+		// raw int32 type
182
+		p.v.Elem().Set(reflect.ValueOf(v))
183
+		return
184
+	}
185
+	// an enum
186
+	// Allocate a []enum, then assign []int32's values into it.
187
+	// Note: we can't convert []enum to []int32.
188
+	slice := reflect.MakeSlice(p.v.Type().Elem(), len(v), cap(v))
189
+	for i, x := range v {
190
+		slice.Index(i).SetInt(int64(x))
191
+	}
192
+	p.v.Elem().Set(slice)
170 193
 }
171
-
172
-// A structPointerSlice represents the address of a slice of pointers to structs
173
-// (themselves messages or groups). That is, v.Type() is *[]*struct{...}.
174
-type structPointerSlice struct {
175
-	v reflect.Value
194
+func (p pointer) appendInt32Slice(v int32) {
195
+	grow(p.v.Elem()).SetInt(int64(v))
176 196
 }
177 197
 
178
-func (p structPointerSlice) Len() int                  { return p.v.Len() }
179
-func (p structPointerSlice) Index(i int) structPointer { return structPointer{p.v.Index(i)} }
180
-func (p structPointerSlice) Append(q structPointer) {
181
-	p.v.Set(reflect.Append(p.v, q.v))
198
+func (p pointer) toUint64() *uint64 {
199
+	return p.v.Interface().(*uint64)
182 200
 }
183
-
184
-var (
185
-	int32Type   = reflect.TypeOf(int32(0))
186
-	uint32Type  = reflect.TypeOf(uint32(0))
187
-	float32Type = reflect.TypeOf(float32(0))
188
-	int64Type   = reflect.TypeOf(int64(0))
189
-	uint64Type  = reflect.TypeOf(uint64(0))
190
-	float64Type = reflect.TypeOf(float64(0))
191
-)
192
-
193
-// A word32 represents a field of type *int32, *uint32, *float32, or *enum.
194
-// That is, v.Type() is *int32, *uint32, *float32, or *enum and v is assignable.
195
-type word32 struct {
196
-	v reflect.Value
201
+func (p pointer) toUint64Ptr() **uint64 {
202
+	return p.v.Interface().(**uint64)
197 203
 }
198
-
199
-// IsNil reports whether p is nil.
200
-func word32_IsNil(p word32) bool {
201
-	return p.v.IsNil()
204
+func (p pointer) toUint64Slice() *[]uint64 {
205
+	return p.v.Interface().(*[]uint64)
202 206
 }
203
-
204
-// Set sets p to point at a newly allocated word with bits set to x.
205
-func word32_Set(p word32, o *Buffer, x uint32) {
206
-	t := p.v.Type().Elem()
207
-	switch t {
208
-	case int32Type:
209
-		if len(o.int32s) == 0 {
210
-			o.int32s = make([]int32, uint32PoolSize)
211
-		}
212
-		o.int32s[0] = int32(x)
213
-		p.v.Set(reflect.ValueOf(&o.int32s[0]))
214
-		o.int32s = o.int32s[1:]
215
-		return
216
-	case uint32Type:
217
-		if len(o.uint32s) == 0 {
218
-			o.uint32s = make([]uint32, uint32PoolSize)
219
-		}
220
-		o.uint32s[0] = x
221
-		p.v.Set(reflect.ValueOf(&o.uint32s[0]))
222
-		o.uint32s = o.uint32s[1:]
223
-		return
224
-	case float32Type:
225
-		if len(o.float32s) == 0 {
226
-			o.float32s = make([]float32, uint32PoolSize)
227
-		}
228
-		o.float32s[0] = math.Float32frombits(x)
229
-		p.v.Set(reflect.ValueOf(&o.float32s[0]))
230
-		o.float32s = o.float32s[1:]
231
-		return
232
-	}
233
-
234
-	// must be enum
235
-	p.v.Set(reflect.New(t))
236
-	p.v.Elem().SetInt(int64(int32(x)))
207
+func (p pointer) toUint32() *uint32 {
208
+	return p.v.Interface().(*uint32)
237 209
 }
238
-
239
-// Get gets the bits pointed at by p, as a uint32.
240
-func word32_Get(p word32) uint32 {
241
-	elem := p.v.Elem()
242
-	switch elem.Kind() {
243
-	case reflect.Int32:
244
-		return uint32(elem.Int())
245
-	case reflect.Uint32:
246
-		return uint32(elem.Uint())
247
-	case reflect.Float32:
248
-		return math.Float32bits(float32(elem.Float()))
249
-	}
250
-	panic("unreachable")
210
+func (p pointer) toUint32Ptr() **uint32 {
211
+	return p.v.Interface().(**uint32)
251 212
 }
252
-
253
-// Word32 returns a reference to a *int32, *uint32, *float32, or *enum field in the struct.
254
-func structPointer_Word32(p structPointer, f field) word32 {
255
-	return word32{structPointer_field(p, f)}
213
+func (p pointer) toUint32Slice() *[]uint32 {
214
+	return p.v.Interface().(*[]uint32)
256 215
 }
257
-
258
-// A word32Val represents a field of type int32, uint32, float32, or enum.
259
-// That is, v.Type() is int32, uint32, float32, or enum and v is assignable.
260
-type word32Val struct {
261
-	v reflect.Value
216
+func (p pointer) toBool() *bool {
217
+	return p.v.Interface().(*bool)
262 218
 }
263
-
264
-// Set sets *p to x.
265
-func word32Val_Set(p word32Val, x uint32) {
266
-	switch p.v.Type() {
267
-	case int32Type:
268
-		p.v.SetInt(int64(x))
269
-		return
270
-	case uint32Type:
271
-		p.v.SetUint(uint64(x))
272
-		return
273
-	case float32Type:
274
-		p.v.SetFloat(float64(math.Float32frombits(x)))
275
-		return
276
-	}
277
-
278
-	// must be enum
279
-	p.v.SetInt(int64(int32(x)))
219
+func (p pointer) toBoolPtr() **bool {
220
+	return p.v.Interface().(**bool)
280 221
 }
281
-
282
-// Get gets the bits pointed at by p, as a uint32.
283
-func word32Val_Get(p word32Val) uint32 {
284
-	elem := p.v
285
-	switch elem.Kind() {
286
-	case reflect.Int32:
287
-		return uint32(elem.Int())
288
-	case reflect.Uint32:
289
-		return uint32(elem.Uint())
290
-	case reflect.Float32:
291
-		return math.Float32bits(float32(elem.Float()))
292
-	}
293
-	panic("unreachable")
222
+func (p pointer) toBoolSlice() *[]bool {
223
+	return p.v.Interface().(*[]bool)
294 224
 }
295
-
296
-// Word32Val returns a reference to a int32, uint32, float32, or enum field in the struct.
297
-func structPointer_Word32Val(p structPointer, f field) word32Val {
298
-	return word32Val{structPointer_field(p, f)}
225
+func (p pointer) toFloat64() *float64 {
226
+	return p.v.Interface().(*float64)
299 227
 }
300
-
301
-// A word32Slice is a slice of 32-bit values.
302
-// That is, v.Type() is []int32, []uint32, []float32, or []enum.
303
-type word32Slice struct {
304
-	v reflect.Value
228
+func (p pointer) toFloat64Ptr() **float64 {
229
+	return p.v.Interface().(**float64)
305 230
 }
306
-
307
-func (p word32Slice) Append(x uint32) {
308
-	n, m := p.v.Len(), p.v.Cap()
309
-	if n < m {
310
-		p.v.SetLen(n + 1)
311
-	} else {
312
-		t := p.v.Type().Elem()
313
-		p.v.Set(reflect.Append(p.v, reflect.Zero(t)))
314
-	}
315
-	elem := p.v.Index(n)
316
-	switch elem.Kind() {
317
-	case reflect.Int32:
318
-		elem.SetInt(int64(int32(x)))
319
-	case reflect.Uint32:
320
-		elem.SetUint(uint64(x))
321
-	case reflect.Float32:
322
-		elem.SetFloat(float64(math.Float32frombits(x)))
323
-	}
231
+func (p pointer) toFloat64Slice() *[]float64 {
232
+	return p.v.Interface().(*[]float64)
324 233
 }
325
-
326
-func (p word32Slice) Len() int {
327
-	return p.v.Len()
234
+func (p pointer) toFloat32() *float32 {
235
+	return p.v.Interface().(*float32)
328 236
 }
329
-
330
-func (p word32Slice) Index(i int) uint32 {
331
-	elem := p.v.Index(i)
332
-	switch elem.Kind() {
333
-	case reflect.Int32:
334
-		return uint32(elem.Int())
335
-	case reflect.Uint32:
336
-		return uint32(elem.Uint())
337
-	case reflect.Float32:
338
-		return math.Float32bits(float32(elem.Float()))
339
-	}
340
-	panic("unreachable")
237
+func (p pointer) toFloat32Ptr() **float32 {
238
+	return p.v.Interface().(**float32)
341 239
 }
342
-
343
-// Word32Slice returns a reference to a []int32, []uint32, []float32, or []enum field in the struct.
344
-func structPointer_Word32Slice(p structPointer, f field) word32Slice {
345
-	return word32Slice{structPointer_field(p, f)}
240
+func (p pointer) toFloat32Slice() *[]float32 {
241
+	return p.v.Interface().(*[]float32)
346 242
 }
347
-
348
-// word64 is like word32 but for 64-bit values.
349
-type word64 struct {
350
-	v reflect.Value
243
+func (p pointer) toString() *string {
244
+	return p.v.Interface().(*string)
351 245
 }
352
-
353
-func word64_Set(p word64, o *Buffer, x uint64) {
354
-	t := p.v.Type().Elem()
355
-	switch t {
356
-	case int64Type:
357
-		if len(o.int64s) == 0 {
358
-			o.int64s = make([]int64, uint64PoolSize)
359
-		}
360
-		o.int64s[0] = int64(x)
361
-		p.v.Set(reflect.ValueOf(&o.int64s[0]))
362
-		o.int64s = o.int64s[1:]
363
-		return
364
-	case uint64Type:
365
-		if len(o.uint64s) == 0 {
366
-			o.uint64s = make([]uint64, uint64PoolSize)
367
-		}
368
-		o.uint64s[0] = x
369
-		p.v.Set(reflect.ValueOf(&o.uint64s[0]))
370
-		o.uint64s = o.uint64s[1:]
371
-		return
372
-	case float64Type:
373
-		if len(o.float64s) == 0 {
374
-			o.float64s = make([]float64, uint64PoolSize)
375
-		}
376
-		o.float64s[0] = math.Float64frombits(x)
377
-		p.v.Set(reflect.ValueOf(&o.float64s[0]))
378
-		o.float64s = o.float64s[1:]
379
-		return
380
-	}
381
-	panic("unreachable")
246
+func (p pointer) toStringPtr() **string {
247
+	return p.v.Interface().(**string)
382 248
 }
383
-
384
-func word64_IsNil(p word64) bool {
385
-	return p.v.IsNil()
249
+func (p pointer) toStringSlice() *[]string {
250
+	return p.v.Interface().(*[]string)
386 251
 }
387
-
388
-func word64_Get(p word64) uint64 {
389
-	elem := p.v.Elem()
390
-	switch elem.Kind() {
391
-	case reflect.Int64:
392
-		return uint64(elem.Int())
393
-	case reflect.Uint64:
394
-		return elem.Uint()
395
-	case reflect.Float64:
396
-		return math.Float64bits(elem.Float())
397
-	}
398
-	panic("unreachable")
252
+func (p pointer) toBytes() *[]byte {
253
+	return p.v.Interface().(*[]byte)
399 254
 }
400
-
401
-func structPointer_Word64(p structPointer, f field) word64 {
402
-	return word64{structPointer_field(p, f)}
255
+func (p pointer) toBytesSlice() *[][]byte {
256
+	return p.v.Interface().(*[][]byte)
257
+}
258
+func (p pointer) toExtensions() *XXX_InternalExtensions {
259
+	return p.v.Interface().(*XXX_InternalExtensions)
260
+}
261
+func (p pointer) toOldExtensions() *map[int32]Extension {
262
+	return p.v.Interface().(*map[int32]Extension)
263
+}
264
+func (p pointer) getPointer() pointer {
265
+	return pointer{v: p.v.Elem()}
266
+}
267
+func (p pointer) setPointer(q pointer) {
268
+	p.v.Elem().Set(q.v)
269
+}
270
+func (p pointer) appendPointer(q pointer) {
271
+	grow(p.v.Elem()).Set(q.v)
403 272
 }
404 273
 
405
-// word64Val is like word32Val but for 64-bit values.
406
-type word64Val struct {
407
-	v reflect.Value
274
+// getPointerSlice copies []*T from p as a new []pointer.
275
+// This behavior differs from the implementation in pointer_unsafe.go.
276
+func (p pointer) getPointerSlice() []pointer {
277
+	if p.v.IsNil() {
278
+		return nil
279
+	}
280
+	n := p.v.Elem().Len()
281
+	s := make([]pointer, n)
282
+	for i := 0; i < n; i++ {
283
+		s[i] = pointer{v: p.v.Elem().Index(i)}
284
+	}
285
+	return s
408 286
 }
409 287
 
410
-func word64Val_Set(p word64Val, o *Buffer, x uint64) {
411
-	switch p.v.Type() {
412
-	case int64Type:
413
-		p.v.SetInt(int64(x))
414
-		return
415
-	case uint64Type:
416
-		p.v.SetUint(x)
417
-		return
418
-	case float64Type:
419
-		p.v.SetFloat(math.Float64frombits(x))
288
+// setPointerSlice copies []pointer into p as a new []*T.
289
+// This behavior differs from the implementation in pointer_unsafe.go.
290
+func (p pointer) setPointerSlice(v []pointer) {
291
+	if v == nil {
292
+		p.v.Elem().Set(reflect.New(p.v.Elem().Type()).Elem())
420 293
 		return
421 294
 	}
422
-	panic("unreachable")
295
+	s := reflect.MakeSlice(p.v.Elem().Type(), 0, len(v))
296
+	for _, p := range v {
297
+		s = reflect.Append(s, p.v)
298
+	}
299
+	p.v.Elem().Set(s)
423 300
 }
424 301
 
425
-func word64Val_Get(p word64Val) uint64 {
426
-	elem := p.v
427
-	switch elem.Kind() {
428
-	case reflect.Int64:
429
-		return uint64(elem.Int())
430
-	case reflect.Uint64:
431
-		return elem.Uint()
432
-	case reflect.Float64:
433
-		return math.Float64bits(elem.Float())
302
+// getInterfacePointer returns a pointer that points to the
303
+// interface data of the interface pointed by p.
304
+func (p pointer) getInterfacePointer() pointer {
305
+	if p.v.Elem().IsNil() {
306
+		return pointer{v: p.v.Elem()}
434 307
 	}
435
-	panic("unreachable")
308
+	return pointer{v: p.v.Elem().Elem().Elem().Field(0).Addr()} // *interface -> interface -> *struct -> struct
436 309
 }
437 310
 
438
-func structPointer_Word64Val(p structPointer, f field) word64Val {
439
-	return word64Val{structPointer_field(p, f)}
311
+func (p pointer) asPointerTo(t reflect.Type) reflect.Value {
312
+	// TODO: check that p.v.Type().Elem() == t?
313
+	return p.v
440 314
 }
441 315
 
442
-type word64Slice struct {
443
-	v reflect.Value
316
+func atomicLoadUnmarshalInfo(p **unmarshalInfo) *unmarshalInfo {
317
+	atomicLock.Lock()
318
+	defer atomicLock.Unlock()
319
+	return *p
444 320
 }
445
-
446
-func (p word64Slice) Append(x uint64) {
447
-	n, m := p.v.Len(), p.v.Cap()
448
-	if n < m {
449
-		p.v.SetLen(n + 1)
450
-	} else {
451
-		t := p.v.Type().Elem()
452
-		p.v.Set(reflect.Append(p.v, reflect.Zero(t)))
453
-	}
454
-	elem := p.v.Index(n)
455
-	switch elem.Kind() {
456
-	case reflect.Int64:
457
-		elem.SetInt(int64(int64(x)))
458
-	case reflect.Uint64:
459
-		elem.SetUint(uint64(x))
460
-	case reflect.Float64:
461
-		elem.SetFloat(float64(math.Float64frombits(x)))
462
-	}
321
+func atomicStoreUnmarshalInfo(p **unmarshalInfo, v *unmarshalInfo) {
322
+	atomicLock.Lock()
323
+	defer atomicLock.Unlock()
324
+	*p = v
463 325
 }
464
-
465
-func (p word64Slice) Len() int {
466
-	return p.v.Len()
326
+func atomicLoadMarshalInfo(p **marshalInfo) *marshalInfo {
327
+	atomicLock.Lock()
328
+	defer atomicLock.Unlock()
329
+	return *p
467 330
 }
468
-
469
-func (p word64Slice) Index(i int) uint64 {
470
-	elem := p.v.Index(i)
471
-	switch elem.Kind() {
472
-	case reflect.Int64:
473
-		return uint64(elem.Int())
474
-	case reflect.Uint64:
475
-		return uint64(elem.Uint())
476
-	case reflect.Float64:
477
-		return math.Float64bits(float64(elem.Float()))
478
-	}
479
-	panic("unreachable")
331
+func atomicStoreMarshalInfo(p **marshalInfo, v *marshalInfo) {
332
+	atomicLock.Lock()
333
+	defer atomicLock.Unlock()
334
+	*p = v
480 335
 }
481
-
482
-func structPointer_Word64Slice(p structPointer, f field) word64Slice {
483
-	return word64Slice{structPointer_field(p, f)}
336
+func atomicLoadMergeInfo(p **mergeInfo) *mergeInfo {
337
+	atomicLock.Lock()
338
+	defer atomicLock.Unlock()
339
+	return *p
340
+}
341
+func atomicStoreMergeInfo(p **mergeInfo, v *mergeInfo) {
342
+	atomicLock.Lock()
343
+	defer atomicLock.Unlock()
344
+	*p = v
484 345
 }
346
+func atomicLoadDiscardInfo(p **discardInfo) *discardInfo {
347
+	atomicLock.Lock()
348
+	defer atomicLock.Unlock()
349
+	return *p
350
+}
351
+func atomicStoreDiscardInfo(p **discardInfo, v *discardInfo) {
352
+	atomicLock.Lock()
353
+	defer atomicLock.Unlock()
354
+	*p = v
355
+}
356
+
357
+var atomicLock sync.Mutex

+ 202
- 164
vendor/github.com/golang/protobuf/proto/pointer_unsafe.go View File

@@ -29,7 +29,7 @@
29 29
 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 30
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 31
 
32
-// +build !appengine,!js
32
+// +build !purego,!appengine,!js
33 33
 
34 34
 // This file contains the implementation of the proto field accesses using package unsafe.
35 35
 
@@ -37,38 +37,13 @@ package proto
37 37
 
38 38
 import (
39 39
 	"reflect"
40
+	"sync/atomic"
40 41
 	"unsafe"
41 42
 )
42 43
 
43
-// NOTE: These type_Foo functions would more idiomatically be methods,
44
-// but Go does not allow methods on pointer types, and we must preserve
45
-// some pointer type for the garbage collector. We use these
46
-// funcs with clunky names as our poor approximation to methods.
47
-//
48
-// An alternative would be
49
-//	type structPointer struct { p unsafe.Pointer }
50
-// but that does not registerize as well.
51
-
52
-// A structPointer is a pointer to a struct.
53
-type structPointer unsafe.Pointer
54
-
55
-// toStructPointer returns a structPointer equivalent to the given reflect value.
56
-func toStructPointer(v reflect.Value) structPointer {
57
-	return structPointer(unsafe.Pointer(v.Pointer()))
58
-}
59
-
60
-// IsNil reports whether p is nil.
61
-func structPointer_IsNil(p structPointer) bool {
62
-	return p == nil
63
-}
64
-
65
-// Interface returns the struct pointer, assumed to have element type t,
66
-// as an interface value.
67
-func structPointer_Interface(p structPointer, t reflect.Type) interface{} {
68
-	return reflect.NewAt(t, unsafe.Pointer(p)).Interface()
69
-}
44
+const unsafeAllowed = true
70 45
 
71
-// A field identifies a field in a struct, accessible from a structPointer.
46
+// A field identifies a field in a struct, accessible from a pointer.
72 47
 // In this implementation, a field is identified by its byte offset from the start of the struct.
73 48
 type field uintptr
74 49
 
@@ -80,191 +55,254 @@ func toField(f *reflect.StructField) field {
80 55
 // invalidField is an invalid field identifier.
81 56
 const invalidField = ^field(0)
82 57
 
58
+// zeroField is a noop when calling pointer.offset.
59
+const zeroField = field(0)
60
+
83 61
 // IsValid reports whether the field identifier is valid.
84 62
 func (f field) IsValid() bool {
85
-	return f != ^field(0)
63
+	return f != invalidField
86 64
 }
87 65
 
88
-// Bytes returns the address of a []byte field in the struct.
89
-func structPointer_Bytes(p structPointer, f field) *[]byte {
90
-	return (*[]byte)(unsafe.Pointer(uintptr(p) + uintptr(f)))
66
+// The pointer type below is for the new table-driven encoder/decoder.
67
+// The implementation here uses unsafe.Pointer to create a generic pointer.
68
+// In pointer_reflect.go we use reflect instead of unsafe to implement
69
+// the same (but slower) interface.