aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/golang
diff options
context:
space:
mode:
authorAlex Pilon <apilon@hashicorp.com>2019-02-22 18:24:37 -0500
committerAlex Pilon <apilon@hashicorp.com>2019-02-22 18:24:37 -0500
commit15c0b25d011f37e7c20aeca9eaf461f78285b8d9 (patch)
tree255c250a5c9d4801c74092d33b7337d8c14438ff /vendor/github.com/golang
parent07971ca38143c5faf951d152fba370ddcbe26ad5 (diff)
downloadterraform-provider-statuscake-15c0b25d011f37e7c20aeca9eaf461f78285b8d9.tar.gz
terraform-provider-statuscake-15c0b25d011f37e7c20aeca9eaf461f78285b8d9.tar.zst
terraform-provider-statuscake-15c0b25d011f37e7c20aeca9eaf461f78285b8d9.zip
deps: github.com/hashicorp/terraform@sdk-v0.11-with-go-modules
Updated via: go get github.com/hashicorp/terraform@sdk-v0.11-with-go-modules and go mod tidy
Diffstat (limited to 'vendor/github.com/golang')
-rw-r--r--vendor/github.com/golang/protobuf/AUTHORS3
-rw-r--r--vendor/github.com/golang/protobuf/CONTRIBUTORS3
-rw-r--r--vendor/github.com/golang/protobuf/LICENSE28
-rw-r--r--vendor/github.com/golang/protobuf/proto/clone.go253
-rw-r--r--vendor/github.com/golang/protobuf/proto/decode.go428
-rw-r--r--vendor/github.com/golang/protobuf/proto/discard.go350
-rw-r--r--vendor/github.com/golang/protobuf/proto/encode.go203
-rw-r--r--vendor/github.com/golang/protobuf/proto/equal.go300
-rw-r--r--vendor/github.com/golang/protobuf/proto/extensions.go543
-rw-r--r--vendor/github.com/golang/protobuf/proto/lib.go979
-rw-r--r--vendor/github.com/golang/protobuf/proto/message_set.go314
-rw-r--r--vendor/github.com/golang/protobuf/proto/pointer_reflect.go357
-rw-r--r--vendor/github.com/golang/protobuf/proto/pointer_unsafe.go308
-rw-r--r--vendor/github.com/golang/protobuf/proto/properties.go544
-rw-r--r--vendor/github.com/golang/protobuf/proto/table_marshal.go2767
-rw-r--r--vendor/github.com/golang/protobuf/proto/table_merge.go654
-rw-r--r--vendor/github.com/golang/protobuf/proto/table_unmarshal.go2051
-rw-r--r--vendor/github.com/golang/protobuf/proto/text.go843
-rw-r--r--vendor/github.com/golang/protobuf/proto/text_parser.go880
-rw-r--r--vendor/github.com/golang/protobuf/ptypes/any.go141
-rw-r--r--vendor/github.com/golang/protobuf/ptypes/any/any.pb.go191
-rw-r--r--vendor/github.com/golang/protobuf/ptypes/any/any.proto149
-rw-r--r--vendor/github.com/golang/protobuf/ptypes/doc.go35
-rw-r--r--vendor/github.com/golang/protobuf/ptypes/duration.go102
-rw-r--r--vendor/github.com/golang/protobuf/ptypes/duration/duration.pb.go159
-rw-r--r--vendor/github.com/golang/protobuf/ptypes/duration/duration.proto117
-rw-r--r--vendor/github.com/golang/protobuf/ptypes/timestamp.go134
-rw-r--r--vendor/github.com/golang/protobuf/ptypes/timestamp/timestamp.pb.go175
-rw-r--r--vendor/github.com/golang/protobuf/ptypes/timestamp/timestamp.proto133
29 files changed, 13144 insertions, 0 deletions
diff --git a/vendor/github.com/golang/protobuf/AUTHORS b/vendor/github.com/golang/protobuf/AUTHORS
new file mode 100644
index 0000000..15167cd
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/AUTHORS
@@ -0,0 +1,3 @@
1# This source code refers to The Go Authors for copyright purposes.
2# The master list of authors is in the main Go distribution,
3# visible at http://tip.golang.org/AUTHORS.
diff --git a/vendor/github.com/golang/protobuf/CONTRIBUTORS b/vendor/github.com/golang/protobuf/CONTRIBUTORS
new file mode 100644
index 0000000..1c4577e
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/CONTRIBUTORS
@@ -0,0 +1,3 @@
1# This source code was written by the Go contributors.
2# The master list of contributors is in the main Go distribution,
3# visible at http://tip.golang.org/CONTRIBUTORS.
diff --git a/vendor/github.com/golang/protobuf/LICENSE b/vendor/github.com/golang/protobuf/LICENSE
new file mode 100644
index 0000000..0f64693
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/LICENSE
@@ -0,0 +1,28 @@
1Copyright 2010 The Go Authors. All rights reserved.
2
3Redistribution and use in source and binary forms, with or without
4modification, are permitted provided that the following conditions are
5met:
6
7 * Redistributions of source code must retain the above copyright
8notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10copyright notice, this list of conditions and the following disclaimer
11in the documentation and/or other materials provided with the
12distribution.
13 * Neither the name of Google Inc. nor the names of its
14contributors may be used to endorse or promote products derived from
15this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
diff --git a/vendor/github.com/golang/protobuf/proto/clone.go b/vendor/github.com/golang/protobuf/proto/clone.go
new file mode 100644
index 0000000..3cd3249
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/clone.go
@@ -0,0 +1,253 @@
1// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2011 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// Protocol buffer deep copy and merge.
33// TODO: RawMessage.
34
35package proto
36
37import (
38 "fmt"
39 "log"
40 "reflect"
41 "strings"
42)
43
44// Clone returns a deep copy of a protocol buffer.
45func Clone(src Message) Message {
46 in := reflect.ValueOf(src)
47 if in.IsNil() {
48 return src
49 }
50 out := reflect.New(in.Type().Elem())
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.
57type 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.
69type generatedMerger interface {
70 XXX_Merge(src Message)
71}
72
73// Merge merges src into dst.
74// Required and optional fields that are set in src will be set to that value in dst.
75// Elements of repeated fields will be appended.
76// Merge panics if src and dst are not the same type, or if dst is nil.
77func Merge(dst, src Message) {
78 if m, ok := dst.(Merger); ok {
79 m.Merge(src)
80 return
81 }
82
83 in := reflect.ValueOf(src)
84 out := reflect.ValueOf(dst)
85 if out.IsNil() {
86 panic("proto: nil destination")
87 }
88 if in.Type() != out.Type() {
89 panic(fmt.Sprintf("proto.Merge(%T, %T) type mismatch", dst, src))
90 }
91 if in.IsNil() {
92 return // Merge from nil src is a noop
93 }
94 if m, ok := dst.(generatedMerger); ok {
95 m.XXX_Merge(src)
96 return
97 }
98 mergeStruct(out.Elem(), in.Elem())
99}
100
101func mergeStruct(out, in reflect.Value) {
102 sprop := GetProperties(in.Type())
103 for i := 0; i < in.NumField(); i++ {
104 f := in.Type().Field(i)
105 if strings.HasPrefix(f.Name, "XXX_") {
106 continue
107 }
108 mergeAny(out.Field(i), in.Field(i), false, sprop.Prop[i])
109 }
110
111 if emIn, err := extendable(in.Addr().Interface()); err == nil {
112 emOut, _ := extendable(out.Addr().Interface())
113 mIn, muIn := emIn.extensionsRead()
114 if mIn != nil {
115 mOut := emOut.extensionsWrite()
116 muIn.Lock()
117 mergeExtension(mOut, mIn)
118 muIn.Unlock()
119 }
120 }
121
122 uf := in.FieldByName("XXX_unrecognized")
123 if !uf.IsValid() {
124 return
125 }
126 uin := uf.Bytes()
127 if len(uin) > 0 {
128 out.FieldByName("XXX_unrecognized").SetBytes(append([]byte(nil), uin...))
129 }
130}
131
132// mergeAny performs a merge between two values of the same type.
133// viaPtr indicates whether the values were indirected through a pointer (implying proto2).
134// prop is set if this is a struct field (it may be nil).
135func mergeAny(out, in reflect.Value, viaPtr bool, prop *Properties) {
136 if in.Type() == protoMessageType {
137 if !in.IsNil() {
138 if out.IsNil() {
139 out.Set(reflect.ValueOf(Clone(in.Interface().(Message))))
140 } else {
141 Merge(out.Interface().(Message), in.Interface().(Message))
142 }
143 }
144 return
145 }
146 switch in.Kind() {
147 case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int32, reflect.Int64,
148 reflect.String, reflect.Uint32, reflect.Uint64:
149 if !viaPtr && isProto3Zero(in) {
150 return
151 }
152 out.Set(in)
153 case reflect.Interface:
154 // Probably a oneof field; copy non-nil values.
155 if in.IsNil() {
156 return
157 }
158 // Allocate destination if it is not set, or set to a different type.
159 // Otherwise we will merge as normal.
160 if out.IsNil() || out.Elem().Type() != in.Elem().Type() {
161 out.Set(reflect.New(in.Elem().Elem().Type())) // interface -> *T -> T -> new(T)
162 }
163 mergeAny(out.Elem(), in.Elem(), false, nil)
164 case reflect.Map:
165 if in.Len() == 0 {
166 return
167 }
168 if out.IsNil() {
169 out.Set(reflect.MakeMap(in.Type()))
170 }
171 // For maps with value types of *T or []byte we need to deep copy each value.
172 elemKind := in.Type().Elem().Kind()
173 for _, key := range in.MapKeys() {
174 var val reflect.Value
175 switch elemKind {
176 case reflect.Ptr:
177 val = reflect.New(in.Type().Elem().Elem())
178 mergeAny(val, in.MapIndex(key), false, nil)
179 case reflect.Slice:
180 val = in.MapIndex(key)
181 val = reflect.ValueOf(append([]byte{}, val.Bytes()...))
182 default:
183 val = in.MapIndex(key)
184 }
185 out.SetMapIndex(key, val)
186 }
187 case reflect.Ptr:
188 if in.IsNil() {
189 return
190 }
191 if out.IsNil() {
192 out.Set(reflect.New(in.Elem().Type()))
193 }
194 mergeAny(out.Elem(), in.Elem(), true, nil)
195 case reflect.Slice:
196 if in.IsNil() {
197 return
198 }
199 if in.Type().Elem().Kind() == reflect.Uint8 {
200 // []byte is a scalar bytes field, not a repeated field.
201
202 // Edge case: if this is in a proto3 message, a zero length
203 // bytes field is considered the zero value, and should not
204 // be merged.
205 if prop != nil && prop.proto3 && in.Len() == 0 {
206 return
207 }
208
209 // Make a deep copy.
210 // Append to []byte{} instead of []byte(nil) so that we never end up
211 // with a nil result.
212 out.SetBytes(append([]byte{}, in.Bytes()...))
213 return
214 }
215 n := in.Len()
216 if out.IsNil() {
217 out.Set(reflect.MakeSlice(in.Type(), 0, n))
218 }
219 switch in.Type().Elem().Kind() {
220 case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int32, reflect.Int64,
221 reflect.String, reflect.Uint32, reflect.Uint64:
222 out.Set(reflect.AppendSlice(out, in))
223 default:
224 for i := 0; i < n; i++ {
225 x := reflect.Indirect(reflect.New(in.Type().Elem()))
226 mergeAny(x, in.Index(i), false, nil)
227 out.Set(reflect.Append(out, x))
228 }
229 }
230 case reflect.Struct:
231 mergeStruct(out, in)
232 default:
233 // unknown type, so not a protocol buffer
234 log.Printf("proto: don't know how to copy %v", in)
235 }
236}
237
238func mergeExtension(out, in map[int32]Extension) {
239 for extNum, eIn := range in {
240 eOut := Extension{desc: eIn.desc}
241 if eIn.value != nil {
242 v := reflect.New(reflect.TypeOf(eIn.value)).Elem()
243 mergeAny(v, reflect.ValueOf(eIn.value), false, nil)
244 eOut.value = v.Interface()
245 }
246 if eIn.enc != nil {
247 eOut.enc = make([]byte, len(eIn.enc))
248 copy(eOut.enc, eIn.enc)
249 }
250
251 out[extNum] = eOut
252 }
253}
diff --git a/vendor/github.com/golang/protobuf/proto/decode.go b/vendor/github.com/golang/protobuf/proto/decode.go
new file mode 100644
index 0000000..d9aa3c4
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/decode.go
@@ -0,0 +1,428 @@
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
32package proto
33
34/*
35 * Routines for decoding protocol buffer data to construct in-memory representations.
36 */
37
38import (
39 "errors"
40 "fmt"
41 "io"
42)
43
44// errOverflow is returned when an integer is too large to be represented.
45var errOverflow = errors.New("proto: integer overflow")
46
47// ErrInternalBadWireType is returned by generated code when an incorrect
48// wire type is encountered. It does not get returned to user code.
49var ErrInternalBadWireType = errors.New("proto: internal error: bad wiretype for oneof")
50
51// DecodeVarint reads a varint-encoded integer from the slice.
52// It returns the integer and the number of bytes consumed, or
53// zero if there is not enough.
54// This is the format for the
55// int32, int64, uint32, uint64, bool, and enum
56// protocol buffer types.
57func DecodeVarint(buf []byte) (x uint64, n int) {
58 for shift := uint(0); shift < 64; shift += 7 {
59 if n >= len(buf) {
60 return 0, 0
61 }
62 b := uint64(buf[n])
63 n++
64 x |= (b & 0x7F) << shift
65 if (b & 0x80) == 0 {
66 return x, n
67 }
68 }
69
70 // The number is too large to represent in a 64-bit value.
71 return 0, 0
72}
73
74func (p *Buffer) decodeVarintSlow() (x uint64, err error) {
75 i := p.index
76 l := len(p.buf)
77
78 for shift := uint(0); shift < 64; shift += 7 {
79 if i >= l {
80 err = io.ErrUnexpectedEOF
81 return
82 }
83 b := p.buf[i]
84 i++
85 x |= (uint64(b) & 0x7F) << shift
86 if b < 0x80 {
87 p.index = i
88 return
89 }
90 }
91
92 // The number is too large to represent in a 64-bit value.
93 err = errOverflow
94 return
95}
96
97// DecodeVarint reads a varint-encoded integer from the Buffer.
98// This is the format for the
99// int32, int64, uint32, uint64, bool, and enum
100// protocol buffer types.
101func (p *Buffer) DecodeVarint() (x uint64, err error) {
102 i := p.index
103 buf := p.buf
104
105 if i >= len(buf) {
106 return 0, io.ErrUnexpectedEOF
107 } else if buf[i] < 0x80 {
108 p.index++
109 return uint64(buf[i]), nil
110 } else if len(buf)-i < 10 {
111 return p.decodeVarintSlow()
112 }
113
114 var b uint64
115 // we already checked the first byte
116 x = uint64(buf[i]) - 0x80
117 i++
118
119 b = uint64(buf[i])
120 i++
121 x += b << 7
122 if b&0x80 == 0 {
123 goto done
124 }
125 x -= 0x80 << 7
126
127 b = uint64(buf[i])
128 i++
129 x += b << 14
130 if b&0x80 == 0 {
131 goto done
132 }
133 x -= 0x80 << 14
134
135 b = uint64(buf[i])
136 i++
137 x += b << 21
138 if b&0x80 == 0 {
139 goto done
140 }
141 x -= 0x80 << 21
142
143 b = uint64(buf[i])
144 i++
145 x += b << 28
146 if b&0x80 == 0 {
147 goto done
148 }
149 x -= 0x80 << 28
150
151 b = uint64(buf[i])
152 i++
153 x += b << 35
154 if b&0x80 == 0 {
155 goto done
156 }
157 x -= 0x80 << 35
158
159 b = uint64(buf[i])
160 i++
161 x += b << 42
162 if b&0x80 == 0 {
163 goto done
164 }
165 x -= 0x80 << 42
166
167 b = uint64(buf[i])
168 i++
169 x += b << 49
170 if b&0x80 == 0 {
171 goto done
172 }
173 x -= 0x80 << 49
174
175 b = uint64(buf[i])
176 i++
177 x += b << 56
178 if b&0x80 == 0 {
179 goto done
180 }
181 x -= 0x80 << 56
182
183 b = uint64(buf[i])
184 i++
185 x += b << 63
186 if b&0x80 == 0 {
187 goto done
188 }
189 // x -= 0x80 << 63 // Always zero.
190
191 return 0, errOverflow
192
193done:
194 p.index = i
195 return x, nil
196}
197
198// DecodeFixed64 reads a 64-bit integer from the Buffer.
199// This is the format for the
200// fixed64, sfixed64, and double protocol buffer types.
201func (p *Buffer) DecodeFixed64() (x uint64, err error) {
202 // x, err already 0
203 i := p.index + 8
204 if i < 0 || i > len(p.buf) {
205 err = io.ErrUnexpectedEOF
206 return
207 }
208 p.index = i
209
210 x = uint64(p.buf[i-8])
211 x |= uint64(p.buf[i-7]) << 8
212 x |= uint64(p.buf[i-6]) << 16
213 x |= uint64(p.buf[i-5]) << 24
214 x |= uint64(p.buf[i-4]) << 32
215 x |= uint64(p.buf[i-3]) << 40
216 x |= uint64(p.buf[i-2]) << 48
217 x |= uint64(p.buf[i-1]) << 56
218 return
219}
220
221// DecodeFixed32 reads a 32-bit integer from the Buffer.
222// This is the format for the
223// fixed32, sfixed32, and float protocol buffer types.
224func (p *Buffer) DecodeFixed32() (x uint64, err error) {
225 // x, err already 0
226 i := p.index + 4
227 if i < 0 || i > len(p.buf) {
228 err = io.ErrUnexpectedEOF
229 return
230 }
231 p.index = i
232
233 x = uint64(p.buf[i-4])
234 x |= uint64(p.buf[i-3]) << 8
235 x |= uint64(p.buf[i-2]) << 16
236 x |= uint64(p.buf[i-1]) << 24
237 return
238}
239
240// DecodeZigzag64 reads a zigzag-encoded 64-bit integer
241// from the Buffer.
242// This is the format used for the sint64 protocol buffer type.
243func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
244 x, err = p.DecodeVarint()
245 if err != nil {
246 return
247 }
248 x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
249 return
250}
251
252// DecodeZigzag32 reads a zigzag-encoded 32-bit integer
253// from the Buffer.
254// This is the format used for the sint32 protocol buffer type.
255func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
256 x, err = p.DecodeVarint()
257 if err != nil {
258 return
259 }
260 x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
261 return
262}
263
264// DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
265// This is the format used for the bytes protocol buffer
266// type and for embedded messages.
267func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) {
268 n, err := p.DecodeVarint()
269 if err != nil {
270 return nil, err
271 }
272
273 nb := int(n)
274 if nb < 0 {
275 return nil, fmt.Errorf("proto: bad byte length %d", nb)
276 }
277 end := p.index + nb
278 if end < p.index || end > len(p.buf) {
279 return nil, io.ErrUnexpectedEOF
280 }
281
282 if !alloc {
283 // todo: check if can get more uses of alloc=false
284 buf = p.buf[p.index:end]
285 p.index += nb
286 return
287 }
288
289 buf = make([]byte, nb)
290 copy(buf, p.buf[p.index:])
291 p.index += nb
292 return
293}
294
295// DecodeStringBytes reads an encoded string from the Buffer.
296// This is the format used for the proto2 string type.
297func (p *Buffer) DecodeStringBytes() (s string, err error) {
298 buf, err := p.DecodeRawBytes(false)
299 if err != nil {
300 return
301 }
302 return string(buf), nil
303}
304
305// Unmarshaler is the interface representing objects that can
306// unmarshal themselves. The argument points to data that may be
307// overwritten, so implementations should not keep references to the
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.
313type Unmarshaler interface {
314 Unmarshal([]byte) error
315}
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.
324type newUnmarshaler interface {
325 XXX_Unmarshal([]byte) error
326}
327
328// Unmarshal parses the protocol buffer representation in buf and places the
329// decoded result in pb. If the struct underlying pb does not match
330// the data in buf, the results can be unpredictable.
331//
332// Unmarshal resets pb before starting to unmarshal, so any
333// existing data in pb is always removed. Use UnmarshalMerge
334// to preserve and append to existing data.
335func Unmarshal(buf []byte, pb Message) error {
336 pb.Reset()
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)
344}
345
346// UnmarshalMerge parses the protocol buffer representation in buf and
347// writes the decoded result to pb. If the struct underlying pb does not match
348// the data in buf, the results can be unpredictable.
349//
350// UnmarshalMerge merges into existing data in pb.
351// Most code should use Unmarshal instead.
352func UnmarshalMerge(buf []byte, pb Message) error {
353 if u, ok := pb.(newUnmarshaler); ok {
354 return u.XXX_Unmarshal(buf)
355 }
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
363 return u.Unmarshal(buf)
364 }
365 return NewBuffer(buf).Unmarshal(pb)
366}
367
368// DecodeMessage reads a count-delimited message from the Buffer.
369func (p *Buffer) DecodeMessage(pb Message) error {
370 enc, err := p.DecodeRawBytes(false)
371 if err != nil {
372 return err
373 }
374 return NewBuffer(enc).Unmarshal(pb)
375}
376
377// DecodeGroup reads a tag-delimited group from the Buffer.
378// StartGroup tag is already consumed. This function consumes
379// EndGroup tag.
380func (p *Buffer) DecodeGroup(pb Message) error {
381 b := p.buf[p.index:]
382 x, y := findEndGroup(b)
383 if x < 0 {
384 return io.ErrUnexpectedEOF
385 }
386 err := Unmarshal(b[:x], pb)
387 p.index += y
388 return err
389}
390
391// Unmarshal parses the protocol buffer representation in the
392// Buffer and places the decoded result in pb. If the struct
393// underlying pb does not match the data in the buffer, the results can be
394// unpredictable.
395//
396// Unlike proto.Unmarshal, this does not reset pb before starting to unmarshal.
397func (p *Buffer) Unmarshal(pb Message) error {
398 // If the object can unmarshal itself, let it.
399 if u, ok := pb.(newUnmarshaler); ok {
400 err := u.XXX_Unmarshal(p.buf[p.index:])
401 p.index = len(p.buf)
402 return err
403 }
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)
413 return err
414 }
415
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)
427 return err
428}
diff --git a/vendor/github.com/golang/protobuf/proto/discard.go b/vendor/github.com/golang/protobuf/proto/discard.go
new file mode 100644
index 0000000..dea2617
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/discard.go
@@ -0,0 +1,350 @@
1// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2017 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
32package proto
33
34import (
35 "fmt"
36 "reflect"
37 "strings"
38 "sync"
39 "sync/atomic"
40)
41
42type generatedDiscarder interface {
43 XXX_DiscardUnknown()
44}
45
46// DiscardUnknown recursively discards all unknown fields from this message
47// and all embedded messages.
48//
49// When unmarshaling a message with unrecognized fields, the tags and values
50// of such fields are preserved in the Message. This allows a later call to
51// marshal to be able to produce a message that continues to have those
52// unrecognized fields. To avoid this, DiscardUnknown is used to
53// explicitly clear the unknown fields after unmarshaling.
54//
55// For proto2 messages, the unknown fields of message extensions are only
56// discarded from messages that have been accessed via GetExtension.
57func 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.
65 discardLegacy(m)
66}
67
68// DiscardUnknown recursively discards all unknown fields.
69func (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
78type 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
88type discardFieldInfo struct {
89 field field // Offset of field, guaranteed to be valid
90 discard func(src pointer)
91}
92
93var (
94 discardInfoMap = map[reflect.Type]*discardInfo{}
95 discardInfoLock sync.Mutex
96)
97
98func 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
109func (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
140func (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
254func discardLegacy(m Message) {
255 v := reflect.ValueOf(m)
256 if v.Kind() != reflect.Ptr || v.IsNil() {
257 return
258 }
259 v = v.Elem()
260 if v.Kind() != reflect.Struct {
261 return
262 }
263 t := v.Type()
264
265 for i := 0; i < v.NumField(); i++ {
266 f := t.Field(i)
267 if strings.HasPrefix(f.Name, "XXX_") {
268 continue
269 }
270 vf := v.Field(i)
271 tf := f.Type
272
273 // Unwrap tf to get its most basic type.
274 var isPointer, isSlice bool
275 if tf.Kind() == reflect.Slice && tf.Elem().Kind() != reflect.Uint8 {
276 isSlice = true
277 tf = tf.Elem()
278 }
279 if tf.Kind() == reflect.Ptr {
280 isPointer = true
281 tf = tf.Elem()
282 }
283 if isPointer && isSlice && tf.Kind() != reflect.Struct {
284 panic(fmt.Sprintf("%T.%s cannot be a slice of pointers to primitive types", m, f.Name))
285 }
286
287 switch tf.Kind() {
288 case reflect.Struct:
289 switch {
290 case !isPointer:
291 panic(fmt.Sprintf("%T.%s cannot be a direct struct value", m, f.Name))
292 case isSlice: // E.g., []*pb.T
293 for j := 0; j < vf.Len(); j++ {
294 discardLegacy(vf.Index(j).Interface().(Message))
295 }
296 default: // E.g., *pb.T
297 discardLegacy(vf.Interface().(Message))
298 }
299 case reflect.Map:
300 switch {
301 case isPointer || isSlice:
302 panic(fmt.Sprintf("%T.%s cannot be a pointer to a map or a slice of map values", m, f.Name))
303 default: // E.g., map[K]V
304 tv := vf.Type().Elem()
305 if tv.Kind() == reflect.Ptr && tv.Implements(protoMessageType) { // Proto struct (e.g., *T)
306 for _, key := range vf.MapKeys() {
307 val := vf.MapIndex(key)
308 discardLegacy(val.Interface().(Message))
309 }
310 }
311 }
312 case reflect.Interface:
313 // Must be oneof field.
314 switch {
315 case isPointer || isSlice:
316 panic(fmt.Sprintf("%T.%s cannot be a pointer to a interface or a slice of interface values", m, f.Name))
317 default: // E.g., test_proto.isCommunique_Union interface
318 if !vf.IsNil() && f.Tag.Get("protobuf_oneof") != "" {
319 vf = vf.Elem() // E.g., *test_proto.Communique_Msg
320 if !vf.IsNil() {
321 vf = vf.Elem() // E.g., test_proto.Communique_Msg
322 vf = vf.Field(0) // E.g., Proto struct (e.g., *T) or primitive value
323 if vf.Kind() == reflect.Ptr {
324 discardLegacy(vf.Interface().(Message))
325 }
326 }
327 }
328 }
329 }
330 }
331
332 if vf := v.FieldByName("XXX_unrecognized"); vf.IsValid() {
333 if vf.Type() != reflect.TypeOf([]byte{}) {
334 panic("expected XXX_unrecognized to be of type []byte")
335 }
336 vf.Set(reflect.ValueOf([]byte(nil)))
337 }
338
339 // For proto2 messages, only discard unknown fields in message extensions
340 // that have been accessed via GetExtension.
341 if em, err := extendable(m); err == nil {
342 // Ignore lock since discardLegacy is not concurrency safe.
343 emm, _ := em.extensionsRead()
344 for _, mx := range emm {
345 if m, ok := mx.value.(Message); ok {
346 discardLegacy(m)
347 }
348 }
349 }
350}
diff --git a/vendor/github.com/golang/protobuf/proto/encode.go b/vendor/github.com/golang/protobuf/proto/encode.go
new file mode 100644
index 0000000..3abfed2
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/encode.go
@@ -0,0 +1,203 @@
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
32package proto
33
34/*
35 * Routines for encoding data into the wire format for protocol buffers.
36 */
37
38import (
39 "errors"
40 "reflect"
41)
42
43var (
44 // errRepeatedHasNil is the error returned if Marshal is called with
45 // a struct with a repeated field containing a nil element.
46 errRepeatedHasNil = errors.New("proto: repeated field has nil element")
47
48 // errOneofHasNil is the error returned if Marshal is called with
49 // a struct with a oneof field containing a nil element.
50 errOneofHasNil = errors.New("proto: oneof field has nil value")
51
52 // ErrNil is the error returned if Marshal is called with nil.
53 ErrNil = errors.New("proto: Marshal called with nil")
54
55 // ErrTooLarge is the error returned if Marshal is called with a
56 // message that encodes to >2GB.
57 ErrTooLarge = errors.New("proto: message encodes to over 2 GB")
58)
59
60// The fundamental encoders that put bytes on the wire.
61// Those that take integer types all accept uint64 and are
62// therefore of type valueEncoder.
63
64const maxVarintBytes = 10 // maximum length of a varint
65
66// EncodeVarint returns the varint encoding of x.
67// This is the format for the
68// int32, int64, uint32, uint64, bool, and enum
69// protocol buffer types.
70// Not used by the package itself, but helpful to clients
71// wishing to use the same encoding.
72func EncodeVarint(x uint64) []byte {
73 var buf [maxVarintBytes]byte
74 var n int
75 for n = 0; x > 127; n++ {
76 buf[n] = 0x80 | uint8(x&0x7F)
77 x >>= 7
78 }
79 buf[n] = uint8(x)
80 n++
81 return buf[0:n]
82}
83
84// EncodeVarint writes a varint-encoded integer to the Buffer.
85// This is the format for the
86// int32, int64, uint32, uint64, bool, and enum
87// protocol buffer types.
88func (p *Buffer) EncodeVarint(x uint64) error {
89 for x >= 1<<7 {
90 p.buf = append(p.buf, uint8(x&0x7f|0x80))
91 x >>= 7
92 }
93 p.buf = append(p.buf, uint8(x))
94 return nil
95}
96
97// SizeVarint returns the varint encoding size of an integer.
98func SizeVarint(x uint64) int {
99 switch {
100 case x < 1<<7:
101 return 1
102 case x < 1<<14:
103 return 2
104 case x < 1<<21:
105 return 3
106 case x < 1<<28:
107 return 4
108 case x < 1<<35:
109 return 5
110 case x < 1<<42:
111 return 6
112 case x < 1<<49:
113 return 7
114 case x < 1<<56:
115 return 8
116 case x < 1<<63:
117 return 9
118 }
119 return 10
120}
121
122// EncodeFixed64 writes a 64-bit integer to the Buffer.
123// This is the format for the
124// fixed64, sfixed64, and double protocol buffer types.
125func (p *Buffer) EncodeFixed64(x uint64) error {
126 p.buf = append(p.buf,
127 uint8(x),
128 uint8(x>>8),
129 uint8(x>>16),
130 uint8(x>>24),
131 uint8(x>>32),
132 uint8(x>>40),
133 uint8(x>>48),
134 uint8(x>>56))
135 return nil
136}
137
138// EncodeFixed32 writes a 32-bit integer to the Buffer.
139// This is the format for the
140// fixed32, sfixed32, and float protocol buffer types.
141func (p *Buffer) EncodeFixed32(x uint64) error {
142 p.buf = append(p.buf,
143 uint8(x),
144 uint8(x>>8),
145 uint8(x>>16),
146 uint8(x>>24))
147 return nil
148}
149
150// EncodeZigzag64 writes a zigzag-encoded 64-bit integer
151// to the Buffer.
152// This is the format used for the sint64 protocol buffer type.
153func (p *Buffer) EncodeZigzag64(x uint64) error {
154 // use signed number to get arithmetic right shift.
155 return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
156}
157
158// EncodeZigzag32 writes a zigzag-encoded 32-bit integer
159// to the Buffer.
160// This is the format used for the sint32 protocol buffer type.
161func (p *Buffer) EncodeZigzag32(x uint64) error {
162 // use signed number to get arithmetic right shift.
163 return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
164}
165
166// EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
167// This is the format used for the bytes protocol buffer
168// type and for embedded messages.
169func (p *Buffer) EncodeRawBytes(b []byte) error {
170 p.EncodeVarint(uint64(len(b)))
171 p.buf = append(p.buf, b...)
172 return nil
173}
174
175// EncodeStringBytes writes an encoded string to the Buffer.
176// This is the format used for the proto2 string type.
177func (p *Buffer) EncodeStringBytes(s string) error {
178 p.EncodeVarint(uint64(len(s)))
179 p.buf = append(p.buf, s...)
180 return nil
181}
182
183// Marshaler is the interface representing objects that can marshal themselves.
184type Marshaler interface {
185 Marshal() ([]byte, error)
186}
187
188// EncodeMessage writes the protocol buffer to the Buffer,
189// prefixed by a varint-encoded length.
190func (p *Buffer) EncodeMessage(pb Message) error {
191 siz := Size(pb)
192 p.EncodeVarint(uint64(siz))
193 return p.Marshal(pb)
194}
195
196// All protocol buffer fields are nillable, but be careful.
197func isNil(v reflect.Value) bool {
198 switch v.Kind() {
199 case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
200 return v.IsNil()
201 }
202 return false
203}
diff --git a/vendor/github.com/golang/protobuf/proto/equal.go b/vendor/github.com/golang/protobuf/proto/equal.go
new file mode 100644
index 0000000..d4db5a1
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/equal.go
@@ -0,0 +1,300 @@
1// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2011 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// Protocol buffer comparison.
33
34package proto
35
36import (
37 "bytes"
38 "log"
39 "reflect"
40 "strings"
41)
42
43/*
44Equal returns true iff protocol buffers a and b are equal.
45The arguments must both be pointers to protocol buffer structs.
46
47Equality is defined in this way:
48 - Two messages are equal iff they are the same type,
49 corresponding fields are equal, unknown field sets
50 are equal, and extensions sets are equal.
51 - Two set scalar fields are equal iff their values are equal.
52 If the fields are of a floating-point type, remember that
53 NaN != x for all x, including NaN. If the message is defined
54 in a proto3 .proto file, fields are not "set"; specifically,
55 zero length proto3 "bytes" fields are equal (nil == {}).
56 - Two repeated fields are equal iff their lengths are the same,
57 and their corresponding elements are equal. Note a "bytes" field,
58 although represented by []byte, is not a repeated field and the
59 rule for the scalar fields described above applies.
60 - Two unset fields are equal.
61 - Two unknown field sets are equal if their current
62 encoded state is equal.
63 - Two extension sets are equal iff they have corresponding
64 elements that are pairwise equal.
65 - Two map fields are equal iff their lengths are the same,
66 and they contain the same set of elements. Zero-length map
67 fields are equal.
68 - Every other combination of things are not equal.
69
70The return value is undefined if a and b are not protocol buffers.
71*/
72func Equal(a, b Message) bool {
73 if a == nil || b == nil {
74 return a == b
75 }
76 v1, v2 := reflect.ValueOf(a), reflect.ValueOf(b)
77 if v1.Type() != v2.Type() {
78 return false
79 }
80 if v1.Kind() == reflect.Ptr {
81 if v1.IsNil() {
82 return v2.IsNil()
83 }
84 if v2.IsNil() {
85 return false
86 }
87 v1, v2 = v1.Elem(), v2.Elem()
88 }
89 if v1.Kind() != reflect.Struct {
90 return false
91 }
92 return equalStruct(v1, v2)
93}
94
95// v1 and v2 are known to have the same type.
96func equalStruct(v1, v2 reflect.Value) bool {
97 sprop := GetProperties(v1.Type())
98 for i := 0; i < v1.NumField(); i++ {
99 f := v1.Type().Field(i)
100 if strings.HasPrefix(f.Name, "XXX_") {
101 continue
102 }
103 f1, f2 := v1.Field(i), v2.Field(i)
104 if f.Type.Kind() == reflect.Ptr {
105 if n1, n2 := f1.IsNil(), f2.IsNil(); n1 && n2 {
106 // both unset
107 continue
108 } else if n1 != n2 {
109 // set/unset mismatch
110 return false
111 }
112 f1, f2 = f1.Elem(), f2.Elem()
113 }
114 if !equalAny(f1, f2, sprop.Prop[i]) {
115 return false
116 }
117 }
118
119 if em1 := v1.FieldByName("XXX_InternalExtensions"); em1.IsValid() {
120 em2 := v2.FieldByName("XXX_InternalExtensions")
121 if !equalExtensions(v1.Type(), em1.Interface().(XXX_InternalExtensions), em2.Interface().(XXX_InternalExtensions)) {
122 return false
123 }
124 }
125
126 if em1 := v1.FieldByName("XXX_extensions"); em1.IsValid() {
127 em2 := v2.FieldByName("XXX_extensions")
128 if !equalExtMap(v1.Type(), em1.Interface().(map[int32]Extension), em2.Interface().(map[int32]Extension)) {
129 return false
130 }
131 }
132
133 uf := v1.FieldByName("XXX_unrecognized")
134 if !uf.IsValid() {
135 return true
136 }
137
138 u1 := uf.Bytes()
139 u2 := v2.FieldByName("XXX_unrecognized").Bytes()
140 return bytes.Equal(u1, u2)
141}
142
143// v1 and v2 are known to have the same type.
144// prop may be nil.
145func equalAny(v1, v2 reflect.Value, prop *Properties) bool {
146 if v1.Type() == protoMessageType {
147 m1, _ := v1.Interface().(Message)
148 m2, _ := v2.Interface().(Message)
149 return Equal(m1, m2)
150 }
151 switch v1.Kind() {
152 case reflect.Bool:
153 return v1.Bool() == v2.Bool()
154 case reflect.Float32, reflect.Float64:
155 return v1.Float() == v2.Float()
156 case reflect.Int32, reflect.Int64:
157 return v1.Int() == v2.Int()
158 case reflect.Interface:
159 // Probably a oneof field; compare the inner values.
160 n1, n2 := v1.IsNil(), v2.IsNil()
161 if n1 || n2 {
162 return n1 == n2
163 }
164 e1, e2 := v1.Elem(), v2.Elem()
165 if e1.Type() != e2.Type() {
166 return false
167 }
168 return equalAny(e1, e2, nil)
169 case reflect.Map:
170 if v1.Len() != v2.Len() {
171 return false
172 }
173 for _, key := range v1.MapKeys() {
174 val2 := v2.MapIndex(key)
175 if !val2.IsValid() {
176 // This key was not found in the second map.
177 return false
178 }
179 if !equalAny(v1.MapIndex(key), val2, nil) {
180 return false
181 }
182 }
183 return true
184 case reflect.Ptr:
185 // Maps may have nil values in them, so check for nil.
186 if v1.IsNil() && v2.IsNil() {
187 return true
188 }
189 if v1.IsNil() != v2.IsNil() {
190 return false
191 }
192 return equalAny(v1.Elem(), v2.Elem(), prop)
193 case reflect.Slice:
194 if v1.Type().Elem().Kind() == reflect.Uint8 {
195 // short circuit: []byte
196
197 // Edge case: if this is in a proto3 message, a zero length
198 // bytes field is considered the zero value.
199 if prop != nil && prop.proto3 && v1.Len() == 0 && v2.Len() == 0 {
200 return true
201 }
202 if v1.IsNil() != v2.IsNil() {
203 return false
204 }
205 return bytes.Equal(v1.Interface().([]byte), v2.Interface().([]byte))
206 }
207
208 if v1.Len() != v2.Len() {
209 return false
210 }
211 for i := 0; i < v1.Len(); i++ {
212 if !equalAny(v1.Index(i), v2.Index(i), prop) {
213 return false
214 }
215 }
216 return true
217 case reflect.String:
218 return v1.Interface().(string) == v2.Interface().(string)
219 case reflect.Struct:
220 return equalStruct(v1, v2)
221 case reflect.Uint32, reflect.Uint64:
222 return v1.Uint() == v2.Uint()
223 }
224
225 // unknown type, so not a protocol buffer
226 log.Printf("proto: don't know how to compare %v", v1)
227 return false
228}
229
230// base is the struct type that the extensions are based on.
231// x1 and x2 are InternalExtensions.
232func equalExtensions(base reflect.Type, x1, x2 XXX_InternalExtensions) bool {
233 em1, _ := x1.extensionsRead()
234 em2, _ := x2.extensionsRead()
235 return equalExtMap(base, em1, em2)
236}
237
238func equalExtMap(base reflect.Type, em1, em2 map[int32]Extension) bool {
239 if len(em1) != len(em2) {
240 return false
241 }
242
243 for extNum, e1 := range em1 {
244 e2, ok := em2[extNum]
245 if !ok {
246 return false
247 }
248
249 m1, m2 := e1.value, e2.value
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
260 if m1 != nil && m2 != nil {
261 // Both are unencoded.
262 if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2), nil) {
263 return false
264 }
265 continue
266 }
267
268 // At least one is encoded. To do a semantically correct comparison
269 // we need to unmarshal them first.
270 var desc *ExtensionDesc
271 if m := extensionMaps[base]; m != nil {
272 desc = m[extNum]
273 }
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 log.Printf("proto: don't know how to compare extension %d of %v", extNum, base)
280 return false
281 }
282 var err error
283 if m1 == nil {
284 m1, err = decodeExtension(e1.enc, desc)
285 }
286 if m2 == nil && err == nil {
287 m2, err = decodeExtension(e2.enc, desc)
288 }
289 if err != nil {
290 // The encoded form is invalid.
291 log.Printf("proto: badly encoded extension %d of %v: %v", extNum, base, err)
292 return false
293 }
294 if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2), nil) {
295 return false
296 }
297 }
298
299 return true
300}
diff --git a/vendor/github.com/golang/protobuf/proto/extensions.go b/vendor/github.com/golang/protobuf/proto/extensions.go
new file mode 100644
index 0000000..816a3b9
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/extensions.go
@@ -0,0 +1,543 @@
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
32package proto
33
34/*
35 * Types and routines for supporting protocol buffer extensions.
36 */
37
38import (
39 "errors"
40 "fmt"
41 "io"
42 "reflect"
43 "strconv"
44 "sync"
45)
46
47// ErrMissingExtension is the error returned by GetExtension if the named extension is not in the message.
48var ErrMissingExtension = errors.New("proto: missing extension")
49
50// ExtensionRange represents a range of message extensions for a protocol buffer.
51// Used in code generated by the protocol compiler.
52type ExtensionRange struct {
53 Start, End int32 // both inclusive
54}
55
56// extendableProto is an interface implemented by any protocol buffer generated by the current
57// proto compiler that may be extended.
58type extendableProto interface {
59 Message
60 ExtensionRangeArray() []ExtensionRange
61 extensionsWrite() map[int32]Extension
62 extensionsRead() (map[int32]Extension, sync.Locker)
63}
64
65// extendableProtoV1 is an interface implemented by a protocol buffer generated by the previous
66// version of the proto compiler that may be extended.
67type extendableProtoV1 interface {
68 Message
69 ExtensionRangeArray() []ExtensionRange
70 ExtensionMap() map[int32]Extension
71}
72
73// extensionAdapter is a wrapper around extendableProtoV1 that implements extendableProto.
74type extensionAdapter struct {
75 extendableProtoV1
76}
77
78func (e extensionAdapter) extensionsWrite() map[int32]Extension {
79 return e.ExtensionMap()
80}
81
82func (e extensionAdapter) extensionsRead() (map[int32]Extension, sync.Locker) {
83 return e.ExtensionMap(), notLocker{}
84}
85
86// notLocker is a sync.Locker whose Lock and Unlock methods are nops.
87type notLocker struct{}
88
89func (n notLocker) Lock() {}
90func (n notLocker) Unlock() {}
91
92// extendable returns the extendableProto interface for the given generated proto message.
93// If the proto message has the old extension format, it returns a wrapper that implements
94// the extendableProto interface.
95func 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
107 }
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
113var errNotExtendable = errors.New("proto: not an extendable proto.Message")
114
115func isNilPtr(x interface{}) bool {
116 v := reflect.ValueOf(x)
117 return v.Kind() == reflect.Ptr && v.IsNil()
118}
119
120// XXX_InternalExtensions is an internal representation of proto extensions.
121//
122// Each generated message struct type embeds an anonymous XXX_InternalExtensions field,
123// thus gaining the unexported 'extensions' method, which can be called only from the proto package.
124//
125// The methods of XXX_InternalExtensions are not concurrency safe in general,
126// but calls to logically read-only methods such as has and get may be executed concurrently.
127type XXX_InternalExtensions struct {
128 // The struct must be indirect so that if a user inadvertently copies a
129 // generated message and its embedded XXX_InternalExtensions, they
130 // avoid the mayhem of a copied mutex.
131 //
132 // The mutex serializes all logically read-only operations to p.extensionMap.
133 // It is up to the client to ensure that write operations to p.extensionMap are
134 // mutually exclusive with other accesses.
135 p *struct {
136 mu sync.Mutex
137 extensionMap map[int32]Extension
138 }
139}
140
141// extensionsWrite returns the extension map, creating it on first use.
142func (e *XXX_InternalExtensions) extensionsWrite() map[int32]Extension {
143 if e.p == nil {
144 e.p = new(struct {
145 mu sync.Mutex
146 extensionMap map[int32]Extension
147 })
148 e.p.extensionMap = make(map[int32]Extension)
149 }
150 return e.p.extensionMap
151}
152
153// extensionsRead returns the extensions map for read-only use. It may be nil.
154// The caller must hold the returned mutex's lock when accessing Elements within the map.
155func (e *XXX_InternalExtensions) extensionsRead() (map[int32]Extension, sync.Locker) {
156 if e.p == nil {
157 return nil, nil
158 }
159 return e.p.extensionMap, &e.p.mu
160}
161
162// ExtensionDesc represents an extension specification.
163// Used in generated code from the protocol compiler.
164type ExtensionDesc struct {
165 ExtendedType Message // nil pointer to the type that is being extended
166 ExtensionType interface{} // nil pointer to the extension type
167 Field int32 // field number
168 Name string // fully-qualified name of extension, for text formatting
169 Tag string // protobuf tag style
170 Filename string // name of the file in which the extension is defined
171}
172
173func (ed *ExtensionDesc) repeated() bool {
174 t := reflect.TypeOf(ed.ExtensionType)
175 return t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
176}
177
178// Extension represents an extension in a message.
179type Extension struct {
180 // When an extension is stored in a message using SetExtension
181 // only desc and value are set. When the message is marshaled
182 // enc will be set to the encoded form of the message.
183 //
184 // When a message is unmarshaled and contains extensions, each
185 // extension will have only enc set. When such an extension is
186 // accessed using GetExtension (or GetExtensions) desc and value
187 // will be set.
188 desc *ExtensionDesc
189 value interface{}
190 enc []byte
191}
192
193// SetRawExtension is for testing only.
194func SetRawExtension(base Message, id int32, b []byte) {
195 epb, err := extendable(base)
196 if err != nil {
197 return
198 }
199 extmap := epb.extensionsWrite()
200 extmap[id] = Extension{enc: b}
201}
202
203// isExtensionField returns true iff the given field number is in an extension range.
204func isExtensionField(pb extendableProto, field int32) bool {
205 for _, er := range pb.ExtensionRangeArray() {
206 if er.Start <= field && field <= er.End {
207 return true
208 }
209 }
210 return false
211}
212
213// checkExtensionTypes checks that the given extension is valid for pb.
214func checkExtensionTypes(pb extendableProto, extension *ExtensionDesc) error {
215 var pbi interface{} = pb
216 // Check the extended type.
217 if ea, ok := pbi.(extensionAdapter); ok {
218 pbi = ea.extendableProtoV1
219 }
220 if a, b := reflect.TypeOf(pbi), reflect.TypeOf(extension.ExtendedType); a != b {
221 return fmt.Errorf("proto: bad extended type; %v does not extend %v", b, a)
222 }
223 // Check the range.
224 if !isExtensionField(pb, extension.Field) {
225 return errors.New("proto: bad extension number; not in declared ranges")
226 }
227 return nil
228}
229
230// extPropKey is sufficient to uniquely identify an extension.
231type extPropKey struct {
232 base reflect.Type
233 field int32
234}
235
236var extProp = struct {
237 sync.RWMutex
238 m map[extPropKey]*Properties
239}{
240 m: make(map[extPropKey]*Properties),
241}
242
243func extensionProperties(ed *ExtensionDesc) *Properties {
244 key := extPropKey{base: reflect.TypeOf(ed.ExtendedType), field: ed.Field}
245
246 extProp.RLock()
247 if prop, ok := extProp.m[key]; ok {
248 extProp.RUnlock()
249 return prop
250 }
251 extProp.RUnlock()
252
253 extProp.Lock()
254 defer extProp.Unlock()
255 // Check again.
256 if prop, ok := extProp.m[key]; ok {
257 return prop
258 }
259
260 prop := new(Properties)
261 prop.Init(reflect.TypeOf(ed.ExtensionType), "unknown_name", ed.Tag, nil)
262 extProp.m[key] = prop
263 return prop
264}
265
266// HasExtension returns whether the given extension is present in pb.
267func HasExtension(pb Message, extension *ExtensionDesc) bool {
268 // TODO: Check types, field numbers, etc.?
269 epb, err := extendable(pb)
270 if err != nil {
271 return false
272 }
273 extmap, mu := epb.extensionsRead()
274 if extmap == nil {
275 return false
276 }
277 mu.Lock()
278 _, ok := extmap[extension.Field]
279 mu.Unlock()
280 return ok
281}
282
283// ClearExtension removes the given extension from pb.
284func ClearExtension(pb Message, extension *ExtensionDesc) {
285 epb, err := extendable(pb)
286 if err != nil {
287 return
288 }
289 // TODO: Check types, field numbers, etc.?
290 extmap := epb.extensionsWrite()
291 delete(extmap, extension.Field)
292}
293
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.
303func GetExtension(pb Message, extension *ExtensionDesc) (interface{}, error) {
304 epb, err := extendable(pb)
305 if err != nil {
306 return nil, err
307 }
308
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 }
314 }
315
316 emap, mu := epb.extensionsRead()
317 if emap == nil {
318 return defaultExtensionValue(extension)
319 }
320 mu.Lock()
321 defer mu.Unlock()
322 e, ok := emap[extension.Field]
323 if !ok {
324 // defaultExtensionValue returns the default value or
325 // ErrMissingExtension if there is no default.
326 return defaultExtensionValue(extension)
327 }
328
329 if e.value != nil {
330 // Already decoded. Check the descriptor, though.
331 if e.desc != extension {
332 // This shouldn't happen. If it does, it means that
333 // GetExtension was called twice with two different
334 // descriptors with the same field number.
335 return nil, errors.New("proto: descriptor conflict")
336 }
337 return e.value, nil
338 }
339
340 if extension.ExtensionType == nil {
341 // incomplete descriptor
342 return e.enc, nil
343 }
344
345 v, err := decodeExtension(e.enc, extension)
346 if err != nil {
347 return nil, err
348 }
349
350 // Remember the decoded version and drop the encoded version.
351 // That way it is safe to mutate what we return.
352 e.value = v
353 e.desc = extension
354 e.enc = nil
355 emap[extension.Field] = e
356 return e.value, nil
357}
358
359// defaultExtensionValue returns the default value for extension.
360// If no default for an extension is defined ErrMissingExtension is returned.
361func defaultExtensionValue(extension *ExtensionDesc) (interface{}, error) {
362 if extension.ExtensionType == nil {
363 // incomplete descriptor, so no default
364 return nil, ErrMissingExtension
365 }
366
367 t := reflect.TypeOf(extension.ExtensionType)
368 props := extensionProperties(extension)
369
370 sf, _, err := fieldDefault(t, props)
371 if err != nil {
372 return nil, err
373 }
374
375 if sf == nil || sf.value == nil {
376 // There is no default value.
377 return nil, ErrMissingExtension
378 }
379
380 if t.Kind() != reflect.Ptr {
381 // We do not need to return a Ptr, we can directly return sf.value.
382 return sf.value, nil
383 }
384
385 // We need to return an interface{} that is a pointer to sf.value.
386 value := reflect.New(t).Elem()
387 value.Set(reflect.New(value.Type().Elem()))
388 if sf.kind == reflect.Int32 {
389 // We may have an int32 or an enum, but the underlying data is int32.
390 // Since we can't set an int32 into a non int32 reflect.value directly
391 // set it as a int32.
392 value.Elem().SetInt(int64(sf.value.(int32)))
393 } else {
394 value.Elem().Set(reflect.ValueOf(sf.value))
395 }
396 return value.Interface(), nil
397}
398
399// decodeExtension decodes an extension encoded in b.
400func decodeExtension(b []byte, extension *ExtensionDesc) (interface{}, error) {
401 t := reflect.TypeOf(extension.ExtensionType)
402 unmarshal := typeUnmarshaler(t, extension.Tag)
403
404 // t is a pointer to a struct, pointer to basic type or a slice.
405 // Allocate space to store the pointer/slice.
406 value := reflect.New(t).Elem()
407
408 var err error
409 for {
410 x, n := decodeVarint(b)
411 if n == 0 {
412 return nil, io.ErrUnexpectedEOF
413 }
414 b = b[n:]
415 wire := int(x) & 7
416
417 b, err = unmarshal(b, valToPointer(value.Addr()), wire)
418 if err != nil {
419 return nil, err
420 }
421
422 if len(b) == 0 {
423 break
424 }
425 }
426 return value.Interface(), nil
427}
428
429// GetExtensions returns a slice of the extensions present in pb that are also listed in es.
430// The returned slice has the same length as es; missing extensions will appear as nil elements.
431func GetExtensions(pb Message, es []*ExtensionDesc) (extensions []interface{}, err error) {
432 epb, err := extendable(pb)
433 if err != nil {
434 return nil, err
435 }
436 extensions = make([]interface{}, len(es))
437 for i, e := range es {
438 extensions[i], err = GetExtension(epb, e)
439 if err == ErrMissingExtension {
440 err = nil
441 }
442 if err != nil {
443 return
444 }
445 }
446 return
447}
448
449// ExtensionDescs returns a new slice containing pb's extension descriptors, in undefined order.
450// For non-registered extensions, ExtensionDescs returns an incomplete descriptor containing
451// just the Field field, which defines the extension's field number.
452func ExtensionDescs(pb Message) ([]*ExtensionDesc, error) {
453 epb, err := extendable(pb)
454 if err != nil {
455 return nil, err
456 }
457 registeredExtensions := RegisteredExtensions(pb)
458
459 emap, mu := epb.extensionsRead()
460 if emap == nil {
461 return nil, nil
462 }
463 mu.Lock()
464 defer mu.Unlock()
465 extensions := make([]*ExtensionDesc, 0, len(emap))
466 for extid, e := range emap {
467 desc := e.desc
468 if desc == nil {
469 desc = registeredExtensions[extid]
470 if desc == nil {
471 desc = &ExtensionDesc{Field: extid}
472 }
473 }
474
475 extensions = append(extensions, desc)
476 }
477 return extensions, nil
478}
479
480// SetExtension sets the specified extension of pb to the specified value.
481func SetExtension(pb Message, extension *ExtensionDesc, value interface{}) error {
482 epb, err := extendable(pb)
483 if err != nil {
484 return err
485 }
486 if err := checkExtensionTypes(epb, extension); err != nil {
487 return err
488 }
489 typ := reflect.TypeOf(extension.ExtensionType)
490 if typ != reflect.TypeOf(value) {
491 return errors.New("proto: bad extension value type")
492 }
493 // nil extension values need to be caught early, because the
494 // encoder can't distinguish an ErrNil due to a nil extension
495 // from an ErrNil due to a missing field. Extensions are
496 // always optional, so the encoder would just swallow the error
497 // and drop all the extensions from the encoded message.
498 if reflect.ValueOf(value).IsNil() {
499 return fmt.Errorf("proto: SetExtension called with nil value of type %T", value)
500 }
501
502 extmap := epb.extensionsWrite()
503 extmap[extension.Field] = Extension{desc: extension, value: value}
504 return nil
505}
506
507// ClearAllExtensions clears all extensions from pb.
508func ClearAllExtensions(pb Message) {
509 epb, err := extendable(pb)
510 if err != nil {
511 return
512 }
513 m := epb.extensionsWrite()
514 for k := range m {
515 delete(m, k)
516 }
517}
518
519// A global registry of extensions.
520// The generated code will register the generated descriptors by calling RegisterExtension.
521
522var extensionMaps = make(map[reflect.Type]map[int32]*ExtensionDesc)
523
524// RegisterExtension is called from the generated code.
525func RegisterExtension(desc *ExtensionDesc) {
526 st := reflect.TypeOf(desc.ExtendedType).Elem()
527 m := extensionMaps[st]
528 if m == nil {
529 m = make(map[int32]*ExtensionDesc)
530 extensionMaps[st] = m
531 }
532 if _, ok := m[desc.Field]; ok {
533 panic("proto: duplicate extension registered: " + st.String() + " " + strconv.Itoa(int(desc.Field)))
534 }
535 m[desc.Field] = desc
536}
537
538// RegisteredExtensions returns a map of the registered extensions of a
539// protocol buffer struct, indexed by the extension number.
540// The argument pb should be a nil pointer to the struct type.
541func RegisteredExtensions(pb Message) map[int32]*ExtensionDesc {
542 return extensionMaps[reflect.TypeOf(pb).Elem()]
543}
diff --git a/vendor/github.com/golang/protobuf/proto/lib.go b/vendor/github.com/golang/protobuf/proto/lib.go
new file mode 100644
index 0000000..75565cc
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/lib.go
@@ -0,0 +1,979 @@
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/*
33Package proto converts data structures to and from the wire format of
34protocol buffers. It works in concert with the Go source code generated
35for .proto files by the protocol compiler.
36
37A summary of the properties of the protocol buffer interface
38for a protocol buffer variable v:
39
40 - Names are turned from camel_case to CamelCase for export.
41 - There are no methods on v to set fields; just treat
42 them as structure fields.
43 - There are getters that return a field's value if set,
44 and return the field's default value if unset.
45 The getters work even if the receiver is a nil message.
46 - The zero value for a struct is its correct initialization state.
47 All desired fields must be set before marshaling.
48 - A Reset() method will restore a protobuf struct to its zero state.
49 - Non-repeated fields are pointers to the values; nil means unset.
50 That is, optional or required field int32 f becomes F *int32.
51 - Repeated fields are slices.
52 - Helper functions are available to aid the setting of fields.
53 msg.Foo = proto.String("hello") // set field
54 - Constants are defined to hold the default values of all fields that
55 have them. They have the form Default_StructName_FieldName.
56 Because the getter methods handle defaulted values,
57 direct use of these constants should be rare.
58 - Enums are given type names and maps from names to values.
59 Enum values are prefixed by the enclosing message's name, or by the
60 enum's type name if it is a top-level enum. Enum types have a String
61 method, and a Enum method to assist in message construction.
62 - Nested messages, groups and enums have type names prefixed with the name of
63 the surrounding message type.
64 - Extensions are given descriptor names that start with E_,
65 followed by an underscore-delimited list of the nested messages
66 that contain it (if any) followed by the CamelCased name of the
67 extension field itself. HasExtension, ClearExtension, GetExtension
68 and SetExtension are functions for manipulating extensions.
69 - Oneof field sets are given a single field in their message,
70 with distinguished wrapper types for each possible field value.
71 - Marshal and Unmarshal are functions to encode and decode the wire format.
72
73When the .proto file specifies `syntax="proto3"`, there are some differences:
74
75 - Non-repeated fields of non-message type are values instead of pointers.
76 - Enum types do not get an Enum method.
77
78The simplest way to describe this is to see an example.
79Given file test.proto, containing
80
81 package example;
82
83 enum FOO { X = 17; }
84
85 message Test {
86 required string label = 1;
87 optional int32 type = 2 [default=77];
88 repeated int64 reps = 3;
89 optional group OptionalGroup = 4 {
90 required string RequiredField = 5;
91 }
92 oneof union {
93 int32 number = 6;
94 string name = 7;
95 }
96 }
97
98The resulting file, test.pb.go, is:
99
100 package example
101
102 import proto "github.com/golang/protobuf/proto"
103 import math "math"
104
105 type FOO int32
106 const (
107 FOO_X FOO = 17
108 )
109 var FOO_name = map[int32]string{
110 17: "X",
111 }
112 var FOO_value = map[string]int32{
113 "X": 17,
114 }
115
116 func (x FOO) Enum() *FOO {
117 p := new(FOO)
118 *p = x
119 return p
120 }
121 func (x FOO) String() string {
122 return proto.EnumName(FOO_name, int32(x))
123 }
124 func (x *FOO) UnmarshalJSON(data []byte) error {
125 value, err := proto.UnmarshalJSONEnum(FOO_value, data)
126 if err != nil {
127 return err
128 }
129 *x = FOO(value)
130 return nil
131 }
132
133 type Test struct {
134 Label *string `protobuf:"bytes,1,req,name=label" json:"label,omitempty"`
135 Type *int32 `protobuf:"varint,2,opt,name=type,def=77" json:"type,omitempty"`
136 Reps []int64 `protobuf:"varint,3,rep,name=reps" json:"reps,omitempty"`
137 Optionalgroup *Test_OptionalGroup `protobuf:"group,4,opt,name=OptionalGroup" json:"optionalgroup,omitempty"`
138 // Types that are valid to be assigned to Union:
139 // *Test_Number
140 // *Test_Name
141 Union isTest_Union `protobuf_oneof:"union"`
142 XXX_unrecognized []byte `json:"-"`
143 }
144 func (m *Test) Reset() { *m = Test{} }
145 func (m *Test) String() string { return proto.CompactTextString(m) }
146 func (*Test) ProtoMessage() {}
147
148 type isTest_Union interface {
149 isTest_Union()
150 }
151
152 type Test_Number struct {
153 Number int32 `protobuf:"varint,6,opt,name=number"`
154 }
155 type Test_Name struct {
156 Name string `protobuf:"bytes,7,opt,name=name"`
157 }
158
159 func (*Test_Number) isTest_Union() {}
160 func (*Test_Name) isTest_Union() {}
161
162 func (m *Test) GetUnion() isTest_Union {
163 if m != nil {
164 return m.Union
165 }
166 return nil
167 }
168 const Default_Test_Type int32 = 77
169
170 func (m *Test) GetLabel() string {
171 if m != nil && m.Label != nil {
172 return *m.Label
173 }
174 return ""
175 }
176
177 func (m *Test) GetType() int32 {
178 if m != nil && m.Type != nil {
179 return *m.Type
180 }
181 return Default_Test_Type
182 }
183
184 func (m *Test) GetOptionalgroup() *Test_OptionalGroup {
185 if m != nil {
186 return m.Optionalgroup
187 }
188 return nil
189 }
190
191 type Test_OptionalGroup struct {
192 RequiredField *string `protobuf:"bytes,5,req" json:"RequiredField,omitempty"`
193 }
194 func (m *Test_OptionalGroup) Reset() { *m = Test_OptionalGroup{} }
195 func (m *Test_OptionalGroup) String() string { return proto.CompactTextString(m) }
196
197 func (m *Test_OptionalGroup) GetRequiredField() string {
198 if m != nil && m.RequiredField != nil {
199 return *m.RequiredField
200 }
201 return ""
202 }
203
204 func (m *Test) GetNumber() int32 {
205 if x, ok := m.GetUnion().(*Test_Number); ok {
206 return x.Number
207 }
208 return 0
209 }
210
211 func (m *Test) GetName() string {
212 if x, ok := m.GetUnion().(*Test_Name); ok {
213 return x.Name
214 }
215 return ""
216 }
217
218 func init() {
219 proto.RegisterEnum("example.FOO", FOO_name, FOO_value)
220 }
221
222To create and play with a Test object:
223
224 package main
225
226 import (
227 "log"
228
229 "github.com/golang/protobuf/proto"
230 pb "./example.pb"
231 )
232
233 func main() {
234 test := &pb.Test{
235 Label: proto.String("hello"),
236 Type: proto.Int32(17),
237 Reps: []int64{1, 2, 3},
238 Optionalgroup: &pb.Test_OptionalGroup{
239 RequiredField: proto.String("good bye"),
240 },
241 Union: &pb.Test_Name{"fred"},
242 }
243 data, err := proto.Marshal(test)
244 if err != nil {
245 log.Fatal("marshaling error: ", err)
246 }
247 newTest := &pb.Test{}
248 err = proto.Unmarshal(data, newTest)
249 if err != nil {
250 log.Fatal("unmarshaling error: ", err)
251 }
252 // Now test and newTest contain the same data.
253 if test.GetLabel() != newTest.GetLabel() {
254 log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
255 }
256 // Use a type switch to determine which oneof was set.
257 switch u := test.Union.(type) {
258 case *pb.Test_Number: // u.Number contains the number.
259 case *pb.Test_Name: // u.Name contains the string.
260 }
261 // etc.
262 }
263*/
264package proto
265
266import (
267 "encoding/json"
268 "fmt"
269 "log"
270 "reflect"
271 "sort"
272 "strconv"
273 "sync"
274)
275
276// RequiredNotSetError is an error type returned by either Marshal or Unmarshal.
277// Marshal reports this when a required field is not initialized.
278// Unmarshal reports this when a required field is missing from the wire data.
279type RequiredNotSetError struct{ field string }
280
281func (e *RequiredNotSetError) Error() string {
282 if e.field == "" {
283 return fmt.Sprintf("proto: required field not set")
284 }
285 return fmt.Sprintf("proto: required field %q not set", e.field)
286}
287func (e *RequiredNotSetError) RequiredNotSet() bool {
288 return true
289}
290
291type invalidUTF8Error struct{ field string }
292
293func (e *invalidUTF8Error) Error() string {
294 if e.field == "" {
295 return "proto: invalid UTF-8 detected"
296 }
297 return fmt.Sprintf("proto: field %q contains invalid UTF-8", e.field)
298}
299func (e *invalidUTF8Error) InvalidUTF8() bool {
300 return true
301}
302
303// errInvalidUTF8 is a sentinel error to identify fields with invalid UTF-8.
304// This error should not be exposed to the external API as such errors should
305// be recreated with the field information.
306var errInvalidUTF8 = &invalidUTF8Error{}
307
308// isNonFatal reports whether the error is either a RequiredNotSet error
309// or a InvalidUTF8 error.
310func isNonFatal(err error) bool {
311 if re, ok := err.(interface{ RequiredNotSet() bool }); ok && re.RequiredNotSet() {
312 return true
313 }
314 if re, ok := err.(interface{ InvalidUTF8() bool }); ok && re.InvalidUTF8() {
315 return true
316 }
317 return false
318}
319
320type nonFatal struct{ E error }
321
322// Merge merges err into nf and reports whether it was successful.
323// Otherwise it returns false for any fatal non-nil errors.
324func (nf *nonFatal) Merge(err error) (ok bool) {
325 if err == nil {
326 return true // not an error
327 }
328 if !isNonFatal(err) {
329 return false // fatal error
330 }
331 if nf.E == nil {
332 nf.E = err // store first instance of non-fatal error
333 }
334 return true
335}
336
337// Message is implemented by generated protocol buffer messages.
338type Message interface {
339 Reset()
340 String() string
341 ProtoMessage()
342}
343
344// Stats records allocation details about the protocol buffer encoders
345// and decoders. Useful for tuning the library itself.
346type Stats struct {
347 Emalloc uint64 // mallocs in encode
348 Dmalloc uint64 // mallocs in decode
349 Encode uint64 // number of encodes
350 Decode uint64 // number of decodes
351 Chit uint64 // number of cache hits
352 Cmiss uint64 // number of cache misses
353 Size uint64 // number of sizes
354}
355
356// Set to true to enable stats collection.
357const collectStats = false
358
359var stats Stats
360
361// GetStats returns a copy of the global Stats structure.
362func GetStats() Stats { return stats }
363
364// A Buffer is a buffer manager for marshaling and unmarshaling
365// protocol buffers. It may be reused between invocations to
366// reduce memory usage. It is not necessary to use a Buffer;
367// the global functions Marshal and Unmarshal create a
368// temporary Buffer and are fine for most applications.
369type Buffer struct {
370 buf []byte // encode/decode byte stream
371 index int // read point
372
373 deterministic bool
374}
375
376// NewBuffer allocates a new Buffer and initializes its internal data to
377// the contents of the argument slice.
378func NewBuffer(e []byte) *Buffer {
379 return &Buffer{buf: e}
380}
381
382// Reset resets the Buffer, ready for marshaling a new protocol buffer.
383func (p *Buffer) Reset() {
384 p.buf = p.buf[0:0] // for reading/writing
385 p.index = 0 // for reading
386}
387
388// SetBuf replaces the internal buffer with the slice,
389// ready for unmarshaling the contents of the slice.
390func (p *Buffer) SetBuf(s []byte) {
391 p.buf = s
392 p.index = 0
393}
394
395// Bytes returns the contents of the Buffer.
396func (p *Buffer) Bytes() []byte { return p.buf }
397
398// SetDeterministic sets whether to use deterministic serialization.
399//
400// Deterministic serialization guarantees that for a given binary, equal
401// messages will always be serialized to the same bytes. This implies:
402//
403// - Repeated serialization of a message will return the same bytes.
404// - Different processes of the same binary (which may be executing on
405// different machines) will serialize equal messages to the same bytes.
406//
407// Note that the deterministic serialization is NOT canonical across
408// languages. It is not guaranteed to remain stable over time. It is unstable
409// across different builds with schema changes due to unknown fields.
410// Users who need canonical serialization (e.g., persistent storage in a
411// canonical form, fingerprinting, etc.) should define their own
412// canonicalization specification and implement their own serializer rather
413// than relying on this API.
414//
415// If deterministic serialization is requested, map entries will be sorted
416// by keys in lexographical order. This is an implementation detail and
417// subject to change.
418func (p *Buffer) SetDeterministic(deterministic bool) {
419 p.deterministic = deterministic
420}
421
422/*
423 * Helper routines for simplifying the creation of optional fields of basic type.
424 */
425
426// Bool is a helper routine that allocates a new bool value
427// to store v and returns a pointer to it.
428func Bool(v bool) *bool {
429 return &v
430}
431
432// Int32 is a helper routine that allocates a new int32 value
433// to store v and returns a pointer to it.
434func Int32(v int32) *int32 {
435 return &v
436}
437
438// Int is a helper routine that allocates a new int32 value
439// to store v and returns a pointer to it, but unlike Int32
440// its argument value is an int.
441func Int(v int) *int32 {
442 p := new(int32)
443 *p = int32(v)
444 return p
445}
446
447// Int64 is a helper routine that allocates a new int64 value
448// to store v and returns a pointer to it.
449func Int64(v int64) *int64 {
450 return &v
451}
452
453// Float32 is a helper routine that allocates a new float32 value
454// to store v and returns a pointer to it.
455func Float32(v float32) *float32 {
456 return &v
457}
458
459// Float64 is a helper routine that allocates a new float64 value
460// to store v and returns a pointer to it.
461func Float64(v float64) *float64 {
462 return &v
463}
464
465// Uint32 is a helper routine that allocates a new uint32 value
466// to store v and returns a pointer to it.
467func Uint32(v uint32) *uint32 {
468 return &v
469}
470
471// Uint64 is a helper routine that allocates a new uint64 value
472// to store v and returns a pointer to it.
473func Uint64(v uint64) *uint64 {
474 return &v
475}
476
477// String is a helper routine that allocates a new string value
478// to store v and returns a pointer to it.
479func String(v string) *string {
480 return &v
481}
482
483// EnumName is a helper function to simplify printing protocol buffer enums
484// by name. Given an enum map and a value, it returns a useful string.
485func EnumName(m map[int32]string, v int32) string {
486 s, ok := m[v]
487 if ok {
488 return s
489 }
490 return strconv.Itoa(int(v))
491}
492
493// UnmarshalJSONEnum is a helper function to simplify recovering enum int values
494// from their JSON-encoded representation. Given a map from the enum's symbolic
495// names to its int values, and a byte buffer containing the JSON-encoded
496// value, it returns an int32 that can be cast to the enum type by the caller.
497//
498// The function can deal with both JSON representations, numeric and symbolic.
499func UnmarshalJSONEnum(m map[string]int32, data []byte, enumName string) (int32, error) {
500 if data[0] == '"' {
501 // New style: enums are strings.
502 var repr string
503 if err := json.Unmarshal(data, &repr); err != nil {
504 return -1, err
505 }
506 val, ok := m[repr]
507 if !ok {
508 return 0, fmt.Errorf("unrecognized enum %s value %q", enumName, repr)
509 }
510 return val, nil
511 }
512 // Old style: enums are ints.
513 var val int32
514 if err := json.Unmarshal(data, &val); err != nil {
515 return 0, fmt.Errorf("cannot unmarshal %#q into enum %s", data, enumName)
516 }
517 return val, nil
518}
519
520// DebugPrint dumps the encoded data in b in a debugging format with a header
521// including the string s. Used in testing but made available for general debugging.
522func (p *Buffer) DebugPrint(s string, b []byte) {
523 var u uint64
524
525 obuf := p.buf
526 index := p.index
527 p.buf = b
528 p.index = 0
529 depth := 0
530
531 fmt.Printf("\n--- %s ---\n", s)
532
533out:
534 for {
535 for i := 0; i < depth; i++ {
536 fmt.Print(" ")
537 }
538
539 index := p.index
540 if index == len(p.buf) {
541 break
542 }
543
544 op, err := p.DecodeVarint()
545 if err != nil {
546 fmt.Printf("%3d: fetching op err %v\n", index, err)
547 break out
548 }
549 tag := op >> 3
550 wire := op & 7
551
552 switch wire {
553 default:
554 fmt.Printf("%3d: t=%3d unknown wire=%d\n",
555 index, tag, wire)
556 break out
557
558 case WireBytes:
559 var r []byte
560
561 r, err = p.DecodeRawBytes(false)
562 if err != nil {
563 break out
564 }
565 fmt.Printf("%3d: t=%3d bytes [%d]", index, tag, len(r))
566 if len(r) <= 6 {
567 for i := 0; i < len(r); i++ {
568 fmt.Printf(" %.2x", r[i])
569 }
570 } else {
571 for i := 0; i < 3; i++ {
572 fmt.Printf(" %.2x", r[i])
573 }
574 fmt.Printf(" ..")
575 for i := len(r) - 3; i < len(r); i++ {
576 fmt.Printf(" %.2x", r[i])
577 }
578 }
579 fmt.Printf("\n")
580
581 case WireFixed32:
582 u, err = p.DecodeFixed32()
583 if err != nil {
584 fmt.Printf("%3d: t=%3d fix32 err %v\n", index, tag, err)
585 break out
586 }
587 fmt.Printf("%3d: t=%3d fix32 %d\n", index, tag, u)
588
589 case WireFixed64:
590 u, err = p.DecodeFixed64()
591 if err != nil {
592 fmt.Printf("%3d: t=%3d fix64 err %v\n", index, tag, err)
593 break out
594 }
595 fmt.Printf("%3d: t=%3d fix64 %d\n", index, tag, u)
596
597 case WireVarint:
598 u, err = p.DecodeVarint()
599 if err != nil {
600 fmt.Printf("%3d: t=%3d varint err %v\n", index, tag, err)
601 break out
602 }
603 fmt.Printf("%3d: t=%3d varint %d\n", index, tag, u)
604
605 case WireStartGroup:
606 fmt.Printf("%3d: t=%3d start\n", index, tag)
607 depth++
608
609 case WireEndGroup:
610 depth--
611 fmt.Printf("%3d: t=%3d end\n", index, tag)
612 }
613 }
614
615 if depth != 0 {
616 fmt.Printf("%3d: start-end not balanced %d\n", p.index, depth)
617 }
618 fmt.Printf("\n")
619
620 p.buf = obuf
621 p.index = index
622}
623
624// SetDefaults sets unset protocol buffer fields to their default values.
625// It only modifies fields that are both unset and have defined defaults.
626// It recursively sets default values in any non-nil sub-messages.
627func SetDefaults(pb Message) {
628 setDefaults(reflect.ValueOf(pb), true, false)
629}
630
631// v is a pointer to a struct.
632func setDefaults(v reflect.Value, recur, zeros bool) {
633 v = v.Elem()
634
635 defaultMu.RLock()
636 dm, ok := defaults[v.Type()]
637 defaultMu.RUnlock()
638 if !ok {
639 dm = buildDefaultMessage(v.Type())
640 defaultMu.Lock()
641 defaults[v.Type()] = dm
642 defaultMu.Unlock()
643 }
644
645 for _, sf := range dm.scalars {
646 f := v.Field(sf.index)
647 if !f.IsNil() {
648 // field already set
649 continue
650 }
651 dv := sf.value
652 if dv == nil && !zeros {
653 // no explicit default, and don't want to set zeros
654 continue
655 }
656 fptr := f.Addr().Interface() // **T
657 // TODO: Consider batching the allocations we do here.
658 switch sf.kind {
659 case reflect.Bool:
660 b := new(bool)
661 if dv != nil {
662 *b = dv.(bool)
663 }
664 *(fptr.(**bool)) = b
665 case reflect.Float32:
666 f := new(float32)
667 if dv != nil {
668 *f = dv.(float32)
669 }
670 *(fptr.(**float32)) = f
671 case reflect.Float64:
672 f := new(float64)
673 if dv != nil {
674 *f = dv.(float64)
675 }
676 *(fptr.(**float64)) = f
677 case reflect.Int32:
678 // might be an enum
679 if ft := f.Type(); ft != int32PtrType {
680 // enum
681 f.Set(reflect.New(ft.Elem()))
682 if dv != nil {
683 f.Elem().SetInt(int64(dv.(int32)))
684 }
685 } else {
686 // int32 field
687 i := new(int32)
688 if dv != nil {
689 *i = dv.(int32)
690 }
691 *(fptr.(**int32)) = i
692 }
693 case reflect.Int64:
694 i := new(int64)
695 if dv != nil {
696 *i = dv.(int64)
697 }
698 *(fptr.(**int64)) = i
699 case reflect.String:
700 s := new(string)
701 if dv != nil {
702 *s = dv.(string)
703 }
704 *(fptr.(**string)) = s
705 case reflect.Uint8:
706 // exceptional case: []byte
707 var b []byte
708 if dv != nil {
709 db := dv.([]byte)
710 b = make([]byte, len(db))
711 copy(b, db)
712 } else {
713 b = []byte{}
714 }
715 *(fptr.(*[]byte)) = b
716 case reflect.Uint32:
717 u := new(uint32)
718 if dv != nil {
719 *u = dv.(uint32)
720 }
721 *(fptr.(**uint32)) = u
722 case reflect.Uint64:
723 u := new(uint64)
724 if dv != nil {
725 *u = dv.(uint64)
726 }
727 *(fptr.(**uint64)) = u
728 default:
729 log.Printf("proto: can't set default for field %v (sf.kind=%v)", f, sf.kind)
730 }
731 }
732
733 for _, ni := range dm.nested {
734 f := v.Field(ni)
735 // f is *T or []*T or map[T]*T
736 switch f.Kind() {
737 case reflect.Ptr:
738 if f.IsNil() {
739 continue
740 }
741 setDefaults(f, recur, zeros)
742
743 case reflect.Slice:
744 for i := 0; i < f.Len(); i++ {
745 e := f.Index(i)
746 if e.IsNil() {
747 continue
748 }
749 setDefaults(e, recur, zeros)
750 }
751
752 case reflect.Map:
753 for _, k := range f.MapKeys() {
754 e := f.MapIndex(k)
755 if e.IsNil() {
756 continue
757 }
758 setDefaults(e, recur, zeros)
759 }
760 }
761 }
762}
763
764var (
765 // defaults maps a protocol buffer struct type to a slice of the fields,
766 // with its scalar fields set to their proto-declared non-zero default values.
767 defaultMu sync.RWMutex
768 defaults = make(map[reflect.Type]defaultMessage)
769
770 int32PtrType = reflect.TypeOf((*int32)(nil))
771)
772
773// defaultMessage represents information about the default values of a message.
774type defaultMessage struct {
775 scalars []scalarField
776 nested []int // struct field index of nested messages
777}
778
779type scalarField struct {
780 index int // struct field index
781 kind reflect.Kind // element type (the T in *T or []T)
782 value interface{} // the proto-declared default value, or nil
783}
784
785// t is a struct type.
786func buildDefaultMessage(t reflect.Type) (dm defaultMessage) {
787 sprop := GetProperties(t)
788 for _, prop := range sprop.Prop {
789 fi, ok := sprop.decoderTags.get(prop.Tag)
790 if !ok {
791 // XXX_unrecognized
792 continue
793 }
794 ft := t.Field(fi).Type
795
796 sf, nested, err := fieldDefault(ft, prop)
797 switch {
798 case err != nil:
799 log.Print(err)
800 case nested:
801 dm.nested = append(dm.nested, fi)
802 case sf != nil:
803 sf.index = fi
804 dm.scalars = append(dm.scalars, *sf)
805 }
806 }
807
808 return dm
809}
810
811// fieldDefault returns the scalarField for field type ft.
812// sf will be nil if the field can not have a default.
813// nestedMessage will be true if this is a nested message.
814// Note that sf.index is not set on return.
815func fieldDefault(ft reflect.Type, prop *Properties) (sf *scalarField, nestedMessage bool, err error) {
816 var canHaveDefault bool
817 switch ft.Kind() {
818 case reflect.Ptr:
819 if ft.Elem().Kind() == reflect.Struct {
820 nestedMessage = true
821 } else {
822 canHaveDefault = true // proto2 scalar field
823 }
824
825 case reflect.Slice:
826 switch ft.Elem().Kind() {
827 case reflect.Ptr:
828 nestedMessage = true // repeated message
829 case reflect.Uint8:
830 canHaveDefault = true // bytes field
831 }
832
833 case reflect.Map:
834 if ft.Elem().Kind() == reflect.Ptr {
835 nestedMessage = true // map with message values
836 }
837 }
838
839 if !canHaveDefault {
840 if nestedMessage {
841 return nil, true, nil
842 }
843 return nil, false, nil
844 }
845
846 // We now know that ft is a pointer or slice.
847 sf = &scalarField{kind: ft.Elem().Kind()}
848
849 // scalar fields without defaults
850 if !prop.HasDefault {
851 return sf, false, nil
852 }
853
854 // a scalar field: either *T or []byte
855 switch ft.Elem().Kind() {
856 case reflect.Bool:
857 x, err := strconv.ParseBool(prop.Default)
858 if err != nil {
859 return nil, false, fmt.Errorf("proto: bad default bool %q: %v", prop.Default, err)
860 }
861 sf.value = x
862 case reflect.Float32:
863 x, err := strconv.ParseFloat(prop.Default, 32)
864 if err != nil {
865 return nil, false, fmt.Errorf("proto: bad default float32 %q: %v", prop.Default, err)
866 }
867 sf.value = float32(x)
868 case reflect.Float64:
869 x, err := strconv.ParseFloat(prop.Default, 64)
870 if err != nil {
871 return nil, false, fmt.Errorf("proto: bad default float64 %q: %v", prop.Default, err)
872 }
873 sf.value = x
874 case reflect.Int32:
875 x, err := strconv.ParseInt(prop.Default, 10, 32)
876 if err != nil {
877 return nil, false, fmt.Errorf("proto: bad default int32 %q: %v", prop.Default, err)
878 }
879 sf.value = int32(x)
880 case reflect.Int64:
881 x, err := strconv.ParseInt(prop.Default, 10, 64)
882 if err != nil {
883 return nil, false, fmt.Errorf("proto: bad default int64 %q: %v", prop.Default, err)
884 }
885 sf.value = x
886 case reflect.String:
887 sf.value = prop.Default
888 case reflect.Uint8:
889 // []byte (not *uint8)
890 sf.value = []byte(prop.Default)
891 case reflect.Uint32:
892 x, err := strconv.ParseUint(prop.Default, 10, 32)
893 if err != nil {
894 return nil, false, fmt.Errorf("proto: bad default uint32 %q: %v", prop.Default, err)
895 }
896 sf.value = uint32(x)
897 case reflect.Uint64:
898 x, err := strconv.ParseUint(prop.Default, 10, 64)
899 if err != nil {
900 return nil, false, fmt.Errorf("proto: bad default uint64 %q: %v", prop.Default, err)
901 }
902 sf.value = x
903 default:
904 return nil, false, fmt.Errorf("proto: unhandled def kind %v", ft.Elem().Kind())
905 }
906
907 return sf, false, nil
908}
909
910// mapKeys returns a sort.Interface to be used for sorting the map keys.
911// Map fields may have key types of non-float scalars, strings and enums.
912func mapKeys(vs []reflect.Value) sort.Interface {
913 s := mapKeySorter{vs: vs}
914
915 // Type specialization per https://developers.google.com/protocol-buffers/docs/proto#maps.
916 if len(vs) == 0 {
917 return s
918 }
919 switch vs[0].Kind() {
920 case reflect.Int32, reflect.Int64:
921 s.less = func(a, b reflect.Value) bool { return a.Int() < b.Int() }
922 case reflect.Uint32, reflect.Uint64:
923 s.less = func(a, b reflect.Value) bool { return a.Uint() < b.Uint() }
924 case reflect.Bool:
925 s.less = func(a, b reflect.Value) bool { return !a.Bool() && b.Bool() } // false < true
926 case reflect.String:
927 s.less = func(a, b reflect.Value) bool { return a.String() < b.String() }
928 default:
929 panic(fmt.Sprintf("unsupported map key type: %v", vs[0].Kind()))
930 }
931
932 return s
933}
934
935type mapKeySorter struct {
936 vs []reflect.Value
937 less func(a, b reflect.Value) bool
938}
939
940func (s mapKeySorter) Len() int { return len(s.vs) }
941func (s mapKeySorter) Swap(i, j int) { s.vs[i], s.vs[j] = s.vs[j], s.vs[i] }
942func (s mapKeySorter) Less(i, j int) bool {
943 return s.less(s.vs[i], s.vs[j])
944}
945
946// isProto3Zero reports whether v is a zero proto3 value.
947func isProto3Zero(v reflect.Value) bool {
948 switch v.Kind() {
949 case reflect.Bool:
950 return !v.Bool()
951 case reflect.Int32, reflect.Int64:
952 return v.Int() == 0
953 case reflect.Uint32, reflect.Uint64:
954 return v.Uint() == 0
955 case reflect.Float32, reflect.Float64:
956 return v.Float() == 0
957 case reflect.String:
958 return v.String() == ""
959 }
960 return false
961}
962
963// ProtoPackageIsVersion2 is referenced from generated protocol buffer files
964// to assert that that code is compatible with this version of the proto package.
965const ProtoPackageIsVersion2 = true
966
967// ProtoPackageIsVersion1 is referenced from generated protocol buffer files
968// to assert that that code is compatible with this version of the proto package.
969const ProtoPackageIsVersion1 = true
970
971// InternalMessageInfo is a type used internally by generated .pb.go files.
972// This type is not intended to be used by non-generated code.
973// This type is not subject to any compatibility guarantee.
974type InternalMessageInfo struct {
975 marshal *marshalInfo
976 unmarshal *unmarshalInfo
977 merge *mergeInfo
978 discard *discardInfo
979}
diff --git a/vendor/github.com/golang/protobuf/proto/message_set.go b/vendor/github.com/golang/protobuf/proto/message_set.go
new file mode 100644
index 0000000..3b6ca41
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/message_set.go
@@ -0,0 +1,314 @@
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
32package proto
33
34/*
35 * Support for message sets.
36 */
37
38import (
39 "bytes"
40 "encoding/json"
41 "errors"
42 "fmt"
43 "reflect"
44 "sort"
45 "sync"
46)
47
48// errNoMessageTypeID occurs when a protocol buffer does not have a message type ID.
49// A message type ID is required for storing a protocol buffer in a message set.
50var errNoMessageTypeID = errors.New("proto does not have a message type ID")
51
52// The first two types (_MessageSet_Item and messageSet)
53// model what the protocol compiler produces for the following protocol message:
54// message MessageSet {
55// repeated group Item = 1 {
56// required int32 type_id = 2;
57// required string message = 3;
58// };
59// }
60// That is the MessageSet wire format. We can't use a proto to generate these
61// because that would introduce a circular dependency between it and this package.
62
63type _MessageSet_Item struct {
64 TypeId *int32 `protobuf:"varint,2,req,name=type_id"`
65 Message []byte `protobuf:"bytes,3,req,name=message"`
66}
67
68type messageSet struct {
69 Item []*_MessageSet_Item `protobuf:"group,1,rep"`
70 XXX_unrecognized []byte
71 // TODO: caching?
72}
73
74// Make sure messageSet is a Message.
75var _ Message = (*messageSet)(nil)
76
77// messageTypeIder is an interface satisfied by a protocol buffer type
78// that may be stored in a MessageSet.
79type messageTypeIder interface {
80 MessageTypeId() int32
81}
82
83func (ms *messageSet) find(pb Message) *_MessageSet_Item {
84 mti, ok := pb.(messageTypeIder)
85 if !ok {
86 return nil
87 }
88 id := mti.MessageTypeId()
89 for _, item := range ms.Item {
90 if *item.TypeId == id {
91 return item
92 }
93 }
94 return nil
95}
96
97func (ms *messageSet) Has(pb Message) bool {
98 return ms.find(pb) != nil
99}
100
101func (ms *messageSet) Unmarshal(pb Message) error {
102 if item := ms.find(pb); item != nil {
103 return Unmarshal(item.Message, pb)
104 }
105 if _, ok := pb.(messageTypeIder); !ok {
106 return errNoMessageTypeID
107 }
108 return nil // TODO: return error instead?
109}
110
111func (ms *messageSet) Marshal(pb Message) error {
112 msg, err := Marshal(pb)
113 if err != nil {
114 return err
115 }
116 if item := ms.find(pb); item != nil {
117 // reuse existing item
118 item.Message = msg
119 return nil
120 }
121
122 mti, ok := pb.(messageTypeIder)
123 if !ok {
124 return errNoMessageTypeID
125 }
126
127 mtid := mti.MessageTypeId()
128 ms.Item = append(ms.Item, &_MessageSet_Item{
129 TypeId: &mtid,
130 Message: msg,
131 })
132 return nil
133}
134
135func (ms *messageSet) Reset() { *ms = messageSet{} }
136func (ms *messageSet) String() string { return CompactTextString(ms) }
137func (*messageSet) ProtoMessage() {}
138
139// Support for the message_set_wire_format message option.
140
141func skipVarint(buf []byte) []byte {
142 i := 0
143 for ; buf[i]&0x80 != 0; i++ {
144 }
145 return buf[i+1:]
146}
147
148// MarshalMessageSet encodes the extension map represented by m in the message set wire format.
149// It is called by generated Marshal methods on protocol buffer messages with the message_set_wire_format option.
150func MarshalMessageSet(exts interface{}) ([]byte, error) {
151 return marshalMessageSet(exts, false)
152}
153
154// marshaMessageSet implements above function, with the opt to turn on / off deterministic during Marshal.
155func marshalMessageSet(exts interface{}, deterministic bool) ([]byte, error) {
156 switch exts := exts.(type) {
157 case *XXX_InternalExtensions:
158 var u marshalInfo
159 siz := u.sizeMessageSet(exts)
160 b := make([]byte, 0, siz)
161 return u.appendMessageSet(b, exts, deterministic)
162
163 case map[int32]Extension:
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 },
173 }
174
175 var u marshalInfo
176 siz := u.sizeMessageSet(&ie)
177 b := make([]byte, 0, siz)
178 return u.appendMessageSet(b, &ie, deterministic)
179
180 default:
181 return nil, errors.New("proto: not an extension map")
182 }
183}
184
185// UnmarshalMessageSet decodes the extension map encoded in buf in the message set wire format.
186// It is called by Unmarshal methods on protocol buffer messages with the message_set_wire_format option.
187func UnmarshalMessageSet(buf []byte, exts interface{}) error {
188 var m map[int32]Extension
189 switch exts := exts.(type) {
190 case *XXX_InternalExtensions:
191 m = exts.extensionsWrite()
192 case map[int32]Extension:
193 m = exts
194 default:
195 return errors.New("proto: not an extension map")
196 }
197
198 ms := new(messageSet)
199 if err := Unmarshal(buf, ms); err != nil {
200 return err
201 }
202 for _, item := range ms.Item {
203 id := *item.TypeId
204 msg := item.Message
205
206 // Restore wire type and field number varint, plus length varint.
207 // Be careful to preserve duplicate items.
208 b := EncodeVarint(uint64(id)<<3 | WireBytes)
209 if ext, ok := m[id]; ok {
210 // Existing data; rip off the tag and length varint
211 // so we join the new data correctly.
212 // We can assume that ext.enc is set because we are unmarshaling.
213 o := ext.enc[len(b):] // skip wire type and field number
214 _, n := DecodeVarint(o) // calculate length of length varint
215 o = o[n:] // skip length varint
216 msg = append(o, msg...) // join old data and new data
217 }
218 b = append(b, EncodeVarint(uint64(len(msg)))...)
219 b = append(b, msg...)
220
221 m[id] = Extension{enc: b}
222 }
223 return nil
224}
225
226// MarshalMessageSetJSON encodes the extension map represented by m in JSON format.
227// It is called by generated MarshalJSON methods on protocol buffer messages with the message_set_wire_format option.
228func MarshalMessageSetJSON(exts interface{}) ([]byte, error) {
229 var m map[int32]Extension
230 switch exts := exts.(type) {
231 case *XXX_InternalExtensions:
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 }
241 case map[int32]Extension:
242 m = exts
243 default:
244 return nil, errors.New("proto: not an extension map")
245 }
246 var b bytes.Buffer
247 b.WriteByte('{')
248
249 // Process the map in key order for deterministic output.
250 ids := make([]int32, 0, len(m))
251 for id := range m {
252 ids = append(ids, id)
253 }
254 sort.Sort(int32Slice(ids)) // int32Slice defined in text.go
255
256 for i, id := range ids {
257 ext := m[id]
258 msd, ok := messageSetMap[id]
259 if !ok {
260 // Unknown type; we can't render it, so skip it.
261 continue
262 }
263
264 if i > 0 && b.Len() > 1 {
265 b.WriteByte(',')
266 }
267
268 fmt.Fprintf(&b, `"[%s]":`, msd.name)
269
270 x := ext.value
271 if x == nil {
272 x = reflect.New(msd.t.Elem()).Interface()
273 if err := Unmarshal(ext.enc, x.(Message)); err != nil {
274 return nil, err
275 }
276 }
277 d, err := json.Marshal(x)
278 if err != nil {
279 return nil, err
280 }
281 b.Write(d)
282 }
283 b.WriteByte('}')
284 return b.Bytes(), nil
285}
286
287// UnmarshalMessageSetJSON decodes the extension map encoded in buf in JSON format.
288// It is called by generated UnmarshalJSON methods on protocol buffer messages with the message_set_wire_format option.
289func UnmarshalMessageSetJSON(buf []byte, exts interface{}) error {
290 // Common-case fast path.
291 if len(buf) == 0 || bytes.Equal(buf, []byte("{}")) {
292 return nil
293 }
294
295 // This is fairly tricky, and it's not clear that it is needed.
296 return errors.New("TODO: UnmarshalMessageSetJSON not yet implemented")
297}
298
299// A global registry of types that can be used in a MessageSet.
300
301var messageSetMap = make(map[int32]messageSetDesc)
302
303type messageSetDesc struct {
304 t reflect.Type // pointer to struct
305 name string
306}
307
308// RegisterMessageSetType is called from the generated code.
309func RegisterMessageSetType(m Message, fieldNum int32, name string) {
310 messageSetMap[fieldNum] = messageSetDesc{
311 t: reflect.TypeOf(m),
312 name: name,
313 }
314}
diff --git a/vendor/github.com/golang/protobuf/proto/pointer_reflect.go b/vendor/github.com/golang/protobuf/proto/pointer_reflect.go
new file mode 100644
index 0000000..b6cad90
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/pointer_reflect.go
@@ -0,0 +1,357 @@
1// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2012 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// +build purego appengine js
33
34// This file contains an implementation of proto field accesses using package reflect.
35// It is slower than the code in pointer_unsafe.go but it avoids package unsafe and can
36// be used on App Engine.
37
38package proto
39
40import (
41 "reflect"
42 "sync"
43)
44
45const unsafeAllowed = false
46
47// A field identifies a field in a struct, accessible from a pointer.
48// In this implementation, a field is identified by the sequence of field indices
49// passed to reflect's FieldByIndex.
50type field []int
51
52// toField returns a field equivalent to the given reflect field.
53func toField(f *reflect.StructField) field {
54 return f.Index
55}
56
57// invalidField is an invalid field identifier.
58var invalidField = field(nil)
59
60// zeroField is a noop when calling pointer.offset.
61var zeroField = field([]int{})
62
63// IsValid reports whether the field identifier is valid.
64func (f field) IsValid() bool { return f != nil }
65
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.
70type pointer struct {
71 v reflect.Value
72}
73
74// toPointer converts an interface of pointer type to a pointer
75// that points to the same target.
76func toPointer(i *Message) pointer {
77 return pointer{v: reflect.ValueOf(*i)}
78}
79
80// toAddrPointer converts an interface to a pointer that points to
81// the interface data.
82func 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}
87}
88
89// valToPointer converts v to a pointer. v must be of pointer type.
90func valToPointer(v reflect.Value) pointer {
91 return pointer{v: v}
92}
93
94// offset converts from a pointer to a structure to a pointer to
95// one of its fields.
96func (p pointer) offset(f field) pointer {
97 return pointer{v: p.v.Elem().FieldByIndex(f).Addr()}
98}
99
100func (p pointer) isNil() bool {
101 return p.v.IsNil()
102}
103
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.
107func 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)
115}
116
117func (p pointer) toInt64() *int64 {
118 return p.v.Interface().(*int64)
119}
120func (p pointer) toInt64Ptr() **int64 {
121 return p.v.Interface().(**int64)
122}
123func (p pointer) toInt64Slice() *[]int64 {
124 return p.v.Interface().(*[]int64)
125}
126
127var int32ptr = reflect.TypeOf((*int32)(nil))
128
129func (p pointer) toInt32() *int32 {
130 return p.v.Convert(int32ptr).Interface().(*int32)
131}
132
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)
138}
139 func (p pointer) toInt32Slice() *[]int32 {
140 return p.v.Interface().(*[]int32)
141}
142*/
143func (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}
151func (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.
161func (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
175}
176
177// setInt32Slice copies []int32 into p as a new slice.
178// This behavior differs from the implementation in pointer_unsafe.go.
179func (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)
193}
194func (p pointer) appendInt32Slice(v int32) {
195 grow(p.v.Elem()).SetInt(int64(v))
196}
197
198func (p pointer) toUint64() *uint64 {
199 return p.v.Interface().(*uint64)
200}
201func (p pointer) toUint64Ptr() **uint64 {
202 return p.v.Interface().(**uint64)
203}
204func (p pointer) toUint64Slice() *[]uint64 {
205 return p.v.Interface().(*[]uint64)
206}
207func (p pointer) toUint32() *uint32 {
208 return p.v.Interface().(*uint32)
209}
210func (p pointer) toUint32Ptr() **uint32 {
211 return p.v.Interface().(**uint32)
212}
213func (p pointer) toUint32Slice() *[]uint32 {
214 return p.v.Interface().(*[]uint32)
215}
216func (p pointer) toBool() *bool {
217 return p.v.Interface().(*bool)
218}
219func (p pointer) toBoolPtr() **bool {
220 return p.v.Interface().(**bool)
221}
222func (p pointer) toBoolSlice() *[]bool {
223 return p.v.Interface().(*[]bool)
224}
225func (p pointer) toFloat64() *float64 {
226 return p.v.Interface().(*float64)
227}
228func (p pointer) toFloat64Ptr() **float64 {
229 return p.v.Interface().(**float64)
230}
231func (p pointer) toFloat64Slice() *[]float64 {
232 return p.v.Interface().(*[]float64)
233}
234func (p pointer) toFloat32() *float32 {
235 return p.v.Interface().(*float32)
236}
237func (p pointer) toFloat32Ptr() **float32 {
238 return p.v.Interface().(**float32)
239}
240func (p pointer) toFloat32Slice() *[]float32 {
241 return p.v.Interface().(*[]float32)
242}
243func (p pointer) toString() *string {
244 return p.v.Interface().(*string)
245}
246func (p pointer) toStringPtr() **string {
247 return p.v.Interface().(**string)
248}
249func (p pointer) toStringSlice() *[]string {
250 return p.v.Interface().(*[]string)
251}
252func (p pointer) toBytes() *[]byte {
253 return p.v.Interface().(*[]byte)
254}
255func (p pointer) toBytesSlice() *[][]byte {
256 return p.v.Interface().(*[][]byte)
257}
258func (p pointer) toExtensions() *XXX_InternalExtensions {
259 return p.v.Interface().(*XXX_InternalExtensions)
260}
261func (p pointer) toOldExtensions() *map[int32]Extension {
262 return p.v.Interface().(*map[int32]Extension)
263}
264func (p pointer) getPointer() pointer {
265 return pointer{v: p.v.Elem()}
266}
267func (p pointer) setPointer(q pointer) {
268 p.v.Elem().Set(q.v)
269}
270func (p pointer) appendPointer(q pointer) {
271 grow(p.v.Elem()).Set(q.v)
272}
273
274// getPointerSlice copies []*T from p as a new []pointer.
275// This behavior differs from the implementation in pointer_unsafe.go.
276func (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
286}
287
288// setPointerSlice copies []pointer into p as a new []*T.
289// This behavior differs from the implementation in pointer_unsafe.go.
290func (p pointer) setPointerSlice(v []pointer) {
291 if v == nil {
292 p.v.Elem().Set(reflect.New(p.v.Elem().Type()).Elem())
293 return
294 }
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)
300}
301
302// getInterfacePointer returns a pointer that points to the
303// interface data of the interface pointed by p.
304func (p pointer) getInterfacePointer() pointer {
305 if p.v.Elem().IsNil() {
306 return pointer{v: p.v.Elem()}
307 }
308 return pointer{v: p.v.Elem().Elem().Elem().Field(0).Addr()} // *interface -> interface -> *struct -> struct
309}
310
311func (p pointer) asPointerTo(t reflect.Type) reflect.Value {
312 // TODO: check that p.v.Type().Elem() == t?
313 return p.v
314}
315
316func atomicLoadUnmarshalInfo(p **unmarshalInfo) *unmarshalInfo {
317 atomicLock.Lock()
318 defer atomicLock.Unlock()
319 return *p
320}
321func atomicStoreUnmarshalInfo(p **unmarshalInfo, v *unmarshalInfo) {
322 atomicLock.Lock()
323 defer atomicLock.Unlock()
324 *p = v
325}
326func atomicLoadMarshalInfo(p **marshalInfo) *marshalInfo {
327 atomicLock.Lock()
328 defer atomicLock.Unlock()
329 return *p
330}
331func atomicStoreMarshalInfo(p **marshalInfo, v *marshalInfo) {
332 atomicLock.Lock()
333 defer atomicLock.Unlock()
334 *p = v
335}
336func atomicLoadMergeInfo(p **mergeInfo) *mergeInfo {
337 atomicLock.Lock()
338 defer atomicLock.Unlock()
339 return *p
340}
341func atomicStoreMergeInfo(p **mergeInfo, v *mergeInfo) {
342 atomicLock.Lock()
343 defer atomicLock.Unlock()
344 *p = v
345}
346func atomicLoadDiscardInfo(p **discardInfo) *discardInfo {
347 atomicLock.Lock()
348 defer atomicLock.Unlock()
349 return *p
350}
351func atomicStoreDiscardInfo(p **discardInfo, v *discardInfo) {
352 atomicLock.Lock()
353 defer atomicLock.Unlock()
354 *p = v
355}
356
357var atomicLock sync.Mutex
diff --git a/vendor/github.com/golang/protobuf/proto/pointer_unsafe.go b/vendor/github.com/golang/protobuf/proto/pointer_unsafe.go
new file mode 100644
index 0000000..d55a335
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/pointer_unsafe.go
@@ -0,0 +1,308 @@
1// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2012 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// +build !purego,!appengine,!js
33
34// This file contains the implementation of the proto field accesses using package unsafe.
35
36package proto
37
38import (
39 "reflect"
40 "sync/atomic"
41 "unsafe"
42)
43
44const unsafeAllowed = true
45
46// A field identifies a field in a struct, accessible from a pointer.
47// In this implementation, a field is identified by its byte offset from the start of the struct.
48type field uintptr
49
50// toField returns a field equivalent to the given reflect field.
51func toField(f *reflect.StructField) field {
52 return field(f.Offset)
53}
54
55// invalidField is an invalid field identifier.
56const invalidField = ^field(0)
57
58// zeroField is a noop when calling pointer.offset.
59const zeroField = field(0)
60
61// IsValid reports whether the field identifier is valid.
62func (f field) IsValid() bool {
63 return f != invalidField
64}
65
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.
70type pointer struct {
71 p unsafe.Pointer
72}
73
74// size of pointer
75var ptrSize = unsafe.Sizeof(uintptr(0))
76
77// toPointer converts an interface of pointer type to a pointer
78// that points to the same target.
79func toPointer(i *Message) pointer {
80 // Super-tricky - read pointer out of data word of interface value.
81 // Saves ~25ns over the equivalent:
82 // return valToPointer(reflect.ValueOf(*i))
83 return pointer{p: (*[2]unsafe.Pointer)(unsafe.Pointer(i))[1]}
84}
85
86// toAddrPointer converts an interface to a pointer that points to
87// the interface data.
88func toAddrPointer(i *interface{}, isptr bool) pointer {
89 // Super-tricky - read or get the address of data word of interface value.
90 if isptr {
91 // The interface is of pointer type, thus it is a direct interface.
92 // The data word is the pointer data itself. We take its address.
93 return pointer{p: unsafe.Pointer(uintptr(unsafe.Pointer(i)) + ptrSize)}
94 }
95 // The interface is not of pointer type. The data word is the pointer
96 // to the data.
97 return pointer{p: (*[2]unsafe.Pointer)(unsafe.Pointer(i))[1]}
98}
99
100// valToPointer converts v to a pointer. v must be of pointer type.
101func valToPointer(v reflect.Value) pointer {
102 return pointer{p: unsafe.Pointer(v.Pointer())}
103}
104
105// offset converts from a pointer to a structure to a pointer to
106// one of its fields.
107func (p pointer) offset(f field) pointer {
108 // For safety, we should panic if !f.IsValid, however calling panic causes
109 // this to no longer be inlineable, which is a serious performance cost.
110 /*
111 if !f.IsValid() {
112 panic("invalid field")
113 }
114 */
115 return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
116}
117
118func (p pointer) isNil() bool {
119 return p.p == nil
120}
121
122func (p pointer) toInt64() *int64 {
123 return (*int64)(p.p)
124}
125func (p pointer) toInt64Ptr() **int64 {
126 return (**int64)(p.p)
127}
128func (p pointer) toInt64Slice() *[]int64 {
129 return (*[]int64)(p.p)
130}
131func (p pointer) toInt32() *int32 {
132 return (*int32)(p.p)
133}
134
135// See pointer_reflect.go for why toInt32Ptr/Slice doesn't exist.
136/*
137 func (p pointer) toInt32Ptr() **int32 {
138 return (**int32)(p.p)
139 }
140 func (p pointer) toInt32Slice() *[]int32 {
141 return (*[]int32)(p.p)
142 }
143*/
144func (p pointer) getInt32Ptr() *int32 {
145 return *(**int32)(p.p)
146}
147func (p pointer) setInt32Ptr(v int32) {
148 *(**int32)(p.p) = &v
149}
150
151// getInt32Slice loads a []int32 from p.
152// The value returned is aliased with the original slice.
153// This behavior differs from the implementation in pointer_reflect.go.
154func (p pointer) getInt32Slice() []int32 {
155 return *(*[]int32)(p.p)
156}
157
158// setInt32Slice stores a []int32 to p.
159// The value set is aliased with the input slice.
160// This behavior differs from the implementation in pointer_reflect.go.
161func (p pointer) setInt32Slice(v []int32) {
162 *(*[]int32)(p.p) = v
163}
164
165// TODO: Can we get rid of appendInt32Slice and use setInt32Slice instead?
166func (p pointer) appendInt32Slice(v int32) {
167 s := (*[]int32)(p.p)
168 *s = append(*s, v)
169}
170
171func (p pointer) toUint64() *uint64 {
172 return (*uint64)(p.p)
173}
174func (p pointer) toUint64Ptr() **uint64 {
175 return (**uint64)(p.p)
176}
177func (p pointer) toUint64Slice() *[]uint64 {
178 return (*[]uint64)(p.p)
179}
180func (p pointer) toUint32() *uint32 {
181 return (*uint32)(p.p)
182}
183func (p pointer) toUint32Ptr() **uint32 {
184 return (**uint32)(p.p)
185}
186func (p pointer) toUint32Slice() *[]uint32 {
187 return (*[]uint32)(p.p)
188}
189func (p pointer) toBool() *bool {
190 return (*bool)(p.p)
191}
192func (p pointer) toBoolPtr() **bool {
193 return (**bool)(p.p)
194}
195func (p pointer) toBoolSlice() *[]bool {
196 return (*[]bool)(p.p)
197}
198func (p pointer) toFloat64() *float64 {
199 return (*float64)(p.p)
200}
201func (p pointer) toFloat64Ptr() **float64 {
202 return (**float64)(p.p)
203}
204func (p pointer) toFloat64Slice() *[]float64 {
205 return (*[]float64)(p.p)
206}
207func (p pointer) toFloat32() *float32 {
208 return (*float32)(p.p)
209}
210func (p pointer) toFloat32Ptr() **float32 {
211 return (**float32)(p.p)
212}
213func (p pointer) toFloat32Slice() *[]float32 {
214 return (*[]float32)(p.p)
215}
216func (p pointer) toString() *string {
217 return (*string)(p.p)
218}
219func (p pointer) toStringPtr() **string {
220 return (**string)(p.p)
221}
222func (p pointer) toStringSlice() *[]string {
223 return (*[]string)(p.p)
224}
225func (p pointer) toBytes() *[]byte {
226 return (*[]byte)(p.p)
227}
228func (p pointer) toBytesSlice() *[][]byte {
229 return (*[][]byte)(p.p)
230}
231func (p pointer) toExtensions() *XXX_InternalExtensions {
232 return (*XXX_InternalExtensions)(p.p)
233}
234func (p pointer) toOldExtensions() *map[int32]Extension {
235 return (*map[int32]Extension)(p.p)
236}
237
238// getPointerSlice loads []*T from p as a []pointer.
239// The value returned is aliased with the original slice.
240// This behavior differs from the implementation in pointer_reflect.go.
241func (p pointer) getPointerSlice() []pointer {
242 // Super-tricky - p should point to a []*T where T is a
243 // message type. We load it as []pointer.
244 return *(*[]pointer)(p.p)
245}
246
247// setPointerSlice stores []pointer into p as a []*T.
248// The value set is aliased with the input slice.
249// This behavior differs from the implementation in pointer_reflect.go.
250func (p pointer) setPointerSlice(v []pointer) {
251 // Super-tricky - p should point to a []*T where T is a
252 // message type. We store it as []pointer.
253 *(*[]pointer)(p.p) = v
254}
255
256// getPointer loads the pointer at p and returns it.
257func (p pointer) getPointer() pointer {
258 return pointer{p: *(*unsafe.Pointer)(p.p)}
259}
260
261// setPointer stores the pointer q at p.
262func (p pointer) setPointer(q pointer) {
263 *(*unsafe.Pointer)(p.p) = q.p
264}
265
266// append q to the slice pointed to by p.
267func (p pointer) appendPointer(q pointer) {
268 s := (*[]unsafe.Pointer)(p.p)
269 *s = append(*s, q.p)
270}
271
272// getInterfacePointer returns a pointer that points to the
273// interface data of the interface pointed by p.
274func (p pointer) getInterfacePointer() pointer {
275 // Super-tricky - read pointer out of data word of interface value.
276 return pointer{p: (*(*[2]unsafe.Pointer)(p.p))[1]}
277}
278
279// asPointerTo returns a reflect.Value that is a pointer to an
280// object of type t stored at p.
281func (p pointer) asPointerTo(t reflect.Type) reflect.Value {
282 return reflect.NewAt(t, p.p)
283}
284
285func atomicLoadUnmarshalInfo(p **unmarshalInfo) *unmarshalInfo {
286 return (*unmarshalInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
287}
288func atomicStoreUnmarshalInfo(p **unmarshalInfo, v *unmarshalInfo) {
289 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
290}
291func atomicLoadMarshalInfo(p **marshalInfo) *marshalInfo {
292 return (*marshalInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
293}
294func atomicStoreMarshalInfo(p **marshalInfo, v *marshalInfo) {
295 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
296}
297func atomicLoadMergeInfo(p **mergeInfo) *mergeInfo {
298 return (*mergeInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
299}
300func atomicStoreMergeInfo(p **mergeInfo, v *mergeInfo) {
301 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
302}
303func atomicLoadDiscardInfo(p **discardInfo) *discardInfo {
304 return (*discardInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
305}
306func atomicStoreDiscardInfo(p **discardInfo, v *discardInfo) {
307 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
308}
diff --git a/vendor/github.com/golang/protobuf/proto/properties.go b/vendor/github.com/golang/protobuf/proto/properties.go
new file mode 100644
index 0000000..50b99b8
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/properties.go
@@ -0,0 +1,544 @@
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
32package proto
33
34/*
35 * Routines for encoding data into the wire format for protocol buffers.
36 */
37
38import (
39 "fmt"
40 "log"
41 "os"
42 "reflect"
43 "sort"
44 "strconv"
45 "strings"
46 "sync"
47)
48
49const debug bool = false
50
51// Constants that identify the encoding of a value on the wire.
52const (
53 WireVarint = 0
54 WireFixed64 = 1
55 WireBytes = 2
56 WireStartGroup = 3
57 WireEndGroup = 4
58 WireFixed32 = 5
59)
60
61// tagMap is an optimization over map[int]int for typical protocol buffer
62// use-cases. Encoded protocol buffers are often in tag order with small tag
63// numbers.
64type tagMap struct {
65 fastTags []int
66 slowTags map[int]int
67}
68
69// tagMapFastLimit is the upper bound on the tag number that will be stored in
70// the tagMap slice rather than its map.
71const tagMapFastLimit = 1024
72
73func (p *tagMap) get(t int) (int, bool) {
74 if t > 0 && t < tagMapFastLimit {
75 if t >= len(p.fastTags) {
76 return 0, false
77 }
78 fi := p.fastTags[t]
79 return fi, fi >= 0
80 }
81 fi, ok := p.slowTags[t]
82 return fi, ok
83}
84
85func (p *tagMap) put(t int, fi int) {
86 if t > 0 && t < tagMapFastLimit {
87 for len(p.fastTags) < t+1 {
88 p.fastTags = append(p.fastTags, -1)
89 }
90 p.fastTags[t] = fi
91 return
92 }
93 if p.slowTags == nil {
94 p.slowTags = make(map[int]int)
95 }
96 p.slowTags[t] = fi
97}
98
99// StructProperties represents properties for all the fields of a struct.
100// decoderTags and decoderOrigNames should only be used by the decoder.
101type StructProperties struct {
102 Prop []*Properties // properties for each field
103 reqCount int // required count
104 decoderTags tagMap // map from proto tag to struct field number
105 decoderOrigNames map[string]int // map from original name to struct field number
106 order []int // list of struct field numbers in tag order
107
108 // OneofTypes contains information about the oneof fields in this message.
109 // It is keyed by the original name of a field.
110 OneofTypes map[string]*OneofProperties
111}
112
113// OneofProperties represents information about a specific field in a oneof.
114type OneofProperties struct {
115 Type reflect.Type // pointer to generated struct type for this oneof field
116 Field int // struct field number of the containing oneof in the message
117 Prop *Properties
118}
119
120// Implement the sorting interface so we can sort the fields in tag order, as recommended by the spec.
121// See encode.go, (*Buffer).enc_struct.
122
123func (sp *StructProperties) Len() int { return len(sp.order) }
124func (sp *StructProperties) Less(i, j int) bool {
125 return sp.Prop[sp.order[i]].Tag < sp.Prop[sp.order[j]].Tag
126}
127func (sp *StructProperties) Swap(i, j int) { sp.order[i], sp.order[j] = sp.order[j], sp.order[i] }
128
129// Properties represents the protocol-specific behavior of a single struct field.
130type Properties struct {
131 Name string // name of the field, for error messages
132 OrigName string // original name before protocol compiler (always set)
133 JSONName string // name to use for JSON; determined by protoc
134 Wire string
135 WireType int
136 Tag int
137 Required bool
138 Optional bool
139 Repeated bool
140 Packed bool // relevant for repeated primitives only
141 Enum string // set for enum types only
142 proto3 bool // whether this is known to be a proto3 field
143 oneof bool // whether this is a oneof field
144
145 Default string // default value
146 HasDefault bool // whether an explicit default was provided
147
148 stype reflect.Type // set for struct types only
149 sprop *StructProperties // set for struct types only
150
151 mtype reflect.Type // set for map types only
152 MapKeyProp *Properties // set for map types only
153 MapValProp *Properties // set for map types only
154}
155
156// String formats the properties in the protobuf struct field tag style.
157func (p *Properties) String() string {
158 s := p.Wire
159 s += ","
160 s += strconv.Itoa(p.Tag)
161 if p.Required {
162 s += ",req"
163 }
164 if p.Optional {
165 s += ",opt"
166 }
167 if p.Repeated {
168 s += ",rep"
169 }
170 if p.Packed {
171 s += ",packed"
172 }
173 s += ",name=" + p.OrigName
174 if p.JSONName != p.OrigName {
175 s += ",json=" + p.JSONName
176 }
177 if p.proto3 {
178 s += ",proto3"
179 }
180 if p.oneof {
181 s += ",oneof"
182 }
183 if len(p.Enum) > 0 {
184 s += ",enum=" + p.Enum
185 }
186 if p.HasDefault {
187 s += ",def=" + p.Default
188 }
189 return s
190}
191
192// Parse populates p by parsing a string in the protobuf struct field tag style.
193func (p *Properties) Parse(s string) {
194 // "bytes,49,opt,name=foo,def=hello!"
195 fields := strings.Split(s, ",") // breaks def=, but handled below.
196 if len(fields) < 2 {
197 fmt.Fprintf(os.Stderr, "proto: tag has too few fields: %q\n", s)
198 return
199 }
200
201 p.Wire = fields[0]
202 switch p.Wire {
203 case "varint":
204 p.WireType = WireVarint
205 case "fixed32":
206 p.WireType = WireFixed32
207 case "fixed64":
208 p.WireType = WireFixed64
209 case "zigzag32":
210 p.WireType = WireVarint
211 case "zigzag64":
212 p.WireType = WireVarint
213 case "bytes", "group":
214 p.WireType = WireBytes
215 // no numeric converter for non-numeric types
216 default:
217 fmt.Fprintf(os.Stderr, "proto: tag has unknown wire type: %q\n", s)
218 return
219 }
220
221 var err error
222 p.Tag, err = strconv.Atoi(fields[1])
223 if err != nil {
224 return
225 }
226
227outer:
228 for i := 2; i < len(fields); i++ {
229 f := fields[i]
230 switch {
231 case f == "req":
232 p.Required = true
233 case f == "opt":
234 p.Optional = true
235 case f == "rep":
236 p.Repeated = true
237 case f == "packed":
238 p.Packed = true
239 case strings.HasPrefix(f, "name="):
240 p.OrigName = f[5:]
241 case strings.HasPrefix(f, "json="):
242 p.JSONName = f[5:]
243 case strings.HasPrefix(f, "enum="):
244 p.Enum = f[5:]
245 case f == "proto3":
246 p.proto3 = true
247 case f == "oneof":
248 p.oneof = true
249 case strings.HasPrefix(f, "def="):
250 p.HasDefault = true
251 p.Default = f[4:] // rest of string
252 if i+1 < len(fields) {
253 // Commas aren't escaped, and def is always last.
254 p.Default += "," + strings.Join(fields[i+1:], ",")
255 break outer
256 }
257 }
258 }
259}
260
261var protoMessageType = reflect.TypeOf((*Message)(nil)).Elem()
262
263// setFieldProps initializes the field properties for submessages and maps.
264func (p *Properties) setFieldProps(typ reflect.Type, f *reflect.StructField, lockGetProp bool) {
265 switch t1 := typ; t1.Kind() {
266 case reflect.Ptr:
267 if t1.Elem().Kind() == reflect.Struct {
268 p.stype = t1.Elem()
269 }
270
271 case reflect.Slice:
272 if t2 := t1.Elem(); t2.Kind() == reflect.Ptr && t2.Elem().Kind() == reflect.Struct {
273 p.stype = t2.Elem()
274 }
275
276 case reflect.Map:
277 p.mtype = t1
278 p.MapKeyProp = &Properties{}
279 p.MapKeyProp.init(reflect.PtrTo(p.mtype.Key()), "Key", f.Tag.Get("protobuf_key"), nil, lockGetProp)
280 p.MapValProp = &Properties{}
281 vtype := p.mtype.Elem()
282 if vtype.Kind() != reflect.Ptr && vtype.Kind() != reflect.Slice {
283 // The value type is not a message (*T) or bytes ([]byte),
284 // so we need encoders for the pointer to this type.
285 vtype = reflect.PtrTo(vtype)
286 }
287 p.MapValProp.init(vtype, "Value", f.Tag.Get("protobuf_val"), nil, lockGetProp)
288 }
289
290 if p.stype != nil {
291 if lockGetProp {
292 p.sprop = GetProperties(p.stype)
293 } else {
294 p.sprop = getPropertiesLocked(p.stype)
295 }
296 }
297}
298
299var (
300 marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem()
301)
302
303// Init populates the properties from a protocol buffer struct tag.
304func (p *Properties) Init(typ reflect.Type, name, tag string, f *reflect.StructField) {
305 p.init(typ, name, tag, f, true)
306}
307
308func (p *Properties) init(typ reflect.Type, name, tag string, f *reflect.StructField, lockGetProp bool) {
309 // "bytes,49,opt,def=hello!"
310 p.Name = name
311 p.OrigName = name
312 if tag == "" {
313 return
314 }
315 p.Parse(tag)
316 p.setFieldProps(typ, f, lockGetProp)
317}
318
319var (
320 propertiesMu sync.RWMutex
321 propertiesMap = make(map[reflect.Type]*StructProperties)
322)
323
324// GetProperties returns the list of properties for the type represented by t.
325// t must represent a generated struct type of a protocol message.
326func GetProperties(t reflect.Type) *StructProperties {
327 if t.Kind() != reflect.Struct {
328 panic("proto: type must have kind struct")
329 }
330
331 // Most calls to GetProperties in a long-running program will be
332 // retrieving details for types we have seen before.
333 propertiesMu.RLock()
334 sprop, ok := propertiesMap[t]
335 propertiesMu.RUnlock()
336 if ok {
337 if collectStats {
338 stats.Chit++
339 }
340 return sprop
341 }
342
343 propertiesMu.Lock()
344 sprop = getPropertiesLocked(t)
345 propertiesMu.Unlock()
346 return sprop
347}
348
349// getPropertiesLocked requires that propertiesMu is held.
350func getPropertiesLocked(t reflect.Type) *StructProperties {
351 if prop, ok := propertiesMap[t]; ok {
352 if collectStats {
353 stats.Chit++
354 }
355 return prop
356 }
357 if collectStats {
358 stats.Cmiss++
359 }
360
361 prop := new(StructProperties)
362 // in case of recursive protos, fill this in now.
363 propertiesMap[t] = prop
364
365 // build properties
366 prop.Prop = make([]*Properties, t.NumField())
367 prop.order = make([]int, t.NumField())
368
369 for i := 0; i < t.NumField(); i++ {
370 f := t.Field(i)
371 p := new(Properties)
372 name := f.Name
373 p.init(f.Type, name, f.Tag.Get("protobuf"), &f, false)
374
375 oneof := f.Tag.Get("protobuf_oneof") // special case
376 if oneof != "" {
377 // Oneof fields don't use the traditional protobuf tag.
378 p.OrigName = oneof
379 }
380 prop.Prop[i] = p
381 prop.order[i] = i
382 if debug {
383 print(i, " ", f.Name, " ", t.String(), " ")
384 if p.Tag > 0 {
385 print(p.String())
386 }
387 print("\n")
388 }
389 }
390
391 // Re-order prop.order.
392 sort.Sort(prop)
393
394 type oneofMessage interface {
395 XXX_OneofFuncs() (func(Message, *Buffer) error, func(Message, int, int, *Buffer) (bool, error), func(Message) int, []interface{})
396 }
397 if om, ok := reflect.Zero(reflect.PtrTo(t)).Interface().(oneofMessage); ok {
398 var oots []interface{}
399 _, _, _, oots = om.XXX_OneofFuncs()
400
401 // Interpret oneof metadata.
402 prop.OneofTypes = make(map[string]*OneofProperties)
403 for _, oot := range oots {
404 oop := &OneofProperties{
405 Type: reflect.ValueOf(oot).Type(), // *T
406 Prop: new(Properties),
407 }
408 sft := oop.Type.Elem().Field(0)
409 oop.Prop.Name = sft.Name
410 oop.Prop.Parse(sft.Tag.Get("protobuf"))
411 // There will be exactly one interface field that
412 // this new value is assignable to.
413 for i := 0; i < t.NumField(); i++ {
414 f := t.Field(i)
415 if f.Type.Kind() != reflect.Interface {
416 continue
417 }
418 if !oop.Type.AssignableTo(f.Type) {
419 continue
420 }
421 oop.Field = i
422 break
423 }
424 prop.OneofTypes[oop.Prop.OrigName] = oop
425 }
426 }
427
428 // build required counts
429 // build tags
430 reqCount := 0
431 prop.decoderOrigNames = make(map[string]int)
432 for i, p := range prop.Prop {
433 if strings.HasPrefix(p.Name, "XXX_") {
434 // Internal fields should not appear in tags/origNames maps.
435 // They are handled specially when encoding and decoding.
436 continue
437 }
438 if p.Required {
439 reqCount++
440 }
441 prop.decoderTags.put(p.Tag, i)
442 prop.decoderOrigNames[p.OrigName] = i
443 }
444 prop.reqCount = reqCount
445
446 return prop
447}
448
449// A global registry of enum types.
450// The generated code will register the generated maps by calling RegisterEnum.
451
452var enumValueMaps = make(map[string]map[string]int32)
453
454// RegisterEnum is called from the generated code to install the enum descriptor
455// maps into the global table to aid parsing text format protocol buffers.
456func RegisterEnum(typeName string, unusedNameMap map[int32]string, valueMap map[string]int32) {
457 if _, ok := enumValueMaps[typeName]; ok {
458 panic("proto: duplicate enum registered: " + typeName)
459 }
460 enumValueMaps[typeName] = valueMap
461}
462
463// EnumValueMap returns the mapping from names to integers of the
464// enum type enumType, or a nil if not found.
465func EnumValueMap(enumType string) map[string]int32 {
466 return enumValueMaps[enumType]
467}
468
469// A registry of all linked message types.
470// The string is a fully-qualified proto name ("pkg.Message").
471var (
472 protoTypedNils = make(map[string]Message) // a map from proto names to typed nil pointers
473 protoMapTypes = make(map[string]reflect.Type) // a map from proto names to map types
474 revProtoTypes = make(map[reflect.Type]string)
475)
476
477// RegisterType is called from generated code and maps from the fully qualified
478// proto name to the type (pointer to struct) of the protocol buffer.
479func RegisterType(x Message, name string) {
480 if _, ok := protoTypedNils[name]; ok {
481 // TODO: Some day, make this a panic.
482 log.Printf("proto: duplicate proto type registered: %s", name)
483 return
484 }
485 t := reflect.TypeOf(x)
486 if v := reflect.ValueOf(x); v.Kind() == reflect.Ptr && v.Pointer() == 0 {
487 // Generated code always calls RegisterType with nil x.
488 // This check is just for extra safety.
489 protoTypedNils[name] = x
490 } else {
491 protoTypedNils[name] = reflect.Zero(t).Interface().(Message)
492 }
493 revProtoTypes[t] = name
494}
495
496// RegisterMapType is called from generated code and maps from the fully qualified
497// proto name to the native map type of the proto map definition.
498func RegisterMapType(x interface{}, name string) {
499 if reflect.TypeOf(x).Kind() != reflect.Map {
500 panic(fmt.Sprintf("RegisterMapType(%T, %q); want map", x, name))
501 }
502 if _, ok := protoMapTypes[name]; ok {
503 log.Printf("proto: duplicate proto type registered: %s", name)
504 return
505 }
506 t := reflect.TypeOf(x)
507 protoMapTypes[name] = t
508 revProtoTypes[t] = name
509}
510
511// MessageName returns the fully-qualified proto name for the given message type.
512func MessageName(x Message) string {
513 type xname interface {
514 XXX_MessageName() string
515 }
516 if m, ok := x.(xname); ok {
517 return m.XXX_MessageName()
518 }
519 return revProtoTypes[reflect.TypeOf(x)]
520}
521
522// MessageType returns the message type (pointer to struct) for a named message.
523// The type is not guaranteed to implement proto.Message if the name refers to a
524// map entry.
525func MessageType(name string) reflect.Type {
526 if t, ok := protoTypedNils[name]; ok {
527 return reflect.TypeOf(t)
528 }
529 return protoMapTypes[name]
530}
531
532// A registry of all linked proto files.
533var (
534 protoFiles = make(map[string][]byte) // file name => fileDescriptor
535)
536
537// RegisterFile is called from generated code and maps from the
538// full file name of a .proto file to its compressed FileDescriptorProto.
539func RegisterFile(filename string, fileDescriptor []byte) {
540 protoFiles[filename] = fileDescriptor
541}
542
543// FileDescriptor returns the compressed FileDescriptorProto for a .proto file.
544func FileDescriptor(filename string) []byte { return protoFiles[filename] }
diff --git a/vendor/github.com/golang/protobuf/proto/table_marshal.go b/vendor/github.com/golang/protobuf/proto/table_marshal.go
new file mode 100644
index 0000000..b167944
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/table_marshal.go
@@ -0,0 +1,2767 @@
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
32package proto
33
34import (
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.
49type 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).
53type marshaler func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error)
54
55// marshalInfo is the information used for marshaling a message.
56type marshalInfo struct {
57 typ reflect.Type
58 fields []*marshalFieldInfo
59 unrecognized field // offset of XXX_unrecognized
60 extensions field // offset of XXX_InternalExtensions
61 v1extensions field // offset of XXX_extensions
62 sizecache field // offset of XXX_sizecache
63 initialized int32 // 0 -- only typ is set, 1 -- fully initialized
64 messageset bool // uses message set wire format
65 hasmarshaler bool // has custom marshaler
66 sync.RWMutex // protect extElems map, also for initialization
67 extElems map[int32]*marshalElemInfo // info of extension elements
68}
69
70// marshalFieldInfo is the information used for marshaling a field of a message.
71type marshalFieldInfo struct {
72 field field
73 wiretag uint64 // tag in wire format
74 tagsize int // size of tag in wire format
75 sizer sizer
76 marshaler marshaler
77 isPointer bool
78 required bool // field is required
79 name string // name of the field, for error reporting
80 oneofElems map[reflect.Type]*marshalElemInfo // info of oneof elements
81}
82
83// marshalElemInfo is the information used for marshaling an extension or oneof element.
84type marshalElemInfo struct {
85 wiretag uint64 // tag in wire format
86 tagsize int // size of tag in wire format
87 sizer sizer
88 marshaler marshaler
89 isptr bool // elem is pointer typed, thus interface of this type is a direct interface (extension only)
90}
91
92var (
93 marshalInfoMap = map[reflect.Type]*marshalInfo{}
94 marshalInfoLock sync.Mutex
95)
96
97// getMarshalInfo returns the information to marshal a given type of message.
98// The info it returns may not necessarily initialized.
99// t is the type of the message (NOT the pointer to it).
100func getMarshalInfo(t reflect.Type) *marshalInfo {
101 marshalInfoLock.Lock()
102 u, ok := marshalInfoMap[t]
103 if !ok {
104 u = &marshalInfo{typ: t}
105 marshalInfoMap[t] = u
106 }
107 marshalInfoLock.Unlock()
108 return u
109}
110
111// Size is the entry point from generated code,
112// and should be ONLY called by generated code.
113// It computes the size of encoded data of msg.
114// a is a pointer to a place to store cached marshal info.
115func (a *InternalMessageInfo) Size(msg Message) int {
116 u := getMessageMarshalInfo(msg, a)
117 ptr := toPointer(&msg)
118 if ptr.isNil() {
119 // We get here if msg is a typed nil ((*SomeMessage)(nil)),
120 // so it satisfies the interface, and msg == nil wouldn't
121 // catch it. We don't want crash in this case.
122 return 0
123 }
124 return u.size(ptr)
125}
126
127// Marshal is the entry point from generated code,
128// and should be ONLY called by generated code.
129// It marshals msg to the end of b.
130// a is a pointer to a place to store cached marshal info.
131func (a *InternalMessageInfo) Marshal(b []byte, msg Message, deterministic bool) ([]byte, error) {
132 u := getMessageMarshalInfo(msg, a)
133 ptr := toPointer(&msg)
134 if ptr.isNil() {
135 // We get here if msg is a typed nil ((*SomeMessage)(nil)),
136 // so it satisfies the interface, and msg == nil wouldn't
137 // catch it. We don't want crash in this case.
138 return b, ErrNil
139 }
140 return u.marshal(b, ptr, deterministic)
141}
142
143func getMessageMarshalInfo(msg interface{}, a *InternalMessageInfo) *marshalInfo {
144 // u := a.marshal, but atomically.
145 // We use an atomic here to ensure memory consistency.
146 u := atomicLoadMarshalInfo(&a.marshal)
147 if u == nil {
148 // Get marshal information from type of message.
149 t := reflect.ValueOf(msg).Type()
150 if t.Kind() != reflect.Ptr {
151 panic(fmt.Sprintf("cannot handle non-pointer message type %v", t))
152 }
153 u = getMarshalInfo(t.Elem())
154 // Store it in the cache for later users.
155 // a.marshal = u, but atomically.
156 atomicStoreMarshalInfo(&a.marshal, u)
157 }
158 return u
159}
160
161// size is the main function to compute the size of the encoded data of a message.
162// ptr is the pointer to the message.
163func (u *marshalInfo) size(ptr pointer) int {
164 if atomic.LoadInt32(&u.initialized) == 0 {
165 u.computeMarshalInfo()
166 }
167
168 // If the message can marshal itself, let it do it, for compatibility.
169 // NOTE: This is not efficient.
170 if u.hasmarshaler {
171 m := ptr.asPointerTo(u.typ).Interface().(Marshaler)
172 b, _ := m.Marshal()
173 return len(b)
174 }
175
176 n := 0
177 for _, f := range u.fields {
178 if f.isPointer && ptr.offset(f.field).getPointer().isNil() {
179 // nil pointer always marshals to nothing
180 continue
181 }
182 n += f.sizer(ptr.offset(f.field), f.tagsize)
183 }
184 if u.extensions.IsValid() {
185 e := ptr.offset(u.extensions).toExtensions()
186 if u.messageset {
187 n += u.sizeMessageSet(e)
188 } else {
189 n += u.sizeExtensions(e)
190 }
191 }
192 if u.v1extensions.IsValid() {
193 m := *ptr.offset(u.v1extensions).toOldExtensions()
194 n += u.sizeV1Extensions(m)
195 }
196 if u.unrecognized.IsValid() {
197 s := *ptr.offset(u.unrecognized).toBytes()
198 n += len(s)
199 }
200 // cache the result for use in marshal
201 if u.sizecache.IsValid() {
202 atomic.StoreInt32(ptr.offset(u.sizecache).toInt32(), int32(n))
203 }
204 return n
205}
206
207// cachedsize gets the size from cache. If there is no cache (i.e. message is not generated),
208// fall back to compute the size.
209func (u *marshalInfo) cachedsize(ptr pointer) int {
210 if u.sizecache.IsValid() {
211 return int(atomic.LoadInt32(ptr.offset(u.sizecache).toInt32()))
212 }
213 return u.size(ptr)
214}
215
216// marshal is the main function to marshal a message. It takes a byte slice and appends
217// the encoded data to the end of the slice, returns the slice and error (if any).
218// ptr is the pointer to the message.
219// If deterministic is true, map is marshaled in deterministic order.
220func (u *marshalInfo) marshal(b []byte, ptr pointer, deterministic bool) ([]byte, error) {
221 if atomic.LoadInt32(&u.initialized) == 0 {
222 u.computeMarshalInfo()
223 }
224
225 // If the message can marshal itself, let it do it, for compatibility.
226 // NOTE: This is not efficient.
227 if u.hasmarshaler {
228 m := ptr.asPointerTo(u.typ).Interface().(Marshaler)
229 b1, err := m.Marshal()
230 b = append(b, b1...)
231 return b, err
232 }
233
234 var err, errLater error
235 // The old marshaler encodes extensions at beginning.
236 if u.extensions.IsValid() {
237 e := ptr.offset(u.extensions).toExtensions()
238 if u.messageset {
239 b, err = u.appendMessageSet(b, e, deterministic)
240 } else {
241 b, err = u.appendExtensions(b, e, deterministic)
242 }
243 if err != nil {
244 return b, err
245 }
246 }
247 if u.v1extensions.IsValid() {
248 m := *ptr.offset(u.v1extensions).toOldExtensions()
249 b, err = u.appendV1Extensions(b, m, deterministic)
250 if err != nil {
251 return b, err
252 }
253 }
254 for _, f := range u.fields {
255 if f.required {
256 if ptr.offset(f.field).getPointer().isNil() {
257 // Required field is not set.
258 // We record the error but keep going, to give a complete marshaling.
259 if errLater == nil {
260 errLater = &RequiredNotSetError{f.name}
261 }
262 continue
263 }
264 }
265 if f.isPointer && ptr.offset(f.field).getPointer().isNil() {
266 // nil pointer always marshals to nothing
267 continue
268 }
269 b, err = f.marshaler(b, ptr.offset(f.field), f.wiretag, deterministic)
270 if err != nil {
271 if err1, ok := err.(*RequiredNotSetError); ok {
272 // Required field in submessage is not set.
273 // We record the error but keep going, to give a complete marshaling.
274 if errLater == nil {
275 errLater = &RequiredNotSetError{f.name + "." + err1.field}
276 }
277 continue
278 }
279 if err == errRepeatedHasNil {
280 err = errors.New("proto: repeated field " + f.name + " has nil element")
281 }
282 if err == errInvalidUTF8 {
283 if errLater == nil {
284 fullName := revProtoTypes[reflect.PtrTo(u.typ)] + "." + f.name
285 errLater = &invalidUTF8Error{fullName}
286 }
287 continue
288 }
289 return b, err
290 }
291 }
292 if u.unrecognized.IsValid() {
293 s := *ptr.offset(u.unrecognized).toBytes()
294 b = append(b, s...)
295 }
296 return b, errLater
297}
298
299// computeMarshalInfo initializes the marshal info.
300func (u *marshalInfo) computeMarshalInfo() {
301 u.Lock()
302 defer u.Unlock()
303 if u.initialized != 0 { // non-atomic read is ok as it is protected by the lock
304 return
305 }
306
307 t := u.typ
308 u.unrecognized = invalidField
309 u.extensions = invalidField
310 u.v1extensions = invalidField
311 u.sizecache = invalidField
312
313 // If the message can marshal itself, let it do it, for compatibility.
314 // NOTE: This is not efficient.
315 if reflect.PtrTo(t).Implements(marshalerType) {
316 u.hasmarshaler = true
317 atomic.StoreInt32(&u.initialized, 1)
318 return
319 }
320
321 // get oneof implementers
322 var oneofImplementers []interface{}
323 if m, ok := reflect.Zero(reflect.PtrTo(t)).Interface().(oneofMessage); ok {
324 _, _, _, oneofImplementers = m.XXX_OneofFuncs()
325 }
326
327 n := t.NumField()
328
329 // deal with XXX fields first
330 for i := 0; i < t.NumField(); i++ {
331 f := t.Field(i)
332 if !strings.HasPrefix(f.Name, "XXX_") {
333 continue
334 }
335 switch f.Name {
336 case "XXX_sizecache":
337 u.sizecache = toField(&f)
338 case "XXX_unrecognized":
339 u.unrecognized = toField(&f)
340 case "XXX_InternalExtensions":
341 u.extensions = toField(&f)
342 u.messageset = f.Tag.Get("protobuf_messageset") == "1"
343 case "XXX_extensions":
344 u.v1extensions = toField(&f)
345 case "XXX_NoUnkeyedLiteral":
346 // nothing to do
347 default:
348 panic("unknown XXX field: " + f.Name)
349 }
350 n--
351 }
352
353 // normal fields
354 fields := make([]marshalFieldInfo, n) // batch allocation
355 u.fields = make([]*marshalFieldInfo, 0, n)
356 for i, j := 0, 0; i < t.NumField(); i++ {
357 f := t.Field(i)
358
359 if strings.HasPrefix(f.Name, "XXX_") {
360 continue
361 }
362 field := &fields[j]
363 j++
364 field.name = f.Name
365 u.fields = append(u.fields, field)
366 if f.Tag.Get("protobuf_oneof") != "" {
367 field.computeOneofFieldInfo(&f, oneofImplementers)
368 continue
369 }
370 if f.Tag.Get("protobuf") == "" {
371 // field has no tag (not in generated message), ignore it
372 u.fields = u.fields[:len(u.fields)-1]
373 j--
374 continue
375 }
376 field.computeMarshalFieldInfo(&f)
377 }
378
379 // fields are marshaled in tag order on the wire.
380 sort.Sort(byTag(u.fields))
381
382 atomic.StoreInt32(&u.initialized, 1)
383}
384
385// helper for sorting fields by tag
386type byTag []*marshalFieldInfo
387
388func (a byTag) Len() int { return len(a) }
389func (a byTag) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
390func (a byTag) Less(i, j int) bool { return a[i].wiretag < a[j].wiretag }
391
392// getExtElemInfo returns the information to marshal an extension element.
393// The info it returns is initialized.
394func (u *marshalInfo) getExtElemInfo(desc *ExtensionDesc) *marshalElemInfo {
395 // get from cache first
396 u.RLock()
397 e, ok := u.extElems[desc.Field]
398 u.RUnlock()
399 if ok {
400 return e
401 }
402
403 t := reflect.TypeOf(desc.ExtensionType) // pointer or slice to basic type or struct
404 tags := strings.Split(desc.Tag, ",")
405 tag, err := strconv.Atoi(tags[1])
406 if err != nil {
407 panic("tag is not an integer")
408 }
409 wt := wiretype(tags[0])
410 sizer, marshaler := typeMarshaler(t, tags, false, false)
411 e = &marshalElemInfo{
412 wiretag: uint64(tag)<<3 | wt,
413 tagsize: SizeVarint(uint64(tag) << 3),
414 sizer: sizer,
415 marshaler: marshaler,
416 isptr: t.Kind() == reflect.Ptr,
417 }
418
419 // update cache
420 u.Lock()
421 if u.extElems == nil {
422 u.extElems = make(map[int32]*marshalElemInfo)
423 }
424 u.extElems[desc.Field] = e
425 u.Unlock()
426 return e
427}
428
429// computeMarshalFieldInfo fills up the information to marshal a field.
430func (fi *marshalFieldInfo) computeMarshalFieldInfo(f *reflect.StructField) {
431 // parse protobuf tag of the field.
432 // tag has format of "bytes,49,opt,name=foo,def=hello!"
433 tags := strings.Split(f.Tag.Get("protobuf"), ",")
434 if tags[0] == "" {
435 return
436 }
437 tag, err := strconv.Atoi(tags[1])
438 if err != nil {
439 panic("tag is not an integer")
440 }
441 wt := wiretype(tags[0])
442 if tags[2] == "req" {
443 fi.required = true
444 }
445 fi.setTag(f, tag, wt)
446 fi.setMarshaler(f, tags)
447}
448
449func (fi *marshalFieldInfo) computeOneofFieldInfo(f *reflect.StructField, oneofImplementers []interface{}) {
450 fi.field = toField(f)
451 fi.wiretag = 1<<31 - 1 // Use a large tag number, make oneofs sorted at the end. This tag will not appear on the wire.
452 fi.isPointer = true
453 fi.sizer, fi.marshaler = makeOneOfMarshaler(fi, f)
454 fi.oneofElems = make(map[reflect.Type]*marshalElemInfo)
455
456 ityp := f.Type // interface type
457 for _, o := range oneofImplementers {
458 t := reflect.TypeOf(o)
459 if !t.Implements(ityp) {
460 continue
461 }
462 sf := t.Elem().Field(0) // oneof implementer is a struct with a single field
463 tags := strings.Split(sf.Tag.Get("protobuf"), ",")
464 tag, err := strconv.Atoi(tags[1])
465 if err != nil {
466 panic("tag is not an integer")
467 }
468 wt := wiretype(tags[0])
469 sizer, marshaler := typeMarshaler(sf.Type, tags, false, true) // oneof should not omit any zero value
470 fi.oneofElems[t.Elem()] = &marshalElemInfo{
471 wiretag: uint64(tag)<<3 | wt,
472 tagsize: SizeVarint(uint64(tag) << 3),
473 sizer: sizer,
474 marshaler: marshaler,
475 }
476 }
477}
478
479type oneofMessage interface {
480 XXX_OneofFuncs() (func(Message, *Buffer) error, func(Message, int, int, *Buffer) (bool, error), func(Message) int, []interface{})
481}
482
483// wiretype returns the wire encoding of the type.
484func wiretype(encoding string) uint64 {
485 switch encoding {
486 case "fixed32":
487 return WireFixed32
488 case "fixed64":
489 return WireFixed64
490 case "varint", "zigzag32", "zigzag64":
491 return WireVarint
492 case "bytes":
493 return WireBytes
494 case "group":
495 return WireStartGroup
496 }
497 panic("unknown wire type " + encoding)
498}
499
500// setTag fills up the tag (in wire format) and its size in the info of a field.
501func (fi *marshalFieldInfo) setTag(f *reflect.StructField, tag int, wt uint64) {
502 fi.field = toField(f)
503 fi.wiretag = uint64(tag)<<3 | wt
504 fi.tagsize = SizeVarint(uint64(tag) << 3)
505}
506
507// setMarshaler fills up the sizer and marshaler in the info of a field.
508func (fi *marshalFieldInfo) setMarshaler(f *reflect.StructField, tags []string) {
509 switch f.Type.Kind() {
510 case reflect.Map:
511 // map field
512 fi.isPointer = true
513 fi.sizer, fi.marshaler = makeMapMarshaler(f)
514 return
515 case reflect.Ptr, reflect.Slice:
516 fi.isPointer = true
517 }
518 fi.sizer, fi.marshaler = typeMarshaler(f.Type, tags, true, false)
519}
520
521// typeMarshaler returns the sizer and marshaler of a given field.
522// t is the type of the field.
523// tags is the generated "protobuf" tag of the field.
524// If nozero is true, zero value is not marshaled to the wire.
525// If oneof is true, it is a oneof field.
526func typeMarshaler(t reflect.Type, tags []string, nozero, oneof bool) (sizer, marshaler) {
527 encoding := tags[0]
528
529 pointer := false
530 slice := false
531 if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
532 slice = true
533 t = t.Elem()
534 }
535 if t.Kind() == reflect.Ptr {
536 pointer = true
537 t = t.Elem()
538 }
539
540 packed := false
541 proto3 := false
542 validateUTF8 := true
543 for i := 2; i < len(tags); i++ {
544 if tags[i] == "packed" {
545 packed = true
546 }
547 if tags[i] == "proto3" {
548 proto3 = true
549 }
550 }
551 validateUTF8 = validateUTF8 && proto3
552
553 switch t.Kind() {
554 case reflect.Bool:
555 if pointer {
556 return sizeBoolPtr, appendBoolPtr
557 }
558 if slice {
559 if packed {
560 return sizeBoolPackedSlice, appendBoolPackedSlice
561 }
562 return sizeBoolSlice, appendBoolSlice
563 }
564 if nozero {
565 return sizeBoolValueNoZero, appendBoolValueNoZero
566 }
567 return sizeBoolValue, appendBoolValue
568 case reflect.Uint32:
569 switch encoding {
570 case "fixed32":
571 if pointer {
572 return sizeFixed32Ptr, appendFixed32Ptr
573 }
574 if slice {
575 if packed {
576 return sizeFixed32PackedSlice, appendFixed32PackedSlice
577 }
578 return sizeFixed32Slice, appendFixed32Slice
579 }
580 if nozero {
581 return sizeFixed32ValueNoZero, appendFixed32ValueNoZero
582 }
583 return sizeFixed32Value, appendFixed32Value
584 case "varint":
585 if pointer {
586 return sizeVarint32Ptr, appendVarint32Ptr
587 }
588 if slice {
589 if packed {
590 return sizeVarint32PackedSlice, appendVarint32PackedSlice
591 }
592 return sizeVarint32Slice, appendVarint32Slice
593 }
594 if nozero {
595 return sizeVarint32ValueNoZero, appendVarint32ValueNoZero
596 }
597 return sizeVarint32Value, appendVarint32Value
598 }
599 case reflect.Int32:
600 switch encoding {
601 case "fixed32":
602 if pointer {
603 return sizeFixedS32Ptr, appendFixedS32Ptr
604 }
605 if slice {
606 if packed {
607 return sizeFixedS32PackedSlice, appendFixedS32PackedSlice
608 }
609 return sizeFixedS32Slice, appendFixedS32Slice
610 }
611 if nozero {
612 return sizeFixedS32ValueNoZero, appendFixedS32ValueNoZero
613 }
614 return sizeFixedS32Value, appendFixedS32Value
615 case "varint":
616 if pointer {
617 return sizeVarintS32Ptr, appendVarintS32Ptr
618 }
619 if slice {
620 if packed {
621 return sizeVarintS32PackedSlice, appendVarintS32PackedSlice
622 }
623 return sizeVarintS32Slice, appendVarintS32Slice
624 }
625 if nozero {
626 return sizeVarintS32ValueNoZero, appendVarintS32ValueNoZero
627 }
628 return sizeVarintS32Value, appendVarintS32Value
629 case "zigzag32":
630 if pointer {
631 return sizeZigzag32Ptr, appendZigzag32Ptr
632 }
633 if slice {
634 if packed {
635 return sizeZigzag32PackedSlice, appendZigzag32PackedSlice
636 }
637 return sizeZigzag32Slice, appendZigzag32Slice
638 }
639 if nozero {
640 return sizeZigzag32ValueNoZero, appendZigzag32ValueNoZero
641 }
642 return sizeZigzag32Value, appendZigzag32Value
643 }
644 case reflect.Uint64:
645 switch encoding {
646 case "fixed64":
647 if pointer {
648 return sizeFixed64Ptr, appendFixed64Ptr
649 }
650 if slice {
651 if packed {
652 return sizeFixed64PackedSlice, appendFixed64PackedSlice
653 }
654 return sizeFixed64Slice, appendFixed64Slice
655 }
656 if nozero {
657 return sizeFixed64ValueNoZero, appendFixed64ValueNoZero
658 }
659 return sizeFixed64Value, appendFixed64Value
660 case "varint":
661 if pointer {
662 return sizeVarint64Ptr, appendVarint64Ptr
663 }
664 if slice {
665 if packed {
666 return sizeVarint64PackedSlice, appendVarint64PackedSlice
667 }
668 return sizeVarint64Slice, appendVarint64Slice
669 }
670 if nozero {
671 return sizeVarint64ValueNoZero, appendVarint64ValueNoZero
672 }
673 return sizeVarint64Value, appendVarint64Value
674 }
675 case reflect.Int64:
676 switch encoding {
677 case "fixed64":
678 if pointer {
679 return sizeFixedS64Ptr, appendFixedS64Ptr
680 }
681 if slice {
682 if packed {
683 return sizeFixedS64PackedSlice, appendFixedS64PackedSlice
684 }
685 return sizeFixedS64Slice, appendFixedS64Slice
686 }
687 if nozero {
688 return sizeFixedS64ValueNoZero, appendFixedS64ValueNoZero
689 }
690 return sizeFixedS64Value, appendFixedS64Value
691 case "varint":
692 if pointer {
693 return sizeVarintS64Ptr, appendVarintS64Ptr
694 }
695 if slice {
696 if packed {
697 return sizeVarintS64PackedSlice, appendVarintS64PackedSlice
698 }
699 return sizeVarintS64Slice, appendVarintS64Slice
700 }
701 if nozero {
702 return sizeVarintS64ValueNoZero, appendVarintS64ValueNoZero
703 }
704 return sizeVarintS64Value, appendVarintS64Value
705 case "zigzag64":
706 if pointer {
707 return sizeZigzag64Ptr, appendZigzag64Ptr
708 }
709 if slice {
710 if packed {
711 return sizeZigzag64PackedSlice, appendZigzag64PackedSlice
712 }
713 return sizeZigzag64Slice, appendZigzag64Slice
714 }
715 if nozero {
716 return sizeZigzag64ValueNoZero, appendZigzag64ValueNoZero
717 }
718 return sizeZigzag64Value, appendZigzag64Value
719 }
720 case reflect.Float32:
721 if pointer {
722 return sizeFloat32Ptr, appendFloat32Ptr
723 }
724 if slice {
725 if packed {
726 return sizeFloat32PackedSlice, appendFloat32PackedSlice
727 }
728 return sizeFloat32Slice, appendFloat32Slice
729 }
730 if nozero {
731 return sizeFloat32ValueNoZero, appendFloat32ValueNoZero
732 }
733 return sizeFloat32Value, appendFloat32Value
734 case reflect.Float64:
735 if pointer {
736 return sizeFloat64Ptr, appendFloat64Ptr
737 }
738 if slice {
739 if packed {
740 return sizeFloat64PackedSlice, appendFloat64PackedSlice
741 }
742 return sizeFloat64Slice, appendFloat64Slice
743 }
744 if nozero {
745 return sizeFloat64ValueNoZero, appendFloat64ValueNoZero
746 }
747 return sizeFloat64Value, appendFloat64Value
748 case reflect.String:
749 if validateUTF8 {
750 if pointer {
751 return sizeStringPtr, appendUTF8StringPtr
752 }
753 if slice {
754 return sizeStringSlice, appendUTF8StringSlice
755 }
756 if nozero {
757 return sizeStringValueNoZero, appendUTF8StringValueNoZero
758 }
759 return sizeStringValue, appendUTF8StringValue
760 }
761 if pointer {
762 return sizeStringPtr, appendStringPtr
763 }
764 if slice {
765 return sizeStringSlice, appendStringSlice
766 }
767 if nozero {
768 return sizeStringValueNoZero, appendStringValueNoZero
769 }
770 return sizeStringValue, appendStringValue
771 case reflect.Slice:
772 if slice {
773 return sizeBytesSlice, appendBytesSlice
774 }
775 if oneof {
776 // Oneof bytes field may also have "proto3" tag.
777 // We want to marshal it as a oneof field. Do this
778 // check before the proto3 check.
779 return sizeBytesOneof, appendBytesOneof
780 }
781 if proto3 {
782 return sizeBytes3, appendBytes3
783 }
784 return sizeBytes, appendBytes
785 case reflect.Struct:
786 switch encoding {
787 case "group":
788 if slice {
789 return makeGroupSliceMarshaler(getMarshalInfo(t))
790 }
791 return makeGroupMarshaler(getMarshalInfo(t))
792 case "bytes":
793 if slice {
794 return makeMessageSliceMarshaler(getMarshalInfo(t))
795 }
796 return makeMessageMarshaler(getMarshalInfo(t))
797 }
798 }
799 panic(fmt.Sprintf("unknown or mismatched type: type: %v, wire type: %v", t, encoding))
800}
801
802// Below are functions to size/marshal a specific type of a field.
803// They are stored in the field's info, and called by function pointers.
804// They have type sizer or marshaler.
805
806func sizeFixed32Value(_ pointer, tagsize int) int {
807 return 4 + tagsize
808}
809func sizeFixed32ValueNoZero(ptr pointer, tagsize int) int {
810 v := *ptr.toUint32()
811 if v == 0 {
812 return 0
813 }
814 return 4 + tagsize
815}
816func sizeFixed32Ptr(ptr pointer, tagsize int) int {
817 p := *ptr.toUint32Ptr()
818 if p == nil {
819 return 0
820 }
821 return 4 + tagsize
822}
823func sizeFixed32Slice(ptr pointer, tagsize int) int {
824 s := *ptr.toUint32Slice()
825 return (4 + tagsize) * len(s)
826}
827func sizeFixed32PackedSlice(ptr pointer, tagsize int) int {
828 s := *ptr.toUint32Slice()
829 if len(s) == 0 {
830 return 0
831 }
832 return 4*len(s) + SizeVarint(uint64(4*len(s))) + tagsize
833}
834func sizeFixedS32Value(_ pointer, tagsize int) int {
835 return 4 + tagsize
836}
837func sizeFixedS32ValueNoZero(ptr pointer, tagsize int) int {
838 v := *ptr.toInt32()
839 if v == 0 {
840 return 0
841 }
842 return 4 + tagsize
843}
844func sizeFixedS32Ptr(ptr pointer, tagsize int) int {
845 p := ptr.getInt32Ptr()
846 if p == nil {
847 return 0
848 }
849 return 4 + tagsize
850}
851func sizeFixedS32Slice(ptr pointer, tagsize int) int {
852 s := ptr.getInt32Slice()
853 return (4 + tagsize) * len(s)
854}
855func sizeFixedS32PackedSlice(ptr pointer, tagsize int) int {
856 s := ptr.getInt32Slice()
857 if len(s) == 0 {
858 return 0
859 }
860 return 4*len(s) + SizeVarint(uint64(4*len(s))) + tagsize
861}
862func sizeFloat32Value(_ pointer, tagsize int) int {
863 return 4 + tagsize
864}
865func sizeFloat32ValueNoZero(ptr pointer, tagsize int) int {
866 v := math.Float32bits(*ptr.toFloat32())
867 if v == 0 {
868 return 0
869 }
870 return 4 + tagsize
871}
872func sizeFloat32Ptr(ptr pointer, tagsize int) int {
873 p := *ptr.toFloat32Ptr()
874 if p == nil {
875 return 0
876 }
877 return 4 + tagsize
878}
879func sizeFloat32Slice(ptr pointer, tagsize int) int {
880 s := *ptr.toFloat32Slice()
881 return (4 + tagsize) * len(s)
882}
883func sizeFloat32PackedSlice(ptr pointer, tagsize int) int {
884 s := *ptr.toFloat32Slice()
885 if len(s) == 0 {
886 return 0
887 }
888 return 4*len(s) + SizeVarint(uint64(4*len(s))) + tagsize
889}
890func sizeFixed64Value(_ pointer, tagsize int) int {
891 return 8 + tagsize
892}
893func sizeFixed64ValueNoZero(ptr pointer, tagsize int) int {
894 v := *ptr.toUint64()
895 if v == 0 {
896 return 0
897 }
898 return 8 + tagsize
899}
900func sizeFixed64Ptr(ptr pointer, tagsize int) int {
901 p := *ptr.toUint64Ptr()
902 if p == nil {
903 return 0
904 }
905 return 8 + tagsize
906}
907func sizeFixed64Slice(ptr pointer, tagsize int) int {
908 s := *ptr.toUint64Slice()
909 return (8 + tagsize) * len(s)
910}
911func sizeFixed64PackedSlice(ptr pointer, tagsize int) int {
912 s := *ptr.toUint64Slice()
913 if len(s) == 0 {
914 return 0
915 }
916 return 8*len(s) + SizeVarint(uint64(8*len(s))) + tagsize
917}
918func sizeFixedS64Value(_ pointer, tagsize int) int {
919 return 8 + tagsize
920}
921func sizeFixedS64ValueNoZero(ptr pointer, tagsize int) int {
922 v := *ptr.toInt64()
923 if v == 0 {
924 return 0
925 }
926 return 8 + tagsize
927}
928func sizeFixedS64Ptr(ptr pointer, tagsize int) int {
929 p := *ptr.toInt64Ptr()
930 if p == nil {
931 return 0
932 }
933 return 8 + tagsize
934}
935func sizeFixedS64Slice(ptr pointer, tagsize int) int {
936 s := *ptr.toInt64Slice()
937 return (8 + tagsize) * len(s)
938}
939func sizeFixedS64PackedSlice(ptr pointer, tagsize int) int {
940 s := *ptr.toInt64Slice()
941 if len(s) == 0 {
942 return 0
943 }
944 return 8*len(s) + SizeVarint(uint64(8*len(s))) + tagsize
945}
946func sizeFloat64Value(_ pointer, tagsize int) int {
947 return 8 + tagsize
948}
949func sizeFloat64ValueNoZero(ptr pointer, tagsize int) int {
950 v := math.Float64bits(*ptr.toFloat64())
951 if v == 0 {
952 return 0
953 }
954 return 8 + tagsize
955}
956func sizeFloat64Ptr(ptr pointer, tagsize int) int {
957 p := *ptr.toFloat64Ptr()
958 if p == nil {
959 return 0
960 }
961 return 8 + tagsize
962}
963func sizeFloat64Slice(ptr pointer, tagsize int) int {
964 s := *ptr.toFloat64Slice()
965 return (8 + tagsize) * len(s)
966}
967func sizeFloat64PackedSlice(ptr pointer, tagsize int) int {
968 s := *ptr.toFloat64Slice()
969 if len(s) == 0 {
970 return 0
971 }
972 return 8*len(s) + SizeVarint(uint64(8*len(s))) + tagsize
973}
974func sizeVarint32Value(ptr pointer, tagsize int) int {
975 v := *ptr.toUint32()
976 return SizeVarint(uint64(v)) + tagsize
977}
978func sizeVarint32ValueNoZero(ptr pointer, tagsize int) int {
979 v := *ptr.toUint32()
980 if v == 0 {
981 return 0
982 }
983 return SizeVarint(uint64(v)) + tagsize
984}
985func sizeVarint32Ptr(ptr pointer, tagsize int) int {
986 p := *ptr.toUint32Ptr()
987 if p == nil {
988 return 0
989 }
990 return SizeVarint(uint64(*p)) + tagsize
991}
992func sizeVarint32Slice(ptr pointer, tagsize int) int {
993 s := *ptr.toUint32Slice()
994 n := 0
995 for _, v := range s {
996 n += SizeVarint(uint64(v)) + tagsize
997 }
998 return n
999}
1000func sizeVarint32PackedSlice(ptr pointer, tagsize int) int {
1001 s := *ptr.toUint32Slice()
1002 if len(s) == 0 {
1003 return 0
1004 }
1005 n := 0
1006 for _, v := range s {
1007 n += SizeVarint(uint64(v))
1008 }
1009 return n + SizeVarint(uint64(n)) + tagsize
1010}
1011func sizeVarintS32Value(ptr pointer, tagsize int) int {
1012 v := *ptr.toInt32()
1013 return SizeVarint(uint64(v)) + tagsize
1014}
1015func sizeVarintS32ValueNoZero(ptr pointer, tagsize int) int {
1016 v := *ptr.toInt32()
1017 if v == 0 {
1018 return 0
1019 }
1020 return SizeVarint(uint64(v)) + tagsize
1021}
1022func sizeVarintS32Ptr(ptr pointer, tagsize int) int {
1023 p := ptr.getInt32Ptr()
1024 if p == nil {
1025 return 0
1026 }
1027 return SizeVarint(uint64(*p)) + tagsize
1028}
1029func sizeVarintS32Slice(ptr pointer, tagsize int) int {
1030 s := ptr.getInt32Slice()
1031 n := 0
1032 for _, v := range s {
1033 n += SizeVarint(uint64(v)) + tagsize
1034 }
1035 return n
1036}
1037func sizeVarintS32PackedSlice(ptr pointer, tagsize int) int {
1038 s := ptr.getInt32Slice()
1039 if len(s) == 0 {
1040 return 0
1041 }
1042 n := 0
1043 for _, v := range s {
1044 n += SizeVarint(uint64(v))
1045 }
1046 return n + SizeVarint(uint64(n)) + tagsize
1047}
1048func sizeVarint64Value(ptr pointer, tagsize int) int {
1049 v := *ptr.toUint64()
1050 return SizeVarint(v) + tagsize
1051}
1052func sizeVarint64ValueNoZero(ptr pointer, tagsize int) int {
1053 v := *ptr.toUint64()
1054 if v == 0 {
1055 return 0
1056 }
1057 return SizeVarint(v) + tagsize
1058}
1059func sizeVarint64Ptr(ptr pointer, tagsize int) int {
1060 p := *ptr.toUint64Ptr()
1061 if p == nil {
1062 return 0
1063 }
1064 return SizeVarint(*p) + tagsize
1065}
1066func sizeVarint64Slice(ptr pointer, tagsize int) int {
1067 s := *ptr.toUint64Slice()
1068 n := 0
1069 for _, v := range s {
1070 n += SizeVarint(v) + tagsize
1071 }
1072 return n
1073}
1074func sizeVarint64PackedSlice(ptr pointer, tagsize int) int {
1075 s := *ptr.toUint64Slice()
1076 if len(s) == 0 {
1077 return 0
1078 }
1079 n := 0
1080 for _, v := range s {
1081 n += SizeVarint(v)
1082 }
1083 return n + SizeVarint(uint64(n)) + tagsize
1084}
1085func sizeVarintS64Value(ptr pointer, tagsize int) int {
1086 v := *ptr.toInt64()
1087 return SizeVarint(uint64(v)) + tagsize
1088}
1089func sizeVarintS64ValueNoZero(ptr pointer, tagsize int) int {
1090 v := *ptr.toInt64()
1091 if v == 0 {
1092 return 0
1093 }
1094 return SizeVarint(uint64(v)) + tagsize
1095}
1096func sizeVarintS64Ptr(ptr pointer, tagsize int) int {
1097 p := *ptr.toInt64Ptr()
1098 if p == nil {
1099 return 0
1100 }
1101 return SizeVarint(uint64(*p)) + tagsize
1102}
1103func sizeVarintS64Slice(ptr pointer, tagsize int) int {
1104 s := *ptr.toInt64Slice()
1105 n := 0
1106 for _, v := range s {
1107 n += SizeVarint(uint64(v)) + tagsize
1108 }
1109 return n
1110}
1111func sizeVarintS64PackedSlice(ptr pointer, tagsize int) int {
1112 s := *ptr.toInt64Slice()
1113 if len(s) == 0 {
1114 return 0
1115 }
1116 n := 0
1117 for _, v := range s {
1118 n += SizeVarint(uint64(v))
1119 }
1120 return n + SizeVarint(uint64(n)) + tagsize
1121}
1122func sizeZigzag32Value(ptr pointer, tagsize int) int {
1123 v := *ptr.toInt32()
1124 return SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
1125}
1126func sizeZigzag32ValueNoZero(ptr pointer, tagsize int) int {
1127 v := *ptr.toInt32()
1128 if v == 0 {
1129 return 0
1130 }
1131 return SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
1132}
1133func sizeZigzag32Ptr(ptr pointer, tagsize int) int {
1134 p := ptr.getInt32Ptr()
1135 if p == nil {
1136 return 0
1137 }
1138 v := *p
1139 return SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
1140}
1141func sizeZigzag32Slice(ptr pointer, tagsize int) int {
1142 s := ptr.getInt32Slice()
1143 n := 0
1144 for _, v := range s {
1145 n += SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
1146 }
1147 return n
1148}
1149func sizeZigzag32PackedSlice(ptr pointer, tagsize int) int {
1150 s := ptr.getInt32Slice()
1151 if len(s) == 0 {
1152 return 0
1153 }
1154 n := 0
1155 for _, v := range s {
1156 n += SizeVarint(uint64((uint32(v) << 1) ^ uint32((int32(v) >> 31))))
1157 }
1158 return n + SizeVarint(uint64(n)) + tagsize
1159}
1160func sizeZigzag64Value(ptr pointer, tagsize int) int {
1161 v := *ptr.toInt64()
1162 return SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
1163}
1164func sizeZigzag64ValueNoZero(ptr pointer, tagsize int) int {
1165 v := *ptr.toInt64()
1166 if v == 0 {
1167 return 0
1168 }
1169 return SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
1170}
1171func sizeZigzag64Ptr(ptr pointer, tagsize int) int {
1172 p := *ptr.toInt64Ptr()
1173 if p == nil {
1174 return 0
1175 }
1176 v := *p
1177 return SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
1178}
1179func sizeZigzag64Slice(ptr pointer, tagsize int) int {
1180 s := *ptr.toInt64Slice()
1181 n := 0
1182 for _, v := range s {
1183 n += SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
1184 }
1185 return n
1186}
1187func sizeZigzag64PackedSlice(ptr pointer, tagsize int) int {
1188 s := *ptr.toInt64Slice()
1189 if len(s) == 0 {
1190 return 0
1191 }
1192 n := 0
1193 for _, v := range s {
1194 n += SizeVarint(uint64(v<<1) ^ uint64((int64(v) >> 63)))
1195 }
1196 return n + SizeVarint(uint64(n)) + tagsize
1197}
1198func sizeBoolValue(_ pointer, tagsize int) int {
1199 return 1 + tagsize
1200}
1201func sizeBoolValueNoZero(ptr pointer, tagsize int) int {
1202 v := *ptr.toBool()
1203 if !v {
1204 return 0
1205 }
1206 return 1 + tagsize
1207}
1208func sizeBoolPtr(ptr pointer, tagsize int) int {
1209 p := *ptr.toBoolPtr()
1210 if p == nil {
1211 return 0
1212 }
1213 return 1 + tagsize
1214}
1215func sizeBoolSlice(ptr pointer, tagsize int) int {
1216 s := *ptr.toBoolSlice()
1217 return (1 + tagsize) * len(s)
1218}
1219func sizeBoolPackedSlice(ptr pointer, tagsize int) int {
1220 s := *ptr.toBoolSlice()
1221 if len(s) == 0 {
1222 return 0
1223 }
1224 return len(s) + SizeVarint(uint64(len(s))) + tagsize
1225}
1226func sizeStringValue(ptr pointer, tagsize int) int {
1227 v := *ptr.toString()
1228 return len(v) + SizeVarint(uint64(len(v))) + tagsize
1229}
1230func sizeStringValueNoZero(ptr pointer, tagsize int) int {
1231 v := *ptr.toString()
1232 if v == "" {
1233 return 0
1234 }
1235 return len(v) + SizeVarint(uint64(len(v))) + tagsize
1236}
1237func sizeStringPtr(ptr pointer, tagsize int) int {
1238 p := *ptr.toStringPtr()
1239 if p == nil {
1240 return 0
1241 }
1242 v := *p
1243 return len(v) + SizeVarint(uint64(len(v))) + tagsize
1244}
1245func sizeStringSlice(ptr pointer, tagsize int) int {
1246 s := *ptr.toStringSlice()
1247 n := 0
1248 for _, v := range s {
1249 n += len(v) + SizeVarint(uint64(len(v))) + tagsize
1250 }
1251 return n
1252}
1253func sizeBytes(ptr pointer, tagsize int) int {
1254 v := *ptr.toBytes()
1255 if v == nil {
1256 return 0
1257 }
1258 return len(v) + SizeVarint(uint64(len(v))) + tagsize
1259}
1260func sizeBytes3(ptr pointer, tagsize int) int {
1261 v := *ptr.toBytes()
1262 if len(v) == 0 {
1263 return 0
1264 }
1265 return len(v) + SizeVarint(uint64(len(v))) + tagsize
1266}
1267func sizeBytesOneof(ptr pointer, tagsize int) int {
1268 v := *ptr.toBytes()
1269 return len(v) + SizeVarint(uint64(len(v))) + tagsize
1270}
1271func sizeBytesSlice(ptr pointer, tagsize int) int {
1272 s := *ptr.toBytesSlice()
1273 n := 0
1274 for _, v := range s {
1275 n += len(v) + SizeVarint(uint64(len(v))) + tagsize
1276 }
1277 return n
1278}
1279
1280// appendFixed32 appends an encoded fixed32 to b.
1281func appendFixed32(b []byte, v uint32) []byte {
1282 b = append(b,
1283 byte(v),
1284 byte(v>>8),
1285 byte(v>>16),
1286 byte(v>>24))
1287 return b
1288}
1289
1290// appendFixed64 appends an encoded fixed64 to b.
1291func appendFixed64(b []byte, v uint64) []byte {
1292 b = append(b,
1293 byte(v),
1294 byte(v>>8),
1295 byte(v>>16),
1296 byte(v>>24),
1297 byte(v>>32),
1298 byte(v>>40),
1299 byte(v>>48),
1300 byte(v>>56))
1301 return b
1302}
1303
1304// appendVarint appends an encoded varint to b.
1305func appendVarint(b []byte, v uint64) []byte {
1306 // TODO: make 1-byte (maybe 2-byte) case inline-able, once we
1307 // have non-leaf inliner.
1308 switch {
1309 case v < 1<<7:
1310 b = append(b, byte(v))
1311 case v < 1<<14:
1312 b = append(b,
1313 byte(v&0x7f|0x80),
1314 byte(v>>7))
1315 case v < 1<<21:
1316 b = append(b,
1317 byte(v&0x7f|0x80),
1318 byte((v>>7)&0x7f|0x80),
1319 byte(v>>14))
1320 case v < 1<<28:
1321 b = append(b,
1322 byte(v&0x7f|0x80),
1323 byte((v>>7)&0x7f|0x80),
1324 byte((v>>14)&0x7f|0x80),
1325 byte(v>>21))
1326 case v < 1<<35:
1327 b = append(b,
1328 byte(v&0x7f|0x80),
1329 byte((v>>7)&0x7f|0x80),
1330 byte((v>>14)&0x7f|0x80),
1331 byte((v>>21)&0x7f|0x80),
1332 byte(v>>28))
1333 case v < 1<<42:
1334 b = append(b,
1335 byte(v&0x7f|0x80),
1336 byte((v>>7)&0x7f|0x80),
1337 byte((v>>14)&0x7f|0x80),
1338 byte((v>>21)&0x7f|0x80),
1339 byte((v>>28)&0x7f|0x80),
1340 byte(v>>35))
1341 case v < 1<<49:
1342 b = append(b,
1343 byte(v&0x7f|0x80),
1344 byte((v>>7)&0x7f|0x80),
1345 byte((v>>14)&0x7f|0x80),
1346 byte((v>>21)&0x7f|0x80),
1347 byte((v>>28)&0x7f|0x80),
1348 byte((v>>35)&0x7f|0x80),
1349 byte(v>>42))
1350 case v < 1<<56:
1351 b = append(b,
1352 byte(v&0x7f|0x80),
1353 byte((v>>7)&0x7f|0x80),
1354 byte((v>>14)&0x7f|0x80),
1355 byte((v>>21)&0x7f|0x80),
1356 byte((v>>28)&0x7f|0x80),
1357 byte((v>>35)&0x7f|0x80),
1358 byte((v>>42)&0x7f|0x80),
1359 byte(v>>49))
1360 case v < 1<<63:
1361 b = append(b,
1362 byte(v&0x7f|0x80),
1363 byte((v>>7)&0x7f|0x80),
1364 byte((v>>14)&0x7f|0x80),
1365 byte((v>>21)&0x7f|0x80),
1366 byte((v>>28)&0x7f|0x80),
1367 byte((v>>35)&0x7f|0x80),
1368 byte((v>>42)&0x7f|0x80),
1369 byte((v>>49)&0x7f|0x80),
1370 byte(v>>56))
1371 default:
1372 b = append(b,
1373 byte(v&0x7f|0x80),
1374 byte((v>>7)&0x7f|0x80),
1375 byte((v>>14)&0x7f|0x80),
1376 byte((v>>21)&0x7f|0x80),
1377 byte((v>>28)&0x7f|0x80),
1378 byte((v>>35)&0x7f|0x80),
1379 byte((v>>42)&0x7f|0x80),
1380 byte((v>>49)&0x7f|0x80),
1381 byte((v>>56)&0x7f|0x80),
1382 1)
1383 }
1384 return b
1385}
1386
1387func appendFixed32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1388 v := *ptr.toUint32()
1389 b = appendVarint(b, wiretag)
1390 b = appendFixed32(b, v)
1391 return b, nil
1392}
1393func appendFixed32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1394 v := *ptr.toUint32()
1395 if v == 0 {
1396 return b, nil
1397 }
1398 b = appendVarint(b, wiretag)
1399 b = appendFixed32(b, v)
1400 return b, nil
1401}
1402func appendFixed32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1403 p := *ptr.toUint32Ptr()
1404 if p == nil {
1405 return b, nil
1406 }
1407 b = appendVarint(b, wiretag)
1408 b = appendFixed32(b, *p)
1409 return b, nil
1410}
1411func appendFixed32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1412 s := *ptr.toUint32Slice()
1413 for _, v := range s {
1414 b = appendVarint(b, wiretag)
1415 b = appendFixed32(b, v)
1416 }
1417 return b, nil
1418}
1419func appendFixed32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1420 s := *ptr.toUint32Slice()
1421 if len(s) == 0 {
1422 return b, nil
1423 }
1424 b = appendVarint(b, wiretag&^7|WireBytes)
1425 b = appendVarint(b, uint64(4*len(s)))
1426 for _, v := range s {
1427 b = appendFixed32(b, v)
1428 }
1429 return b, nil
1430}
1431func appendFixedS32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1432 v := *ptr.toInt32()
1433 b = appendVarint(b, wiretag)
1434 b = appendFixed32(b, uint32(v))
1435 return b, nil
1436}
1437func appendFixedS32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1438 v := *ptr.toInt32()
1439 if v == 0 {
1440 return b, nil
1441 }
1442 b = appendVarint(b, wiretag)
1443 b = appendFixed32(b, uint32(v))
1444 return b, nil
1445}
1446func appendFixedS32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1447 p := ptr.getInt32Ptr()
1448 if p == nil {
1449 return b, nil
1450 }
1451 b = appendVarint(b, wiretag)
1452 b = appendFixed32(b, uint32(*p))
1453 return b, nil
1454}
1455func appendFixedS32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1456 s := ptr.getInt32Slice()
1457 for _, v := range s {
1458 b = appendVarint(b, wiretag)
1459 b = appendFixed32(b, uint32(v))
1460 }
1461 return b, nil
1462}
1463func appendFixedS32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1464 s := ptr.getInt32Slice()
1465 if len(s) == 0 {
1466 return b, nil
1467 }
1468 b = appendVarint(b, wiretag&^7|WireBytes)
1469 b = appendVarint(b, uint64(4*len(s)))
1470 for _, v := range s {
1471 b = appendFixed32(b, uint32(v))
1472 }
1473 return b, nil
1474}
1475func appendFloat32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1476 v := math.Float32bits(*ptr.toFloat32())
1477 b = appendVarint(b, wiretag)
1478 b = appendFixed32(b, v)
1479 return b, nil
1480}
1481func appendFloat32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1482 v := math.Float32bits(*ptr.toFloat32())
1483 if v == 0 {
1484 return b, nil
1485 }
1486 b = appendVarint(b, wiretag)
1487 b = appendFixed32(b, v)
1488 return b, nil
1489}
1490func appendFloat32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1491 p := *ptr.toFloat32Ptr()
1492 if p == nil {
1493 return b, nil
1494 }
1495 b = appendVarint(b, wiretag)
1496 b = appendFixed32(b, math.Float32bits(*p))
1497 return b, nil
1498}
1499func appendFloat32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1500 s := *ptr.toFloat32Slice()
1501 for _, v := range s {
1502 b = appendVarint(b, wiretag)
1503 b = appendFixed32(b, math.Float32bits(v))
1504 }
1505 return b, nil
1506}
1507func appendFloat32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1508 s := *ptr.toFloat32Slice()
1509 if len(s) == 0 {
1510 return b, nil
1511 }
1512 b = appendVarint(b, wiretag&^7|WireBytes)
1513 b = appendVarint(b, uint64(4*len(s)))
1514 for _, v := range s {
1515 b = appendFixed32(b, math.Float32bits(v))
1516 }
1517 return b, nil
1518}
1519func appendFixed64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1520 v := *ptr.toUint64()
1521 b = appendVarint(b, wiretag)
1522 b = appendFixed64(b, v)
1523 return b, nil
1524}
1525func appendFixed64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1526 v := *ptr.toUint64()
1527 if v == 0 {
1528 return b, nil
1529 }
1530 b = appendVarint(b, wiretag)
1531 b = appendFixed64(b, v)
1532 return b, nil
1533}
1534func appendFixed64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1535 p := *ptr.toUint64Ptr()
1536 if p == nil {
1537 return b, nil
1538 }
1539 b = appendVarint(b, wiretag)
1540 b = appendFixed64(b, *p)
1541 return b, nil
1542}
1543func appendFixed64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1544 s := *ptr.toUint64Slice()
1545 for _, v := range s {
1546 b = appendVarint(b, wiretag)
1547 b = appendFixed64(b, v)
1548 }
1549 return b, nil
1550}
1551func appendFixed64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1552 s := *ptr.toUint64Slice()
1553 if len(s) == 0 {
1554 return b, nil
1555 }
1556 b = appendVarint(b, wiretag&^7|WireBytes)
1557 b = appendVarint(b, uint64(8*len(s)))
1558 for _, v := range s {
1559 b = appendFixed64(b, v)
1560 }
1561 return b, nil
1562}
1563func appendFixedS64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1564 v := *ptr.toInt64()
1565 b = appendVarint(b, wiretag)
1566 b = appendFixed64(b, uint64(v))
1567 return b, nil
1568}
1569func appendFixedS64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1570 v := *ptr.toInt64()
1571 if v == 0 {
1572 return b, nil
1573 }
1574 b = appendVarint(b, wiretag)
1575 b = appendFixed64(b, uint64(v))
1576 return b, nil
1577}
1578func appendFixedS64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1579 p := *ptr.toInt64Ptr()
1580 if p == nil {
1581 return b, nil
1582 }
1583 b = appendVarint(b, wiretag)
1584 b = appendFixed64(b, uint64(*p))
1585 return b, nil
1586}
1587func appendFixedS64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1588 s := *ptr.toInt64Slice()
1589 for _, v := range s {
1590 b = appendVarint(b, wiretag)
1591 b = appendFixed64(b, uint64(v))
1592 }
1593 return b, nil
1594}
1595func appendFixedS64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1596 s := *ptr.toInt64Slice()
1597 if len(s) == 0 {
1598 return b, nil
1599 }
1600 b = appendVarint(b, wiretag&^7|WireBytes)
1601 b = appendVarint(b, uint64(8*len(s)))
1602 for _, v := range s {
1603 b = appendFixed64(b, uint64(v))
1604 }
1605 return b, nil
1606}
1607func appendFloat64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1608 v := math.Float64bits(*ptr.toFloat64())
1609 b = appendVarint(b, wiretag)
1610 b = appendFixed64(b, v)
1611 return b, nil
1612}
1613func appendFloat64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1614 v := math.Float64bits(*ptr.toFloat64())
1615 if v == 0 {
1616 return b, nil
1617 }
1618 b = appendVarint(b, wiretag)
1619 b = appendFixed64(b, v)
1620 return b, nil
1621}
1622func appendFloat64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1623 p := *ptr.toFloat64Ptr()
1624 if p == nil {
1625 return b, nil
1626 }
1627 b = appendVarint(b, wiretag)
1628 b = appendFixed64(b, math.Float64bits(*p))
1629 return b, nil
1630}
1631func appendFloat64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1632 s := *ptr.toFloat64Slice()
1633 for _, v := range s {
1634 b = appendVarint(b, wiretag)
1635 b = appendFixed64(b, math.Float64bits(v))
1636 }
1637 return b, nil
1638}
1639func appendFloat64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1640 s := *ptr.toFloat64Slice()
1641 if len(s) == 0 {
1642 return b, nil
1643 }
1644 b = appendVarint(b, wiretag&^7|WireBytes)
1645 b = appendVarint(b, uint64(8*len(s)))
1646 for _, v := range s {
1647 b = appendFixed64(b, math.Float64bits(v))
1648 }
1649 return b, nil
1650}
1651func appendVarint32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1652 v := *ptr.toUint32()
1653 b = appendVarint(b, wiretag)
1654 b = appendVarint(b, uint64(v))
1655 return b, nil
1656}
1657func appendVarint32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1658 v := *ptr.toUint32()
1659 if v == 0 {
1660 return b, nil
1661 }
1662 b = appendVarint(b, wiretag)
1663 b = appendVarint(b, uint64(v))
1664 return b, nil
1665}
1666func appendVarint32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1667 p := *ptr.toUint32Ptr()
1668 if p == nil {
1669 return b, nil
1670 }
1671 b = appendVarint(b, wiretag)
1672 b = appendVarint(b, uint64(*p))
1673 return b, nil
1674}
1675func appendVarint32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1676 s := *ptr.toUint32Slice()
1677 for _, v := range s {
1678 b = appendVarint(b, wiretag)
1679 b = appendVarint(b, uint64(v))
1680 }
1681 return b, nil
1682}
1683func appendVarint32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1684 s := *ptr.toUint32Slice()
1685 if len(s) == 0 {
1686 return b, nil
1687 }
1688 b = appendVarint(b, wiretag&^7|WireBytes)
1689 // compute size
1690 n := 0
1691 for _, v := range s {
1692 n += SizeVarint(uint64(v))
1693 }
1694 b = appendVarint(b, uint64(n))
1695 for _, v := range s {
1696 b = appendVarint(b, uint64(v))
1697 }
1698 return b, nil
1699}
1700func appendVarintS32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1701 v := *ptr.toInt32()
1702 b = appendVarint(b, wiretag)
1703 b = appendVarint(b, uint64(v))
1704 return b, nil
1705}
1706func appendVarintS32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1707 v := *ptr.toInt32()
1708 if v == 0 {
1709 return b, nil
1710 }
1711 b = appendVarint(b, wiretag)
1712 b = appendVarint(b, uint64(v))
1713 return b, nil
1714}
1715func appendVarintS32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1716 p := ptr.getInt32Ptr()
1717 if p == nil {
1718 return b, nil
1719 }
1720 b = appendVarint(b, wiretag)
1721 b = appendVarint(b, uint64(*p))
1722 return b, nil
1723}
1724func appendVarintS32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1725 s := ptr.getInt32Slice()
1726 for _, v := range s {
1727 b = appendVarint(b, wiretag)
1728 b = appendVarint(b, uint64(v))
1729 }
1730 return b, nil
1731}
1732func appendVarintS32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1733 s := ptr.getInt32Slice()
1734 if len(s) == 0 {
1735 return b, nil
1736 }
1737 b = appendVarint(b, wiretag&^7|WireBytes)
1738 // compute size
1739 n := 0
1740 for _, v := range s {
1741 n += SizeVarint(uint64(v))
1742 }
1743 b = appendVarint(b, uint64(n))
1744 for _, v := range s {
1745 b = appendVarint(b, uint64(v))
1746 }
1747 return b, nil
1748}
1749func appendVarint64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1750 v := *ptr.toUint64()
1751 b = appendVarint(b, wiretag)
1752 b = appendVarint(b, v)
1753 return b, nil
1754}
1755func appendVarint64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1756 v := *ptr.toUint64()
1757 if v == 0 {
1758 return b, nil
1759 }
1760 b = appendVarint(b, wiretag)
1761 b = appendVarint(b, v)
1762 return b, nil
1763}
1764func appendVarint64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1765 p := *ptr.toUint64Ptr()
1766 if p == nil {
1767 return b, nil
1768 }
1769 b = appendVarint(b, wiretag)
1770 b = appendVarint(b, *p)
1771 return b, nil
1772}
1773func appendVarint64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1774 s := *ptr.toUint64Slice()
1775 for _, v := range s {
1776 b = appendVarint(b, wiretag)
1777 b = appendVarint(b, v)
1778 }
1779 return b, nil
1780}
1781func appendVarint64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1782 s := *ptr.toUint64Slice()
1783 if len(s) == 0 {
1784 return b, nil
1785 }
1786 b = appendVarint(b, wiretag&^7|WireBytes)
1787 // compute size
1788 n := 0
1789 for _, v := range s {
1790 n += SizeVarint(v)
1791 }
1792 b = appendVarint(b, uint64(n))
1793 for _, v := range s {
1794 b = appendVarint(b, v)
1795 }
1796 return b, nil
1797}
1798func appendVarintS64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1799 v := *ptr.toInt64()
1800 b = appendVarint(b, wiretag)
1801 b = appendVarint(b, uint64(v))
1802 return b, nil
1803}
1804func appendVarintS64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1805 v := *ptr.toInt64()
1806 if v == 0 {
1807 return b, nil
1808 }
1809 b = appendVarint(b, wiretag)
1810 b = appendVarint(b, uint64(v))
1811 return b, nil
1812}
1813func appendVarintS64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1814 p := *ptr.toInt64Ptr()
1815 if p == nil {
1816 return b, nil
1817 }
1818 b = appendVarint(b, wiretag)
1819 b = appendVarint(b, uint64(*p))
1820 return b, nil
1821}
1822func appendVarintS64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1823 s := *ptr.toInt64Slice()
1824 for _, v := range s {
1825 b = appendVarint(b, wiretag)
1826 b = appendVarint(b, uint64(v))
1827 }
1828 return b, nil
1829}
1830func appendVarintS64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1831 s := *ptr.toInt64Slice()
1832 if len(s) == 0 {
1833 return b, nil
1834 }
1835 b = appendVarint(b, wiretag&^7|WireBytes)
1836 // compute size
1837 n := 0
1838 for _, v := range s {
1839 n += SizeVarint(uint64(v))
1840 }
1841 b = appendVarint(b, uint64(n))
1842 for _, v := range s {
1843 b = appendVarint(b, uint64(v))
1844 }
1845 return b, nil
1846}
1847func appendZigzag32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1848 v := *ptr.toInt32()
1849 b = appendVarint(b, wiretag)
1850 b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
1851 return b, nil
1852}
1853func appendZigzag32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1854 v := *ptr.toInt32()
1855 if v == 0 {
1856 return b, nil
1857 }
1858 b = appendVarint(b, wiretag)
1859 b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
1860 return b, nil
1861}
1862func appendZigzag32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1863 p := ptr.getInt32Ptr()
1864 if p == nil {
1865 return b, nil
1866 }
1867 b = appendVarint(b, wiretag)
1868 v := *p
1869 b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
1870 return b, nil
1871}
1872func appendZigzag32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1873 s := ptr.getInt32Slice()
1874 for _, v := range s {
1875 b = appendVarint(b, wiretag)
1876 b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
1877 }
1878 return b, nil
1879}
1880func appendZigzag32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1881 s := ptr.getInt32Slice()
1882 if len(s) == 0 {
1883 return b, nil
1884 }
1885 b = appendVarint(b, wiretag&^7|WireBytes)
1886 // compute size
1887 n := 0
1888 for _, v := range s {
1889 n += SizeVarint(uint64((uint32(v) << 1) ^ uint32((int32(v) >> 31))))
1890 }
1891 b = appendVarint(b, uint64(n))
1892 for _, v := range s {
1893 b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
1894 }
1895 return b, nil
1896}
1897func appendZigzag64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1898 v := *ptr.toInt64()
1899 b = appendVarint(b, wiretag)
1900 b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
1901 return b, nil
1902}
1903func appendZigzag64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1904 v := *ptr.toInt64()
1905 if v == 0 {
1906 return b, nil
1907 }
1908 b = appendVarint(b, wiretag)
1909 b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
1910 return b, nil
1911}
1912func appendZigzag64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1913 p := *ptr.toInt64Ptr()
1914 if p == nil {
1915 return b, nil
1916 }
1917 b = appendVarint(b, wiretag)
1918 v := *p
1919 b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
1920 return b, nil
1921}
1922func appendZigzag64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1923 s := *ptr.toInt64Slice()
1924 for _, v := range s {
1925 b = appendVarint(b, wiretag)
1926 b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
1927 }
1928 return b, nil
1929}
1930func appendZigzag64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1931 s := *ptr.toInt64Slice()
1932 if len(s) == 0 {
1933 return b, nil
1934 }
1935 b = appendVarint(b, wiretag&^7|WireBytes)
1936 // compute size
1937 n := 0
1938 for _, v := range s {
1939 n += SizeVarint(uint64(v<<1) ^ uint64((int64(v) >> 63)))
1940 }
1941 b = appendVarint(b, uint64(n))
1942 for _, v := range s {
1943 b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
1944 }
1945 return b, nil
1946}
1947func appendBoolValue(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1948 v := *ptr.toBool()
1949 b = appendVarint(b, wiretag)
1950 if v {
1951 b = append(b, 1)
1952 } else {
1953 b = append(b, 0)
1954 }
1955 return b, nil
1956}
1957func appendBoolValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1958 v := *ptr.toBool()
1959 if !v {
1960 return b, nil
1961 }
1962 b = appendVarint(b, wiretag)
1963 b = append(b, 1)
1964 return b, nil
1965}
1966
1967func appendBoolPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1968 p := *ptr.toBoolPtr()
1969 if p == nil {
1970 return b, nil
1971 }
1972 b = appendVarint(b, wiretag)
1973 if *p {
1974 b = append(b, 1)
1975 } else {
1976 b = append(b, 0)
1977 }
1978 return b, nil
1979}
1980func appendBoolSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1981 s := *ptr.toBoolSlice()
1982 for _, v := range s {
1983 b = appendVarint(b, wiretag)
1984 if v {
1985 b = append(b, 1)
1986 } else {
1987 b = append(b, 0)
1988 }
1989 }
1990 return b, nil
1991}
1992func appendBoolPackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1993 s := *ptr.toBoolSlice()
1994 if len(s) == 0 {
1995 return b, nil
1996 }
1997 b = appendVarint(b, wiretag&^7|WireBytes)
1998 b = appendVarint(b, uint64(len(s)))
1999 for _, v := range s {
2000 if v {
2001 b = append(b, 1)
2002 } else {
2003 b = append(b, 0)
2004 }
2005 }
2006 return b, nil
2007}
2008func appendStringValue(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2009 v := *ptr.toString()
2010 b = appendVarint(b, wiretag)
2011 b = appendVarint(b, uint64(len(v)))
2012 b = append(b, v...)
2013 return b, nil
2014}
2015func appendStringValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2016 v := *ptr.toString()
2017 if v == "" {
2018 return b, nil
2019 }
2020 b = appendVarint(b, wiretag)
2021 b = appendVarint(b, uint64(len(v)))
2022 b = append(b, v...)
2023 return b, nil
2024}
2025func appendStringPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2026 p := *ptr.toStringPtr()
2027 if p == nil {
2028 return b, nil
2029 }
2030 v := *p
2031 b = appendVarint(b, wiretag)
2032 b = appendVarint(b, uint64(len(v)))
2033 b = append(b, v...)
2034 return b, nil
2035}
2036func appendStringSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2037 s := *ptr.toStringSlice()
2038 for _, v := range s {
2039 b = appendVarint(b, wiretag)
2040 b = appendVarint(b, uint64(len(v)))
2041 b = append(b, v...)
2042 }
2043 return b, nil
2044}
2045func appendUTF8StringValue(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2046 var invalidUTF8 bool
2047 v := *ptr.toString()
2048 if !utf8.ValidString(v) {
2049 invalidUTF8 = true
2050 }
2051 b = appendVarint(b, wiretag)
2052 b = appendVarint(b, uint64(len(v)))
2053 b = append(b, v...)
2054 if invalidUTF8 {
2055 return b, errInvalidUTF8
2056 }
2057 return b, nil
2058}
2059func appendUTF8StringValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2060 var invalidUTF8 bool
2061 v := *ptr.toString()
2062 if v == "" {
2063 return b, nil
2064 }
2065 if !utf8.ValidString(v) {
2066 invalidUTF8 = true
2067 }
2068 b = appendVarint(b, wiretag)
2069 b = appendVarint(b, uint64(len(v)))
2070 b = append(b, v...)
2071 if invalidUTF8 {
2072 return b, errInvalidUTF8
2073 }
2074 return b, nil
2075}
2076func appendUTF8StringPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2077 var invalidUTF8 bool
2078 p := *ptr.toStringPtr()
2079 if p == nil {
2080 return b, nil
2081 }
2082 v := *p
2083 if !utf8.ValidString(v) {
2084 invalidUTF8 = true
2085 }
2086 b = appendVarint(b, wiretag)
2087 b = appendVarint(b, uint64(len(v)))
2088 b = append(b, v...)
2089 if invalidUTF8 {
2090 return b, errInvalidUTF8
2091 }
2092 return b, nil
2093}
2094func appendUTF8StringSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2095 var invalidUTF8 bool
2096 s := *ptr.toStringSlice()
2097 for _, v := range s {
2098 if !utf8.ValidString(v) {
2099 invalidUTF8 = true
2100 }
2101 b = appendVarint(b, wiretag)
2102 b = appendVarint(b, uint64(len(v)))
2103 b = append(b, v...)
2104 }
2105 if invalidUTF8 {
2106 return b, errInvalidUTF8
2107 }
2108 return b, nil
2109}
2110func appendBytes(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2111 v := *ptr.toBytes()
2112 if v == nil {
2113 return b, nil
2114 }
2115 b = appendVarint(b, wiretag)
2116 b = appendVarint(b, uint64(len(v)))
2117 b = append(b, v...)
2118 return b, nil
2119}
2120func appendBytes3(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2121 v := *ptr.toBytes()
2122 if len(v) == 0 {
2123 return b, nil
2124 }
2125 b = appendVarint(b, wiretag)
2126 b = appendVarint(b, uint64(len(v)))
2127 b = append(b, v...)
2128 return b, nil
2129}
2130func appendBytesOneof(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2131 v := *ptr.toBytes()
2132 b = appendVarint(b, wiretag)
2133 b = appendVarint(b, uint64(len(v)))
2134 b = append(b, v...)
2135 return b, nil
2136}
2137func appendBytesSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2138 s := *ptr.toBytesSlice()
2139 for _, v := range s {
2140 b = appendVarint(b, wiretag)
2141 b = appendVarint(b, uint64(len(v)))
2142 b = append(b, v...)
2143 }
2144 return b, nil
2145}
2146
2147// makeGroupMarshaler returns the sizer and marshaler for a group.
2148// u is the marshal info of the underlying message.
2149func makeGroupMarshaler(u *marshalInfo) (sizer, marshaler) {
2150 return func(ptr pointer, tagsize int) int {
2151 p := ptr.getPointer()
2152 if p.isNil() {
2153 return 0
2154 }
2155 return u.size(p) + 2*tagsize
2156 },
2157 func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
2158 p := ptr.getPointer()
2159 if p.isNil() {
2160 return b, nil
2161 }
2162 var err error
2163 b = appendVarint(b, wiretag) // start group
2164 b, err = u.marshal(b, p, deterministic)
2165 b = appendVarint(b, wiretag+(WireEndGroup-WireStartGroup)) // end group
2166 return b, err
2167 }
2168}
2169
2170// makeGroupSliceMarshaler returns the sizer and marshaler for a group slice.
2171// u is the marshal info of the underlying message.
2172func makeGroupSliceMarshaler(u *marshalInfo) (sizer, marshaler) {
2173 return func(ptr pointer, tagsize int) int {
2174 s := ptr.getPointerSlice()
2175 n := 0
2176 for _, v := range s {
2177 if v.isNil() {
2178 continue
2179 }
2180 n += u.size(v) + 2*tagsize
2181 }
2182 return n
2183 },
2184 func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
2185 s := ptr.getPointerSlice()
2186 var err error
2187 var nerr nonFatal
2188 for _, v := range s {
2189 if v.isNil() {
2190 return b, errRepeatedHasNil
2191 }
2192 b = appendVarint(b, wiretag) // start group
2193 b, err = u.marshal(b, v, deterministic)
2194 b = appendVarint(b, wiretag+(WireEndGroup-WireStartGroup)) // end group
2195 if !nerr.Merge(err) {
2196 if err == ErrNil {
2197 err = errRepeatedHasNil
2198 }
2199 return b, err
2200 }
2201 }
2202 return b, nerr.E
2203 }
2204}
2205
2206// makeMessageMarshaler returns the sizer and marshaler for a message field.
2207// u is the marshal info of the message.
2208func makeMessageMarshaler(u *marshalInfo) (sizer, marshaler) {
2209 return func(ptr pointer, tagsize int) int {
2210 p := ptr.getPointer()
2211 if p.isNil() {
2212 return 0
2213 }
2214 siz := u.size(p)
2215 return siz + SizeVarint(uint64(siz)) + tagsize
2216 },
2217 func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
2218 p := ptr.getPointer()
2219 if p.isNil() {
2220 return b, nil
2221 }
2222 b = appendVarint(b, wiretag)
2223 siz := u.cachedsize(p)
2224 b = appendVarint(b, uint64(siz))
2225 return u.marshal(b, p, deterministic)
2226 }
2227}
2228
2229// makeMessageSliceMarshaler returns the sizer and marshaler for a message slice.
2230// u is the marshal info of the message.
2231func makeMessageSliceMarshaler(u *marshalInfo) (sizer, marshaler) {
2232 return func(ptr pointer, tagsize int) int {
2233 s := ptr.getPointerSlice()
2234 n := 0
2235 for _, v := range s {
2236 if v.isNil() {
2237 continue
2238 }
2239 siz := u.size(v)
2240 n += siz + SizeVarint(uint64(siz)) + tagsize
2241 }
2242 return n
2243 },
2244 func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
2245 s := ptr.getPointerSlice()
2246 var err error
2247 var nerr nonFatal
2248 for _, v := range s {
2249 if v.isNil() {
2250 return b, errRepeatedHasNil
2251 }
2252 b = appendVarint(b, wiretag)
2253 siz := u.cachedsize(v)
2254 b = appendVarint(b, uint64(siz))
2255 b, err = u.marshal(b, v, deterministic)
2256
2257 if !nerr.Merge(err) {
2258 if err == ErrNil {
2259 err = errRepeatedHasNil
2260 }
2261 return b, err
2262 }
2263 }
2264 return b, nerr.E
2265 }
2266}
2267
2268// makeMapMarshaler returns the sizer and marshaler for a map field.
2269// f is the pointer to the reflect data structure of the field.
2270func makeMapMarshaler(f *reflect.StructField) (sizer, marshaler) {
2271 // figure out key and value type
2272 t := f.Type
2273 keyType := t.Key()
2274 valType := t.Elem()
2275 keyTags := strings.Split(f.Tag.Get("protobuf_key"), ",")
2276 valTags := strings.Split(f.Tag.Get("protobuf_val"), ",")
2277 keySizer, keyMarshaler := typeMarshaler(keyType, keyTags, false, false) // don't omit zero value in map
2278 valSizer, valMarshaler := typeMarshaler(valType, valTags, false, false) // don't omit zero value in map
2279 keyWireTag := 1<<3 | wiretype(keyTags[0])
2280 valWireTag := 2<<3 | wiretype(valTags[0])
2281
2282 // We create an interface to get the addresses of the map key and value.
2283 // If value is pointer-typed, the interface is a direct interface, the
2284 // idata itself is the value. Otherwise, the idata is the pointer to the
2285 // value.
2286 // Key cannot be pointer-typed.
2287 valIsPtr := valType.Kind() == reflect.Ptr
2288
2289 // If value is a message with nested maps, calling
2290 // valSizer in marshal may be quadratic. We should use
2291 // cached version in marshal (but not in size).
2292 // If value is not message type, we don't have size cache,
2293 // but it cannot be nested either. Just use valSizer.
2294 valCachedSizer := valSizer
2295 if valIsPtr && valType.Elem().Kind() == reflect.Struct {
2296 u := getMarshalInfo(valType.Elem())
2297 valCachedSizer = func(ptr pointer, tagsize int) int {
2298 // Same as message sizer, but use cache.
2299 p := ptr.getPointer()
2300 if p.isNil() {
2301 return 0
2302 }
2303 siz := u.cachedsize(p)
2304 return siz + SizeVarint(uint64(siz)) + tagsize
2305 }
2306 }
2307 return func(ptr pointer, tagsize int) int {
2308 m := ptr.asPointerTo(t).Elem() // the map
2309 n := 0
2310 for _, k := range m.MapKeys() {
2311 ki := k.Interface()
2312 vi := m.MapIndex(k).Interface()
2313 kaddr := toAddrPointer(&ki, false) // pointer to key
2314 vaddr := toAddrPointer(&vi, valIsPtr) // pointer to value
2315 siz := keySizer(kaddr, 1) + valSizer(vaddr, 1) // tag of key = 1 (size=1), tag of val = 2 (size=1)
2316 n += siz + SizeVarint(uint64(siz)) + tagsize
2317 }
2318 return n
2319 },
2320 func(b []byte, ptr pointer, tag uint64, deterministic bool) ([]byte, error) {
2321 m := ptr.asPointerTo(t).Elem() // the map
2322 var err error
2323 keys := m.MapKeys()
2324 if len(keys) > 1 && deterministic {
2325 sort.Sort(mapKeys(keys))
2326 }
2327
2328 var nerr nonFatal
2329 for _, k := range keys {
2330 ki := k.Interface()
2331 vi := m.MapIndex(k).Interface()
2332 kaddr := toAddrPointer(&ki, false) // pointer to key
2333 vaddr := toAddrPointer(&vi, valIsPtr) // pointer to value
2334 b = appendVarint(b, tag)
2335 siz := keySizer(kaddr, 1) + valCachedSizer(vaddr, 1) // tag of key = 1 (size=1), tag of val = 2 (size=1)
2336 b = appendVarint(b, uint64(siz))
2337 b, err = keyMarshaler(b, kaddr, keyWireTag, deterministic)
2338 if !nerr.Merge(err) {
2339 return b, err
2340 }
2341 b, err = valMarshaler(b, vaddr, valWireTag, deterministic)
2342 if err != ErrNil && !nerr.Merge(err) { // allow nil value in map
2343 return b, err
2344 }
2345 }
2346 return b, nerr.E
2347 }
2348}
2349
2350// makeOneOfMarshaler returns the sizer and marshaler for a oneof field.
2351// fi is the marshal info of the field.
2352// f is the pointer to the reflect data structure of the field.
2353func makeOneOfMarshaler(fi *marshalFieldInfo, f *reflect.StructField) (sizer, marshaler) {
2354 // Oneof field is an interface. We need to get the actual data type on the fly.
2355 t := f.Type
2356 return func(ptr pointer, _ int) int {
2357 p := ptr.getInterfacePointer()
2358 if p.isNil() {
2359 return 0
2360 }
2361 v := ptr.asPointerTo(t).Elem().Elem().Elem() // *interface -> interface -> *struct -> struct
2362 telem := v.Type()
2363 e := fi.oneofElems[telem]
2364 return e.sizer(p, e.tagsize)
2365 },
2366 func(b []byte, ptr pointer, _ uint64, deterministic bool) ([]byte, error) {
2367 p := ptr.getInterfacePointer()
2368 if p.isNil() {
2369 return b, nil
2370 }
2371 v := ptr.asPointerTo(t).Elem().Elem().Elem() // *interface -> interface -> *struct -> struct
2372 telem := v.Type()
2373 if telem.Field(0).Type.Kind() == reflect.Ptr && p.getPointer().isNil() {
2374 return b, errOneofHasNil
2375 }
2376 e := fi.oneofElems[telem]
2377 return e.marshaler(b, p, e.wiretag, deterministic)
2378 }
2379}
2380
2381// sizeExtensions computes the size of encoded data for a XXX_InternalExtensions field.
2382func (u *marshalInfo) sizeExtensions(ext *XXX_InternalExtensions) int {
2383 m, mu := ext.extensionsRead()
2384 if m == nil {
2385 return 0
2386 }
2387 mu.Lock()
2388
2389 n := 0
2390 for _, e := range m {
2391 if e.value == nil || e.desc == nil {
2392 // Extension is only in its encoded form.
2393 n += len(e.enc)
2394 continue
2395 }
2396
2397 // We don't skip extensions that have an encoded form set,
2398 // because the extension value may have been mutated after
2399 // the last time this function was called.
2400 ei := u.getExtElemInfo(e.desc)
2401 v := e.value
2402 p := toAddrPointer(&v, ei.isptr)
2403 n += ei.sizer(p, ei.tagsize)
2404 }
2405 mu.Unlock()
2406 return n
2407}
2408
2409// appendExtensions marshals a XXX_InternalExtensions field to the end of byte slice b.
2410func (u *marshalInfo) appendExtensions(b []byte, ext *XXX_InternalExtensions, deterministic bool) ([]byte, error) {
2411 m, mu := ext.extensionsRead()
2412 if m == nil {
2413 return b, nil
2414 }
2415 mu.Lock()
2416 defer mu.Unlock()
2417
2418 var err error
2419 var nerr nonFatal
2420
2421 // Fast-path for common cases: zero or one extensions.
2422 // Don't bother sorting the keys.
2423 if len(m) <= 1 {
2424 for _, e := range m {
2425 if e.value == nil || e.desc == nil {
2426 // Extension is only in its encoded form.
2427 b = append(b, e.enc...)
2428 continue
2429 }
2430
2431 // We don't skip extensions that have an encoded form set,
2432 // because the extension value may have been mutated after
2433 // the last time this function was called.
2434
2435 ei := u.getExtElemInfo(e.desc)
2436 v := e.value
2437 p := toAddrPointer(&v, ei.isptr)
2438 b, err = ei.marshaler(b, p, ei.wiretag, deterministic)
2439 if !nerr.Merge(err) {
2440 return b, err
2441 }
2442 }
2443 return b, nerr.E
2444 }
2445
2446 // Sort the keys to provide a deterministic encoding.
2447 // Not sure this is required, but the old code does it.
2448 keys := make([]int, 0, len(m))
2449 for k := range m {
2450 keys = append(keys, int(k))
2451 }
2452 sort.Ints(keys)
2453
2454 for _, k := range keys {
2455 e := m[int32(k)]
2456 if e.value == nil || e.desc == nil {
2457 // Extension is only in its encoded form.
2458 b = append(b, e.enc...)
2459 continue
2460 }
2461
2462 // We don't skip extensions that have an encoded form set,
2463 // because the extension value may have been mutated after
2464 // the last time this function was called.
2465
2466 ei := u.getExtElemInfo(e.desc)
2467 v := e.value
2468 p := toAddrPointer(&v, ei.isptr)
2469 b, err = ei.marshaler(b, p, ei.wiretag, deterministic)
2470 if !nerr.Merge(err) {
2471 return b, err
2472 }
2473 }
2474 return b, nerr.E
2475}
2476
2477// message set format is:
2478// message MessageSet {
2479// repeated group Item = 1 {
2480// required int32 type_id = 2;
2481// required string message = 3;
2482// };
2483// }
2484
2485// sizeMessageSet computes the size of encoded data for a XXX_InternalExtensions field
2486// in message set format (above).
2487func (u *marshalInfo) sizeMessageSet(ext *XXX_InternalExtensions) int {
2488 m, mu := ext.extensionsRead()
2489 if m == nil {
2490 return 0
2491 }
2492 mu.Lock()
2493
2494 n := 0
2495 for id, e := range m {
2496 n += 2 // start group, end group. tag = 1 (size=1)
2497 n += SizeVarint(uint64(id)) + 1 // type_id, tag = 2 (size=1)
2498
2499 if e.value == nil || e.desc == nil {
2500 // Extension is only in its encoded form.
2501 msgWithLen := skipVarint(e.enc) // skip old tag, but leave the length varint
2502 siz := len(msgWithLen)
2503 n += siz + 1 // message, tag = 3 (size=1)
2504 continue
2505 }
2506
2507 // We don't skip extensions that have an encoded form set,
2508 // because the extension value may have been mutated after
2509 // the last time this function was called.
2510
2511 ei := u.getExtElemInfo(e.desc)
2512 v := e.value
2513 p := toAddrPointer(&v, ei.isptr)
2514 n += ei.sizer(p, 1) // message, tag = 3 (size=1)
2515 }
2516 mu.Unlock()
2517 return n
2518}
2519
2520// appendMessageSet marshals a XXX_InternalExtensions field in message set format (above)
2521// to the end of byte slice b.
2522func (u *marshalInfo) appendMessageSet(b []byte, ext *XXX_InternalExtensions, deterministic bool) ([]byte, error) {
2523 m, mu := ext.extensionsRead()
2524 if m == nil {
2525 return b, nil
2526 }
2527 mu.Lock()
2528 defer mu.Unlock()
2529
2530 var err error
2531 var nerr nonFatal
2532
2533 // Fast-path for common cases: zero or one extensions.
2534 // Don't bother sorting the keys.
2535 if len(m) <= 1 {
2536 for id, e := range m {
2537 b = append(b, 1<<3|WireStartGroup)
2538 b = append(b, 2<<3|WireVarint)
2539 b = appendVarint(b, uint64(id))
2540
2541 if e.value == nil || e.desc == nil {
2542 // Extension is only in its encoded form.
2543 msgWithLen := skipVarint(e.enc) // skip old tag, but leave the length varint
2544 b = append(b, 3<<3|WireBytes)
2545 b = append(b, msgWithLen...)
2546 b = append(b, 1<<3|WireEndGroup)
2547 continue
2548 }
2549
2550 // We don't skip extensions that have an encoded form set,
2551 // because the extension value may have been mutated after
2552 // the last time this function was called.
2553
2554 ei := u.getExtElemInfo(e.desc)
2555 v := e.value
2556 p := toAddrPointer(&v, ei.isptr)
2557 b, err = ei.marshaler(b, p, 3<<3|WireBytes, deterministic)
2558 if !nerr.Merge(err) {
2559 return b, err
2560 }
2561 b = append(b, 1<<3|WireEndGroup)
2562 }
2563 return b, nerr.E
2564 }
2565
2566 // Sort the keys to provide a deterministic encoding.
2567 keys := make([]int, 0, len(m))
2568 for k := range m {
2569 keys = append(keys, int(k))
2570 }
2571 sort.Ints(keys)
2572
2573 for _, id := range keys {
2574 e := m[int32(id)]
2575 b = append(b, 1<<3|WireStartGroup)
2576 b = append(b, 2<<3|WireVarint)
2577 b = appendVarint(b, uint64(id))
2578
2579 if e.value == nil || e.desc == nil {
2580 // Extension is only in its encoded form.
2581 msgWithLen := skipVarint(e.enc) // skip old tag, but leave the length varint
2582 b = append(b, 3<<3|WireBytes)
2583 b = append(b, msgWithLen...)
2584 b = append(b, 1<<3|WireEndGroup)
2585 continue
2586 }
2587
2588 // We don't skip extensions that have an encoded form set,
2589 // because the extension value may have been mutated after
2590 // the last time this function was called.
2591
2592 ei := u.getExtElemInfo(e.desc)
2593 v := e.value
2594 p := toAddrPointer(&v, ei.isptr)
2595 b, err = ei.marshaler(b, p, 3<<3|WireBytes, deterministic)
2596 b = append(b, 1<<3|WireEndGroup)
2597 if !nerr.Merge(err) {
2598 return b, err
2599 }
2600 }
2601 return b, nerr.E
2602}
2603
2604// sizeV1Extensions computes the size of encoded data for a V1-API extension field.
2605func (u *marshalInfo) sizeV1Extensions(m map[int32]Extension) int {
2606 if m == nil {
2607 return 0
2608 }
2609
2610 n := 0
2611 for _, e := range m {
2612 if e.value == nil || e.desc == nil {
2613 // Extension is only in its encoded form.
2614 n += len(e.enc)
2615 continue
2616 }
2617
2618 // We don't skip extensions that have an encoded form set,
2619 // because the extension value may have been mutated after
2620 // the last time this function was called.
2621
2622 ei := u.getExtElemInfo(e.desc)
2623 v := e.value
2624 p := toAddrPointer(&v, ei.isptr)
2625 n += ei.sizer(p, ei.tagsize)
2626 }
2627 return n
2628}
2629
2630// appendV1Extensions marshals a V1-API extension field to the end of byte slice b.
2631func (u *marshalInfo) appendV1Extensions(b []byte, m map[int32]Extension, deterministic bool) ([]byte, error) {
2632 if m == nil {
2633 return b, nil
2634 }
2635
2636 // Sort the keys to provide a deterministic encoding.
2637 keys := make([]int, 0, len(m))
2638 for k := range m {
2639 keys = append(keys, int(k))
2640 }
2641 sort.Ints(keys)
2642
2643 var err error
2644 var nerr nonFatal
2645 for _, k := range keys {
2646 e := m[int32(k)]
2647 if e.value == nil || e.desc == nil {
2648 // Extension is only in its encoded form.
2649 b = append(b, e.enc...)
2650 continue
2651 }
2652
2653 // We don't skip extensions that have an encoded form set,
2654 // because the extension value may have been mutated after
2655 // the last time this function was called.
2656
2657 ei := u.getExtElemInfo(e.desc)
2658 v := e.value
2659 p := toAddrPointer(&v, ei.isptr)
2660 b, err = ei.marshaler(b, p, ei.wiretag, deterministic)
2661 if !nerr.Merge(err) {
2662 return b, err
2663 }
2664 }
2665 return b, nerr.E
2666}
2667
2668// newMarshaler is the interface representing objects that can marshal themselves.
2669//
2670// This exists to support protoc-gen-go generated messages.
2671// The proto package will stop type-asserting to this interface in the future.
2672//
2673// DO NOT DEPEND ON THIS.
2674type newMarshaler interface {
2675 XXX_Size() int
2676 XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
2677}
2678
2679// Size returns the encoded size of a protocol buffer message.
2680// This is the main entry point.
2681func Size(pb Message) int {
2682 if m, ok := pb.(newMarshaler); ok {
2683 return m.XXX_Size()
2684 }
2685 if m, ok := pb.(Marshaler); ok {
2686 // If the message can marshal itself, let it do it, for compatibility.
2687 // NOTE: This is not efficient.
2688 b, _ := m.Marshal()
2689 return len(b)
2690 }
2691 // in case somehow we didn't generate the wrapper
2692 if pb == nil {
2693 return 0
2694 }
2695 var info InternalMessageInfo
2696 return info.Size(pb)
2697}
2698
2699// Marshal takes a protocol buffer message
2700// and encodes it into the wire format, returning the data.
2701// This is the main entry point.
2702func Marshal(pb Message) ([]byte, error) {
2703 if m, ok := pb.(newMarshaler); ok {
2704 siz := m.XXX_Size()
2705 b := make([]byte, 0, siz)
2706 return m.XXX_Marshal(b, false)
2707 }
2708 if m, ok := pb.(Marshaler); ok {
2709 // If the message can marshal itself, let it do it, for compatibility.
2710 // NOTE: This is not efficient.
2711 return m.Marshal()
2712 }
2713 // in case somehow we didn't generate the wrapper
2714 if pb == nil {
2715 return nil, ErrNil
2716 }
2717 var info InternalMessageInfo
2718 siz := info.Size(pb)
2719 b := make([]byte, 0, siz)
2720 return info.Marshal(b, pb, false)
2721}
2722
2723// Marshal takes a protocol buffer message
2724// and encodes it into the wire format, writing the result to the
2725// Buffer.
2726// This is an alternative entry point. It is not necessary to use
2727// a Buffer for most applications.
2728func (p *Buffer) Marshal(pb Message) error {
2729 var err error
2730 if m, ok := pb.(newMarshaler); ok {
2731 siz := m.XXX_Size()
2732 p.grow(siz) // make sure buf has enough capacity
2733 p.buf, err = m.XXX_Marshal(p.buf, p.deterministic)
2734 return err
2735 }
2736 if m, ok := pb.(Marshaler); ok {
2737 // If the message can marshal itself, let it do it, for compatibility.
2738 // NOTE: This is not efficient.
2739 b, err := m.Marshal()
2740 p.buf = append(p.buf, b...)
2741 return err
2742 }
2743 // in case somehow we didn't generate the wrapper
2744 if pb == nil {
2745 return ErrNil
2746 }
2747 var info InternalMessageInfo
2748 siz := info.Size(pb)
2749 p.grow(siz) // make sure buf has enough capacity
2750 p.buf, err = info.Marshal(p.buf, pb, p.deterministic)
2751 return err
2752}
2753
2754// grow grows the buffer's capacity, if necessary, to guarantee space for
2755// another n bytes. After grow(n), at least n bytes can be written to the
2756// buffer without another allocation.
2757func (p *Buffer) grow(n int) {
2758 need := len(p.buf) + n
2759 if need <= cap(p.buf) {
2760 return
2761 }
2762 newCap := len(p.buf) * 2
2763 if newCap < need {
2764 newCap = need
2765 }
2766 p.buf = append(make([]byte, 0, newCap), p.buf...)
2767}
diff --git a/vendor/github.com/golang/protobuf/proto/table_merge.go b/vendor/github.com/golang/protobuf/proto/table_merge.go
new file mode 100644
index 0000000..5525def
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/table_merge.go
@@ -0,0 +1,654 @@
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
32package proto
33
34import (
35 "fmt"
36 "reflect"
37 "strings"
38 "sync"
39 "sync/atomic"
40)
41
42// Merge merges the src message into dst.
43// This assumes that dst and src of the same type and are non-nil.
44func (a *InternalMessageInfo) Merge(dst, src Message) {
45 mi := atomicLoadMergeInfo(&a.merge)
46 if mi == nil {
47 mi = getMergeInfo(reflect.TypeOf(dst).Elem())
48 atomicStoreMergeInfo(&a.merge, mi)
49 }
50 mi.merge(toPointer(&dst), toPointer(&src))
51}
52
53type mergeInfo struct {
54 typ reflect.Type
55
56 initialized int32 // 0: only typ is valid, 1: everything is valid
57 lock sync.Mutex
58
59 fields []mergeFieldInfo
60 unrecognized field // Offset of XXX_unrecognized
61}
62
63type mergeFieldInfo struct {
64 field field // Offset of field, guaranteed to be valid
65
66 // isPointer reports whether the value in the field is a pointer.
67 // This is true for the following situations:
68 // * Pointer to struct
69 // * Pointer to basic type (proto2 only)
70 // * Slice (first value in slice header is a pointer)
71 // * String (first value in string header is a pointer)
72 isPointer bool
73
74 // basicWidth reports the width of the field assuming that it is directly
75 // embedded in the struct (as is the case for basic types in proto3).
76 // The possible values are:
77 // 0: invalid
78 // 1: bool
79 // 4: int32, uint32, float32
80 // 8: int64, uint64, float64
81 basicWidth int
82
83 // Where dst and src are pointers to the types being merged.
84 merge func(dst, src pointer)
85}
86
87var (
88 mergeInfoMap = map[reflect.Type]*mergeInfo{}
89 mergeInfoLock sync.Mutex
90)
91
92func getMergeInfo(t reflect.Type) *mergeInfo {
93 mergeInfoLock.Lock()
94 defer mergeInfoLock.Unlock()
95 mi := mergeInfoMap[t]
96 if mi == nil {
97 mi = &mergeInfo{typ: t}
98 mergeInfoMap[t] = mi
99 }
100 return mi
101}
102
103// merge merges src into dst assuming they are both of type *mi.typ.
104func (mi *mergeInfo) merge(dst, src pointer) {
105 if dst.isNil() {
106 panic("proto: nil destination")
107 }
108 if src.isNil() {
109 return // Nothing to do.
110 }
111
112 if atomic.LoadInt32(&mi.initialized) == 0 {
113 mi.computeMergeInfo()
114 }
115
116 for _, fi := range mi.fields {
117 sfp := src.offset(fi.field)
118
119 // As an optimization, we can avoid the merge function call cost
120 // if we know for sure that the source will have no effect
121 // by checking if it is the zero value.
122 if unsafeAllowed {
123 if fi.isPointer && sfp.getPointer().isNil() { // Could be slice or string
124 continue
125 }
126 if fi.basicWidth > 0 {
127 switch {
128 case fi.basicWidth == 1 && !*sfp.toBool():
129 continue
130 case fi.basicWidth == 4 && *sfp.toUint32() == 0:
131 continue
132 case fi.basicWidth == 8 && *sfp.toUint64() == 0:
133 continue
134 }
135 }
136 }
137
138 dfp := dst.offset(fi.field)
139 fi.merge(dfp, sfp)
140 }
141
142 // TODO: Make this faster?
143 out := dst.asPointerTo(mi.typ).Elem()
144 in := src.asPointerTo(mi.typ).Elem()
145 if emIn, err := extendable(in.Addr().Interface()); err == nil {
146 emOut, _ := extendable(out.Addr().Interface())
147 mIn, muIn := emIn.extensionsRead()
148 if mIn != nil {
149 mOut := emOut.extensionsWrite()
150 muIn.Lock()
151 mergeExtension(mOut, mIn)
152 muIn.Unlock()
153 }
154 }
155
156 if mi.unrecognized.IsValid() {
157 if b := *src.offset(mi.unrecognized).toBytes(); len(b) > 0 {
158 *dst.offset(mi.unrecognized).toBytes() = append([]byte(nil), b...)
159 }
160 }
161}
162
163func (mi *mergeInfo) computeMergeInfo() {
164 mi.lock.Lock()
165 defer mi.lock.Unlock()
166 if mi.initialized != 0 {
167 return
168 }
169 t := mi.typ
170 n := t.NumField()
171
172 props := GetProperties(t)
173 for i := 0; i < n; i++ {
174 f := t.Field(i)
175 if strings.HasPrefix(f.Name, "XXX_") {
176 continue
177 }
178
179 mfi := mergeFieldInfo{field: toField(&f)}
180 tf := f.Type
181
182 // As an optimization, we can avoid the merge function call cost
183 // if we know for sure that the source will have no effect
184 // by checking if it is the zero value.
185 if unsafeAllowed {
186 switch tf.Kind() {
187 case reflect.Ptr, reflect.Slice, reflect.String:
188 // As a special case, we assume slices and strings are pointers
189 // since we know that the first field in the SliceSlice or
190 // StringHeader is a data pointer.
191 mfi.isPointer = true
192 case reflect.Bool:
193 mfi.basicWidth = 1
194 case reflect.Int32, reflect.Uint32, reflect.Float32:
195 mfi.basicWidth = 4
196 case reflect.Int64, reflect.Uint64, reflect.Float64:
197 mfi.basicWidth = 8
198 }
199 }
200
201 // Unwrap tf to get at its most basic type.
202 var isPointer, isSlice bool
203 if tf.Kind() == reflect.Slice && tf.Elem().Kind() != reflect.Uint8 {
204 isSlice = true
205 tf = tf.Elem()
206 }
207 if tf.Kind() == reflect.Ptr {
208 isPointer = true
209 tf = tf.Elem()
210 }
211 if isPointer && isSlice && tf.Kind() != reflect.Struct {
212 panic("both pointer and slice for basic type in " + tf.Name())
213 }
214
215 switch tf.Kind() {
216 case reflect.Int32:
217 switch {
218 case isSlice: // E.g., []int32
219 mfi.merge = func(dst, src pointer) {
220 // NOTE: toInt32Slice is not defined (see pointer_reflect.go).
221 /*
222 sfsp := src.toInt32Slice()
223 if *sfsp != nil {
224 dfsp := dst.toInt32Slice()
225 *dfsp = append(*dfsp, *sfsp...)
226 if *dfsp == nil {
227 *dfsp = []int64{}
228 }
229 }
230 */
231 sfs := src.getInt32Slice()
232 if sfs != nil {
233 dfs := dst.getInt32Slice()
234 dfs = append(dfs, sfs...)
235 if dfs == nil {
236 dfs = []int32{}
237 }
238 dst.setInt32Slice(dfs)
239 }
240 }
241 case isPointer: // E.g., *int32
242 mfi.merge = func(dst, src pointer) {
243 // NOTE: toInt32Ptr is not defined (see pointer_reflect.go).
244 /*
245 sfpp := src.toInt32Ptr()
246 if *sfpp != nil {
247 dfpp := dst.toInt32Ptr()
248 if *dfpp == nil {
249 *dfpp = Int32(**sfpp)
250 } else {
251 **dfpp = **sfpp
252 }
253 }
254 */
255 sfp := src.getInt32Ptr()
256 if sfp != nil {
257 dfp := dst.getInt32Ptr()
258 if dfp == nil {
259 dst.setInt32Ptr(*sfp)
260 } else {
261 *dfp = *sfp
262 }
263 }
264 }
265 default: // E.g., int32
266 mfi.merge = func(dst, src pointer) {
267 if v := *src.toInt32(); v != 0 {
268 *dst.toInt32() = v
269 }
270 }
271 }
272 case reflect.Int64:
273 switch {
274 case isSlice: // E.g., []int64
275 mfi.merge = func(dst, src pointer) {
276 sfsp := src.toInt64Slice()
277 if *sfsp != nil {
278 dfsp := dst.toInt64Slice()
279 *dfsp = append(*dfsp, *sfsp...)
280 if *dfsp == nil {
281 *dfsp = []int64{}
282 }
283 }
284 }
285 case isPointer: // E.g., *int64
286 mfi.merge = func(dst, src pointer) {
287 sfpp := src.toInt64Ptr()
288 if *sfpp != nil {
289 dfpp := dst.toInt64Ptr()
290 if *dfpp == nil {
291 *dfpp = Int64(**sfpp)
292 } else {
293 **dfpp = **sfpp
294 }
295 }
296 }
297 default: // E.g., int64
298 mfi.merge = func(dst, src pointer) {
299 if v := *src.toInt64(); v != 0 {
300 *dst.toInt64() = v
301 }
302 }
303 }
304 case reflect.Uint32:
305 switch {
306 case isSlice: // E.g., []uint32
307 mfi.merge = func(dst, src pointer) {
308 sfsp := src.toUint32Slice()
309 if *sfsp != nil {
310 dfsp := dst.toUint32Slice()
311 *dfsp = append(*dfsp, *sfsp...)
312 if *dfsp == nil {
313 *dfsp = []uint32{}
314 }
315 }
316 }
317 case isPointer: // E.g., *uint32
318 mfi.merge = func(dst, src pointer) {
319 sfpp := src.toUint32Ptr()
320 if *sfpp != nil {
321 dfpp := dst.toUint32Ptr()
322 if *dfpp == nil {
323 *dfpp = Uint32(**sfpp)
324 } else {
325 **dfpp = **sfpp
326 }
327 }
328 }
329 default: // E.g., uint32
330 mfi.merge = func(dst, src pointer) {
331 if v := *src.toUint32(); v != 0 {
332 *dst.toUint32() = v
333 }
334 }
335 }
336 case reflect.Uint64:
337 switch {
338 case isSlice: // E.g., []uint64
339 mfi.merge = func(dst, src pointer) {
340 sfsp := src.toUint64Slice()
341 if *sfsp != nil {
342 dfsp := dst.toUint64Slice()
343 *dfsp = append(*dfsp, *sfsp...)
344 if *dfsp == nil {
345 *dfsp = []uint64{}
346 }
347 }
348 }
349 case isPointer: // E.g., *uint64
350 mfi.merge = func(dst, src pointer) {
351 sfpp := src.toUint64Ptr()
352 if *sfpp != nil {
353 dfpp := dst.toUint64Ptr()
354 if *dfpp == nil {
355 *dfpp = Uint64(**sfpp)
356 } else {
357 **dfpp = **sfpp
358 }
359 }
360 }
361 default: // E.g., uint64
362 mfi.merge = func(dst, src pointer) {
363 if v := *src.toUint64(); v != 0 {
364 *dst.toUint64() = v
365 }
366 }
367 }
368 case reflect.Float32:
369 switch {
370 case isSlice: // E.g., []float32
371 mfi.merge = func(dst, src pointer) {
372 sfsp := src.toFloat32Slice()
373 if *sfsp != nil {
374 dfsp := dst.toFloat32Slice()
375 *dfsp = append(*dfsp, *sfsp...)
376 if *dfsp == nil {
377 *dfsp = []float32{}
378 }
379 }
380 }
381 case isPointer: // E.g., *float32
382 mfi.merge = func(dst, src pointer) {
383 sfpp := src.toFloat32Ptr()
384 if *sfpp != nil {
385 dfpp := dst.toFloat32Ptr()
386 if *dfpp == nil {
387 *dfpp = Float32(**sfpp)
388 } else {
389 **dfpp = **sfpp
390 }
391 }
392 }
393 default: // E.g., float32
394 mfi.merge = func(dst, src pointer) {
395 if v := *src.toFloat32(); v != 0 {
396 *dst.toFloat32() = v
397 }
398 }
399 }
400 case reflect.Float64:
401 switch {
402 case isSlice: // E.g., []float64
403 mfi.merge = func(dst, src pointer) {
404 sfsp := src.toFloat64Slice()
405 if *sfsp != nil {
406 dfsp := dst.toFloat64Slice()
407 *dfsp = append(*dfsp, *sfsp...)
408 if *dfsp == nil {
409 *dfsp = []float64{}
410 }
411 }
412 }
413 case isPointer: // E.g., *float64
414 mfi.merge = func(dst, src pointer) {
415 sfpp := src.toFloat64Ptr()
416 if *sfpp != nil {
417 dfpp := dst.toFloat64Ptr()
418 if *dfpp == nil {
419 *dfpp = Float64(**sfpp)
420 } else {
421 **dfpp = **sfpp
422 }
423 }
424 }
425 default: // E.g., float64
426 mfi.merge = func(dst, src pointer) {
427 if v := *src.toFloat64(); v != 0 {
428 *dst.toFloat64() = v
429 }
430 }
431 }
432 case reflect.Bool:
433 switch {
434 case isSlice: // E.g., []bool
435 mfi.merge = func(dst, src pointer) {
436 sfsp := src.toBoolSlice()
437 if *sfsp != nil {
438 dfsp := dst.toBoolSlice()
439 *dfsp = append(*dfsp, *sfsp...)
440 if *dfsp == nil {
441 *dfsp = []bool{}
442 }
443 }
444 }
445 case isPointer: // E.g., *bool
446 mfi.merge = func(dst, src pointer) {
447 sfpp := src.toBoolPtr()
448 if *sfpp != nil {
449 dfpp := dst.toBoolPtr()
450 if *dfpp == nil {
451 *dfpp = Bool(**sfpp)
452 } else {
453 **dfpp = **sfpp
454 }
455 }
456 }
457 default: // E.g., bool
458 mfi.merge = func(dst, src pointer) {
459 if v := *src.toBool(); v {
460 *dst.toBool() = v
461 }
462 }
463 }
464 case reflect.String:
465 switch {
466 case isSlice: // E.g., []string
467 mfi.merge = func(dst, src pointer) {
468 sfsp := src.toStringSlice()
469 if *sfsp != nil {
470 dfsp := dst.toStringSlice()
471 *dfsp = append(*dfsp, *sfsp...)
472 if *dfsp == nil {
473 *dfsp = []string{}
474 }
475 }
476 }
477 case isPointer: // E.g., *string
478 mfi.merge = func(dst, src pointer) {
479 sfpp := src.toStringPtr()
480 if *sfpp != nil {
481 dfpp := dst.toStringPtr()
482 if *dfpp == nil {
483 *dfpp = String(**sfpp)
484 } else {
485 **dfpp = **sfpp
486 }
487 }
488 }
489 default: // E.g., string
490 mfi.merge = func(dst, src pointer) {
491 if v := *src.toString(); v != "" {
492 *dst.toString() = v
493 }
494 }
495 }
496 case reflect.Slice:
497 isProto3 := props.Prop[i].proto3
498 switch {
499 case isPointer:
500 panic("bad pointer in byte slice case in " + tf.Name())
501 case tf.Elem().Kind() != reflect.Uint8:
502 panic("bad element kind in byte slice case in " + tf.Name())
503 case isSlice: // E.g., [][]byte
504 mfi.merge = func(dst, src pointer) {
505 sbsp := src.toBytesSlice()
506 if *sbsp != nil {
507 dbsp := dst.toBytesSlice()
508 for _, sb := range *sbsp {
509 if sb == nil {
510 *dbsp = append(*dbsp, nil)
511 } else {
512 *dbsp = append(*dbsp, append([]byte{}, sb...))
513 }
514 }
515 if *dbsp == nil {
516 *dbsp = [][]byte{}
517 }
518 }
519 }
520 default: // E.g., []byte
521 mfi.merge = func(dst, src pointer) {
522 sbp := src.toBytes()
523 if *sbp != nil {
524 dbp := dst.toBytes()
525 if !isProto3 || len(*sbp) > 0 {
526 *dbp = append([]byte{}, *sbp...)
527 }
528 }
529 }
530 }
531 case reflect.Struct:
532 switch {
533 case !isPointer:
534 panic(fmt.Sprintf("message field %s without pointer", tf))
535 case isSlice: // E.g., []*pb.T
536 mi := getMergeInfo(tf)
537 mfi.merge = func(dst, src pointer) {
538 sps := src.getPointerSlice()
539 if sps != nil {
540 dps := dst.getPointerSlice()
541 for _, sp := range sps {
542 var dp pointer
543 if !sp.isNil() {
544 dp = valToPointer(reflect.New(tf))
545 mi.merge(dp, sp)
546 }
547 dps = append(dps, dp)
548 }
549 if dps == nil {
550 dps = []pointer{}
551 }
552 dst.setPointerSlice(dps)
553 }
554 }
555 default: // E.g., *pb.T
556 mi := getMergeInfo(tf)
557 mfi.merge = func(dst, src pointer) {
558 sp := src.getPointer()
559 if !sp.isNil() {
560 dp := dst.getPointer()
561 if dp.isNil() {
562 dp = valToPointer(reflect.New(tf))
563 dst.setPointer(dp)
564 }
565 mi.merge(dp, sp)
566 }
567 }
568 }
569 case reflect.Map:
570 switch {
571 case isPointer || isSlice:
572 panic("bad pointer or slice in map case in " + tf.Name())
573 default: // E.g., map[K]V
574 mfi.merge = func(dst, src pointer) {
575 sm := src.asPointerTo(tf).Elem()
576 if sm.Len() == 0 {
577 return
578 }
579 dm := dst.asPointerTo(tf).Elem()
580 if dm.IsNil() {
581 dm.Set(reflect.MakeMap(tf))
582 }
583
584 switch tf.Elem().Kind() {
585 case reflect.Ptr: // Proto struct (e.g., *T)
586 for _, key := range sm.MapKeys() {
587 val := sm.MapIndex(key)
588 val = reflect.ValueOf(Clone(val.Interface().(Message)))
589 dm.SetMapIndex(key, val)
590 }
591 case reflect.Slice: // E.g. Bytes type (e.g., []byte)
592 for _, key := range sm.MapKeys() {
593 val := sm.MapIndex(key)
594 val = reflect.ValueOf(append([]byte{}, val.Bytes()...))
595 dm.SetMapIndex(key, val)
596 }
597 default: // Basic type (e.g., string)
598 for _, key := range sm.MapKeys() {
599 val := sm.MapIndex(key)
600 dm.SetMapIndex(key, val)
601 }
602 }
603 }
604 }
605 case reflect.Interface:
606 // Must be oneof field.
607 switch {
608 case isPointer || isSlice:
609 panic("bad pointer or slice in interface case in " + tf.Name())
610 default: // E.g., interface{}
611 // TODO: Make this faster?
612 mfi.merge = func(dst, src pointer) {
613 su := src.asPointerTo(tf).Elem()
614 if !su.IsNil() {
615 du := dst.asPointerTo(tf).Elem()
616 typ := su.Elem().Type()
617 if du.IsNil() || du.Elem().Type() != typ {
618 du.Set(reflect.New(typ.Elem())) // Initialize interface if empty
619 }
620 sv := su.Elem().Elem().Field(0)
621 if sv.Kind() == reflect.Ptr && sv.IsNil() {
622 return
623 }
624 dv := du.Elem().Elem().Field(0)
625 if dv.Kind() == reflect.Ptr && dv.IsNil() {
626 dv.Set(reflect.New(sv.Type().Elem())) // Initialize proto message if empty
627 }
628 switch sv.Type().Kind() {
629 case reflect.Ptr: // Proto struct (e.g., *T)
630 Merge(dv.Interface().(Message), sv.Interface().(Message))
631 case reflect.Slice: // E.g. Bytes type (e.g., []byte)
632 dv.Set(reflect.ValueOf(append([]byte{}, sv.Bytes()...)))
633 default: // Basic type (e.g., string)
634 dv.Set(sv)
635 }
636 }
637 }
638 }
639 default:
640 panic(fmt.Sprintf("merger not found for type:%s", tf))
641 }
642 mi.fields = append(mi.fields, mfi)
643 }
644
645 mi.unrecognized = invalidField
646 if f, ok := t.FieldByName("XXX_unrecognized"); ok {
647 if f.Type != reflect.TypeOf([]byte{}) {
648 panic("expected XXX_unrecognized to be of type []byte")
649 }
650 mi.unrecognized = toField(&f)
651 }
652
653 atomic.StoreInt32(&mi.initialized, 1)
654}
diff --git a/vendor/github.com/golang/protobuf/proto/table_unmarshal.go b/vendor/github.com/golang/protobuf/proto/table_unmarshal.go
new file mode 100644
index 0000000..ebf1caa
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/table_unmarshal.go
@@ -0,0 +1,2051 @@
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
32package proto
33
34import (
35 "errors"
36 "fmt"
37 "io"
38 "math"
39 "reflect"
40 "strconv"
41 "strings"
42 "sync"
43 "sync/atomic"
44 "unicode/utf8"
45)
46
47// Unmarshal is the entry point from the generated .pb.go files.
48// This function is not intended to be used by non-generated code.
49// This function is not subject to any compatibility guarantee.
50// msg contains a pointer to a protocol buffer struct.
51// b is the data to be unmarshaled into the protocol buffer.
52// a is a pointer to a place to store cached unmarshal information.
53func (a *InternalMessageInfo) Unmarshal(msg Message, b []byte) error {
54 // Load the unmarshal information for this message type.
55 // The atomic load ensures memory consistency.
56 u := atomicLoadUnmarshalInfo(&a.unmarshal)
57 if u == nil {
58 // Slow path: find unmarshal info for msg, update a with it.
59 u = getUnmarshalInfo(reflect.TypeOf(msg).Elem())
60 atomicStoreUnmarshalInfo(&a.unmarshal, u)
61 }
62 // Then do the unmarshaling.
63 err := u.unmarshal(toPointer(&msg), b)
64 return err
65}
66
67type unmarshalInfo struct {
68 typ reflect.Type // type of the protobuf struct
69
70 // 0 = only typ field is initialized
71 // 1 = completely initialized
72 initialized int32
73 lock sync.Mutex // prevents double initialization
74 dense []unmarshalFieldInfo // fields indexed by tag #
75 sparse map[uint64]unmarshalFieldInfo // fields indexed by tag #
76 reqFields []string // names of required fields
77 reqMask uint64 // 1<<len(reqFields)-1
78 unrecognized field // offset of []byte to put unrecognized data (or invalidField if we should throw it away)
79 extensions field // offset of extensions field (of type proto.XXX_InternalExtensions), or invalidField if it does not exist
80 oldExtensions field // offset of old-form extensions field (of type map[int]Extension)
81 extensionRanges []ExtensionRange // if non-nil, implies extensions field is valid
82 isMessageSet bool // if true, implies extensions field is valid
83}
84
85// An unmarshaler takes a stream of bytes and a pointer to a field of a message.
86// It decodes the field, stores it at f, and returns the unused bytes.
87// w is the wire encoding.
88// b is the data after the tag and wire encoding have been read.
89type unmarshaler func(b []byte, f pointer, w int) ([]byte, error)
90
91type unmarshalFieldInfo struct {
92 // location of the field in the proto message structure.
93 field field
94
95 // function to unmarshal the data for the field.
96 unmarshal unmarshaler
97
98 // if a required field, contains a single set bit at this field's index in the required field list.
99 reqMask uint64
100
101 name string // name of the field, for error reporting
102}
103
104var (
105 unmarshalInfoMap = map[reflect.Type]*unmarshalInfo{}
106 unmarshalInfoLock sync.Mutex
107)
108
109// getUnmarshalInfo returns the data structure which can be
110// subsequently used to unmarshal a message of the given type.
111// t is the type of the message (note: not pointer to message).
112func getUnmarshalInfo(t reflect.Type) *unmarshalInfo {
113 // It would be correct to return a new unmarshalInfo
114 // unconditionally. We would end up allocating one
115 // per occurrence of that type as a message or submessage.
116 // We use a cache here just to reduce memory usage.
117 unmarshalInfoLock.Lock()
118 defer unmarshalInfoLock.Unlock()
119 u := unmarshalInfoMap[t]
120 if u == nil {
121 u = &unmarshalInfo{typ: t}
122 // Note: we just set the type here. The rest of the fields
123 // will be initialized on first use.
124 unmarshalInfoMap[t] = u
125 }
126 return u
127}
128
129// unmarshal does the main work of unmarshaling a message.
130// u provides type information used to unmarshal the message.
131// m is a pointer to a protocol buffer message.
132// b is a byte stream to unmarshal into m.
133// This is top routine used when recursively unmarshaling submessages.
134func (u *unmarshalInfo) unmarshal(m pointer, b []byte) error {
135 if atomic.LoadInt32(&u.initialized) == 0 {
136 u.computeUnmarshalInfo()
137 }
138 if u.isMessageSet {
139 return UnmarshalMessageSet(b, m.offset(u.extensions).toExtensions())
140 }
141 var reqMask uint64 // bitmask of required fields we've seen.
142 var errLater error
143 for len(b) > 0 {
144 // Read tag and wire type.
145 // Special case 1 and 2 byte varints.
146 var x uint64
147 if b[0] < 128 {
148 x = uint64(b[0])
149 b = b[1:]
150 } else if len(b) >= 2 && b[1] < 128 {
151 x = uint64(b[0]&0x7f) + uint64(b[1])<<7
152 b = b[2:]
153 } else {
154 var n int
155 x, n = decodeVarint(b)
156 if n == 0 {
157 return io.ErrUnexpectedEOF
158 }
159 b = b[n:]
160 }
161 tag := x >> 3
162 wire := int(x) & 7
163
164 // Dispatch on the tag to one of the unmarshal* functions below.
165 var f unmarshalFieldInfo
166 if tag < uint64(len(u.dense)) {
167 f = u.dense[tag]
168 } else {
169 f = u.sparse[tag]
170 }
171 if fn := f.unmarshal; fn != nil {
172 var err error
173 b, err = fn(b, m.offset(f.field), wire)
174 if err == nil {
175 reqMask |= f.reqMask
176 continue
177 }
178 if r, ok := err.(*RequiredNotSetError); ok {
179 // Remember this error, but keep parsing. We need to produce
180 // a full parse even if a required field is missing.
181 if errLater == nil {
182 errLater = r
183 }
184 reqMask |= f.reqMask
185 continue
186 }
187 if err != errInternalBadWireType {
188 if err == errInvalidUTF8 {
189 if errLater == nil {
190 fullName := revProtoTypes[reflect.PtrTo(u.typ)] + "." + f.name
191 errLater = &invalidUTF8Error{fullName}
192 }
193 continue
194 }
195 return err
196 }
197 // Fragments with bad wire type are treated as unknown fields.
198 }
199
200 // Unknown tag.
201 if !u.unrecognized.IsValid() {
202 // Don't keep unrecognized data; just skip it.
203 var err error
204 b, err = skipField(b, wire)
205 if err != nil {
206 return err
207 }
208 continue
209 }
210 // Keep unrecognized data around.
211 // maybe in extensions, maybe in the unrecognized field.
212 z := m.offset(u.unrecognized).toBytes()
213 var emap map[int32]Extension
214 var e Extension
215 for _, r := range u.extensionRanges {
216 if uint64(r.Start) <= tag && tag <= uint64(r.End) {
217 if u.extensions.IsValid() {
218 mp := m.offset(u.extensions).toExtensions()
219 emap = mp.extensionsWrite()
220 e = emap[int32(tag)]
221 z = &e.enc
222 break
223 }
224 if u.oldExtensions.IsValid() {
225 p := m.offset(u.oldExtensions).toOldExtensions()
226 emap = *p
227 if emap == nil {
228 emap = map[int32]Extension{}
229 *p = emap
230 }
231 e = emap[int32(tag)]
232 z = &e.enc
233 break
234 }
235 panic("no extensions field available")
236 }
237 }
238
239 // Use wire type to skip data.
240 var err error
241 b0 := b
242 b, err = skipField(b, wire)
243 if err != nil {
244 return err
245 }
246 *z = encodeVarint(*z, tag<<3|uint64(wire))
247 *z = append(*z, b0[:len(b0)-len(b)]...)
248
249 if emap != nil {
250 emap[int32(tag)] = e
251 }
252 }
253 if reqMask != u.reqMask && errLater == nil {
254 // A required field of this message is missing.
255 for _, n := range u.reqFields {
256 if reqMask&1 == 0 {
257 errLater = &RequiredNotSetError{n}
258 }
259 reqMask >>= 1
260 }
261 }
262 return errLater
263}
264
265// computeUnmarshalInfo fills in u with information for use
266// in unmarshaling protocol buffers of type u.typ.
267func (u *unmarshalInfo) computeUnmarshalInfo() {
268 u.lock.Lock()
269 defer u.lock.Unlock()
270 if u.initialized != 0 {
271 return
272 }
273 t := u.typ
274 n := t.NumField()
275
276 // Set up the "not found" value for the unrecognized byte buffer.
277 // This is the default for proto3.
278 u.unrecognized = invalidField
279 u.extensions = invalidField
280 u.oldExtensions = invalidField
281
282 // List of the generated type and offset for each oneof field.
283 type oneofField struct {
284 ityp reflect.Type // interface type of oneof field
285 field field // offset in containing message
286 }
287 var oneofFields []oneofField
288
289 for i := 0; i < n; i++ {
290 f := t.Field(i)
291 if f.Name == "XXX_unrecognized" {
292 // The byte slice used to hold unrecognized input is special.
293 if f.Type != reflect.TypeOf(([]byte)(nil)) {
294 panic("bad type for XXX_unrecognized field: " + f.Type.Name())
295 }
296 u.unrecognized = toField(&f)
297 continue
298 }
299 if f.Name == "XXX_InternalExtensions" {
300 // Ditto here.
301 if f.Type != reflect.TypeOf(XXX_InternalExtensions{}) {
302 panic("bad type for XXX_InternalExtensions field: " + f.Type.Name())
303 }
304 u.extensions = toField(&f)
305 if f.Tag.Get("protobuf_messageset") == "1" {
306 u.isMessageSet = true
307 }
308 continue
309 }
310 if f.Name == "XXX_extensions" {
311 // An older form of the extensions field.
312 if f.Type != reflect.TypeOf((map[int32]Extension)(nil)) {
313 panic("bad type for XXX_extensions field: " + f.Type.Name())
314 }
315 u.oldExtensions = toField(&f)
316 continue
317 }
318 if f.Name == "XXX_NoUnkeyedLiteral" || f.Name == "XXX_sizecache" {
319 continue
320 }
321
322 oneof := f.Tag.Get("protobuf_oneof")
323 if oneof != "" {
324 oneofFields = append(oneofFields, oneofField{f.Type, toField(&f)})
325 // The rest of oneof processing happens below.
326 continue
327 }
328
329 tags := f.Tag.Get("protobuf")
330 tagArray := strings.Split(tags, ",")
331 if len(tagArray) < 2 {
332 panic("protobuf tag not enough fields in " + t.Name() + "." + f.Name + ": " + tags)
333 }
334 tag, err := strconv.Atoi(tagArray[1])
335 if err != nil {
336 panic("protobuf tag field not an integer: " + tagArray[1])
337 }
338
339 name := ""
340 for _, tag := range tagArray[3:] {
341 if strings.HasPrefix(tag, "name=") {
342 name = tag[5:]
343 }
344 }
345
346 // Extract unmarshaling function from the field (its type and tags).
347 unmarshal := fieldUnmarshaler(&f)
348
349 // Required field?
350 var reqMask uint64
351 if tagArray[2] == "req" {
352 bit := len(u.reqFields)
353 u.reqFields = append(u.reqFields, name)
354 reqMask = uint64(1) << uint(bit)
355 // TODO: if we have more than 64 required fields, we end up
356 // not verifying that all required fields are present.
357 // Fix this, perhaps using a count of required fields?
358 }
359
360 // Store the info in the correct slot in the message.
361 u.setTag(tag, toField(&f), unmarshal, reqMask, name)
362 }
363
364 // Find any types associated with oneof fields.
365 // TODO: XXX_OneofFuncs returns more info than we need. Get rid of some of it?
366 fn := reflect.Zero(reflect.PtrTo(t)).MethodByName("XXX_OneofFuncs")
367 if fn.IsValid() {
368 res := fn.Call(nil)[3] // last return value from XXX_OneofFuncs: []interface{}
369 for i := res.Len() - 1; i >= 0; i-- {
370 v := res.Index(i) // interface{}
371 tptr := reflect.ValueOf(v.Interface()).Type() // *Msg_X
372 typ := tptr.Elem() // Msg_X
373
374 f := typ.Field(0) // oneof implementers have one field
375 baseUnmarshal := fieldUnmarshaler(&f)
376 tags := strings.Split(f.Tag.Get("protobuf"), ",")
377 fieldNum, err := strconv.Atoi(tags[1])
378 if err != nil {
379 panic("protobuf tag field not an integer: " + tags[1])
380 }
381 var name string
382 for _, tag := range tags {
383 if strings.HasPrefix(tag, "name=") {
384 name = strings.TrimPrefix(tag, "name=")
385 break
386 }
387 }
388
389 // Find the oneof field that this struct implements.
390 // Might take O(n^2) to process all of the oneofs, but who cares.
391 for _, of := range oneofFields {
392 if tptr.Implements(of.ityp) {
393 // We have found the corresponding interface for this struct.
394 // That lets us know where this struct should be stored
395 // when we encounter it during unmarshaling.
396 unmarshal := makeUnmarshalOneof(typ, of.ityp, baseUnmarshal)
397 u.setTag(fieldNum, of.field, unmarshal, 0, name)
398 }
399 }
400 }
401 }
402
403 // Get extension ranges, if any.
404 fn = reflect.Zero(reflect.PtrTo(t)).MethodByName("ExtensionRangeArray")
405 if fn.IsValid() {
406 if !u.extensions.IsValid() && !u.oldExtensions.IsValid() {
407 panic("a message with extensions, but no extensions field in " + t.Name())
408 }
409 u.extensionRanges = fn.Call(nil)[0].Interface().([]ExtensionRange)
410 }
411
412 // Explicitly disallow tag 0. This will ensure we flag an error
413 // when decoding a buffer of all zeros. Without this code, we
414 // would decode and skip an all-zero buffer of even length.
415 // [0 0] is [tag=0/wiretype=varint varint-encoded-0].
416 u.setTag(0, zeroField, func(b []byte, f pointer, w int) ([]byte, error) {
417 return nil, fmt.Errorf("proto: %s: illegal tag 0 (wire type %d)", t, w)
418 }, 0, "")
419
420 // Set mask for required field check.
421 u.reqMask = uint64(1)<<uint(len(u.reqFields)) - 1
422
423 atomic.StoreInt32(&u.initialized, 1)
424}
425
426// setTag stores the unmarshal information for the given tag.
427// tag = tag # for field
428// field/unmarshal = unmarshal info for that field.
429// reqMask = if required, bitmask for field position in required field list. 0 otherwise.
430// name = short name of the field.
431func (u *unmarshalInfo) setTag(tag int, field field, unmarshal unmarshaler, reqMask uint64, name string) {
432 i := unmarshalFieldInfo{field: field, unmarshal: unmarshal, reqMask: reqMask, name: name}
433 n := u.typ.NumField()
434 if tag >= 0 && (tag < 16 || tag < 2*n) { // TODO: what are the right numbers here?
435 for len(u.dense) <= tag {
436 u.dense = append(u.dense, unmarshalFieldInfo{})
437 }
438 u.dense[tag] = i
439 return
440 }
441 if u.sparse == nil {
442 u.sparse = map[uint64]unmarshalFieldInfo{}
443 }
444 u.sparse[uint64(tag)] = i
445}
446
447// fieldUnmarshaler returns an unmarshaler for the given field.
448func fieldUnmarshaler(f *reflect.StructField) unmarshaler {
449 if f.Type.Kind() == reflect.Map {
450 return makeUnmarshalMap(f)
451 }
452 return typeUnmarshaler(f.Type, f.Tag.Get("protobuf"))
453}
454
455// typeUnmarshaler returns an unmarshaler for the given field type / field tag pair.
456func typeUnmarshaler(t reflect.Type, tags string) unmarshaler {
457 tagArray := strings.Split(tags, ",")
458 encoding := tagArray[0]
459 name := "unknown"
460 proto3 := false
461 validateUTF8 := true
462 for _, tag := range tagArray[3:] {
463 if strings.HasPrefix(tag, "name=") {
464 name = tag[5:]
465 }
466 if tag == "proto3" {
467 proto3 = true
468 }
469 }
470 validateUTF8 = validateUTF8 && proto3
471
472 // Figure out packaging (pointer, slice, or both)
473 slice := false
474 pointer := false
475 if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
476 slice = true
477 t = t.Elem()
478 }
479 if t.Kind() == reflect.Ptr {
480 pointer = true
481 t = t.Elem()
482 }
483
484 // We'll never have both pointer and slice for basic types.
485 if pointer && slice && t.Kind() != reflect.Struct {
486 panic("both pointer and slice for basic type in " + t.Name())
487 }
488
489 switch t.Kind() {
490 case reflect.Bool:
491 if pointer {
492 return unmarshalBoolPtr
493 }
494 if slice {
495 return unmarshalBoolSlice
496 }
497 return unmarshalBoolValue
498 case reflect.Int32:
499 switch encoding {
500 case "fixed32":
501 if pointer {
502 return unmarshalFixedS32Ptr
503 }
504 if slice {
505 return unmarshalFixedS32Slice
506 }
507 return unmarshalFixedS32Value
508 case "varint":
509 // this could be int32 or enum
510 if pointer {
511 return unmarshalInt32Ptr
512 }
513 if slice {
514 return unmarshalInt32Slice
515 }
516 return unmarshalInt32Value
517 case "zigzag32":
518 if pointer {
519 return unmarshalSint32Ptr
520 }
521 if slice {
522 return unmarshalSint32Slice
523 }
524 return unmarshalSint32Value
525 }
526 case reflect.Int64:
527 switch encoding {
528 case "fixed64":
529 if pointer {
530 return unmarshalFixedS64Ptr
531 }
532 if slice {
533 return unmarshalFixedS64Slice
534 }
535 return unmarshalFixedS64Value
536 case "varint":
537 if pointer {
538 return unmarshalInt64Ptr
539 }
540 if slice {
541 return unmarshalInt64Slice
542 }
543 return unmarshalInt64Value
544 case "zigzag64":
545 if pointer {
546 return unmarshalSint64Ptr
547 }
548 if slice {
549 return unmarshalSint64Slice
550 }
551 return unmarshalSint64Value
552 }
553 case reflect.Uint32:
554 switch encoding {
555 case "fixed32":
556 if pointer {
557 return unmarshalFixed32Ptr
558 }
559 if slice {
560 return unmarshalFixed32Slice
561 }
562 return unmarshalFixed32Value
563 case "varint":
564 if pointer {
565 return unmarshalUint32Ptr
566 }
567 if slice {
568 return unmarshalUint32Slice
569 }
570 return unmarshalUint32Value
571 }
572 case reflect.Uint64:
573 switch encoding {
574 case "fixed64":
575 if pointer {
576 return unmarshalFixed64Ptr
577 }
578 if slice {
579 return unmarshalFixed64Slice
580 }
581 return unmarshalFixed64Value
582 case "varint":
583 if pointer {
584 return unmarshalUint64Ptr
585 }
586 if slice {
587 return unmarshalUint64Slice
588 }
589 return unmarshalUint64Value
590 }
591 case reflect.Float32:
592 if pointer {
593 return unmarshalFloat32Ptr
594 }
595 if slice {
596 return unmarshalFloat32Slice
597 }
598 return unmarshalFloat32Value
599 case reflect.Float64:
600 if pointer {
601 return unmarshalFloat64Ptr
602 }
603 if slice {
604 return unmarshalFloat64Slice
605 }
606 return unmarshalFloat64Value
607 case reflect.Map:
608 panic("map type in typeUnmarshaler in " + t.Name())
609 case reflect.Slice:
610 if pointer {
611 panic("bad pointer in slice case in " + t.Name())
612 }
613 if slice {
614 return unmarshalBytesSlice
615 }
616 return unmarshalBytesValue
617 case reflect.String:
618 if validateUTF8 {
619 if pointer {
620 return unmarshalUTF8StringPtr
621 }
622 if slice {
623 return unmarshalUTF8StringSlice
624 }
625 return unmarshalUTF8StringValue
626 }
627 if pointer {
628 return unmarshalStringPtr
629 }
630 if slice {
631 return unmarshalStringSlice
632 }
633 return unmarshalStringValue
634 case reflect.Struct:
635 // message or group field
636 if !pointer {
637 panic(fmt.Sprintf("message/group field %s:%s without pointer", t, encoding))
638 }
639 switch encoding {
640 case "bytes":
641 if slice {
642 return makeUnmarshalMessageSlicePtr(getUnmarshalInfo(t), name)
643 }
644 return makeUnmarshalMessagePtr(getUnmarshalInfo(t), name)
645 case "group":
646 if slice {
647 return makeUnmarshalGroupSlicePtr(getUnmarshalInfo(t), name)
648 }
649 return makeUnmarshalGroupPtr(getUnmarshalInfo(t), name)
650 }
651 }
652 panic(fmt.Sprintf("unmarshaler not found type:%s encoding:%s", t, encoding))
653}
654
655// Below are all the unmarshalers for individual fields of various types.
656
657func unmarshalInt64Value(b []byte, f pointer, w int) ([]byte, error) {
658 if w != WireVarint {
659 return b, errInternalBadWireType
660 }
661 x, n := decodeVarint(b)
662 if n == 0 {
663 return nil, io.ErrUnexpectedEOF
664 }
665 b = b[n:]
666 v := int64(x)
667 *f.toInt64() = v
668 return b, nil
669}
670
671func unmarshalInt64Ptr(b []byte, f pointer, w int) ([]byte, error) {
672 if w != WireVarint {
673 return b, errInternalBadWireType
674 }
675 x, n := decodeVarint(b)
676 if n == 0 {
677 return nil, io.ErrUnexpectedEOF
678 }
679 b = b[n:]
680 v := int64(x)
681 *f.toInt64Ptr() = &v
682 return b, nil
683}
684
685func unmarshalInt64Slice(b []byte, f pointer, w int) ([]byte, error) {
686 if w == WireBytes { // packed
687 x, n := decodeVarint(b)
688 if n == 0 {
689 return nil, io.ErrUnexpectedEOF
690 }
691 b = b[n:]
692 if x > uint64(len(b)) {
693 return nil, io.ErrUnexpectedEOF
694 }
695 res := b[x:]
696 b = b[:x]
697 for len(b) > 0 {
698 x, n = decodeVarint(b)
699 if n == 0 {
700 return nil, io.ErrUnexpectedEOF
701 }
702 b = b[n:]
703 v := int64(x)
704 s := f.toInt64Slice()
705 *s = append(*s, v)
706 }
707 return res, nil
708 }
709 if w != WireVarint {
710 return b, errInternalBadWireType
711 }
712 x, n := decodeVarint(b)
713 if n == 0 {
714 return nil, io.ErrUnexpectedEOF
715 }
716 b = b[n:]
717 v := int64(x)
718 s := f.toInt64Slice()
719 *s = append(*s, v)
720 return b, nil
721}
722
723func unmarshalSint64Value(b []byte, f pointer, w int) ([]byte, error) {
724 if w != WireVarint {
725 return b, errInternalBadWireType
726 }
727 x, n := decodeVarint(b)
728 if n == 0 {
729 return nil, io.ErrUnexpectedEOF
730 }
731 b = b[n:]
732 v := int64(x>>1) ^ int64(x)<<63>>63
733 *f.toInt64() = v
734 return b, nil
735}
736
737func unmarshalSint64Ptr(b []byte, f pointer, w int) ([]byte, error) {
738 if w != WireVarint {
739 return b, errInternalBadWireType
740 }
741 x, n := decodeVarint(b)
742 if n == 0 {
743 return nil, io.ErrUnexpectedEOF
744 }
745 b = b[n:]
746 v := int64(x>>1) ^ int64(x)<<63>>63
747 *f.toInt64Ptr() = &v
748 return b, nil
749}
750
751func unmarshalSint64Slice(b []byte, f pointer, w int) ([]byte, error) {
752 if w == WireBytes { // packed
753 x, n := decodeVarint(b)
754 if n == 0 {
755 return nil, io.ErrUnexpectedEOF
756 }
757 b = b[n:]
758 if x > uint64(len(b)) {
759 return nil, io.ErrUnexpectedEOF
760 }
761 res := b[x:]
762 b = b[:x]
763 for len(b) > 0 {
764 x, n = decodeVarint(b)
765 if n == 0 {
766 return nil, io.ErrUnexpectedEOF
767 }
768 b = b[n:]
769 v := int64(x>>1) ^ int64(x)<<63>>63
770 s := f.toInt64Slice()
771 *s = append(*s, v)
772 }
773 return res, nil
774 }
775 if w != WireVarint {
776 return b, errInternalBadWireType
777 }
778 x, n := decodeVarint(b)
779 if n == 0 {
780 return nil, io.ErrUnexpectedEOF
781 }
782 b = b[n:]
783 v := int64(x>>1) ^ int64(x)<<63>>63
784 s := f.toInt64Slice()
785 *s = append(*s, v)
786 return b, nil
787}
788
789func unmarshalUint64Value(b []byte, f pointer, w int) ([]byte, error) {
790 if w != WireVarint {
791 return b, errInternalBadWireType
792 }
793 x, n := decodeVarint(b)
794 if n == 0 {
795 return nil, io.ErrUnexpectedEOF
796 }
797 b = b[n:]
798 v := uint64(x)
799 *f.toUint64() = v
800 return b, nil
801}
802
803func unmarshalUint64Ptr(b []byte, f pointer, w int) ([]byte, error) {
804 if w != WireVarint {
805 return b, errInternalBadWireType
806 }
807 x, n := decodeVarint(b)
808 if n == 0 {
809 return nil, io.ErrUnexpectedEOF
810 }
811 b = b[n:]
812 v := uint64(x)
813 *f.toUint64Ptr() = &v
814 return b, nil
815}
816
817func unmarshalUint64Slice(b []byte, f pointer, w int) ([]byte, error) {
818 if w == WireBytes { // packed
819 x, n := decodeVarint(b)
820 if n == 0 {
821 return nil, io.ErrUnexpectedEOF
822 }
823 b = b[n:]
824 if x > uint64(len(b)) {
825 return nil, io.ErrUnexpectedEOF
826 }
827 res := b[x:]
828 b = b[:x]
829 for len(b) > 0 {
830 x, n = decodeVarint(b)
831 if n == 0 {
832 return nil, io.ErrUnexpectedEOF
833 }
834 b = b[n:]
835 v := uint64(x)
836 s := f.toUint64Slice()
837 *s = append(*s, v)
838 }
839 return res, nil
840 }
841 if w != WireVarint {
842 return b, errInternalBadWireType
843 }
844 x, n := decodeVarint(b)
845 if n == 0 {
846 return nil, io.ErrUnexpectedEOF
847 }
848 b = b[n:]
849 v := uint64(x)
850 s := f.toUint64Slice()
851 *s = append(*s, v)
852 return b, nil
853}
854
855func unmarshalInt32Value(b []byte, f pointer, w int) ([]byte, error) {
856 if w != WireVarint {
857 return b, errInternalBadWireType
858 }
859 x, n := decodeVarint(b)
860 if n == 0 {
861 return nil, io.ErrUnexpectedEOF
862 }
863 b = b[n:]
864 v := int32(x)
865 *f.toInt32() = v
866 return b, nil
867}
868
869func unmarshalInt32Ptr(b []byte, f pointer, w int) ([]byte, error) {
870 if w != WireVarint {
871 return b, errInternalBadWireType
872 }
873 x, n := decodeVarint(b)
874 if n == 0 {
875 return nil, io.ErrUnexpectedEOF
876 }
877 b = b[n:]
878 v := int32(x)
879 f.setInt32Ptr(v)
880 return b, nil
881}
882
883func unmarshalInt32Slice(b []byte, f pointer, w int) ([]byte, error) {
884 if w == WireBytes { // packed
885 x, n := decodeVarint(b)
886 if n == 0 {
887 return nil, io.ErrUnexpectedEOF
888 }
889 b = b[n:]
890 if x > uint64(len(b)) {
891 return nil, io.ErrUnexpectedEOF
892 }
893 res := b[x:]
894 b = b[:x]
895 for len(b) > 0 {
896 x, n = decodeVarint(b)
897 if n == 0 {
898 return nil, io.ErrUnexpectedEOF
899 }
900 b = b[n:]
901 v := int32(x)
902 f.appendInt32Slice(v)
903 }
904 return res, nil
905 }
906 if w != WireVarint {
907 return b, errInternalBadWireType
908 }
909 x, n := decodeVarint(b)
910 if n == 0 {
911 return nil, io.ErrUnexpectedEOF
912 }
913 b = b[n:]
914 v := int32(x)
915 f.appendInt32Slice(v)
916 return b, nil
917}
918
919func unmarshalSint32Value(b []byte, f pointer, w int) ([]byte, error) {
920 if w != WireVarint {
921 return b, errInternalBadWireType
922 }
923 x, n := decodeVarint(b)
924 if n == 0 {
925 return nil, io.ErrUnexpectedEOF
926 }
927 b = b[n:]
928 v := int32(x>>1) ^ int32(x)<<31>>31
929 *f.toInt32() = v
930 return b, nil
931}
932
933func unmarshalSint32Ptr(b []byte, f pointer, w int) ([]byte, error) {
934 if w != WireVarint {
935 return b, errInternalBadWireType
936 }
937 x, n := decodeVarint(b)
938 if n == 0 {
939 return nil, io.ErrUnexpectedEOF
940 }
941 b = b[n:]
942 v := int32(x>>1) ^ int32(x)<<31>>31
943 f.setInt32Ptr(v)
944 return b, nil
945}
946
947func unmarshalSint32Slice(b []byte, f pointer, w int) ([]byte, error) {
948 if w == WireBytes { // packed
949 x, n := decodeVarint(b)
950 if n == 0 {
951 return nil, io.ErrUnexpectedEOF
952 }
953 b = b[n:]
954 if x > uint64(len(b)) {
955 return nil, io.ErrUnexpectedEOF
956 }
957 res := b[x:]
958 b = b[:x]
959 for len(b) > 0 {
960 x, n = decodeVarint(b)
961 if n == 0 {
962 return nil, io.ErrUnexpectedEOF
963 }
964 b = b[n:]
965 v := int32(x>>1) ^ int32(x)<<31>>31
966 f.appendInt32Slice(v)
967 }
968 return res, nil
969 }
970 if w != WireVarint {
971 return b, errInternalBadWireType
972 }
973 x, n := decodeVarint(b)
974 if n == 0 {
975 return nil, io.ErrUnexpectedEOF
976 }
977 b = b[n:]
978 v := int32(x>>1) ^ int32(x)<<31>>31
979 f.appendInt32Slice(v)
980 return b, nil
981}
982
983func unmarshalUint32Value(b []byte, f pointer, w int) ([]byte, error) {
984 if w != WireVarint {
985 return b, errInternalBadWireType
986 }
987 x, n := decodeVarint(b)
988 if n == 0 {
989 return nil, io.ErrUnexpectedEOF
990 }
991 b = b[n:]
992 v := uint32(x)
993 *f.toUint32() = v
994 return b, nil
995}
996
997func unmarshalUint32Ptr(b []byte, f pointer, w int) ([]byte, error) {
998 if w != WireVarint {
999 return b, errInternalBadWireType
1000 }
1001 x, n := decodeVarint(b)
1002 if n == 0 {
1003 return nil, io.ErrUnexpectedEOF
1004 }
1005 b = b[n:]
1006 v := uint32(x)
1007 *f.toUint32Ptr() = &v
1008 return b, nil
1009}
1010
1011func unmarshalUint32Slice(b []byte, f pointer, w int) ([]byte, error) {
1012 if w == WireBytes { // packed
1013 x, n := decodeVarint(b)
1014 if n == 0 {
1015 return nil, io.ErrUnexpectedEOF
1016 }
1017 b = b[n:]
1018 if x > uint64(len(b)) {
1019 return nil, io.ErrUnexpectedEOF
1020 }
1021 res := b[x:]
1022 b = b[:x]
1023 for len(b) > 0 {
1024 x, n = decodeVarint(b)
1025 if n == 0 {
1026 return nil, io.ErrUnexpectedEOF
1027 }
1028 b = b[n:]
1029 v := uint32(x)
1030 s := f.toUint32Slice()
1031 *s = append(*s, v)
1032 }
1033 return res, nil
1034 }
1035 if w != WireVarint {
1036 return b, errInternalBadWireType
1037 }
1038 x, n := decodeVarint(b)
1039 if n == 0 {
1040 return nil, io.ErrUnexpectedEOF
1041 }
1042 b = b[n:]
1043 v := uint32(x)
1044 s := f.toUint32Slice()
1045 *s = append(*s, v)
1046 return b, nil
1047}
1048
1049func unmarshalFixed64Value(b []byte, f pointer, w int) ([]byte, error) {
1050 if w != WireFixed64 {
1051 return b, errInternalBadWireType
1052 }
1053 if len(b) < 8 {
1054 return nil, io.ErrUnexpectedEOF
1055 }
1056 v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
1057 *f.toUint64() = v
1058 return b[8:], nil
1059}
1060
1061func unmarshalFixed64Ptr(b []byte, f pointer, w int) ([]byte, error) {
1062 if w != WireFixed64 {
1063 return b, errInternalBadWireType
1064 }
1065 if len(b) < 8 {
1066 return nil, io.ErrUnexpectedEOF
1067 }
1068 v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
1069 *f.toUint64Ptr() = &v
1070 return b[8:], nil
1071}
1072
1073func unmarshalFixed64Slice(b []byte, f pointer, w int) ([]byte, error) {
1074 if w == WireBytes { // packed
1075 x, n := decodeVarint(b)
1076 if n == 0 {
1077 return nil, io.ErrUnexpectedEOF
1078 }
1079 b = b[n:]
1080 if x > uint64(len(b)) {
1081 return nil, io.ErrUnexpectedEOF
1082 }
1083 res := b[x:]
1084 b = b[:x]
1085 for len(b) > 0 {
1086 if len(b) < 8 {
1087 return nil, io.ErrUnexpectedEOF
1088 }
1089 v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
1090 s := f.toUint64Slice()
1091 *s = append(*s, v)
1092 b = b[8:]
1093 }
1094 return res, nil
1095 }
1096 if w != WireFixed64 {
1097 return b, errInternalBadWireType
1098 }
1099 if len(b) < 8 {
1100 return nil, io.ErrUnexpectedEOF
1101 }
1102 v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
1103 s := f.toUint64Slice()
1104 *s = append(*s, v)
1105 return b[8:], nil
1106}
1107
1108func unmarshalFixedS64Value(b []byte, f pointer, w int) ([]byte, error) {
1109 if w != WireFixed64 {
1110 return b, errInternalBadWireType
1111 }
1112 if len(b) < 8 {
1113 return nil, io.ErrUnexpectedEOF
1114 }
1115 v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
1116 *f.toInt64() = v
1117 return b[8:], nil
1118}
1119
1120func unmarshalFixedS64Ptr(b []byte, f pointer, w int) ([]byte, error) {
1121 if w != WireFixed64 {
1122 return b, errInternalBadWireType
1123 }
1124 if len(b) < 8 {
1125 return nil, io.ErrUnexpectedEOF
1126 }
1127 v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
1128 *f.toInt64Ptr() = &v
1129 return b[8:], nil
1130}
1131
1132func unmarshalFixedS64Slice(b []byte, f pointer, w int) ([]byte, error) {
1133 if w == WireBytes { // packed
1134 x, n := decodeVarint(b)
1135 if n == 0 {
1136 return nil, io.ErrUnexpectedEOF
1137 }
1138 b = b[n:]
1139 if x > uint64(len(b)) {
1140 return nil, io.ErrUnexpectedEOF
1141 }
1142 res := b[x:]
1143 b = b[:x]
1144 for len(b) > 0 {
1145 if len(b) < 8 {
1146 return nil, io.ErrUnexpectedEOF
1147 }
1148 v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
1149 s := f.toInt64Slice()
1150 *s = append(*s, v)
1151 b = b[8:]
1152 }
1153 return res, nil
1154 }
1155 if w != WireFixed64 {
1156 return b, errInternalBadWireType
1157 }
1158 if len(b) < 8 {
1159 return nil, io.ErrUnexpectedEOF
1160 }
1161 v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
1162 s := f.toInt64Slice()
1163 *s = append(*s, v)
1164 return b[8:], nil
1165}
1166
1167func unmarshalFixed32Value(b []byte, f pointer, w int) ([]byte, error) {
1168 if w != WireFixed32 {
1169 return b, errInternalBadWireType
1170 }
1171 if len(b) < 4 {
1172 return nil, io.ErrUnexpectedEOF
1173 }
1174 v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1175 *f.toUint32() = v
1176 return b[4:], nil
1177}
1178
1179func unmarshalFixed32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1180 if w != WireFixed32 {
1181 return b, errInternalBadWireType
1182 }
1183 if len(b) < 4 {
1184 return nil, io.ErrUnexpectedEOF
1185 }
1186 v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1187 *f.toUint32Ptr() = &v
1188 return b[4:], nil
1189}
1190
1191func unmarshalFixed32Slice(b []byte, f pointer, w int) ([]byte, error) {
1192 if w == WireBytes { // packed
1193 x, n := decodeVarint(b)
1194 if n == 0 {
1195 return nil, io.ErrUnexpectedEOF
1196 }
1197 b = b[n:]
1198 if x > uint64(len(b)) {
1199 return nil, io.ErrUnexpectedEOF
1200 }
1201 res := b[x:]
1202 b = b[:x]
1203 for len(b) > 0 {
1204 if len(b) < 4 {
1205 return nil, io.ErrUnexpectedEOF
1206 }
1207 v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1208 s := f.toUint32Slice()
1209 *s = append(*s, v)
1210 b = b[4:]
1211 }
1212 return res, nil
1213 }
1214 if w != WireFixed32 {
1215 return b, errInternalBadWireType
1216 }
1217 if len(b) < 4 {
1218 return nil, io.ErrUnexpectedEOF
1219 }
1220 v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1221 s := f.toUint32Slice()
1222 *s = append(*s, v)
1223 return b[4:], nil
1224}
1225
1226func unmarshalFixedS32Value(b []byte, f pointer, w int) ([]byte, error) {
1227 if w != WireFixed32 {
1228 return b, errInternalBadWireType
1229 }
1230 if len(b) < 4 {
1231 return nil, io.ErrUnexpectedEOF
1232 }
1233 v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
1234 *f.toInt32() = v
1235 return b[4:], nil
1236}
1237
1238func unmarshalFixedS32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1239 if w != WireFixed32 {
1240 return b, errInternalBadWireType
1241 }
1242 if len(b) < 4 {
1243 return nil, io.ErrUnexpectedEOF
1244 }
1245 v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
1246 f.setInt32Ptr(v)
1247 return b[4:], nil
1248}
1249
1250func unmarshalFixedS32Slice(b []byte, f pointer, w int) ([]byte, error) {
1251 if w == WireBytes { // packed
1252 x, n := decodeVarint(b)
1253 if n == 0 {
1254 return nil, io.ErrUnexpectedEOF
1255 }
1256 b = b[n:]
1257 if x > uint64(len(b)) {
1258 return nil, io.ErrUnexpectedEOF
1259 }
1260 res := b[x:]
1261 b = b[:x]
1262 for len(b) > 0 {
1263 if len(b) < 4 {
1264 return nil, io.ErrUnexpectedEOF
1265 }
1266 v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
1267 f.appendInt32Slice(v)
1268 b = b[4:]
1269 }
1270 return res, nil
1271 }
1272 if w != WireFixed32 {
1273 return b, errInternalBadWireType
1274 }
1275 if len(b) < 4 {
1276 return nil, io.ErrUnexpectedEOF
1277 }
1278 v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
1279 f.appendInt32Slice(v)
1280 return b[4:], nil
1281}
1282
1283func unmarshalBoolValue(b []byte, f pointer, w int) ([]byte, error) {
1284 if w != WireVarint {
1285 return b, errInternalBadWireType
1286 }
1287 // Note: any length varint is allowed, even though any sane
1288 // encoder will use one byte.
1289 // See https://github.com/golang/protobuf/issues/76
1290 x, n := decodeVarint(b)
1291 if n == 0 {
1292 return nil, io.ErrUnexpectedEOF
1293 }
1294 // TODO: check if x>1? Tests seem to indicate no.
1295 v := x != 0
1296 *f.toBool() = v
1297 return b[n:], nil
1298}
1299
1300func unmarshalBoolPtr(b []byte, f pointer, w int) ([]byte, error) {
1301 if w != WireVarint {
1302 return b, errInternalBadWireType
1303 }
1304 x, n := decodeVarint(b)
1305 if n == 0 {
1306 return nil, io.ErrUnexpectedEOF
1307 }
1308 v := x != 0
1309 *f.toBoolPtr() = &v
1310 return b[n:], nil
1311}
1312
1313func unmarshalBoolSlice(b []byte, f pointer, w int) ([]byte, error) {
1314 if w == WireBytes { // packed
1315 x, n := decodeVarint(b)
1316 if n == 0 {
1317 return nil, io.ErrUnexpectedEOF
1318 }
1319 b = b[n:]
1320 if x > uint64(len(b)) {
1321 return nil, io.ErrUnexpectedEOF
1322 }
1323 res := b[x:]
1324 b = b[:x]
1325 for len(b) > 0 {
1326 x, n = decodeVarint(b)
1327 if n == 0 {
1328 return nil, io.ErrUnexpectedEOF
1329 }
1330 v := x != 0
1331 s := f.toBoolSlice()
1332 *s = append(*s, v)
1333 b = b[n:]
1334 }
1335 return res, nil
1336 }
1337 if w != WireVarint {
1338 return b, errInternalBadWireType
1339 }
1340 x, n := decodeVarint(b)
1341 if n == 0 {
1342 return nil, io.ErrUnexpectedEOF
1343 }
1344 v := x != 0
1345 s := f.toBoolSlice()
1346 *s = append(*s, v)
1347 return b[n:], nil
1348}
1349
1350func unmarshalFloat64Value(b []byte, f pointer, w int) ([]byte, error) {
1351 if w != WireFixed64 {
1352 return b, errInternalBadWireType
1353 }
1354 if len(b) < 8 {
1355 return nil, io.ErrUnexpectedEOF
1356 }
1357 v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
1358 *f.toFloat64() = v
1359 return b[8:], nil
1360}
1361
1362func unmarshalFloat64Ptr(b []byte, f pointer, w int) ([]byte, error) {
1363 if w != WireFixed64 {
1364 return b, errInternalBadWireType
1365 }
1366 if len(b) < 8 {
1367 return nil, io.ErrUnexpectedEOF
1368 }
1369 v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
1370 *f.toFloat64Ptr() = &v
1371 return b[8:], nil
1372}
1373
1374func unmarshalFloat64Slice(b []byte, f pointer, w int) ([]byte, error) {
1375 if w == WireBytes { // packed
1376 x, n := decodeVarint(b)
1377 if n == 0 {
1378 return nil, io.ErrUnexpectedEOF
1379 }
1380 b = b[n:]
1381 if x > uint64(len(b)) {
1382 return nil, io.ErrUnexpectedEOF
1383 }
1384 res := b[x:]
1385 b = b[:x]
1386 for len(b) > 0 {
1387 if len(b) < 8 {
1388 return nil, io.ErrUnexpectedEOF
1389 }
1390 v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
1391 s := f.toFloat64Slice()
1392 *s = append(*s, v)
1393 b = b[8:]
1394 }
1395 return res, nil
1396 }
1397 if w != WireFixed64 {
1398 return b, errInternalBadWireType
1399 }
1400 if len(b) < 8 {
1401 return nil, io.ErrUnexpectedEOF
1402 }
1403 v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
1404 s := f.toFloat64Slice()
1405 *s = append(*s, v)
1406 return b[8:], nil
1407}
1408
1409func unmarshalFloat32Value(b []byte, f pointer, w int) ([]byte, error) {
1410 if w != WireFixed32 {
1411 return b, errInternalBadWireType
1412 }
1413 if len(b) < 4 {
1414 return nil, io.ErrUnexpectedEOF
1415 }
1416 v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
1417 *f.toFloat32() = v
1418 return b[4:], nil
1419}
1420
1421func unmarshalFloat32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1422 if w != WireFixed32 {
1423 return b, errInternalBadWireType
1424 }
1425 if len(b) < 4 {
1426 return nil, io.ErrUnexpectedEOF
1427 }
1428 v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
1429 *f.toFloat32Ptr() = &v
1430 return b[4:], nil
1431}
1432
1433func unmarshalFloat32Slice(b []byte, f pointer, w int) ([]byte, error) {
1434 if w == WireBytes { // packed
1435 x, n := decodeVarint(b)
1436 if n == 0 {
1437 return nil, io.ErrUnexpectedEOF
1438 }
1439 b = b[n:]
1440 if x > uint64(len(b)) {
1441 return nil, io.ErrUnexpectedEOF
1442 }
1443 res := b[x:]
1444 b = b[:x]
1445 for len(b) > 0 {
1446 if len(b) < 4 {
1447 return nil, io.ErrUnexpectedEOF
1448 }
1449 v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
1450 s := f.toFloat32Slice()
1451 *s = append(*s, v)
1452 b = b[4:]
1453 }
1454 return res, nil
1455 }
1456 if w != WireFixed32 {
1457 return b, errInternalBadWireType
1458 }
1459 if len(b) < 4 {
1460 return nil, io.ErrUnexpectedEOF
1461 }
1462 v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
1463 s := f.toFloat32Slice()
1464 *s = append(*s, v)
1465 return b[4:], nil
1466}
1467
1468func unmarshalStringValue(b []byte, f pointer, w int) ([]byte, error) {
1469 if w != WireBytes {
1470 return b, errInternalBadWireType
1471 }
1472 x, n := decodeVarint(b)
1473 if n == 0 {
1474 return nil, io.ErrUnexpectedEOF
1475 }
1476 b = b[n:]
1477 if x > uint64(len(b)) {
1478 return nil, io.ErrUnexpectedEOF
1479 }
1480 v := string(b[:x])
1481 *f.toString() = v
1482 return b[x:], nil
1483}
1484
1485func unmarshalStringPtr(b []byte, f pointer, w int) ([]byte, error) {
1486 if w != WireBytes {
1487 return b, errInternalBadWireType
1488 }
1489 x, n := decodeVarint(b)
1490 if n == 0 {
1491 return nil, io.ErrUnexpectedEOF
1492 }
1493 b = b[n:]
1494 if x > uint64(len(b)) {
1495 return nil, io.ErrUnexpectedEOF
1496 }
1497 v := string(b[:x])
1498 *f.toStringPtr() = &v
1499 return b[x:], nil
1500}
1501
1502func unmarshalStringSlice(b []byte, f pointer, w int) ([]byte, error) {
1503 if w != WireBytes {
1504 return b, errInternalBadWireType
1505 }
1506 x, n := decodeVarint(b)
1507 if n == 0 {
1508 return nil, io.ErrUnexpectedEOF
1509 }
1510 b = b[n:]
1511 if x > uint64(len(b)) {
1512 return nil, io.ErrUnexpectedEOF
1513 }
1514 v := string(b[:x])
1515 s := f.toStringSlice()
1516 *s = append(*s, v)
1517 return b[x:], nil
1518}
1519
1520func unmarshalUTF8StringValue(b []byte, f pointer, w int) ([]byte, error) {
1521 if w != WireBytes {
1522 return b, errInternalBadWireType
1523 }
1524 x, n := decodeVarint(b)
1525 if n == 0 {
1526 return nil, io.ErrUnexpectedEOF
1527 }
1528 b = b[n:]
1529 if x > uint64(len(b)) {
1530 return nil, io.ErrUnexpectedEOF
1531 }
1532 v := string(b[:x])
1533 *f.toString() = v
1534 if !utf8.ValidString(v) {
1535 return b[x:], errInvalidUTF8
1536 }
1537 return b[x:], nil
1538}
1539
1540func unmarshalUTF8StringPtr(b []byte, f pointer, w int) ([]byte, error) {
1541 if w != WireBytes {
1542 return b, errInternalBadWireType
1543 }
1544 x, n := decodeVarint(b)
1545 if n == 0 {
1546 return nil, io.ErrUnexpectedEOF
1547 }
1548 b = b[n:]
1549 if x > uint64(len(b)) {
1550 return nil, io.ErrUnexpectedEOF
1551 }
1552 v := string(b[:x])
1553 *f.toStringPtr() = &v
1554 if !utf8.ValidString(v) {
1555 return b[x:], errInvalidUTF8
1556 }
1557 return b[x:], nil
1558}
1559
1560func unmarshalUTF8StringSlice(b []byte, f pointer, w int) ([]byte, error) {
1561 if w != WireBytes {
1562 return b, errInternalBadWireType
1563 }
1564 x, n := decodeVarint(b)
1565 if n == 0 {
1566 return nil, io.ErrUnexpectedEOF
1567 }
1568 b = b[n:]
1569 if x > uint64(len(b)) {
1570 return nil, io.ErrUnexpectedEOF
1571 }
1572 v := string(b[:x])
1573 s := f.toStringSlice()
1574 *s = append(*s, v)
1575 if !utf8.ValidString(v) {
1576 return b[x:], errInvalidUTF8
1577 }
1578 return b[x:], nil
1579}
1580
1581var emptyBuf [0]byte
1582
1583func unmarshalBytesValue(b []byte, f pointer, w int) ([]byte, error) {
1584 if w != WireBytes {
1585 return b, errInternalBadWireType
1586 }
1587 x, n := decodeVarint(b)
1588 if n == 0 {
1589 return nil, io.ErrUnexpectedEOF
1590 }
1591 b = b[n:]
1592 if x > uint64(len(b)) {
1593 return nil, io.ErrUnexpectedEOF
1594 }
1595 // The use of append here is a trick which avoids the zeroing
1596 // that would be required if we used a make/copy pair.
1597 // We append to emptyBuf instead of nil because we want
1598 // a non-nil result even when the length is 0.
1599 v := append(emptyBuf[:], b[:x]...)
1600 *f.toBytes() = v
1601 return b[x:], nil
1602}
1603
1604func unmarshalBytesSlice(b []byte, f pointer, w int) ([]byte, error) {
1605 if w != WireBytes {
1606 return b, errInternalBadWireType
1607 }
1608 x, n := decodeVarint(b)
1609 if n == 0 {
1610 return nil, io.ErrUnexpectedEOF
1611 }
1612 b = b[n:]
1613 if x > uint64(len(b)) {
1614 return nil, io.ErrUnexpectedEOF
1615 }
1616 v := append(emptyBuf[:], b[:x]...)
1617 s := f.toBytesSlice()
1618 *s = append(*s, v)
1619 return b[x:], nil
1620}
1621
1622func makeUnmarshalMessagePtr(sub *unmarshalInfo, name string) unmarshaler {
1623 return func(b []byte, f pointer, w int) ([]byte, error) {
1624 if w != WireBytes {
1625 return b, errInternalBadWireType
1626 }
1627 x, n := decodeVarint(b)
1628 if n == 0 {
1629 return nil, io.ErrUnexpectedEOF
1630 }
1631 b = b[n:]
1632 if x > uint64(len(b)) {
1633 return nil, io.ErrUnexpectedEOF
1634 }
1635 // First read the message field to see if something is there.
1636 // The semantics of multiple submessages are weird. Instead of
1637 // the last one winning (as it is for all other fields), multiple
1638 // submessages are merged.
1639 v := f.getPointer()
1640 if v.isNil() {
1641 v = valToPointer(reflect.New(sub.typ))
1642 f.setPointer(v)
1643 }
1644 err := sub.unmarshal(v, b[:x])
1645 if err != nil {
1646 if r, ok := err.(*RequiredNotSetError); ok {
1647 r.field = name + "." + r.field
1648 } else {
1649 return nil, err
1650 }
1651 }
1652 return b[x:], err
1653 }
1654}
1655
1656func makeUnmarshalMessageSlicePtr(sub *unmarshalInfo, name string) unmarshaler {
1657 return func(b []byte, f pointer, w int) ([]byte, error) {
1658 if w != WireBytes {
1659 return b, errInternalBadWireType
1660 }
1661 x, n := decodeVarint(b)
1662 if n == 0 {
1663 return nil, io.ErrUnexpectedEOF
1664 }
1665 b = b[n:]
1666 if x > uint64(len(b)) {
1667 return nil, io.ErrUnexpectedEOF
1668 }
1669 v := valToPointer(reflect.New(sub.typ))
1670 err := sub.unmarshal(v, b[:x])
1671 if err != nil {
1672 if r, ok := err.(*RequiredNotSetError); ok {
1673 r.field = name + "." + r.field
1674 } else {
1675 return nil, err
1676 }
1677 }
1678 f.appendPointer(v)
1679 return b[x:], err
1680 }
1681}
1682
1683func makeUnmarshalGroupPtr(sub *unmarshalInfo, name string) unmarshaler {
1684 return func(b []byte, f pointer, w int) ([]byte, error) {
1685 if w != WireStartGroup {
1686 return b, errInternalBadWireType
1687 }
1688 x, y := findEndGroup(b)
1689 if x < 0 {
1690 return nil, io.ErrUnexpectedEOF
1691 }
1692 v := f.getPointer()
1693 if v.isNil() {
1694 v = valToPointer(reflect.New(sub.typ))
1695 f.setPointer(v)
1696 }
1697 err := sub.unmarshal(v, b[:x])
1698 if err != nil {
1699 if r, ok := err.(*RequiredNotSetError); ok {
1700 r.field = name + "." + r.field
1701 } else {
1702 return nil, err
1703 }
1704 }
1705 return b[y:], err
1706 }
1707}
1708
1709func makeUnmarshalGroupSlicePtr(sub *unmarshalInfo, name string) unmarshaler {
1710 return func(b []byte, f pointer, w int) ([]byte, error) {
1711 if w != WireStartGroup {
1712 return b, errInternalBadWireType
1713 }
1714 x, y := findEndGroup(b)
1715 if x < 0 {
1716 return nil, io.ErrUnexpectedEOF
1717 }
1718 v := valToPointer(reflect.New(sub.typ))
1719 err := sub.unmarshal(v, b[:x])
1720 if err != nil {
1721 if r, ok := err.(*RequiredNotSetError); ok {
1722 r.field = name + "." + r.field
1723 } else {
1724 return nil, err
1725 }
1726 }
1727 f.appendPointer(v)
1728 return b[y:], err
1729 }
1730}
1731
1732func makeUnmarshalMap(f *reflect.StructField) unmarshaler {
1733 t := f.Type
1734 kt := t.Key()
1735 vt := t.Elem()
1736 unmarshalKey := typeUnmarshaler(kt, f.Tag.Get("protobuf_key"))
1737 unmarshalVal := typeUnmarshaler(vt, f.Tag.Get("protobuf_val"))
1738 return func(b []byte, f pointer, w int) ([]byte, error) {
1739 // The map entry is a submessage. Figure out how big it is.
1740 if w != WireBytes {
1741 return nil, fmt.Errorf("proto: bad wiretype for map field: got %d want %d", w, WireBytes)
1742 }
1743 x, n := decodeVarint(b)
1744 if n == 0 {
1745 return nil, io.ErrUnexpectedEOF
1746 }
1747 b = b[n:]
1748 if x > uint64(len(b)) {
1749 return nil, io.ErrUnexpectedEOF
1750 }
1751 r := b[x:] // unused data to return
1752 b = b[:x] // data for map entry
1753
1754 // Note: we could use #keys * #values ~= 200 functions
1755 // to do map decoding without reflection. Probably not worth it.
1756 // Maps will be somewhat slow. Oh well.
1757
1758 // Read key and value from data.
1759 var nerr nonFatal
1760 k := reflect.New(kt)
1761 v := reflect.New(vt)
1762 for len(b) > 0 {
1763 x, n := decodeVarint(b)
1764 if n == 0 {
1765 return nil, io.ErrUnexpectedEOF
1766 }
1767 wire := int(x) & 7
1768 b = b[n:]
1769
1770 var err error
1771 switch x >> 3 {
1772 case 1:
1773 b, err = unmarshalKey(b, valToPointer(k), wire)
1774 case 2:
1775 b, err = unmarshalVal(b, valToPointer(v), wire)
1776 default:
1777 err = errInternalBadWireType // skip unknown tag
1778 }
1779
1780 if nerr.Merge(err) {
1781 continue
1782 }
1783 if err != errInternalBadWireType {
1784 return nil, err
1785 }
1786
1787 // Skip past unknown fields.
1788 b, err = skipField(b, wire)
1789 if err != nil {
1790 return nil, err
1791 }
1792 }
1793
1794 // Get map, allocate if needed.
1795 m := f.asPointerTo(t).Elem() // an addressable map[K]T
1796 if m.IsNil() {
1797 m.Set(reflect.MakeMap(t))
1798 }
1799
1800 // Insert into map.
1801 m.SetMapIndex(k.Elem(), v.Elem())
1802
1803 return r, nerr.E
1804 }
1805}
1806
1807// makeUnmarshalOneof makes an unmarshaler for oneof fields.
1808// for:
1809// message Msg {
1810// oneof F {
1811// int64 X = 1;
1812// float64 Y = 2;
1813// }
1814// }
1815// typ is the type of the concrete entry for a oneof case (e.g. Msg_X).
1816// ityp is the interface type of the oneof field (e.g. isMsg_F).
1817// unmarshal is the unmarshaler for the base type of the oneof case (e.g. int64).
1818// Note that this function will be called once for each case in the oneof.
1819func makeUnmarshalOneof(typ, ityp reflect.Type, unmarshal unmarshaler) unmarshaler {
1820 sf := typ.Field(0)
1821 field0 := toField(&sf)
1822 return func(b []byte, f pointer, w int) ([]byte, error) {
1823 // Allocate holder for value.
1824 v := reflect.New(typ)
1825
1826 // Unmarshal data into holder.
1827 // We unmarshal into the first field of the holder object.
1828 var err error
1829 var nerr nonFatal
1830 b, err = unmarshal(b, valToPointer(v).offset(field0), w)
1831 if !nerr.Merge(err) {
1832 return nil, err
1833 }
1834
1835 // Write pointer to holder into target field.
1836 f.asPointerTo(ityp).Elem().Set(v)
1837
1838 return b, nerr.E
1839 }
1840}
1841
1842// Error used by decode internally.
1843var errInternalBadWireType = errors.New("proto: internal error: bad wiretype")
1844
1845// skipField skips past a field of type wire and returns the remaining bytes.
1846func skipField(b []byte, wire int) ([]byte, error) {
1847 switch wire {
1848 case WireVarint:
1849 _, k := decodeVarint(b)
1850 if k == 0 {
1851 return b, io.ErrUnexpectedEOF
1852 }
1853 b = b[k:]
1854 case WireFixed32:
1855 if len(b) < 4 {
1856 return b, io.ErrUnexpectedEOF
1857 }
1858 b = b[4:]
1859 case WireFixed64:
1860 if len(b) < 8 {
1861 return b, io.ErrUnexpectedEOF
1862 }
1863 b = b[8:]
1864 case WireBytes:
1865 m, k := decodeVarint(b)
1866 if k == 0 || uint64(len(b)-k) < m {
1867 return b, io.ErrUnexpectedEOF
1868 }
1869 b = b[uint64(k)+m:]
1870 case WireStartGroup:
1871 _, i := findEndGroup(b)
1872 if i == -1 {
1873 return b, io.ErrUnexpectedEOF
1874 }
1875 b = b[i:]
1876 default:
1877 return b, fmt.Errorf("proto: can't skip unknown wire type %d", wire)
1878 }
1879 return b, nil
1880}
1881
1882// findEndGroup finds the index of the next EndGroup tag.
1883// Groups may be nested, so the "next" EndGroup tag is the first
1884// unpaired EndGroup.
1885// findEndGroup returns the indexes of the start and end of the EndGroup tag.
1886// Returns (-1,-1) if it can't find one.
1887func findEndGroup(b []byte) (int, int) {
1888 depth := 1
1889 i := 0
1890 for {
1891 x, n := decodeVarint(b[i:])
1892 if n == 0 {
1893 return -1, -1
1894 }
1895 j := i
1896 i += n
1897 switch x & 7 {
1898 case WireVarint:
1899 _, k := decodeVarint(b[i:])
1900 if k == 0 {
1901 return -1, -1
1902 }
1903 i += k
1904 case WireFixed32:
1905 if len(b)-4 < i {
1906 return -1, -1
1907 }
1908 i += 4
1909 case WireFixed64:
1910 if len(b)-8 < i {
1911 return -1, -1
1912 }
1913 i += 8
1914 case WireBytes:
1915 m, k := decodeVarint(b[i:])
1916 if k == 0 {
1917 return -1, -1
1918 }
1919 i += k
1920 if uint64(len(b)-i) < m {
1921 return -1, -1
1922 }
1923 i += int(m)
1924 case WireStartGroup:
1925 depth++
1926 case WireEndGroup:
1927 depth--
1928 if depth == 0 {
1929 return j, i
1930 }
1931 default:
1932 return -1, -1
1933 }
1934 }
1935}
1936
1937// encodeVarint appends a varint-encoded integer to b and returns the result.
1938func encodeVarint(b []byte, x uint64) []byte {
1939 for x >= 1<<7 {
1940 b = append(b, byte(x&0x7f|0x80))
1941 x >>= 7
1942 }
1943 return append(b, byte(x))
1944}
1945
1946// decodeVarint reads a varint-encoded integer from b.
1947// Returns the decoded integer and the number of bytes read.
1948// If there is an error, it returns 0,0.
1949func decodeVarint(b []byte) (uint64, int) {
1950 var x, y uint64
1951 if len(b) <= 0 {
1952 goto bad
1953 }
1954 x = uint64(b[0])
1955 if x < 0x80 {
1956 return x, 1
1957 }
1958 x -= 0x80
1959
1960 if len(b) <= 1 {
1961 goto bad
1962 }
1963 y = uint64(b[1])
1964 x += y << 7
1965 if y < 0x80 {
1966 return x, 2
1967 }
1968 x -= 0x80 << 7
1969
1970 if len(b) <= 2 {
1971 goto bad
1972 }
1973 y = uint64(b[2])
1974 x += y << 14
1975 if y < 0x80 {
1976 return x, 3
1977 }
1978 x -= 0x80 << 14
1979
1980 if len(b) <= 3 {
1981 goto bad
1982 }
1983 y = uint64(b[3])
1984 x += y << 21
1985 if y < 0x80 {
1986 return x, 4
1987 }
1988 x -= 0x80 << 21
1989
1990 if len(b) <= 4 {
1991 goto bad
1992 }
1993 y = uint64(b[4])
1994 x += y << 28
1995 if y < 0x80 {
1996 return x, 5
1997 }
1998 x -= 0x80 << 28
1999
2000 if len(b) <= 5 {
2001 goto bad
2002 }
2003 y = uint64(b[5])
2004 x += y << 35
2005 if y < 0x80 {
2006 return x, 6
2007 }
2008 x -= 0x80 << 35
2009
2010 if len(b) <= 6 {
2011 goto bad
2012 }
2013 y = uint64(b[6])
2014 x += y << 42
2015 if y < 0x80 {
2016 return x, 7
2017 }
2018 x -= 0x80 << 42
2019
2020 if len(b) <= 7 {
2021 goto bad
2022 }
2023 y = uint64(b[7])
2024 x += y << 49
2025 if y < 0x80 {
2026 return x, 8
2027 }
2028 x -= 0x80 << 49
2029
2030 if len(b) <= 8 {
2031 goto bad
2032 }
2033 y = uint64(b[8])
2034 x += y << 56
2035 if y < 0x80 {
2036 return x, 9
2037 }
2038 x -= 0x80 << 56
2039
2040 if len(b) <= 9 {
2041 goto bad
2042 }
2043 y = uint64(b[9])
2044 x += y << 63
2045 if y < 2 {
2046 return x, 10
2047 }
2048
2049bad:
2050 return 0, 0
2051}
diff --git a/vendor/github.com/golang/protobuf/proto/text.go b/vendor/github.com/golang/protobuf/proto/text.go
new file mode 100644
index 0000000..1aaee72
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/text.go
@@ -0,0 +1,843 @@
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
32package proto
33
34// Functions for writing the text protocol buffer format.
35
36import (
37 "bufio"
38 "bytes"
39 "encoding"
40 "errors"
41 "fmt"
42 "io"
43 "log"
44 "math"
45 "reflect"
46 "sort"
47 "strings"
48)
49
50var (
51 newline = []byte("\n")
52 spaces = []byte(" ")
53 endBraceNewline = []byte("}\n")
54 backslashN = []byte{'\\', 'n'}
55 backslashR = []byte{'\\', 'r'}
56 backslashT = []byte{'\\', 't'}
57 backslashDQ = []byte{'\\', '"'}
58 backslashBS = []byte{'\\', '\\'}
59 posInf = []byte("inf")
60 negInf = []byte("-inf")
61 nan = []byte("nan")
62)
63
64type writer interface {
65 io.Writer
66 WriteByte(byte) error
67}
68
69// textWriter is an io.Writer that tracks its indentation level.
70type textWriter struct {
71 ind int
72 complete bool // if the current position is a complete line
73 compact bool // whether to write out as a one-liner
74 w writer
75}
76
77func (w *textWriter) WriteString(s string) (n int, err error) {
78 if !strings.Contains(s, "\n") {
79 if !w.compact && w.complete {
80 w.writeIndent()
81 }
82 w.complete = false
83 return io.WriteString(w.w, s)
84 }
85 // WriteString is typically called without newlines, so this
86 // codepath and its copy are rare. We copy to avoid
87 // duplicating all of Write's logic here.
88 return w.Write([]byte(s))
89}
90
91func (w *textWriter) Write(p []byte) (n int, err error) {
92 newlines := bytes.Count(p, newline)
93 if newlines == 0 {
94 if !w.compact && w.complete {
95 w.writeIndent()
96 }
97 n, err = w.w.Write(p)
98 w.complete = false
99 return n, err
100 }
101
102 frags := bytes.SplitN(p, newline, newlines+1)
103 if w.compact {
104 for i, frag := range frags {
105 if i > 0 {
106 if err := w.w.WriteByte(' '); err != nil {
107 return n, err
108 }
109 n++
110 }
111 nn, err := w.w.Write(frag)
112 n += nn
113 if err != nil {
114 return n, err
115 }
116 }
117 return n, nil
118 }
119
120 for i, frag := range frags {
121 if w.complete {
122 w.writeIndent()
123 }
124 nn, err := w.w.Write(frag)
125 n += nn
126 if err != nil {
127 return n, err
128 }
129 if i+1 < len(frags) {
130 if err := w.w.WriteByte('\n'); err != nil {
131 return n, err
132 }
133 n++
134 }
135 }
136 w.complete = len(frags[len(frags)-1]) == 0
137 return n, nil
138}
139
140func (w *textWriter) WriteByte(c byte) error {
141 if w.compact && c == '\n' {
142 c = ' '
143 }
144 if !w.compact && w.complete {
145 w.writeIndent()
146 }
147 err := w.w.WriteByte(c)
148 w.complete = c == '\n'
149 return err
150}
151
152func (w *textWriter) indent() { w.ind++ }
153
154func (w *textWriter) unindent() {
155 if w.ind == 0 {
156 log.Print("proto: textWriter unindented too far")
157 return
158 }
159 w.ind--
160}
161
162func writeName(w *textWriter, props *Properties) error {
163 if _, err := w.WriteString(props.OrigName); err != nil {
164 return err
165 }
166 if props.Wire != "group" {
167 return w.WriteByte(':')
168 }
169 return nil
170}
171
172func requiresQuotes(u string) bool {
173 // When type URL contains any characters except [0-9A-Za-z./\-]*, it must be quoted.
174 for _, ch := range u {
175 switch {
176 case ch == '.' || ch == '/' || ch == '_':
177 continue
178 case '0' <= ch && ch <= '9':
179 continue
180 case 'A' <= ch && ch <= 'Z':
181 continue
182 case 'a' <= ch && ch <= 'z':
183 continue
184 default:
185 return true
186 }
187 }
188 return false
189}
190
191// isAny reports whether sv is a google.protobuf.Any message
192func isAny(sv reflect.Value) bool {
193 type wkt interface {
194 XXX_WellKnownType() string
195 }
196 t, ok := sv.Addr().Interface().(wkt)
197 return ok && t.XXX_WellKnownType() == "Any"
198}
199
200// writeProto3Any writes an expanded google.protobuf.Any message.
201//
202// It returns (false, nil) if sv value can't be unmarshaled (e.g. because
203// required messages are not linked in).
204//
205// It returns (true, error) when sv was written in expanded format or an error
206// was encountered.
207func (tm *TextMarshaler) writeProto3Any(w *textWriter, sv reflect.Value) (bool, error) {
208 turl := sv.FieldByName("TypeUrl")
209 val := sv.FieldByName("Value")
210 if !turl.IsValid() || !val.IsValid() {
211 return true, errors.New("proto: invalid google.protobuf.Any message")
212 }
213
214 b, ok := val.Interface().([]byte)
215 if !ok {
216 return true, errors.New("proto: invalid google.protobuf.Any message")
217 }
218
219 parts := strings.Split(turl.String(), "/")
220 mt := MessageType(parts[len(parts)-1])
221 if mt == nil {
222 return false, nil
223 }
224 m := reflect.New(mt.Elem())
225 if err := Unmarshal(b, m.Interface().(Message)); err != nil {
226 return false, nil
227 }
228 w.Write([]byte("["))
229 u := turl.String()
230 if requiresQuotes(u) {
231 writeString(w, u)
232 } else {
233 w.Write([]byte(u))
234 }
235 if w.compact {
236 w.Write([]byte("]:<"))
237 } else {
238 w.Write([]byte("]: <\n"))
239 w.ind++
240 }
241 if err := tm.writeStruct(w, m.Elem()); err != nil {
242 return true, err
243 }
244 if w.compact {
245 w.Write([]byte("> "))
246 } else {
247 w.ind--
248 w.Write([]byte(">\n"))
249 }
250 return true, nil
251}
252
253func (tm *TextMarshaler) writeStruct(w *textWriter, sv reflect.Value) error {
254 if tm.ExpandAny && isAny(sv) {
255 if canExpand, err := tm.writeProto3Any(w, sv); canExpand {
256 return err
257 }
258 }
259 st := sv.Type()
260 sprops := GetProperties(st)
261 for i := 0; i < sv.NumField(); i++ {
262 fv := sv.Field(i)
263 props := sprops.Prop[i]
264 name := st.Field(i).Name
265
266 if name == "XXX_NoUnkeyedLiteral" {
267 continue
268 }
269
270 if strings.HasPrefix(name, "XXX_") {
271 // There are two XXX_ fields:
272 // XXX_unrecognized []byte
273 // XXX_extensions map[int32]proto.Extension
274 // The first is handled here;
275 // the second is handled at the bottom of this function.
276 if name == "XXX_unrecognized" && !fv.IsNil() {
277 if err := writeUnknownStruct(w, fv.Interface().([]byte)); err != nil {
278 return err
279 }
280 }
281 continue
282 }
283 if fv.Kind() == reflect.Ptr && fv.IsNil() {
284 // Field not filled in. This could be an optional field or
285 // a required field that wasn't filled in. Either way, there
286 // isn't anything we can show for it.
287 continue
288 }
289 if fv.Kind() == reflect.Slice && fv.IsNil() {
290 // Repeated field that is empty, or a bytes field that is unused.
291 continue
292 }
293
294 if props.Repeated && fv.Kind() == reflect.Slice {
295 // Repeated field.
296 for j := 0; j < fv.Len(); j++ {
297 if err := writeName(w, props); err != nil {
298 return err
299 }
300 if !w.compact {
301 if err := w.WriteByte(' '); err != nil {
302 return err
303 }
304 }
305 v := fv.Index(j)
306 if v.Kind() == reflect.Ptr && v.IsNil() {
307 // A nil message in a repeated field is not valid,
308 // but we can handle that more gracefully than panicking.
309 if _, err := w.Write([]byte("<nil>\n")); err != nil {
310 return err
311 }
312 continue
313 }
314 if err := tm.writeAny(w, v, props); err != nil {
315 return err
316 }
317 if err := w.WriteByte('\n'); err != nil {
318 return err
319 }
320 }
321 continue
322 }
323 if fv.Kind() == reflect.Map {
324 // Map fields are rendered as a repeated struct with key/value fields.
325 keys := fv.MapKeys()
326 sort.Sort(mapKeys(keys))
327 for _, key := range keys {
328 val := fv.MapIndex(key)
329 if err := writeName(w, props); err != nil {
330 return err
331 }
332 if !w.compact {
333 if err := w.WriteByte(' '); err != nil {
334 return err
335 }
336 }
337 // open struct
338 if err := w.WriteByte('<'); err != nil {
339 return err
340 }
341 if !w.compact {
342 if err := w.WriteByte('\n'); err != nil {
343 return err
344 }
345 }
346 w.indent()
347 // key
348 if _, err := w.WriteString("key:"); err != nil {
349 return err
350 }
351 if !w.compact {
352 if err := w.WriteByte(' '); err != nil {
353 return err
354 }
355 }
356 if err := tm.writeAny(w, key, props.MapKeyProp); err != nil {
357 return err
358 }
359 if err := w.WriteByte('\n'); err != nil {
360 return err
361 }
362 // nil values aren't legal, but we can avoid panicking because of them.
363 if val.Kind() != reflect.Ptr || !val.IsNil() {
364 // value
365 if _, err := w.WriteString("value:"); err != nil {
366 return err
367 }
368 if !w.compact {
369 if err := w.WriteByte(' '); err != nil {
370 return err
371 }
372 }
373 if err := tm.writeAny(w, val, props.MapValProp); err != nil {
374 return err
375 }
376 if err := w.WriteByte('\n'); err != nil {
377 return err
378 }
379 }
380 // close struct
381 w.unindent()
382 if err := w.WriteByte('>'); err != nil {
383 return err
384 }
385 if err := w.WriteByte('\n'); err != nil {
386 return err
387 }
388 }
389 continue
390 }
391 if props.proto3 && fv.Kind() == reflect.Slice && fv.Len() == 0 {
392 // empty bytes field
393 continue
394 }
395 if fv.Kind() != reflect.Ptr && fv.Kind() != reflect.Slice {
396 // proto3 non-repeated scalar field; skip if zero value
397 if isProto3Zero(fv) {
398 continue
399 }
400 }
401
402 if fv.Kind() == reflect.Interface {
403 // Check if it is a oneof.
404 if st.Field(i).Tag.Get("protobuf_oneof") != "" {
405 // fv is nil, or holds a pointer to generated struct.
406 // That generated struct has exactly one field,
407 // which has a protobuf struct tag.
408 if fv.IsNil() {
409 continue
410 }
411 inner := fv.Elem().Elem() // interface -> *T -> T
412 tag := inner.Type().Field(0).Tag.Get("protobuf")
413 props = new(Properties) // Overwrite the outer props var, but not its pointee.
414 props.Parse(tag)
415 // Write the value in the oneof, not the oneof itself.
416 fv = inner.Field(0)
417
418 // Special case to cope with malformed messages gracefully:
419 // If the value in the oneof is a nil pointer, don't panic
420 // in writeAny.
421 if fv.Kind() == reflect.Ptr && fv.IsNil() {
422 // Use errors.New so writeAny won't render quotes.
423 msg := errors.New("/* nil */")
424 fv = reflect.ValueOf(&msg).Elem()
425 }
426 }
427 }
428
429 if err := writeName(w, props); err != nil {
430 return err
431 }
432 if !w.compact {
433 if err := w.WriteByte(' '); err != nil {
434 return err
435 }
436 }
437
438 // Enums have a String method, so writeAny will work fine.
439 if err := tm.writeAny(w, fv, props); err != nil {
440 return err
441 }
442
443 if err := w.WriteByte('\n'); err != nil {
444 return err
445 }
446 }
447
448 // Extensions (the XXX_extensions field).
449 pv := sv.Addr()
450 if _, err := extendable(pv.Interface()); err == nil {
451 if err := tm.writeExtensions(w, pv); err != nil {
452 return err
453 }
454 }
455
456 return nil
457}
458
459// writeAny writes an arbitrary field.
460func (tm *TextMarshaler) writeAny(w *textWriter, v reflect.Value, props *Properties) error {
461 v = reflect.Indirect(v)
462
463 // Floats have special cases.
464 if v.Kind() == reflect.Float32 || v.Kind() == reflect.Float64 {
465 x := v.Float()
466 var b []byte
467 switch {
468 case math.IsInf(x, 1):
469 b = posInf
470 case math.IsInf(x, -1):
471 b = negInf
472 case math.IsNaN(x):
473 b = nan
474 }
475 if b != nil {
476 _, err := w.Write(b)
477 return err
478 }
479 // Other values are handled below.
480 }
481
482 // We don't attempt to serialise every possible value type; only those
483 // that can occur in protocol buffers.
484 switch v.Kind() {
485 case reflect.Slice:
486 // Should only be a []byte; repeated fields are handled in writeStruct.
487 if err := writeString(w, string(v.Bytes())); err != nil {
488 return err
489 }
490 case reflect.String:
491 if err := writeString(w, v.String()); err != nil {
492 return err
493 }
494 case reflect.Struct:
495 // Required/optional group/message.
496 var bra, ket byte = '<', '>'
497 if props != nil && props.Wire == "group" {
498 bra, ket = '{', '}'
499 }
500 if err := w.WriteByte(bra); err != nil {
501 return err
502 }
503 if !w.compact {
504 if err := w.WriteByte('\n'); err != nil {
505 return err
506 }
507 }
508 w.indent()
509 if v.CanAddr() {
510 // Calling v.Interface on a struct causes the reflect package to
511 // copy the entire struct. This is racy with the new Marshaler
512 // since we atomically update the XXX_sizecache.
513 //
514 // Thus, we retrieve a pointer to the struct if possible to avoid
515 // a race since v.Interface on the pointer doesn't copy the struct.
516 //
517 // If v is not addressable, then we are not worried about a race
518 // since it implies that the binary Marshaler cannot possibly be
519 // mutating this value.
520 v = v.Addr()
521 }
522 if etm, ok := v.Interface().(encoding.TextMarshaler); ok {
523 text, err := etm.MarshalText()
524 if err != nil {
525 return err
526 }
527 if _, err = w.Write(text); err != nil {
528 return err
529 }
530 } else {
531 if v.Kind() == reflect.Ptr {
532 v = v.Elem()
533 }
534 if err := tm.writeStruct(w, v); err != nil {
535 return err
536 }
537 }
538 w.unindent()
539 if err := w.WriteByte(ket); err != nil {
540 return err
541 }
542 default:
543 _, err := fmt.Fprint(w, v.Interface())
544 return err
545 }
546 return nil
547}
548
549// equivalent to C's isprint.
550func isprint(c byte) bool {
551 return c >= 0x20 && c < 0x7f
552}
553
554// writeString writes a string in the protocol buffer text format.
555// It is similar to strconv.Quote except we don't use Go escape sequences,
556// we treat the string as a byte sequence, and we use octal escapes.
557// These differences are to maintain interoperability with the other
558// languages' implementations of the text format.
559func writeString(w *textWriter, s string) error {
560 // use WriteByte here to get any needed indent
561 if err := w.WriteByte('"'); err != nil {
562 return err
563 }
564 // Loop over the bytes, not the runes.
565 for i := 0; i < len(s); i++ {
566 var err error
567 // Divergence from C++: we don't escape apostrophes.
568 // There's no need to escape them, and the C++ parser
569 // copes with a naked apostrophe.
570 switch c := s[i]; c {
571 case '\n':
572 _, err = w.w.Write(backslashN)
573 case '\r':
574 _, err = w.w.Write(backslashR)
575 case '\t':
576 _, err = w.w.Write(backslashT)
577 case '"':
578 _, err = w.w.Write(backslashDQ)
579 case '\\':
580 _, err = w.w.Write(backslashBS)
581 default:
582 if isprint(c) {
583 err = w.w.WriteByte(c)
584 } else {
585 _, err = fmt.Fprintf(w.w, "\\%03o", c)
586 }
587 }
588 if err != nil {
589 return err
590 }
591 }
592 return w.WriteByte('"')
593}
594
595func writeUnknownStruct(w *textWriter, data []byte) (err error) {
596 if !w.compact {
597 if _, err := fmt.Fprintf(w, "/* %d unknown bytes */\n", len(data)); err != nil {
598 return err
599 }
600 }
601 b := NewBuffer(data)
602 for b.index < len(b.buf) {
603 x, err := b.DecodeVarint()
604 if err != nil {
605 _, err := fmt.Fprintf(w, "/* %v */\n", err)
606 return err
607 }
608 wire, tag := x&7, x>>3
609 if wire == WireEndGroup {
610 w.unindent()
611 if _, err := w.Write(endBraceNewline); err != nil {
612 return err
613 }
614 continue
615 }
616 if _, err := fmt.Fprint(w, tag); err != nil {
617 return err
618 }
619 if wire != WireStartGroup {
620 if err := w.WriteByte(':'); err != nil {
621 return err
622 }
623 }
624 if !w.compact || wire == WireStartGroup {
625 if err := w.WriteByte(' '); err != nil {
626 return err
627 }
628 }
629 switch wire {
630 case WireBytes:
631 buf, e := b.DecodeRawBytes(false)
632 if e == nil {
633 _, err = fmt.Fprintf(w, "%q", buf)
634 } else {
635 _, err = fmt.Fprintf(w, "/* %v */", e)
636 }
637 case WireFixed32:
638 x, err = b.DecodeFixed32()
639 err = writeUnknownInt(w, x, err)
640 case WireFixed64:
641 x, err = b.DecodeFixed64()
642 err = writeUnknownInt(w, x, err)
643 case WireStartGroup:
644 err = w.WriteByte('{')
645 w.indent()
646 case WireVarint:
647 x, err = b.DecodeVarint()
648 err = writeUnknownInt(w, x, err)
649 default:
650 _, err = fmt.Fprintf(w, "/* unknown wire type %d */", wire)
651 }
652 if err != nil {
653 return err
654 }
655 if err = w.WriteByte('\n'); err != nil {
656 return err
657 }
658 }
659 return nil
660}
661
662func writeUnknownInt(w *textWriter, x uint64, err error) error {
663 if err == nil {
664 _, err = fmt.Fprint(w, x)
665 } else {
666 _, err = fmt.Fprintf(w, "/* %v */", err)
667 }
668 return err
669}
670
671type int32Slice []int32
672
673func (s int32Slice) Len() int { return len(s) }
674func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] }
675func (s int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
676
677// writeExtensions writes all the extensions in pv.
678// pv is assumed to be a pointer to a protocol message struct that is extendable.
679func (tm *TextMarshaler) writeExtensions(w *textWriter, pv reflect.Value) error {
680 emap := extensionMaps[pv.Type().Elem()]
681 ep, _ := extendable(pv.Interface())
682
683 // Order the extensions by ID.
684 // This isn't strictly necessary, but it will give us
685 // canonical output, which will also make testing easier.
686 m, mu := ep.extensionsRead()
687 if m == nil {
688 return nil
689 }
690 mu.Lock()
691 ids := make([]int32, 0, len(m))
692 for id := range m {
693 ids = append(ids, id)
694 }
695 sort.Sort(int32Slice(ids))
696 mu.Unlock()
697
698 for _, extNum := range ids {
699 ext := m[extNum]
700 var desc *ExtensionDesc
701 if emap != nil {
702 desc = emap[extNum]
703 }
704 if desc == nil {
705 // Unknown extension.
706 if err := writeUnknownStruct(w, ext.enc); err != nil {
707 return err
708 }
709 continue
710 }
711
712 pb, err := GetExtension(ep, desc)
713 if err != nil {
714 return fmt.Errorf("failed getting extension: %v", err)
715 }
716
717 // Repeated extensions will appear as a slice.
718 if !desc.repeated() {
719 if err := tm.writeExtension(w, desc.Name, pb); err != nil {
720 return err
721 }
722 } else {
723 v := reflect.ValueOf(pb)
724 for i := 0; i < v.Len(); i++ {
725 if err := tm.writeExtension(w, desc.Name, v.Index(i).Interface()); err != nil {
726 return err
727 }
728 }
729 }
730 }
731 return nil
732}
733
734func (tm *TextMarshaler) writeExtension(w *textWriter, name string, pb interface{}) error {
735 if _, err := fmt.Fprintf(w, "[%s]:", name); err != nil {
736 return err
737 }
738 if !w.compact {
739 if err := w.WriteByte(' '); err != nil {
740 return err
741 }
742 }
743 if err := tm.writeAny(w, reflect.ValueOf(pb), nil); err != nil {
744 return err
745 }
746 if err := w.WriteByte('\n'); err != nil {
747 return err
748 }
749 return nil
750}
751
752func (w *textWriter) writeIndent() {
753 if !w.complete {
754 return
755 }
756 remain := w.ind * 2
757 for remain > 0 {
758 n := remain
759 if n > len(spaces) {
760 n = len(spaces)
761 }
762 w.w.Write(spaces[:n])
763 remain -= n
764 }
765 w.complete = false
766}
767
768// TextMarshaler is a configurable text format marshaler.
769type TextMarshaler struct {
770 Compact bool // use compact text format (one line).
771 ExpandAny bool // expand google.protobuf.Any messages of known types
772}
773
774// Marshal writes a given protocol buffer in text format.
775// The only errors returned are from w.
776func (tm *TextMarshaler) Marshal(w io.Writer, pb Message) error {
777 val := reflect.ValueOf(pb)
778 if pb == nil || val.IsNil() {
779 w.Write([]byte("<nil>"))
780 return nil
781 }
782 var bw *bufio.Writer
783 ww, ok := w.(writer)
784 if !ok {
785 bw = bufio.NewWriter(w)
786 ww = bw
787 }
788 aw := &textWriter{
789 w: ww,
790 complete: true,
791 compact: tm.Compact,
792 }
793
794 if etm, ok := pb.(encoding.TextMarshaler); ok {
795 text, err := etm.MarshalText()
796 if err != nil {
797 return err
798 }
799 if _, err = aw.Write(text); err != nil {
800 return err
801 }
802 if bw != nil {
803 return bw.Flush()
804 }
805 return nil
806 }
807 // Dereference the received pointer so we don't have outer < and >.
808 v := reflect.Indirect(val)
809 if err := tm.writeStruct(aw, v); err != nil {
810 return err
811 }
812 if bw != nil {
813 return bw.Flush()
814 }
815 return nil
816}
817
818// Text is the same as Marshal, but returns the string directly.
819func (tm *TextMarshaler) Text(pb Message) string {
820 var buf bytes.Buffer
821 tm.Marshal(&buf, pb)
822 return buf.String()
823}
824
825var (
826 defaultTextMarshaler = TextMarshaler{}
827 compactTextMarshaler = TextMarshaler{Compact: true}
828)
829
830// TODO: consider removing some of the Marshal functions below.
831
832// MarshalText writes a given protocol buffer in text format.
833// The only errors returned are from w.
834func MarshalText(w io.Writer, pb Message) error { return defaultTextMarshaler.Marshal(w, pb) }
835
836// MarshalTextString is the same as MarshalText, but returns the string directly.
837func MarshalTextString(pb Message) string { return defaultTextMarshaler.Text(pb) }
838
839// CompactText writes a given protocol buffer in compact text format (one line).
840func CompactText(w io.Writer, pb Message) error { return compactTextMarshaler.Marshal(w, pb) }
841
842// CompactTextString is the same as CompactText, but returns the string directly.
843func CompactTextString(pb Message) string { return compactTextMarshaler.Text(pb) }
diff --git a/vendor/github.com/golang/protobuf/proto/text_parser.go b/vendor/github.com/golang/protobuf/proto/text_parser.go
new file mode 100644
index 0000000..bb55a3a
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/proto/text_parser.go
@@ -0,0 +1,880 @@
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
32package proto
33
34// Functions for parsing the Text protocol buffer format.
35// TODO: message sets.
36
37import (
38 "encoding"
39 "errors"
40 "fmt"
41 "reflect"
42 "strconv"
43 "strings"
44 "unicode/utf8"
45)
46
47// Error string emitted when deserializing Any and fields are already set
48const anyRepeatedlyUnpacked = "Any message unpacked multiple times, or %q already set"
49
50type ParseError struct {
51 Message string
52 Line int // 1-based line number
53 Offset int // 0-based byte offset from start of input
54}
55
56func (p *ParseError) Error() string {
57 if p.Line == 1 {
58 // show offset only for first line
59 return fmt.Sprintf("line 1.%d: %v", p.Offset, p.Message)
60 }
61 return fmt.Sprintf("line %d: %v", p.Line, p.Message)
62}
63
64type token struct {
65 value string
66 err *ParseError
67 line int // line number
68 offset int // byte number from start of input, not start of line
69 unquoted string // the unquoted version of value, if it was a quoted string
70}
71
72func (t *token) String() string {
73 if t.err == nil {
74 return fmt.Sprintf("%q (line=%d, offset=%d)", t.value, t.line, t.offset)
75 }
76 return fmt.Sprintf("parse error: %v", t.err)
77}
78
79type textParser struct {
80 s string // remaining input
81 done bool // whether the parsing is finished (success or error)
82 backed bool // whether back() was called
83 offset, line int
84 cur token
85}
86
87func newTextParser(s string) *textParser {
88 p := new(textParser)
89 p.s = s
90 p.line = 1
91 p.cur.line = 1
92 return p
93}
94
95func (p *textParser) errorf(format string, a ...interface{}) *ParseError {
96 pe := &ParseError{fmt.Sprintf(format, a...), p.cur.line, p.cur.offset}
97 p.cur.err = pe
98 p.done = true
99 return pe
100}
101
102// Numbers and identifiers are matched by [-+._A-Za-z0-9]
103func isIdentOrNumberChar(c byte) bool {
104 switch {
105 case 'A' <= c && c <= 'Z', 'a' <= c && c <= 'z':
106 return true
107 case '0' <= c && c <= '9':
108 return true
109 }
110 switch c {
111 case '-', '+', '.', '_':
112 return true
113 }
114 return false
115}
116
117func isWhitespace(c byte) bool {
118 switch c {
119 case ' ', '\t', '\n', '\r':
120 return true
121 }
122 return false
123}
124
125func isQuote(c byte) bool {
126 switch c {
127 case '"', '\'':
128 return true
129 }
130 return false
131}
132
133func (p *textParser) skipWhitespace() {
134 i := 0
135 for i < len(p.s) && (isWhitespace(p.s[i]) || p.s[i] == '#') {
136 if p.s[i] == '#' {
137 // comment; skip to end of line or input
138 for i < len(p.s) && p.s[i] != '\n' {
139 i++
140 }
141 if i == len(p.s) {
142 break
143 }
144 }
145 if p.s[i] == '\n' {
146 p.line++
147 }
148 i++
149 }
150 p.offset += i
151 p.s = p.s[i:len(p.s)]
152 if len(p.s) == 0 {
153 p.done = true
154 }
155}
156
157func (p *textParser) advance() {
158 // Skip whitespace
159 p.skipWhitespace()
160 if p.done {
161 return
162 }
163
164 // Start of non-whitespace
165 p.cur.err = nil
166 p.cur.offset, p.cur.line = p.offset, p.line
167 p.cur.unquoted = ""
168 switch p.s[0] {
169 case '<', '>', '{', '}', ':', '[', ']', ';', ',', '/':
170 // Single symbol
171 p.cur.value, p.s = p.s[0:1], p.s[1:len(p.s)]
172 case '"', '\'':
173 // Quoted string
174 i := 1
175 for i < len(p.s) && p.s[i] != p.s[0] && p.s[i] != '\n' {
176 if p.s[i] == '\\' && i+1 < len(p.s) {
177 // skip escaped char
178 i++
179 }
180 i++
181 }
182 if i >= len(p.s) || p.s[i] != p.s[0] {
183 p.errorf("unmatched quote")
184 return
185 }
186 unq, err := unquoteC(p.s[1:i], rune(p.s[0]))
187 if err != nil {
188 p.errorf("invalid quoted string %s: %v", p.s[0:i+1], err)
189 return
190 }
191 p.cur.value, p.s = p.s[0:i+1], p.s[i+1:len(p.s)]
192 p.cur.unquoted = unq
193 default:
194 i := 0
195 for i < len(p.s) && isIdentOrNumberChar(p.s[i]) {
196 i++
197 }
198 if i == 0 {
199 p.errorf("unexpected byte %#x", p.s[0])
200 return
201 }
202 p.cur.value, p.s = p.s[0:i], p.s[i:len(p.s)]
203 }
204 p.offset += len(p.cur.value)
205}
206
207var (
208 errBadUTF8 = errors.New("proto: bad UTF-8")
209)
210
211func unquoteC(s string, quote rune) (string, error) {
212 // This is based on C++'s tokenizer.cc.
213 // Despite its name, this is *not* parsing C syntax.
214 // For instance, "\0" is an invalid quoted string.
215
216 // Avoid allocation in trivial cases.
217 simple := true
218 for _, r := range s {
219 if r == '\\' || r == quote {
220 simple = false
221 break
222 }
223 }
224 if simple {
225 return s, nil
226 }
227
228 buf := make([]byte, 0, 3*len(s)/2)
229 for len(s) > 0 {
230 r, n := utf8.DecodeRuneInString(s)
231 if r == utf8.RuneError && n == 1 {
232 return "", errBadUTF8
233 }
234 s = s[n:]
235 if r != '\\' {
236 if r < utf8.RuneSelf {
237 buf = append(buf, byte(r))
238 } else {
239 buf = append(buf, string(r)...)
240 }
241 continue
242 }
243
244 ch, tail, err := unescape(s)
245 if err != nil {
246 return "", err
247 }
248 buf = append(buf, ch...)
249 s = tail
250 }
251 return string(buf), nil
252}
253
254func unescape(s string) (ch string, tail string, err error) {
255 r, n := utf8.DecodeRuneInString(s)
256 if r == utf8.RuneError && n == 1 {
257 return "", "", errBadUTF8
258 }
259 s = s[n:]
260 switch r {
261 case 'a':
262 return "\a", s, nil
263 case 'b':
264 return "\b", s, nil
265 case 'f':
266 return "\f", s, nil
267 case 'n':
268 return "\n", s, nil
269 case 'r':
270 return "\r", s, nil
271 case 't':
272 return "\t", s, nil
273 case 'v':
274 return "\v", s, nil
275 case '?':
276 return "?", s, nil // trigraph workaround
277 case '\'', '"', '\\':
278 return string(r), s, nil
279 case '0', '1', '2', '3', '4', '5', '6', '7':
280 if len(s) < 2 {
281 return "", "", fmt.Errorf(`\%c requires 2 following digits`, r)
282 }
283 ss := string(r) + s[:2]
284 s = s[2:]
285 i, err := strconv.ParseUint(ss, 8, 8)
286 if err != nil {
287 return "", "", fmt.Errorf(`\%s contains non-octal digits`, ss)
288 }
289 return string([]byte{byte(i)}), s, nil
290 case 'x', 'X', 'u', 'U':
291 var n int
292 switch r {
293 case 'x', 'X':
294 n = 2
295 case 'u':
296 n = 4
297 case 'U':
298 n = 8
299 }
300 if len(s) < n {
301 return "", "", fmt.Errorf(`\%c requires %d following digits`, r, n)
302 }
303 ss := s[:n]
304 s = s[n:]
305 i, err := strconv.ParseUint(ss, 16, 64)
306 if err != nil {
307 return "", "", fmt.Errorf(`\%c%s contains non-hexadecimal digits`, r, ss)
308 }
309 if r == 'x' || r == 'X' {
310 return string([]byte{byte(i)}), s, nil
311 }
312 if i > utf8.MaxRune {
313 return "", "", fmt.Errorf(`\%c%s is not a valid Unicode code point`, r, ss)
314 }
315 return string(i), s, nil
316 }
317 return "", "", fmt.Errorf(`unknown escape \%c`, r)
318}
319
320// Back off the parser by one token. Can only be done between calls to next().
321// It makes the next advance() a no-op.
322func (p *textParser) back() { p.backed = true }
323
324// Advances the parser and returns the new current token.
325func (p *textParser) next() *token {
326 if p.backed || p.done {
327 p.backed = false
328 return &p.cur
329 }
330 p.advance()
331 if p.done {
332 p.cur.value = ""
333 } else if len(p.cur.value) > 0 && isQuote(p.cur.value[0]) {
334 // Look for multiple quoted strings separated by whitespace,
335 // and concatenate them.
336 cat := p.cur
337 for {
338 p.skipWhitespace()
339 if p.done || !isQuote(p.s[0]) {
340 break
341 }
342 p.advance()
343 if p.cur.err != nil {
344 return &p.cur
345 }
346 cat.value += " " + p.cur.value
347 cat.unquoted += p.cur.unquoted
348 }
349 p.done = false // parser may have seen EOF, but we want to return cat
350 p.cur = cat
351 }
352 return &p.cur
353}
354
355func (p *textParser) consumeToken(s string) error {
356 tok := p.next()
357 if tok.err != nil {
358 return tok.err
359 }
360 if tok.value != s {
361 p.back()
362 return p.errorf("expected %q, found %q", s, tok.value)
363 }
364 return nil
365}
366
367// Return a RequiredNotSetError indicating which required field was not set.
368func (p *textParser) missingRequiredFieldError(sv reflect.Value) *RequiredNotSetError {
369 st := sv.Type()
370 sprops := GetProperties(st)
371 for i := 0; i < st.NumField(); i++ {
372 if !isNil(sv.Field(i)) {
373 continue
374 }
375
376 props := sprops.Prop[i]
377 if props.Required {
378 return &RequiredNotSetError{fmt.Sprintf("%v.%v", st, props.OrigName)}
379 }
380 }
381 return &RequiredNotSetError{fmt.Sprintf("%v.<unknown field name>", st)} // should not happen
382}
383
384// Returns the index in the struct for the named field, as well as the parsed tag properties.
385func structFieldByName(sprops *StructProperties, name string) (int, *Properties, bool) {
386 i, ok := sprops.decoderOrigNames[name]
387 if ok {
388 return i, sprops.Prop[i], true
389 }
390 return -1, nil, false
391}
392
393// Consume a ':' from the input stream (if the next token is a colon),
394// returning an error if a colon is needed but not present.
395func (p *textParser) checkForColon(props *Properties, typ reflect.Type) *ParseError {
396 tok := p.next()
397 if tok.err != nil {
398 return tok.err
399 }
400 if tok.value != ":" {
401 // Colon is optional when the field is a group or message.
402 needColon := true
403 switch props.Wire {
404 case "group":
405 needColon = false
406 case "bytes":
407 // A "bytes" field is either a message, a string, or a repeated field;
408 // those three become *T, *string and []T respectively, so we can check for
409 // this field being a pointer to a non-string.
410 if typ.Kind() == reflect.Ptr {
411 // *T or *string
412 if typ.Elem().Kind() == reflect.String {
413 break
414 }
415 } else if typ.Kind() == reflect.Slice {
416 // []T or []*T
417 if typ.Elem().Kind() != reflect.Ptr {
418 break
419 }
420 } else if typ.Kind() == reflect.String {
421 // The proto3 exception is for a string field,
422 // which requires a colon.
423 break
424 }
425 needColon = false
426 }
427 if needColon {
428 return p.errorf("expected ':', found %q", tok.value)
429 }
430 p.back()
431 }
432 return nil
433}
434
435func (p *textParser) readStruct(sv reflect.Value, terminator string) error {
436 st := sv.Type()
437 sprops := GetProperties(st)
438 reqCount := sprops.reqCount
439 var reqFieldErr error
440 fieldSet := make(map[string]bool)
441 // A struct is a sequence of "name: value", terminated by one of
442 // '>' or '}', or the end of the input. A name may also be
443 // "[extension]" or "[type/url]".
444 //
445 // The whole struct can also be an expanded Any message, like:
446 // [type/url] < ... struct contents ... >
447 for {
448 tok := p.next()
449 if tok.err != nil {
450 return tok.err
451 }
452 if tok.value == terminator {
453 break
454 }
455 if tok.value == "[" {
456 // Looks like an extension or an Any.
457 //
458 // TODO: Check whether we need to handle
459 // namespace rooted names (e.g. ".something.Foo").
460 extName, err := p.consumeExtName()
461 if err != nil {
462 return err
463 }
464
465 if s := strings.LastIndex(extName, "/"); s >= 0 {
466 // If it contains a slash, it's an Any type URL.
467 messageName := extName[s+1:]
468 mt := MessageType(messageName)
469 if mt == nil {
470 return p.errorf("unrecognized message %q in google.protobuf.Any", messageName)
471 }
472 tok = p.next()
473 if tok.err != nil {
474 return tok.err
475 }
476 // consume an optional colon
477 if tok.value == ":" {
478 tok = p.next()
479 if tok.err != nil {
480 return tok.err
481 }
482 }
483 var terminator string
484 switch tok.value {
485 case "<":
486 terminator = ">"
487 case "{":
488 terminator = "}"
489 default:
490 return p.errorf("expected '{' or '<', found %q", tok.value)
491 }
492 v := reflect.New(mt.Elem())
493 if pe := p.readStruct(v.Elem(), terminator); pe != nil {
494 return pe
495 }
496 b, err := Marshal(v.Interface().(Message))
497 if err != nil {
498 return p.errorf("failed to marshal message of type %q: %v", messageName, err)
499 }
500 if fieldSet["type_url"] {
501 return p.errorf(anyRepeatedlyUnpacked, "type_url")
502 }
503 if fieldSet["value"] {
504 return p.errorf(anyRepeatedlyUnpacked, "value")
505 }
506 sv.FieldByName("TypeUrl").SetString(extName)
507 sv.FieldByName("Value").SetBytes(b)
508 fieldSet["type_url"] = true
509 fieldSet["value"] = true
510 continue
511 }
512
513 var desc *ExtensionDesc
514 // This could be faster, but it's functional.
515 // TODO: Do something smarter than a linear scan.
516 for _, d := range RegisteredExtensions(reflect.New(st).Interface().(Message)) {
517 if d.Name == extName {
518 desc = d
519 break
520 }
521 }
522 if desc == nil {
523 return p.errorf("unrecognized extension %q", extName)
524 }
525
526 props := &Properties{}
527 props.Parse(desc.Tag)
528
529 typ := reflect.TypeOf(desc.ExtensionType)
530 if err := p.checkForColon(props, typ); err != nil {
531 return err
532 }
533
534 rep := desc.repeated()
535
536 // Read the extension structure, and set it in
537 // the value we're constructing.
538 var ext reflect.Value
539 if !rep {
540 ext = reflect.New(typ).Elem()
541 } else {
542 ext = reflect.New(typ.Elem()).Elem()
543 }
544 if err := p.readAny(ext, props); err != nil {
545 if _, ok := err.(*RequiredNotSetError); !ok {
546 return err
547 }
548 reqFieldErr = err
549 }
550 ep := sv.Addr().Interface().(Message)
551 if !rep {
552 SetExtension(ep, desc, ext.Interface())
553 } else {
554 old, err := GetExtension(ep, desc)
555 var sl reflect.Value
556 if err == nil {
557 sl = reflect.ValueOf(old) // existing slice
558 } else {
559 sl = reflect.MakeSlice(typ, 0, 1)
560 }
561 sl = reflect.Append(sl, ext)
562 SetExtension(ep, desc, sl.Interface())
563 }
564 if err := p.consumeOptionalSeparator(); err != nil {
565 return err
566 }
567 continue
568 }
569
570 // This is a normal, non-extension field.
571 name := tok.value
572 var dst reflect.Value
573 fi, props, ok := structFieldByName(sprops, name)
574 if ok {
575 dst = sv.Field(fi)
576 } else if oop, ok := sprops.OneofTypes[name]; ok {
577 // It is a oneof.
578 props = oop.Prop
579 nv := reflect.New(oop.Type.Elem())
580 dst = nv.Elem().Field(0)
581 field := sv.Field(oop.Field)
582 if !field.IsNil() {
583 return p.errorf("field '%s' would overwrite already parsed oneof '%s'", name, sv.Type().Field(oop.Field).Name)
584 }
585 field.Set(nv)
586 }
587 if !dst.IsValid() {
588 return p.errorf("unknown field name %q in %v", name, st)
589 }
590
591 if dst.Kind() == reflect.Map {
592 // Consume any colon.
593 if err := p.checkForColon(props, dst.Type()); err != nil {
594 return err
595 }
596
597 // Construct the map if it doesn't already exist.
598 if dst.IsNil() {
599 dst.Set(reflect.MakeMap(dst.Type()))
600 }
601 key := reflect.New(dst.Type().Key()).Elem()
602 val := reflect.New(dst.Type().Elem()).Elem()
603
604 // The map entry should be this sequence of tokens:
605 // < key : KEY value : VALUE >
606 // However, implementations may omit key or value, and technically
607 // we should support them in any order. See b/28924776 for a time
608 // this went wrong.
609
610 tok := p.next()
611 var terminator string
612 switch tok.value {
613 case "<":
614 terminator = ">"
615 case "{":
616 terminator = "}"
617 default:
618 return p.errorf("expected '{' or '<', found %q", tok.value)
619 }
620 for {
621 tok := p.next()
622 if tok.err != nil {
623 return tok.err
624 }
625 if tok.value == terminator {
626 break
627 }
628 switch tok.value {
629 case "key":
630 if err := p.consumeToken(":"); err != nil {
631 return err
632 }
633 if err := p.readAny(key, props.MapKeyProp); err != nil {
634 return err
635 }
636 if err := p.consumeOptionalSeparator(); err != nil {
637 return err
638 }
639 case "value":
640 if err := p.checkForColon(props.MapValProp, dst.Type().Elem()); err != nil {
641 return err
642 }
643 if err := p.readAny(val, props.MapValProp); err != nil {
644 return err
645 }
646 if err := p.consumeOptionalSeparator(); err != nil {
647 return err
648 }
649 default:
650 p.back()
651 return p.errorf(`expected "key", "value", or %q, found %q`, terminator, tok.value)
652 }
653 }
654
655 dst.SetMapIndex(key, val)
656 continue
657 }
658
659 // Check that it's not already set if it's not a repeated field.
660 if !props.Repeated && fieldSet[name] {
661 return p.errorf("non-repeated field %q was repeated", name)
662 }
663
664 if err := p.checkForColon(props, dst.Type()); err != nil {
665 return err
666 }
667
668 // Parse into the field.
669 fieldSet[name] = true
670 if err := p.readAny(dst, props); err != nil {
671 if _, ok := err.(*RequiredNotSetError); !ok {
672 return err
673 }
674 reqFieldErr = err
675 }
676 if props.Required {
677 reqCount--
678 }
679
680 if err := p.consumeOptionalSeparator(); err != nil {
681 return err
682 }
683
684 }
685
686 if reqCount > 0 {
687 return p.missingRequiredFieldError(sv)
688 }
689 return reqFieldErr
690}
691
692// consumeExtName consumes extension name or expanded Any type URL and the
693// following ']'. It returns the name or URL consumed.
694func (p *textParser) consumeExtName() (string, error) {
695 tok := p.next()
696 if tok.err != nil {
697 return "", tok.err
698 }
699
700 // If extension name or type url is quoted, it's a single token.
701 if len(tok.value) > 2 && isQuote(tok.value[0]) && tok.value[len(tok.value)-1] == tok.value[0] {
702 name, err := unquoteC(tok.value[1:len(tok.value)-1], rune(tok.value[0]))
703 if err != nil {
704 return "", err
705 }
706 return name, p.consumeToken("]")
707 }
708
709 // Consume everything up to "]"
710 var parts []string
711 for tok.value != "]" {
712 parts = append(parts, tok.value)
713 tok = p.next()
714 if tok.err != nil {
715 return "", p.errorf("unrecognized type_url or extension name: %s", tok.err)
716 }
717 if p.done && tok.value != "]" {
718 return "", p.errorf("unclosed type_url or extension name")
719 }
720 }
721 return strings.Join(parts, ""), nil
722}
723
724// consumeOptionalSeparator consumes an optional semicolon or comma.
725// It is used in readStruct to provide backward compatibility.
726func (p *textParser) consumeOptionalSeparator() error {
727 tok := p.next()
728 if tok.err != nil {
729 return tok.err
730 }
731 if tok.value != ";" && tok.value != "," {
732 p.back()
733 }
734 return nil
735}
736
737func (p *textParser) readAny(v reflect.Value, props *Properties) error {
738 tok := p.next()
739 if tok.err != nil {
740 return tok.err
741 }
742 if tok.value == "" {
743 return p.errorf("unexpected EOF")
744 }
745
746 switch fv := v; fv.Kind() {
747 case reflect.Slice:
748 at := v.Type()
749 if at.Elem().Kind() == reflect.Uint8 {
750 // Special case for []byte
751 if tok.value[0] != '"' && tok.value[0] != '\'' {
752 // Deliberately written out here, as the error after
753 // this switch statement would write "invalid []byte: ...",
754 // which is not as user-friendly.
755 return p.errorf("invalid string: %v", tok.value)
756 }
757 bytes := []byte(tok.unquoted)
758 fv.Set(reflect.ValueOf(bytes))
759 return nil
760 }
761 // Repeated field.
762 if tok.value == "[" {
763 // Repeated field with list notation, like [1,2,3].
764 for {
765 fv.Set(reflect.Append(fv, reflect.New(at.Elem()).Elem()))
766 err := p.readAny(fv.Index(fv.Len()-1), props)
767 if err != nil {
768 return err
769 }
770 tok := p.next()
771 if tok.err != nil {
772 return tok.err
773 }
774 if tok.value == "]" {
775 break
776 }
777 if tok.value != "," {
778 return p.errorf("Expected ']' or ',' found %q", tok.value)
779 }
780 }
781 return nil
782 }
783 // One value of the repeated field.
784 p.back()
785 fv.Set(reflect.Append(fv, reflect.New(at.Elem()).Elem()))
786 return p.readAny(fv.Index(fv.Len()-1), props)
787 case reflect.Bool:
788 // true/1/t/True or false/f/0/False.
789 switch tok.value {
790 case "true", "1", "t", "True":
791 fv.SetBool(true)
792 return nil
793 case "false", "0", "f", "False":
794 fv.SetBool(false)
795 return nil
796 }
797 case reflect.Float32, reflect.Float64:
798 v := tok.value
799 // Ignore 'f' for compatibility with output generated by C++, but don't
800 // remove 'f' when the value is "-inf" or "inf".
801 if strings.HasSuffix(v, "f") && tok.value != "-inf" && tok.value != "inf" {
802 v = v[:len(v)-1]
803 }
804 if f, err := strconv.ParseFloat(v, fv.Type().Bits()); err == nil {
805 fv.SetFloat(f)
806 return nil
807 }
808 case reflect.Int32:
809 if x, err := strconv.ParseInt(tok.value, 0, 32); err == nil {
810 fv.SetInt(x)
811 return nil
812 }
813
814 if len(props.Enum) == 0 {
815 break
816 }
817 m, ok := enumValueMaps[props.Enum]
818 if !ok {
819 break
820 }
821 x, ok := m[tok.value]
822 if !ok {
823 break
824 }
825 fv.SetInt(int64(x))
826 return nil
827 case reflect.Int64:
828 if x, err := strconv.ParseInt(tok.value, 0, 64); err == nil {
829 fv.SetInt(x)
830 return nil
831 }
832
833 case reflect.Ptr:
834 // A basic field (indirected through pointer), or a repeated message/group
835 p.back()
836 fv.Set(reflect.New(fv.Type().Elem()))
837 return p.readAny(fv.Elem(), props)
838 case reflect.String:
839 if tok.value[0] == '"' || tok.value[0] == '\'' {
840 fv.SetString(tok.unquoted)
841 return nil
842 }
843 case reflect.Struct:
844 var terminator string
845 switch tok.value {
846 case "{":
847 terminator = "}"
848 case "<":
849 terminator = ">"
850 default:
851 return p.errorf("expected '{' or '<', found %q", tok.value)
852 }
853 // TODO: Handle nested messages which implement encoding.TextUnmarshaler.
854 return p.readStruct(fv, terminator)
855 case reflect.Uint32:
856 if x, err := strconv.ParseUint(tok.value, 0, 32); err == nil {
857 fv.SetUint(uint64(x))
858 return nil
859 }
860 case reflect.Uint64:
861 if x, err := strconv.ParseUint(tok.value, 0, 64); err == nil {
862 fv.SetUint(x)
863 return nil
864 }
865 }
866 return p.errorf("invalid %v: %v", v.Type(), tok.value)
867}
868
869// UnmarshalText reads a protocol buffer in Text format. UnmarshalText resets pb
870// before starting to unmarshal, so any existing data in pb is always removed.
871// If a required field is not set and no other error occurs,
872// UnmarshalText returns *RequiredNotSetError.
873func UnmarshalText(s string, pb Message) error {
874 if um, ok := pb.(encoding.TextUnmarshaler); ok {
875 return um.UnmarshalText([]byte(s))
876 }
877 pb.Reset()
878 v := reflect.ValueOf(pb)
879 return newTextParser(s).readStruct(v.Elem(), "")
880}
diff --git a/vendor/github.com/golang/protobuf/ptypes/any.go b/vendor/github.com/golang/protobuf/ptypes/any.go
new file mode 100644
index 0000000..70276e8
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/ptypes/any.go
@@ -0,0 +1,141 @@
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
32package ptypes
33
34// This file implements functions to marshal proto.Message to/from
35// google.protobuf.Any message.
36
37import (
38 "fmt"
39 "reflect"
40 "strings"
41
42 "github.com/golang/protobuf/proto"
43 "github.com/golang/protobuf/ptypes/any"
44)
45
46const googleApis = "type.googleapis.com/"
47
48// AnyMessageName returns the name of the message contained in a google.protobuf.Any message.
49//
50// Note that regular type assertions should be done using the Is
51// function. AnyMessageName is provided for less common use cases like filtering a
52// sequence of Any messages based on a set of allowed message type names.
53func AnyMessageName(any *any.Any) (string, error) {
54 if any == nil {
55 return "", fmt.Errorf("message is nil")
56 }
57 slash := strings.LastIndex(any.TypeUrl, "/")
58 if slash < 0 {
59 return "", fmt.Errorf("message type url %q is invalid", any.TypeUrl)
60 }
61 return any.TypeUrl[slash+1:], nil
62}
63
64// MarshalAny takes the protocol buffer and encodes it into google.protobuf.Any.
65func MarshalAny(pb proto.Message) (*any.Any, error) {
66 value, err := proto.Marshal(pb)
67 if err != nil {
68 return nil, err
69 }
70 return &any.Any{TypeUrl: googleApis + proto.MessageName(pb), Value: value}, nil
71}
72
73// DynamicAny is a value that can be passed to UnmarshalAny to automatically
74// allocate a proto.Message for the type specified in a google.protobuf.Any
75// message. The allocated message is stored in the embedded proto.Message.
76//
77// Example:
78//
79// var x ptypes.DynamicAny
80// if err := ptypes.UnmarshalAny(a, &x); err != nil { ... }
81// fmt.Printf("unmarshaled message: %v", x.Message)
82type DynamicAny struct {
83 proto.Message
84}
85
86// Empty returns a new proto.Message of the type specified in a
87// google.protobuf.Any message. It returns an error if corresponding message
88// type isn't linked in.
89func Empty(any *any.Any) (proto.Message, error) {
90 aname, err := AnyMessageName(any)
91 if err != nil {
92 return nil, err
93 }
94
95 t := proto.MessageType(aname)
96 if t == nil {
97 return nil, fmt.Errorf("any: message type %q isn't linked in", aname)
98 }
99 return reflect.New(t.Elem()).Interface().(proto.Message), nil
100}
101
102// UnmarshalAny parses the protocol buffer representation in a google.protobuf.Any
103// message and places the decoded result in pb. It returns an error if type of
104// contents of Any message does not match type of pb message.
105//
106// pb can be a proto.Message, or a *DynamicAny.
107func UnmarshalAny(any *any.Any, pb proto.Message) error {
108 if d, ok := pb.(*DynamicAny); ok {
109 if d.Message == nil {
110 var err error
111 d.Message, err = Empty(any)
112 if err != nil {
113 return err
114 }
115 }
116 return UnmarshalAny(any, d.Message)
117 }
118
119 aname, err := AnyMessageName(any)
120 if err != nil {
121 return err
122 }
123
124 mname := proto.MessageName(pb)
125 if aname != mname {
126 return fmt.Errorf("mismatched message type: got %q want %q", aname, mname)
127 }
128 return proto.Unmarshal(any.Value, pb)
129}
130
131// Is returns true if any value contains a given message type.
132func Is(any *any.Any, pb proto.Message) bool {
133 // The following is equivalent to AnyMessageName(any) == proto.MessageName(pb),
134 // but it avoids scanning TypeUrl for the slash.
135 if any == nil {
136 return false
137 }
138 name := proto.MessageName(pb)
139 prefix := len(any.TypeUrl) - len(name)
140 return prefix >= 1 && any.TypeUrl[prefix-1] == '/' && any.TypeUrl[prefix:] == name
141}
diff --git a/vendor/github.com/golang/protobuf/ptypes/any/any.pb.go b/vendor/github.com/golang/protobuf/ptypes/any/any.pb.go
new file mode 100644
index 0000000..e3c56d3
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/ptypes/any/any.pb.go
@@ -0,0 +1,191 @@
1// Code generated by protoc-gen-go. DO NOT EDIT.
2// source: google/protobuf/any.proto
3
4package any // import "github.com/golang/protobuf/ptypes/any"
5
6import proto "github.com/golang/protobuf/proto"
7import fmt "fmt"
8import math "math"
9
10// Reference imports to suppress errors if they are not otherwise used.
11var _ = proto.Marshal
12var _ = fmt.Errorf
13var _ = math.Inf
14
15// This is a compile-time assertion to ensure that this generated file
16// is compatible with the proto package it is being compiled against.
17// A compilation error at this line likely means your copy of the
18// proto package needs to be updated.
19const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
20
21// `Any` contains an arbitrary serialized protocol buffer message along with a
22// URL that describes the type of the serialized message.
23//
24// Protobuf library provides support to pack/unpack Any values in the form
25// of utility functions or additional generated methods of the Any type.
26//
27// Example 1: Pack and unpack a message in C++.
28//
29// Foo foo = ...;
30// Any any;
31// any.PackFrom(foo);
32// ...
33// if (any.UnpackTo(&foo)) {
34// ...
35// }
36//
37// Example 2: Pack and unpack a message in Java.
38//
39// Foo foo = ...;
40// Any any = Any.pack(foo);
41// ...
42// if (any.is(Foo.class)) {
43// foo = any.unpack(Foo.class);
44// }
45//
46// Example 3: Pack and unpack a message in Python.
47//
48// foo = Foo(...)
49// any = Any()
50// any.Pack(foo)
51// ...
52// if any.Is(Foo.DESCRIPTOR):
53// any.Unpack(foo)
54// ...
55//
56// Example 4: Pack and unpack a message in Go
57//
58// foo := &pb.Foo{...}
59// any, err := ptypes.MarshalAny(foo)
60// ...
61// foo := &pb.Foo{}
62// if err := ptypes.UnmarshalAny(any, foo); err != nil {
63// ...
64// }
65//
66// The pack methods provided by protobuf library will by default use
67// 'type.googleapis.com/full.type.name' as the type URL and the unpack
68// methods only use the fully qualified type name after the last '/'
69// in the type URL, for example "foo.bar.com/x/y.z" will yield type
70// name "y.z".
71//
72//
73// JSON
74// ====
75// The JSON representation of an `Any` value uses the regular
76// representation of the deserialized, embedded message, with an
77// additional field `@type` which contains the type URL. Example:
78//
79// package google.profile;
80// message Person {
81// string first_name = 1;
82// string last_name = 2;
83// }
84//
85// {
86// "@type": "type.googleapis.com/google.profile.Person",
87// "firstName": <string>,
88// "lastName": <string>
89// }
90//
91// If the embedded message type is well-known and has a custom JSON
92// representation, that representation will be embedded adding a field
93// `value` which holds the custom JSON in addition to the `@type`
94// field. Example (for message [google.protobuf.Duration][]):
95//
96// {
97// "@type": "type.googleapis.com/google.protobuf.Duration",
98// "value": "1.212s"
99// }
100//
101type Any struct {
102 // A URL/resource name whose content describes the type of the
103 // serialized protocol buffer message.
104 //
105 // For URLs which use the scheme `http`, `https`, or no scheme, the
106 // following restrictions and interpretations apply:
107 //
108 // * If no scheme is provided, `https` is assumed.
109 // * The last segment of the URL's path must represent the fully
110 // qualified name of the type (as in `path/google.protobuf.Duration`).
111 // The name should be in a canonical form (e.g., leading "." is
112 // not accepted).
113 // * An HTTP GET on the URL must yield a [google.protobuf.Type][]
114 // value in binary format, or produce an error.
115 // * Applications are allowed to cache lookup results based on the
116 // URL, or have them precompiled into a binary to avoid any
117 // lookup. Therefore, binary compatibility needs to be preserved
118 // on changes to types. (Use versioned type names to manage
119 // breaking changes.)
120 //
121 // Schemes other than `http`, `https` (or the empty scheme) might be
122 // used with implementation specific semantics.
123 //
124 TypeUrl string `protobuf:"bytes,1,opt,name=type_url,json=typeUrl,proto3" json:"type_url,omitempty"`
125 // Must be a valid serialized protocol buffer of the above specified type.
126 Value []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
127 XXX_NoUnkeyedLiteral struct{} `json:"-"`
128 XXX_unrecognized []byte `json:"-"`
129 XXX_sizecache int32 `json:"-"`
130}
131
132func (m *Any) Reset() { *m = Any{} }
133func (m *Any) String() string { return proto.CompactTextString(m) }
134func (*Any) ProtoMessage() {}
135func (*Any) Descriptor() ([]byte, []int) {
136 return fileDescriptor_any_744b9ca530f228db, []int{0}
137}
138func (*Any) XXX_WellKnownType() string { return "Any" }
139func (m *Any) XXX_Unmarshal(b []byte) error {
140 return xxx_messageInfo_Any.Unmarshal(m, b)
141}
142func (m *Any) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
143 return xxx_messageInfo_Any.Marshal(b, m, deterministic)
144}
145func (dst *Any) XXX_Merge(src proto.Message) {
146 xxx_messageInfo_Any.Merge(dst, src)
147}
148func (m *Any) XXX_Size() int {
149 return xxx_messageInfo_Any.Size(m)
150}
151func (m *Any) XXX_DiscardUnknown() {
152 xxx_messageInfo_Any.DiscardUnknown(m)
153}
154
155var xxx_messageInfo_Any proto.InternalMessageInfo
156
157func (m *Any) GetTypeUrl() string {
158 if m != nil {
159 return m.TypeUrl
160 }
161 return ""
162}
163
164func (m *Any) GetValue() []byte {
165 if m != nil {
166 return m.Value
167 }
168 return nil
169}
170
171func init() {
172 proto.RegisterType((*Any)(nil), "google.protobuf.Any")
173}
174
175func init() { proto.RegisterFile("google/protobuf/any.proto", fileDescriptor_any_744b9ca530f228db) }
176
177var fileDescriptor_any_744b9ca530f228db = []byte{
178 // 185 bytes of a gzipped FileDescriptorProto
179 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x4c, 0xcf, 0xcf, 0x4f,
180 0xcf, 0x49, 0xd5, 0x2f, 0x28, 0xca, 0x2f, 0xc9, 0x4f, 0x2a, 0x4d, 0xd3, 0x4f, 0xcc, 0xab, 0xd4,
181 0x03, 0x73, 0x84, 0xf8, 0x21, 0x52, 0x7a, 0x30, 0x29, 0x25, 0x33, 0x2e, 0x66, 0xc7, 0xbc, 0x4a,
182 0x21, 0x49, 0x2e, 0x8e, 0x92, 0xca, 0x82, 0xd4, 0xf8, 0xd2, 0xa2, 0x1c, 0x09, 0x46, 0x05, 0x46,
183 0x0d, 0xce, 0x20, 0x76, 0x10, 0x3f, 0xb4, 0x28, 0x47, 0x48, 0x84, 0x8b, 0xb5, 0x2c, 0x31, 0xa7,
184 0x34, 0x55, 0x82, 0x49, 0x81, 0x51, 0x83, 0x27, 0x08, 0xc2, 0x71, 0xca, 0xe7, 0x12, 0x4e, 0xce,
185 0xcf, 0xd5, 0x43, 0x33, 0xce, 0x89, 0xc3, 0x31, 0xaf, 0x32, 0x00, 0xc4, 0x09, 0x60, 0x8c, 0x52,
186 0x4d, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xcf, 0xcf, 0x49, 0xcc,
187 0x4b, 0x47, 0xb8, 0xa8, 0x00, 0x64, 0x7a, 0x31, 0xc8, 0x61, 0x8b, 0x98, 0x98, 0xdd, 0x03, 0x9c,
188 0x56, 0x31, 0xc9, 0xb9, 0x43, 0x8c, 0x0a, 0x80, 0x2a, 0xd1, 0x0b, 0x4f, 0xcd, 0xc9, 0xf1, 0xce,
189 0xcb, 0x2f, 0xcf, 0x0b, 0x01, 0x29, 0x4d, 0x62, 0x03, 0xeb, 0x35, 0x06, 0x04, 0x00, 0x00, 0xff,
190 0xff, 0x13, 0xf8, 0xe8, 0x42, 0xdd, 0x00, 0x00, 0x00,
191}
diff --git a/vendor/github.com/golang/protobuf/ptypes/any/any.proto b/vendor/github.com/golang/protobuf/ptypes/any/any.proto
new file mode 100644
index 0000000..c748667
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/ptypes/any/any.proto
@@ -0,0 +1,149 @@
1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31syntax = "proto3";
32
33package google.protobuf;
34
35option csharp_namespace = "Google.Protobuf.WellKnownTypes";
36option go_package = "github.com/golang/protobuf/ptypes/any";
37option java_package = "com.google.protobuf";
38option java_outer_classname = "AnyProto";
39option java_multiple_files = true;
40option objc_class_prefix = "GPB";
41
42// `Any` contains an arbitrary serialized protocol buffer message along with a
43// URL that describes the type of the serialized message.
44//
45// Protobuf library provides support to pack/unpack Any values in the form
46// of utility functions or additional generated methods of the Any type.
47//
48// Example 1: Pack and unpack a message in C++.
49//
50// Foo foo = ...;
51// Any any;
52// any.PackFrom(foo);
53// ...
54// if (any.UnpackTo(&foo)) {
55// ...
56// }
57//
58// Example 2: Pack and unpack a message in Java.
59//
60// Foo foo = ...;
61// Any any = Any.pack(foo);
62// ...
63// if (any.is(Foo.class)) {
64// foo = any.unpack(Foo.class);
65// }
66//
67// Example 3: Pack and unpack a message in Python.
68//
69// foo = Foo(...)
70// any = Any()
71// any.Pack(foo)
72// ...
73// if any.Is(Foo.DESCRIPTOR):
74// any.Unpack(foo)
75// ...
76//
77// Example 4: Pack and unpack a message in Go
78//
79// foo := &pb.Foo{...}
80// any, err := ptypes.MarshalAny(foo)
81// ...
82// foo := &pb.Foo{}
83// if err := ptypes.UnmarshalAny(any, foo); err != nil {
84// ...
85// }
86//
87// The pack methods provided by protobuf library will by default use
88// 'type.googleapis.com/full.type.name' as the type URL and the unpack
89// methods only use the fully qualified type name after the last '/'
90// in the type URL, for example "foo.bar.com/x/y.z" will yield type
91// name "y.z".
92//
93//
94// JSON
95// ====
96// The JSON representation of an `Any` value uses the regular
97// representation of the deserialized, embedded message, with an
98// additional field `@type` which contains the type URL. Example:
99//
100// package google.profile;
101// message Person {
102// string first_name = 1;
103// string last_name = 2;
104// }
105//
106// {
107// "@type": "type.googleapis.com/google.profile.Person",
108// "firstName": <string>,
109// "lastName": <string>
110// }
111//
112// If the embedded message type is well-known and has a custom JSON
113// representation, that representation will be embedded adding a field
114// `value` which holds the custom JSON in addition to the `@type`
115// field. Example (for message [google.protobuf.Duration][]):
116//
117// {
118// "@type": "type.googleapis.com/google.protobuf.Duration",
119// "value": "1.212s"
120// }
121//
122message Any {
123 // A URL/resource name whose content describes the type of the
124 // serialized protocol buffer message.
125 //
126 // For URLs which use the scheme `http`, `https`, or no scheme, the
127 // following restrictions and interpretations apply:
128 //
129 // * If no scheme is provided, `https` is assumed.
130 // * The last segment of the URL's path must represent the fully
131 // qualified name of the type (as in `path/google.protobuf.Duration`).
132 // The name should be in a canonical form (e.g., leading "." is
133 // not accepted).
134 // * An HTTP GET on the URL must yield a [google.protobuf.Type][]
135 // value in binary format, or produce an error.
136 // * Applications are allowed to cache lookup results based on the
137 // URL, or have them precompiled into a binary to avoid any
138 // lookup. Therefore, binary compatibility needs to be preserved
139 // on changes to types. (Use versioned type names to manage
140 // breaking changes.)
141 //
142 // Schemes other than `http`, `https` (or the empty scheme) might be
143 // used with implementation specific semantics.
144 //
145 string type_url = 1;
146
147 // Must be a valid serialized protocol buffer of the above specified type.
148 bytes value = 2;
149}
diff --git a/vendor/github.com/golang/protobuf/ptypes/doc.go b/vendor/github.com/golang/protobuf/ptypes/doc.go
new file mode 100644
index 0000000..c0d595d
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/ptypes/doc.go
@@ -0,0 +1,35 @@
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/*
33Package ptypes contains code for interacting with well-known types.
34*/
35package ptypes
diff --git a/vendor/github.com/golang/protobuf/ptypes/duration.go b/vendor/github.com/golang/protobuf/ptypes/duration.go
new file mode 100644
index 0000000..65cb0f8
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/ptypes/duration.go
@@ -0,0 +1,102 @@
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
32package ptypes
33
34// This file implements conversions between google.protobuf.Duration
35// and time.Duration.
36
37import (
38 "errors"
39 "fmt"
40 "time"
41
42 durpb "github.com/golang/protobuf/ptypes/duration"
43)
44
45const (
46 // Range of a durpb.Duration in seconds, as specified in
47 // google/protobuf/duration.proto. This is about 10,000 years in seconds.
48 maxSeconds = int64(10000 * 365.25 * 24 * 60 * 60)
49 minSeconds = -maxSeconds
50)
51
52// validateDuration determines whether the durpb.Duration is valid according to the
53// definition in google/protobuf/duration.proto. A valid durpb.Duration
54// may still be too large to fit into a time.Duration (the range of durpb.Duration
55// is about 10,000 years, and the range of time.Duration is about 290).
56func validateDuration(d *durpb.Duration) error {
57 if d == nil {
58 return errors.New("duration: nil Duration")
59 }
60 if d.Seconds < minSeconds || d.Seconds > maxSeconds {
61 return fmt.Errorf("duration: %v: seconds out of range", d)
62 }
63 if d.Nanos <= -1e9 || d.Nanos >= 1e9 {
64 return fmt.Errorf("duration: %v: nanos out of range", d)
65 }
66 // Seconds and Nanos must have the same sign, unless d.Nanos is zero.
67 if (d.Seconds < 0 && d.Nanos > 0) || (d.Seconds > 0 && d.Nanos < 0) {
68 return fmt.Errorf("duration: %v: seconds and nanos have different signs", d)
69 }
70 return nil
71}
72
73// Duration converts a durpb.Duration to a time.Duration. Duration
74// returns an error if the durpb.Duration is invalid or is too large to be
75// represented in a time.Duration.
76func Duration(p *durpb.Duration) (time.Duration, error) {
77 if err := validateDuration(p); err != nil {
78 return 0, err
79 }
80 d := time.Duration(p.Seconds) * time.Second
81 if int64(d/time.Second) != p.Seconds {
82 return 0, fmt.Errorf("duration: %v is out of range for time.Duration", p)
83 }
84 if p.Nanos != 0 {
85 d += time.Duration(p.Nanos)
86 if (d < 0) != (p.Nanos < 0) {
87 return 0, fmt.Errorf("duration: %v is out of range for time.Duration", p)
88 }
89 }
90 return d, nil
91}
92
93// DurationProto converts a time.Duration to a durpb.Duration.
94func DurationProto(d time.Duration) *durpb.Duration {
95 nanos := d.Nanoseconds()
96 secs := nanos / 1e9
97 nanos -= secs * 1e9
98 return &durpb.Duration{
99 Seconds: secs,
100 Nanos: int32(nanos),
101 }
102}
diff --git a/vendor/github.com/golang/protobuf/ptypes/duration/duration.pb.go b/vendor/github.com/golang/protobuf/ptypes/duration/duration.pb.go
new file mode 100644
index 0000000..a7beb2c
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/ptypes/duration/duration.pb.go
@@ -0,0 +1,159 @@
1// Code generated by protoc-gen-go. DO NOT EDIT.
2// source: google/protobuf/duration.proto
3
4package duration // import "github.com/golang/protobuf/ptypes/duration"
5
6import proto "github.com/golang/protobuf/proto"
7import fmt "fmt"
8import math "math"
9
10// Reference imports to suppress errors if they are not otherwise used.
11var _ = proto.Marshal
12var _ = fmt.Errorf
13var _ = math.Inf
14
15// This is a compile-time assertion to ensure that this generated file
16// is compatible with the proto package it is being compiled against.
17// A compilation error at this line likely means your copy of the
18// proto package needs to be updated.
19const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
20
21// A Duration represents a signed, fixed-length span of time represented
22// as a count of seconds and fractions of seconds at nanosecond
23// resolution. It is independent of any calendar and concepts like "day"
24// or "month". It is related to Timestamp in that the difference between
25// two Timestamp values is a Duration and it can be added or subtracted
26// from a Timestamp. Range is approximately +-10,000 years.
27//
28// # Examples
29//
30// Example 1: Compute Duration from two Timestamps in pseudo code.
31//
32// Timestamp start = ...;
33// Timestamp end = ...;
34// Duration duration = ...;
35//
36// duration.seconds = end.seconds - start.seconds;
37// duration.nanos = end.nanos - start.nanos;
38//
39// if (duration.seconds < 0 && duration.nanos > 0) {
40// duration.seconds += 1;
41// duration.nanos -= 1000000000;
42// } else if (durations.seconds > 0 && duration.nanos < 0) {
43// duration.seconds -= 1;
44// duration.nanos += 1000000000;
45// }
46//
47// Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.
48//
49// Timestamp start = ...;
50// Duration duration = ...;
51// Timestamp end = ...;
52//
53// end.seconds = start.seconds + duration.seconds;
54// end.nanos = start.nanos + duration.nanos;
55//
56// if (end.nanos < 0) {
57// end.seconds -= 1;
58// end.nanos += 1000000000;
59// } else if (end.nanos >= 1000000000) {
60// end.seconds += 1;
61// end.nanos -= 1000000000;
62// }
63//
64// Example 3: Compute Duration from datetime.timedelta in Python.
65//
66// td = datetime.timedelta(days=3, minutes=10)
67// duration = Duration()
68// duration.FromTimedelta(td)
69//
70// # JSON Mapping
71//
72// In JSON format, the Duration type is encoded as a string rather than an
73// object, where the string ends in the suffix "s" (indicating seconds) and
74// is preceded by the number of seconds, with nanoseconds expressed as
75// fractional seconds. For example, 3 seconds with 0 nanoseconds should be
76// encoded in JSON format as "3s", while 3 seconds and 1 nanosecond should
77// be expressed in JSON format as "3.000000001s", and 3 seconds and 1
78// microsecond should be expressed in JSON format as "3.000001s".
79//
80//
81type Duration struct {
82 // Signed seconds of the span of time. Must be from -315,576,000,000
83 // to +315,576,000,000 inclusive. Note: these bounds are computed from:
84 // 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years
85 Seconds int64 `protobuf:"varint,1,opt,name=seconds,proto3" json:"seconds,omitempty"`
86 // Signed fractions of a second at nanosecond resolution of the span
87 // of time. Durations less than one second are represented with a 0
88 // `seconds` field and a positive or negative `nanos` field. For durations
89 // of one second or more, a non-zero value for the `nanos` field must be
90 // of the same sign as the `seconds` field. Must be from -999,999,999
91 // to +999,999,999 inclusive.
92 Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"`
93 XXX_NoUnkeyedLiteral struct{} `json:"-"`
94 XXX_unrecognized []byte `json:"-"`
95 XXX_sizecache int32 `json:"-"`
96}
97
98func (m *Duration) Reset() { *m = Duration{} }
99func (m *Duration) String() string { return proto.CompactTextString(m) }
100func (*Duration) ProtoMessage() {}
101func (*Duration) Descriptor() ([]byte, []int) {
102 return fileDescriptor_duration_e7d612259e3f0613, []int{0}
103}
104func (*Duration) XXX_WellKnownType() string { return "Duration" }
105func (m *Duration) XXX_Unmarshal(b []byte) error {
106 return xxx_messageInfo_Duration.Unmarshal(m, b)
107}
108func (m *Duration) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
109 return xxx_messageInfo_Duration.Marshal(b, m, deterministic)
110}
111func (dst *Duration) XXX_Merge(src proto.Message) {
112 xxx_messageInfo_Duration.Merge(dst, src)
113}
114func (m *Duration) XXX_Size() int {
115 return xxx_messageInfo_Duration.Size(m)
116}
117func (m *Duration) XXX_DiscardUnknown() {
118 xxx_messageInfo_Duration.DiscardUnknown(m)
119}
120
121var xxx_messageInfo_Duration proto.InternalMessageInfo
122
123func (m *Duration) GetSeconds() int64 {
124 if m != nil {
125 return m.Seconds
126 }
127 return 0
128}
129
130func (m *Duration) GetNanos() int32 {
131 if m != nil {
132 return m.Nanos
133 }
134 return 0
135}
136
137func init() {
138 proto.RegisterType((*Duration)(nil), "google.protobuf.Duration")
139}
140
141func init() {
142 proto.RegisterFile("google/protobuf/duration.proto", fileDescriptor_duration_e7d612259e3f0613)
143}
144
145var fileDescriptor_duration_e7d612259e3f0613 = []byte{
146 // 190 bytes of a gzipped FileDescriptorProto
147 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x4b, 0xcf, 0xcf, 0x4f,
148 0xcf, 0x49, 0xd5, 0x2f, 0x28, 0xca, 0x2f, 0xc9, 0x4f, 0x2a, 0x4d, 0xd3, 0x4f, 0x29, 0x2d, 0x4a,
149 0x2c, 0xc9, 0xcc, 0xcf, 0xd3, 0x03, 0x8b, 0x08, 0xf1, 0x43, 0xe4, 0xf5, 0x60, 0xf2, 0x4a, 0x56,
150 0x5c, 0x1c, 0x2e, 0x50, 0x25, 0x42, 0x12, 0x5c, 0xec, 0xc5, 0xa9, 0xc9, 0xf9, 0x79, 0x29, 0xc5,
151 0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0xcc, 0x41, 0x30, 0xae, 0x90, 0x08, 0x17, 0x6b, 0x5e, 0x62, 0x5e,
152 0x7e, 0xb1, 0x04, 0x93, 0x02, 0xa3, 0x06, 0x6b, 0x10, 0x84, 0xe3, 0x54, 0xc3, 0x25, 0x9c, 0x9c,
153 0x9f, 0xab, 0x87, 0x66, 0xa4, 0x13, 0x2f, 0xcc, 0xc0, 0x00, 0x90, 0x48, 0x00, 0x63, 0x94, 0x56,
154 0x7a, 0x66, 0x49, 0x46, 0x69, 0x92, 0x5e, 0x72, 0x7e, 0xae, 0x7e, 0x7a, 0x7e, 0x4e, 0x62, 0x5e,
155 0x3a, 0xc2, 0x7d, 0x05, 0x25, 0x95, 0x05, 0xa9, 0xc5, 0x70, 0x67, 0xfe, 0x60, 0x64, 0x5c, 0xc4,
156 0xc4, 0xec, 0x1e, 0xe0, 0xb4, 0x8a, 0x49, 0xce, 0x1d, 0x62, 0x6e, 0x00, 0x54, 0xa9, 0x5e, 0x78,
157 0x6a, 0x4e, 0x8e, 0x77, 0x5e, 0x7e, 0x79, 0x5e, 0x08, 0x48, 0x4b, 0x12, 0x1b, 0xd8, 0x0c, 0x63,
158 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0xdc, 0x84, 0x30, 0xff, 0xf3, 0x00, 0x00, 0x00,
159}
diff --git a/vendor/github.com/golang/protobuf/ptypes/duration/duration.proto b/vendor/github.com/golang/protobuf/ptypes/duration/duration.proto
new file mode 100644
index 0000000..975fce4
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/ptypes/duration/duration.proto
@@ -0,0 +1,117 @@
1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31syntax = "proto3";
32
33package google.protobuf;
34
35option csharp_namespace = "Google.Protobuf.WellKnownTypes";
36option cc_enable_arenas = true;
37option go_package = "github.com/golang/protobuf/ptypes/duration";
38option java_package = "com.google.protobuf";
39option java_outer_classname = "DurationProto";
40option java_multiple_files = true;
41option objc_class_prefix = "GPB";
42
43// A Duration represents a signed, fixed-length span of time represented
44// as a count of seconds and fractions of seconds at nanosecond
45// resolution. It is independent of any calendar and concepts like "day"
46// or "month". It is related to Timestamp in that the difference between
47// two Timestamp values is a Duration and it can be added or subtracted
48// from a Timestamp. Range is approximately +-10,000 years.
49//
50// # Examples
51//
52// Example 1: Compute Duration from two Timestamps in pseudo code.
53//
54// Timestamp start = ...;
55// Timestamp end = ...;
56// Duration duration = ...;
57//
58// duration.seconds = end.seconds - start.seconds;
59// duration.nanos = end.nanos - start.nanos;
60//
61// if (duration.seconds < 0 && duration.nanos > 0) {
62// duration.seconds += 1;
63// duration.nanos -= 1000000000;
64// } else if (durations.seconds > 0 && duration.nanos < 0) {
65// duration.seconds -= 1;
66// duration.nanos += 1000000000;
67// }
68//
69// Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.
70//
71// Timestamp start = ...;
72// Duration duration = ...;
73// Timestamp end = ...;
74//
75// end.seconds = start.seconds + duration.seconds;
76// end.nanos = start.nanos + duration.nanos;
77//
78// if (end.nanos < 0) {
79// end.seconds -= 1;
80// end.nanos += 1000000000;
81// } else if (end.nanos >= 1000000000) {
82// end.seconds += 1;
83// end.nanos -= 1000000000;
84// }
85//
86// Example 3: Compute Duration from datetime.timedelta in Python.
87//
88// td = datetime.timedelta(days=3, minutes=10)
89// duration = Duration()
90// duration.FromTimedelta(td)
91//
92// # JSON Mapping
93//
94// In JSON format, the Duration type is encoded as a string rather than an
95// object, where the string ends in the suffix "s" (indicating seconds) and
96// is preceded by the number of seconds, with nanoseconds expressed as
97// fractional seconds. For example, 3 seconds with 0 nanoseconds should be
98// encoded in JSON format as "3s", while 3 seconds and 1 nanosecond should
99// be expressed in JSON format as "3.000000001s", and 3 seconds and 1
100// microsecond should be expressed in JSON format as "3.000001s".
101//
102//
103message Duration {
104
105 // Signed seconds of the span of time. Must be from -315,576,000,000
106 // to +315,576,000,000 inclusive. Note: these bounds are computed from:
107 // 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years
108 int64 seconds = 1;
109
110 // Signed fractions of a second at nanosecond resolution of the span
111 // of time. Durations less than one second are represented with a 0
112 // `seconds` field and a positive or negative `nanos` field. For durations
113 // of one second or more, a non-zero value for the `nanos` field must be
114 // of the same sign as the `seconds` field. Must be from -999,999,999
115 // to +999,999,999 inclusive.
116 int32 nanos = 2;
117}
diff --git a/vendor/github.com/golang/protobuf/ptypes/timestamp.go b/vendor/github.com/golang/protobuf/ptypes/timestamp.go
new file mode 100644
index 0000000..47f10db
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/ptypes/timestamp.go
@@ -0,0 +1,134 @@
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
32package ptypes
33
34// This file implements operations on google.protobuf.Timestamp.
35
36import (
37 "errors"
38 "fmt"
39 "time"
40
41 tspb "github.com/golang/protobuf/ptypes/timestamp"
42)
43
44const (
45 // Seconds field of the earliest valid Timestamp.
46 // This is time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC).Unix().
47 minValidSeconds = -62135596800
48 // Seconds field just after the latest valid Timestamp.
49 // This is time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC).Unix().
50 maxValidSeconds = 253402300800
51)
52
53// validateTimestamp determines whether a Timestamp is valid.
54// A valid timestamp represents a time in the range
55// [0001-01-01, 10000-01-01) and has a Nanos field
56// in the range [0, 1e9).
57//
58// If the Timestamp is valid, validateTimestamp returns nil.
59// Otherwise, it returns an error that describes
60// the problem.
61//
62// Every valid Timestamp can be represented by a time.Time, but the converse is not true.
63func validateTimestamp(ts *tspb.Timestamp) error {
64 if ts == nil {
65 return errors.New("timestamp: nil Timestamp")
66 }
67 if ts.Seconds < minValidSeconds {
68 return fmt.Errorf("timestamp: %v before 0001-01-01", ts)
69 }
70 if ts.Seconds >= maxValidSeconds {
71 return fmt.Errorf("timestamp: %v after 10000-01-01", ts)
72 }
73 if ts.Nanos < 0 || ts.Nanos >= 1e9 {
74 return fmt.Errorf("timestamp: %v: nanos not in range [0, 1e9)", ts)
75 }
76 return nil
77}
78
79// Timestamp converts a google.protobuf.Timestamp proto to a time.Time.
80// It returns an error if the argument is invalid.
81//
82// Unlike most Go functions, if Timestamp returns an error, the first return value
83// is not the zero time.Time. Instead, it is the value obtained from the
84// time.Unix function when passed the contents of the Timestamp, in the UTC
85// locale. This may or may not be a meaningful time; many invalid Timestamps
86// do map to valid time.Times.
87//
88// A nil Timestamp returns an error. The first return value in that case is
89// undefined.
90func Timestamp(ts *tspb.Timestamp) (time.Time, error) {
91 // Don't return the zero value on error, because corresponds to a valid
92 // timestamp. Instead return whatever time.Unix gives us.
93 var t time.Time
94 if ts == nil {
95 t = time.Unix(0, 0).UTC() // treat nil like the empty Timestamp
96 } else {
97 t = time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()
98 }
99 return t, validateTimestamp(ts)
100}
101
102// TimestampNow returns a google.protobuf.Timestamp for the current time.
103func TimestampNow() *tspb.Timestamp {
104 ts, err := TimestampProto(time.Now())
105 if err != nil {
106 panic("ptypes: time.Now() out of Timestamp range")
107 }
108 return ts
109}
110
111// TimestampProto converts the time.Time to a google.protobuf.Timestamp proto.
112// It returns an error if the resulting Timestamp is invalid.
113func TimestampProto(t time.Time) (*tspb.Timestamp, error) {
114 seconds := t.Unix()
115 nanos := int32(t.Sub(time.Unix(seconds, 0)))
116 ts := &tspb.Timestamp{
117 Seconds: seconds,
118 Nanos: nanos,
119 }
120 if err := validateTimestamp(ts); err != nil {
121 return nil, err
122 }
123 return ts, nil
124}
125
126// TimestampString returns the RFC 3339 string for valid Timestamps. For invalid
127// Timestamps, it returns an error message in parentheses.
128func TimestampString(ts *tspb.Timestamp) string {
129 t, err := Timestamp(ts)
130 if err != nil {
131 return fmt.Sprintf("(%v)", err)
132 }
133 return t.Format(time.RFC3339Nano)
134}
diff --git a/vendor/github.com/golang/protobuf/ptypes/timestamp/timestamp.pb.go b/vendor/github.com/golang/protobuf/ptypes/timestamp/timestamp.pb.go
new file mode 100644
index 0000000..8e76ae9
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/ptypes/timestamp/timestamp.pb.go
@@ -0,0 +1,175 @@
1// Code generated by protoc-gen-go. DO NOT EDIT.
2// source: google/protobuf/timestamp.proto
3
4package timestamp // import "github.com/golang/protobuf/ptypes/timestamp"
5
6import proto "github.com/golang/protobuf/proto"
7import fmt "fmt"
8import math "math"
9
10// Reference imports to suppress errors if they are not otherwise used.
11var _ = proto.Marshal
12var _ = fmt.Errorf
13var _ = math.Inf
14
15// This is a compile-time assertion to ensure that this generated file
16// is compatible with the proto package it is being compiled against.
17// A compilation error at this line likely means your copy of the
18// proto package needs to be updated.
19const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
20
21// A Timestamp represents a point in time independent of any time zone
22// or calendar, represented as seconds and fractions of seconds at
23// nanosecond resolution in UTC Epoch time. It is encoded using the
24// Proleptic Gregorian Calendar which extends the Gregorian calendar
25// backwards to year one. It is encoded assuming all minutes are 60
26// seconds long, i.e. leap seconds are "smeared" so that no leap second
27// table is needed for interpretation. Range is from
28// 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z.
29// By restricting to that range, we ensure that we can convert to
30// and from RFC 3339 date strings.
31// See [https://www.ietf.org/rfc/rfc3339.txt](https://www.ietf.org/rfc/rfc3339.txt).
32//
33// # Examples
34//
35// Example 1: Compute Timestamp from POSIX `time()`.
36//
37// Timestamp timestamp;
38// timestamp.set_seconds(time(NULL));
39// timestamp.set_nanos(0);
40//
41// Example 2: Compute Timestamp from POSIX `gettimeofday()`.
42//
43// struct timeval tv;
44// gettimeofday(&tv, NULL);
45//
46// Timestamp timestamp;
47// timestamp.set_seconds(tv.tv_sec);
48// timestamp.set_nanos(tv.tv_usec * 1000);
49//
50// Example 3: Compute Timestamp from Win32 `GetSystemTimeAsFileTime()`.
51//
52// FILETIME ft;
53// GetSystemTimeAsFileTime(&ft);
54// UINT64 ticks = (((UINT64)ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
55//
56// // A Windows tick is 100 nanoseconds. Windows epoch 1601-01-01T00:00:00Z
57// // is 11644473600 seconds before Unix epoch 1970-01-01T00:00:00Z.
58// Timestamp timestamp;
59// timestamp.set_seconds((INT64) ((ticks / 10000000) - 11644473600LL));
60// timestamp.set_nanos((INT32) ((ticks % 10000000) * 100));
61//
62// Example 4: Compute Timestamp from Java `System.currentTimeMillis()`.
63//
64// long millis = System.currentTimeMillis();
65//
66// Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000)
67// .setNanos((int) ((millis % 1000) * 1000000)).build();
68//
69//
70// Example 5: Compute Timestamp from current time in Python.
71//
72// timestamp = Timestamp()
73// timestamp.GetCurrentTime()
74//
75// # JSON Mapping
76//
77// In JSON format, the Timestamp type is encoded as a string in the
78// [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) format. That is, the
79// format is "{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z"
80// where {year} is always expressed using four digits while {month}, {day},
81// {hour}, {min}, and {sec} are zero-padded to two digits each. The fractional
82// seconds, which can go up to 9 digits (i.e. up to 1 nanosecond resolution),
83// are optional. The "Z" suffix indicates the timezone ("UTC"); the timezone
84// is required, though only UTC (as indicated by "Z") is presently supported.
85//
86// For example, "2017-01-15T01:30:15.01Z" encodes 15.01 seconds past
87// 01:30 UTC on January 15, 2017.
88//
89// In JavaScript, one can convert a Date object to this format using the
90// standard [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString]
91// method. In Python, a standard `datetime.datetime` object can be converted
92// to this format using [`strftime`](https://docs.python.org/2/library/time.html#time.strftime)
93// with the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one
94// can use the Joda Time's [`ISODateTimeFormat.dateTime()`](
95// http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime--)
96// to obtain a formatter capable of generating timestamps in this format.
97//
98//
99type Timestamp struct {
100 // Represents seconds of UTC time since Unix epoch
101 // 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
102 // 9999-12-31T23:59:59Z inclusive.
103 Seconds int64 `protobuf:"varint,1,opt,name=seconds,proto3" json:"seconds,omitempty"`
104 // Non-negative fractions of a second at nanosecond resolution. Negative
105 // second values with fractions must still have non-negative nanos values
106 // that count forward in time. Must be from 0 to 999,999,999
107 // inclusive.
108 Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"`
109 XXX_NoUnkeyedLiteral struct{} `json:"-"`
110 XXX_unrecognized []byte `json:"-"`
111 XXX_sizecache int32 `json:"-"`
112}
113
114func (m *Timestamp) Reset() { *m = Timestamp{} }
115func (m *Timestamp) String() string { return proto.CompactTextString(m) }
116func (*Timestamp) ProtoMessage() {}
117func (*Timestamp) Descriptor() ([]byte, []int) {
118 return fileDescriptor_timestamp_b826e8e5fba671a8, []int{0}
119}
120func (*Timestamp) XXX_WellKnownType() string { return "Timestamp" }
121func (m *Timestamp) XXX_Unmarshal(b []byte) error {
122 return xxx_messageInfo_Timestamp.Unmarshal(m, b)
123}
124func (m *Timestamp) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
125 return xxx_messageInfo_Timestamp.Marshal(b, m, deterministic)
126}
127func (dst *Timestamp) XXX_Merge(src proto.Message) {
128 xxx_messageInfo_Timestamp.Merge(dst, src)
129}
130func (m *Timestamp) XXX_Size() int {
131 return xxx_messageInfo_Timestamp.Size(m)
132}
133func (m *Timestamp) XXX_DiscardUnknown() {
134 xxx_messageInfo_Timestamp.DiscardUnknown(m)
135}
136
137var xxx_messageInfo_Timestamp proto.InternalMessageInfo
138
139func (m *Timestamp) GetSeconds() int64 {
140 if m != nil {
141 return m.Seconds
142 }
143 return 0
144}
145
146func (m *Timestamp) GetNanos() int32 {
147 if m != nil {
148 return m.Nanos
149 }
150 return 0
151}
152
153func init() {
154 proto.RegisterType((*Timestamp)(nil), "google.protobuf.Timestamp")
155}
156
157func init() {
158 proto.RegisterFile("google/protobuf/timestamp.proto", fileDescriptor_timestamp_b826e8e5fba671a8)
159}
160
161var fileDescriptor_timestamp_b826e8e5fba671a8 = []byte{
162 // 191 bytes of a gzipped FileDescriptorProto
163 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x4f, 0xcf, 0xcf, 0x4f,
164 0xcf, 0x49, 0xd5, 0x2f, 0x28, 0xca, 0x2f, 0xc9, 0x4f, 0x2a, 0x4d, 0xd3, 0x2f, 0xc9, 0xcc, 0x4d,
165 0x2d, 0x2e, 0x49, 0xcc, 0x2d, 0xd0, 0x03, 0x0b, 0x09, 0xf1, 0x43, 0x14, 0xe8, 0xc1, 0x14, 0x28,
166 0x59, 0x73, 0x71, 0x86, 0xc0, 0xd4, 0x08, 0x49, 0x70, 0xb1, 0x17, 0xa7, 0x26, 0xe7, 0xe7, 0xa5,
167 0x14, 0x4b, 0x30, 0x2a, 0x30, 0x6a, 0x30, 0x07, 0xc1, 0xb8, 0x42, 0x22, 0x5c, 0xac, 0x79, 0x89,
168 0x79, 0xf9, 0xc5, 0x12, 0x4c, 0x0a, 0x8c, 0x1a, 0xac, 0x41, 0x10, 0x8e, 0x53, 0x1d, 0x97, 0x70,
169 0x72, 0x7e, 0xae, 0x1e, 0x9a, 0x99, 0x4e, 0x7c, 0x70, 0x13, 0x03, 0x40, 0x42, 0x01, 0x8c, 0x51,
170 0xda, 0xe9, 0x99, 0x25, 0x19, 0xa5, 0x49, 0x7a, 0xc9, 0xf9, 0xb9, 0xfa, 0xe9, 0xf9, 0x39, 0x89,
171 0x79, 0xe9, 0x08, 0x27, 0x16, 0x94, 0x54, 0x16, 0xa4, 0x16, 0x23, 0x5c, 0xfa, 0x83, 0x91, 0x71,
172 0x11, 0x13, 0xb3, 0x7b, 0x80, 0xd3, 0x2a, 0x26, 0x39, 0x77, 0x88, 0xc9, 0x01, 0x50, 0xb5, 0x7a,
173 0xe1, 0xa9, 0x39, 0x39, 0xde, 0x79, 0xf9, 0xe5, 0x79, 0x21, 0x20, 0x3d, 0x49, 0x6c, 0x60, 0x43,
174 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xbc, 0x77, 0x4a, 0x07, 0xf7, 0x00, 0x00, 0x00,
175}
diff --git a/vendor/github.com/golang/protobuf/ptypes/timestamp/timestamp.proto b/vendor/github.com/golang/protobuf/ptypes/timestamp/timestamp.proto
new file mode 100644
index 0000000..06750ab
--- /dev/null
+++ b/vendor/github.com/golang/protobuf/ptypes/timestamp/timestamp.proto
@@ -0,0 +1,133 @@
1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31syntax = "proto3";
32
33package google.protobuf;
34
35option csharp_namespace = "Google.Protobuf.WellKnownTypes";
36option cc_enable_arenas = true;
37option go_package = "github.com/golang/protobuf/ptypes/timestamp";
38option java_package = "com.google.protobuf";
39option java_outer_classname = "TimestampProto";
40option java_multiple_files = true;
41option objc_class_prefix = "GPB";
42
43// A Timestamp represents a point in time independent of any time zone
44// or calendar, represented as seconds and fractions of seconds at
45// nanosecond resolution in UTC Epoch time. It is encoded using the
46// Proleptic Gregorian Calendar which extends the Gregorian calendar
47// backwards to year one. It is encoded assuming all minutes are 60
48// seconds long, i.e. leap seconds are "smeared" so that no leap second
49// table is needed for interpretation. Range is from
50// 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z.
51// By restricting to that range, we ensure that we can convert to
52// and from RFC 3339 date strings.
53// See [https://www.ietf.org/rfc/rfc3339.txt](https://www.ietf.org/rfc/rfc3339.txt).
54//
55// # Examples
56//
57// Example 1: Compute Timestamp from POSIX `time()`.
58//
59// Timestamp timestamp;
60// timestamp.set_seconds(time(NULL));
61// timestamp.set_nanos(0);
62//
63// Example 2: Compute Timestamp from POSIX `gettimeofday()`.
64//
65// struct timeval tv;
66// gettimeofday(&tv, NULL);
67//
68// Timestamp timestamp;
69// timestamp.set_seconds(tv.tv_sec);
70// timestamp.set_nanos(tv.tv_usec * 1000);
71//
72// Example 3: Compute Timestamp from Win32 `GetSystemTimeAsFileTime()`.
73//
74// FILETIME ft;
75// GetSystemTimeAsFileTime(&ft);
76// UINT64 ticks = (((UINT64)ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
77//
78// // A Windows tick is 100 nanoseconds. Windows epoch 1601-01-01T00:00:00Z
79// // is 11644473600 seconds before Unix epoch 1970-01-01T00:00:00Z.
80// Timestamp timestamp;
81// timestamp.set_seconds((INT64) ((ticks / 10000000) - 11644473600LL));
82// timestamp.set_nanos((INT32) ((ticks % 10000000) * 100));
83//
84// Example 4: Compute Timestamp from Java `System.currentTimeMillis()`.
85//
86// long millis = System.currentTimeMillis();
87//
88// Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000)
89// .setNanos((int) ((millis % 1000) * 1000000)).build();
90//
91//
92// Example 5: Compute Timestamp from current time in Python.
93//
94// timestamp = Timestamp()
95// timestamp.GetCurrentTime()
96//
97// # JSON Mapping
98//
99// In JSON format, the Timestamp type is encoded as a string in the
100// [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) format. That is, the
101// format is "{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z"
102// where {year} is always expressed using four digits while {month}, {day},
103// {hour}, {min}, and {sec} are zero-padded to two digits each. The fractional
104// seconds, which can go up to 9 digits (i.e. up to 1 nanosecond resolution),
105// are optional. The "Z" suffix indicates the timezone ("UTC"); the timezone
106// is required, though only UTC (as indicated by "Z") is presently supported.
107//
108// For example, "2017-01-15T01:30:15.01Z" encodes 15.01 seconds past
109// 01:30 UTC on January 15, 2017.
110//
111// In JavaScript, one can convert a Date object to this format using the
112// standard [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString]
113// method. In Python, a standard `datetime.datetime` object can be converted
114// to this format using [`strftime`](https://docs.python.org/2/library/time.html#time.strftime)
115// with the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one
116// can use the Joda Time's [`ISODateTimeFormat.dateTime()`](
117// http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime--)
118// to obtain a formatter capable of generating timestamps in this format.
119//
120//
121message Timestamp {
122
123 // Represents seconds of UTC time since Unix epoch
124 // 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
125 // 9999-12-31T23:59:59Z inclusive.
126 int64 seconds = 1;
127
128 // Non-negative fractions of a second at nanosecond resolution. Negative
129 // second values with fractions must still have non-negative nanos values
130 // that count forward in time. Must be from 0 to 999,999,999
131 // inclusive.
132 int32 nanos = 2;
133}