]>
Commit | Line | Data |
---|---|---|
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 | /* | |
33 | Package proto converts data structures to and from the wire format of | |
34 | protocol buffers. It works in concert with the Go source code generated | |
35 | for .proto files by the protocol compiler. | |
36 | ||
37 | A summary of the properties of the protocol buffer interface | |
38 | for 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 | ||
73 | When 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 | ||
78 | The simplest way to describe this is to see an example. | |
79 | Given 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 | ||
98 | The 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 | ||
222 | To 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 | */ | |
264 | package proto | |
265 | ||
266 | import ( | |
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. | |
279 | type RequiredNotSetError struct{ field string } | |
280 | ||
281 | func (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 | } | |
287 | func (e *RequiredNotSetError) RequiredNotSet() bool { | |
288 | return true | |
289 | } | |
290 | ||
291 | type invalidUTF8Error struct{ field string } | |
292 | ||
293 | func (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 | } | |
299 | func (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. | |
306 | var errInvalidUTF8 = &invalidUTF8Error{} | |
307 | ||
308 | // isNonFatal reports whether the error is either a RequiredNotSet error | |
309 | // or a InvalidUTF8 error. | |
310 | func 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 | ||
320 | type 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. | |
324 | func (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. | |
338 | type Message interface { | |
339 | Reset() | |
340 | String() string | |
341 | ProtoMessage() | |
342 | } | |
343 | ||
344 | // A Buffer is a buffer manager for marshaling and unmarshaling | |
345 | // protocol buffers. It may be reused between invocations to | |
346 | // reduce memory usage. It is not necessary to use a Buffer; | |
347 | // the global functions Marshal and Unmarshal create a | |
348 | // temporary Buffer and are fine for most applications. | |
349 | type Buffer struct { | |
350 | buf []byte // encode/decode byte stream | |
351 | index int // read point | |
352 | ||
353 | deterministic bool | |
354 | } | |
355 | ||
356 | // NewBuffer allocates a new Buffer and initializes its internal data to | |
357 | // the contents of the argument slice. | |
358 | func NewBuffer(e []byte) *Buffer { | |
359 | return &Buffer{buf: e} | |
360 | } | |
361 | ||
362 | // Reset resets the Buffer, ready for marshaling a new protocol buffer. | |
363 | func (p *Buffer) Reset() { | |
364 | p.buf = p.buf[0:0] // for reading/writing | |
365 | p.index = 0 // for reading | |
366 | } | |
367 | ||
368 | // SetBuf replaces the internal buffer with the slice, | |
369 | // ready for unmarshaling the contents of the slice. | |
370 | func (p *Buffer) SetBuf(s []byte) { | |
371 | p.buf = s | |
372 | p.index = 0 | |
373 | } | |
374 | ||
375 | // Bytes returns the contents of the Buffer. | |
376 | func (p *Buffer) Bytes() []byte { return p.buf } | |
377 | ||
378 | // SetDeterministic sets whether to use deterministic serialization. | |
379 | // | |
380 | // Deterministic serialization guarantees that for a given binary, equal | |
381 | // messages will always be serialized to the same bytes. This implies: | |
382 | // | |
383 | // - Repeated serialization of a message will return the same bytes. | |
384 | // - Different processes of the same binary (which may be executing on | |
385 | // different machines) will serialize equal messages to the same bytes. | |
386 | // | |
387 | // Note that the deterministic serialization is NOT canonical across | |
388 | // languages. It is not guaranteed to remain stable over time. It is unstable | |
389 | // across different builds with schema changes due to unknown fields. | |
390 | // Users who need canonical serialization (e.g., persistent storage in a | |
391 | // canonical form, fingerprinting, etc.) should define their own | |
392 | // canonicalization specification and implement their own serializer rather | |
393 | // than relying on this API. | |
394 | // | |
395 | // If deterministic serialization is requested, map entries will be sorted | |
396 | // by keys in lexographical order. This is an implementation detail and | |
397 | // subject to change. | |
398 | func (p *Buffer) SetDeterministic(deterministic bool) { | |
399 | p.deterministic = deterministic | |
400 | } | |
401 | ||
402 | /* | |
403 | * Helper routines for simplifying the creation of optional fields of basic type. | |
404 | */ | |
405 | ||
406 | // Bool is a helper routine that allocates a new bool value | |
407 | // to store v and returns a pointer to it. | |
408 | func Bool(v bool) *bool { | |
409 | return &v | |
410 | } | |
411 | ||
412 | // Int32 is a helper routine that allocates a new int32 value | |
413 | // to store v and returns a pointer to it. | |
414 | func Int32(v int32) *int32 { | |
415 | return &v | |
416 | } | |
417 | ||
418 | // Int is a helper routine that allocates a new int32 value | |
419 | // to store v and returns a pointer to it, but unlike Int32 | |
420 | // its argument value is an int. | |
421 | func Int(v int) *int32 { | |
422 | p := new(int32) | |
423 | *p = int32(v) | |
424 | return p | |
425 | } | |
426 | ||
427 | // Int64 is a helper routine that allocates a new int64 value | |
428 | // to store v and returns a pointer to it. | |
429 | func Int64(v int64) *int64 { | |
430 | return &v | |
431 | } | |
432 | ||
433 | // Float32 is a helper routine that allocates a new float32 value | |
434 | // to store v and returns a pointer to it. | |
435 | func Float32(v float32) *float32 { | |
436 | return &v | |
437 | } | |
438 | ||
439 | // Float64 is a helper routine that allocates a new float64 value | |
440 | // to store v and returns a pointer to it. | |
441 | func Float64(v float64) *float64 { | |
442 | return &v | |
443 | } | |
444 | ||
445 | // Uint32 is a helper routine that allocates a new uint32 value | |
446 | // to store v and returns a pointer to it. | |
447 | func Uint32(v uint32) *uint32 { | |
448 | return &v | |
449 | } | |
450 | ||
451 | // Uint64 is a helper routine that allocates a new uint64 value | |
452 | // to store v and returns a pointer to it. | |
453 | func Uint64(v uint64) *uint64 { | |
454 | return &v | |
455 | } | |
456 | ||
457 | // String is a helper routine that allocates a new string value | |
458 | // to store v and returns a pointer to it. | |
459 | func String(v string) *string { | |
460 | return &v | |
461 | } | |
462 | ||
463 | // EnumName is a helper function to simplify printing protocol buffer enums | |
464 | // by name. Given an enum map and a value, it returns a useful string. | |
465 | func EnumName(m map[int32]string, v int32) string { | |
466 | s, ok := m[v] | |
467 | if ok { | |
468 | return s | |
469 | } | |
470 | return strconv.Itoa(int(v)) | |
471 | } | |
472 | ||
473 | // UnmarshalJSONEnum is a helper function to simplify recovering enum int values | |
474 | // from their JSON-encoded representation. Given a map from the enum's symbolic | |
475 | // names to its int values, and a byte buffer containing the JSON-encoded | |
476 | // value, it returns an int32 that can be cast to the enum type by the caller. | |
477 | // | |
478 | // The function can deal with both JSON representations, numeric and symbolic. | |
479 | func UnmarshalJSONEnum(m map[string]int32, data []byte, enumName string) (int32, error) { | |
480 | if data[0] == '"' { | |
481 | // New style: enums are strings. | |
482 | var repr string | |
483 | if err := json.Unmarshal(data, &repr); err != nil { | |
484 | return -1, err | |
485 | } | |
486 | val, ok := m[repr] | |
487 | if !ok { | |
488 | return 0, fmt.Errorf("unrecognized enum %s value %q", enumName, repr) | |
489 | } | |
490 | return val, nil | |
491 | } | |
492 | // Old style: enums are ints. | |
493 | var val int32 | |
494 | if err := json.Unmarshal(data, &val); err != nil { | |
495 | return 0, fmt.Errorf("cannot unmarshal %#q into enum %s", data, enumName) | |
496 | } | |
497 | return val, nil | |
498 | } | |
499 | ||
500 | // DebugPrint dumps the encoded data in b in a debugging format with a header | |
501 | // including the string s. Used in testing but made available for general debugging. | |
502 | func (p *Buffer) DebugPrint(s string, b []byte) { | |
503 | var u uint64 | |
504 | ||
505 | obuf := p.buf | |
506 | index := p.index | |
507 | p.buf = b | |
508 | p.index = 0 | |
509 | depth := 0 | |
510 | ||
511 | fmt.Printf("\n--- %s ---\n", s) | |
512 | ||
513 | out: | |
514 | for { | |
515 | for i := 0; i < depth; i++ { | |
516 | fmt.Print(" ") | |
517 | } | |
518 | ||
519 | index := p.index | |
520 | if index == len(p.buf) { | |
521 | break | |
522 | } | |
523 | ||
524 | op, err := p.DecodeVarint() | |
525 | if err != nil { | |
526 | fmt.Printf("%3d: fetching op err %v\n", index, err) | |
527 | break out | |
528 | } | |
529 | tag := op >> 3 | |
530 | wire := op & 7 | |
531 | ||
532 | switch wire { | |
533 | default: | |
534 | fmt.Printf("%3d: t=%3d unknown wire=%d\n", | |
535 | index, tag, wire) | |
536 | break out | |
537 | ||
538 | case WireBytes: | |
539 | var r []byte | |
540 | ||
541 | r, err = p.DecodeRawBytes(false) | |
542 | if err != nil { | |
543 | break out | |
544 | } | |
545 | fmt.Printf("%3d: t=%3d bytes [%d]", index, tag, len(r)) | |
546 | if len(r) <= 6 { | |
547 | for i := 0; i < len(r); i++ { | |
548 | fmt.Printf(" %.2x", r[i]) | |
549 | } | |
550 | } else { | |
551 | for i := 0; i < 3; i++ { | |
552 | fmt.Printf(" %.2x", r[i]) | |
553 | } | |
554 | fmt.Printf(" ..") | |
555 | for i := len(r) - 3; i < len(r); i++ { | |
556 | fmt.Printf(" %.2x", r[i]) | |
557 | } | |
558 | } | |
559 | fmt.Printf("\n") | |
560 | ||
561 | case WireFixed32: | |
562 | u, err = p.DecodeFixed32() | |
563 | if err != nil { | |
564 | fmt.Printf("%3d: t=%3d fix32 err %v\n", index, tag, err) | |
565 | break out | |
566 | } | |
567 | fmt.Printf("%3d: t=%3d fix32 %d\n", index, tag, u) | |
568 | ||
569 | case WireFixed64: | |
570 | u, err = p.DecodeFixed64() | |
571 | if err != nil { | |
572 | fmt.Printf("%3d: t=%3d fix64 err %v\n", index, tag, err) | |
573 | break out | |
574 | } | |
575 | fmt.Printf("%3d: t=%3d fix64 %d\n", index, tag, u) | |
576 | ||
577 | case WireVarint: | |
578 | u, err = p.DecodeVarint() | |
579 | if err != nil { | |
580 | fmt.Printf("%3d: t=%3d varint err %v\n", index, tag, err) | |
581 | break out | |
582 | } | |
583 | fmt.Printf("%3d: t=%3d varint %d\n", index, tag, u) | |
584 | ||
585 | case WireStartGroup: | |
586 | fmt.Printf("%3d: t=%3d start\n", index, tag) | |
587 | depth++ | |
588 | ||
589 | case WireEndGroup: | |
590 | depth-- | |
591 | fmt.Printf("%3d: t=%3d end\n", index, tag) | |
592 | } | |
593 | } | |
594 | ||
595 | if depth != 0 { | |
596 | fmt.Printf("%3d: start-end not balanced %d\n", p.index, depth) | |
597 | } | |
598 | fmt.Printf("\n") | |
599 | ||
600 | p.buf = obuf | |
601 | p.index = index | |
602 | } | |
603 | ||
604 | // SetDefaults sets unset protocol buffer fields to their default values. | |
605 | // It only modifies fields that are both unset and have defined defaults. | |
606 | // It recursively sets default values in any non-nil sub-messages. | |
607 | func SetDefaults(pb Message) { | |
608 | setDefaults(reflect.ValueOf(pb), true, false) | |
609 | } | |
610 | ||
611 | // v is a pointer to a struct. | |
612 | func setDefaults(v reflect.Value, recur, zeros bool) { | |
613 | v = v.Elem() | |
614 | ||
615 | defaultMu.RLock() | |
616 | dm, ok := defaults[v.Type()] | |
617 | defaultMu.RUnlock() | |
618 | if !ok { | |
619 | dm = buildDefaultMessage(v.Type()) | |
620 | defaultMu.Lock() | |
621 | defaults[v.Type()] = dm | |
622 | defaultMu.Unlock() | |
623 | } | |
624 | ||
625 | for _, sf := range dm.scalars { | |
626 | f := v.Field(sf.index) | |
627 | if !f.IsNil() { | |
628 | // field already set | |
629 | continue | |
630 | } | |
631 | dv := sf.value | |
632 | if dv == nil && !zeros { | |
633 | // no explicit default, and don't want to set zeros | |
634 | continue | |
635 | } | |
636 | fptr := f.Addr().Interface() // **T | |
637 | // TODO: Consider batching the allocations we do here. | |
638 | switch sf.kind { | |
639 | case reflect.Bool: | |
640 | b := new(bool) | |
641 | if dv != nil { | |
642 | *b = dv.(bool) | |
643 | } | |
644 | *(fptr.(**bool)) = b | |
645 | case reflect.Float32: | |
646 | f := new(float32) | |
647 | if dv != nil { | |
648 | *f = dv.(float32) | |
649 | } | |
650 | *(fptr.(**float32)) = f | |
651 | case reflect.Float64: | |
652 | f := new(float64) | |
653 | if dv != nil { | |
654 | *f = dv.(float64) | |
655 | } | |
656 | *(fptr.(**float64)) = f | |
657 | case reflect.Int32: | |
658 | // might be an enum | |
659 | if ft := f.Type(); ft != int32PtrType { | |
660 | // enum | |
661 | f.Set(reflect.New(ft.Elem())) | |
662 | if dv != nil { | |
663 | f.Elem().SetInt(int64(dv.(int32))) | |
664 | } | |
665 | } else { | |
666 | // int32 field | |
667 | i := new(int32) | |
668 | if dv != nil { | |
669 | *i = dv.(int32) | |
670 | } | |
671 | *(fptr.(**int32)) = i | |
672 | } | |
673 | case reflect.Int64: | |
674 | i := new(int64) | |
675 | if dv != nil { | |
676 | *i = dv.(int64) | |
677 | } | |
678 | *(fptr.(**int64)) = i | |
679 | case reflect.String: | |
680 | s := new(string) | |
681 | if dv != nil { | |
682 | *s = dv.(string) | |
683 | } | |
684 | *(fptr.(**string)) = s | |
685 | case reflect.Uint8: | |
686 | // exceptional case: []byte | |
687 | var b []byte | |
688 | if dv != nil { | |
689 | db := dv.([]byte) | |
690 | b = make([]byte, len(db)) | |
691 | copy(b, db) | |
692 | } else { | |
693 | b = []byte{} | |
694 | } | |
695 | *(fptr.(*[]byte)) = b | |
696 | case reflect.Uint32: | |
697 | u := new(uint32) | |
698 | if dv != nil { | |
699 | *u = dv.(uint32) | |
700 | } | |
701 | *(fptr.(**uint32)) = u | |
702 | case reflect.Uint64: | |
703 | u := new(uint64) | |
704 | if dv != nil { | |
705 | *u = dv.(uint64) | |
706 | } | |
707 | *(fptr.(**uint64)) = u | |
708 | default: | |
709 | log.Printf("proto: can't set default for field %v (sf.kind=%v)", f, sf.kind) | |
710 | } | |
711 | } | |
712 | ||
713 | for _, ni := range dm.nested { | |
714 | f := v.Field(ni) | |
715 | // f is *T or []*T or map[T]*T | |
716 | switch f.Kind() { | |
717 | case reflect.Ptr: | |
718 | if f.IsNil() { | |
719 | continue | |
720 | } | |
721 | setDefaults(f, recur, zeros) | |
722 | ||
723 | case reflect.Slice: | |
724 | for i := 0; i < f.Len(); i++ { | |
725 | e := f.Index(i) | |
726 | if e.IsNil() { | |
727 | continue | |
728 | } | |
729 | setDefaults(e, recur, zeros) | |
730 | } | |
731 | ||
732 | case reflect.Map: | |
733 | for _, k := range f.MapKeys() { | |
734 | e := f.MapIndex(k) | |
735 | if e.IsNil() { | |
736 | continue | |
737 | } | |
738 | setDefaults(e, recur, zeros) | |
739 | } | |
740 | } | |
741 | } | |
742 | } | |
743 | ||
744 | var ( | |
745 | // defaults maps a protocol buffer struct type to a slice of the fields, | |
746 | // with its scalar fields set to their proto-declared non-zero default values. | |
747 | defaultMu sync.RWMutex | |
748 | defaults = make(map[reflect.Type]defaultMessage) | |
749 | ||
750 | int32PtrType = reflect.TypeOf((*int32)(nil)) | |
751 | ) | |
752 | ||
753 | // defaultMessage represents information about the default values of a message. | |
754 | type defaultMessage struct { | |
755 | scalars []scalarField | |
756 | nested []int // struct field index of nested messages | |
757 | } | |
758 | ||
759 | type scalarField struct { | |
760 | index int // struct field index | |
761 | kind reflect.Kind // element type (the T in *T or []T) | |
762 | value interface{} // the proto-declared default value, or nil | |
763 | } | |
764 | ||
765 | // t is a struct type. | |
766 | func buildDefaultMessage(t reflect.Type) (dm defaultMessage) { | |
767 | sprop := GetProperties(t) | |
768 | for _, prop := range sprop.Prop { | |
769 | fi, ok := sprop.decoderTags.get(prop.Tag) | |
770 | if !ok { | |
771 | // XXX_unrecognized | |
772 | continue | |
773 | } | |
774 | ft := t.Field(fi).Type | |
775 | ||
776 | sf, nested, err := fieldDefault(ft, prop) | |
777 | switch { | |
778 | case err != nil: | |
779 | log.Print(err) | |
780 | case nested: | |
781 | dm.nested = append(dm.nested, fi) | |
782 | case sf != nil: | |
783 | sf.index = fi | |
784 | dm.scalars = append(dm.scalars, *sf) | |
785 | } | |
786 | } | |
787 | ||
788 | return dm | |
789 | } | |
790 | ||
791 | // fieldDefault returns the scalarField for field type ft. | |
792 | // sf will be nil if the field can not have a default. | |
793 | // nestedMessage will be true if this is a nested message. | |
794 | // Note that sf.index is not set on return. | |
795 | func fieldDefault(ft reflect.Type, prop *Properties) (sf *scalarField, nestedMessage bool, err error) { | |
796 | var canHaveDefault bool | |
797 | switch ft.Kind() { | |
798 | case reflect.Ptr: | |
799 | if ft.Elem().Kind() == reflect.Struct { | |
800 | nestedMessage = true | |
801 | } else { | |
802 | canHaveDefault = true // proto2 scalar field | |
803 | } | |
804 | ||
805 | case reflect.Slice: | |
806 | switch ft.Elem().Kind() { | |
807 | case reflect.Ptr: | |
808 | nestedMessage = true // repeated message | |
809 | case reflect.Uint8: | |
810 | canHaveDefault = true // bytes field | |
811 | } | |
812 | ||
813 | case reflect.Map: | |
814 | if ft.Elem().Kind() == reflect.Ptr { | |
815 | nestedMessage = true // map with message values | |
816 | } | |
817 | } | |
818 | ||
819 | if !canHaveDefault { | |
820 | if nestedMessage { | |
821 | return nil, true, nil | |
822 | } | |
823 | return nil, false, nil | |
824 | } | |
825 | ||
826 | // We now know that ft is a pointer or slice. | |
827 | sf = &scalarField{kind: ft.Elem().Kind()} | |
828 | ||
829 | // scalar fields without defaults | |
830 | if !prop.HasDefault { | |
831 | return sf, false, nil | |
832 | } | |
833 | ||
834 | // a scalar field: either *T or []byte | |
835 | switch ft.Elem().Kind() { | |
836 | case reflect.Bool: | |
837 | x, err := strconv.ParseBool(prop.Default) | |
838 | if err != nil { | |
839 | return nil, false, fmt.Errorf("proto: bad default bool %q: %v", prop.Default, err) | |
840 | } | |
841 | sf.value = x | |
842 | case reflect.Float32: | |
843 | x, err := strconv.ParseFloat(prop.Default, 32) | |
844 | if err != nil { | |
845 | return nil, false, fmt.Errorf("proto: bad default float32 %q: %v", prop.Default, err) | |
846 | } | |
847 | sf.value = float32(x) | |
848 | case reflect.Float64: | |
849 | x, err := strconv.ParseFloat(prop.Default, 64) | |
850 | if err != nil { | |
851 | return nil, false, fmt.Errorf("proto: bad default float64 %q: %v", prop.Default, err) | |
852 | } | |
853 | sf.value = x | |
854 | case reflect.Int32: | |
855 | x, err := strconv.ParseInt(prop.Default, 10, 32) | |
856 | if err != nil { | |
857 | return nil, false, fmt.Errorf("proto: bad default int32 %q: %v", prop.Default, err) | |
858 | } | |
859 | sf.value = int32(x) | |
860 | case reflect.Int64: | |
861 | x, err := strconv.ParseInt(prop.Default, 10, 64) | |
862 | if err != nil { | |
863 | return nil, false, fmt.Errorf("proto: bad default int64 %q: %v", prop.Default, err) | |
864 | } | |
865 | sf.value = x | |
866 | case reflect.String: | |
867 | sf.value = prop.Default | |
868 | case reflect.Uint8: | |
869 | // []byte (not *uint8) | |
870 | sf.value = []byte(prop.Default) | |
871 | case reflect.Uint32: | |
872 | x, err := strconv.ParseUint(prop.Default, 10, 32) | |
873 | if err != nil { | |
874 | return nil, false, fmt.Errorf("proto: bad default uint32 %q: %v", prop.Default, err) | |
875 | } | |
876 | sf.value = uint32(x) | |
877 | case reflect.Uint64: | |
878 | x, err := strconv.ParseUint(prop.Default, 10, 64) | |
879 | if err != nil { | |
880 | return nil, false, fmt.Errorf("proto: bad default uint64 %q: %v", prop.Default, err) | |
881 | } | |
882 | sf.value = x | |
883 | default: | |
884 | return nil, false, fmt.Errorf("proto: unhandled def kind %v", ft.Elem().Kind()) | |
885 | } | |
886 | ||
887 | return sf, false, nil | |
888 | } | |
889 | ||
890 | // mapKeys returns a sort.Interface to be used for sorting the map keys. | |
891 | // Map fields may have key types of non-float scalars, strings and enums. | |
892 | func mapKeys(vs []reflect.Value) sort.Interface { | |
893 | s := mapKeySorter{vs: vs} | |
894 | ||
895 | // Type specialization per https://developers.google.com/protocol-buffers/docs/proto#maps. | |
896 | if len(vs) == 0 { | |
897 | return s | |
898 | } | |
899 | switch vs[0].Kind() { | |
900 | case reflect.Int32, reflect.Int64: | |
901 | s.less = func(a, b reflect.Value) bool { return a.Int() < b.Int() } | |
902 | case reflect.Uint32, reflect.Uint64: | |
903 | s.less = func(a, b reflect.Value) bool { return a.Uint() < b.Uint() } | |
904 | case reflect.Bool: | |
905 | s.less = func(a, b reflect.Value) bool { return !a.Bool() && b.Bool() } // false < true | |
906 | case reflect.String: | |
907 | s.less = func(a, b reflect.Value) bool { return a.String() < b.String() } | |
908 | default: | |
909 | panic(fmt.Sprintf("unsupported map key type: %v", vs[0].Kind())) | |
910 | } | |
911 | ||
912 | return s | |
913 | } | |
914 | ||
915 | type mapKeySorter struct { | |
916 | vs []reflect.Value | |
917 | less func(a, b reflect.Value) bool | |
918 | } | |
919 | ||
920 | func (s mapKeySorter) Len() int { return len(s.vs) } | |
921 | func (s mapKeySorter) Swap(i, j int) { s.vs[i], s.vs[j] = s.vs[j], s.vs[i] } | |
922 | func (s mapKeySorter) Less(i, j int) bool { | |
923 | return s.less(s.vs[i], s.vs[j]) | |
924 | } | |
925 | ||
926 | // isProto3Zero reports whether v is a zero proto3 value. | |
927 | func isProto3Zero(v reflect.Value) bool { | |
928 | switch v.Kind() { | |
929 | case reflect.Bool: | |
930 | return !v.Bool() | |
931 | case reflect.Int32, reflect.Int64: | |
932 | return v.Int() == 0 | |
933 | case reflect.Uint32, reflect.Uint64: | |
934 | return v.Uint() == 0 | |
935 | case reflect.Float32, reflect.Float64: | |
936 | return v.Float() == 0 | |
937 | case reflect.String: | |
938 | return v.String() == "" | |
939 | } | |
940 | return false | |
941 | } | |
942 | ||
943 | const ( | |
944 | // ProtoPackageIsVersion3 is referenced from generated protocol buffer files | |
945 | // to assert that that code is compatible with this version of the proto package. | |
946 | ProtoPackageIsVersion3 = true | |
947 | ||
948 | // ProtoPackageIsVersion2 is referenced from generated protocol buffer files | |
949 | // to assert that that code is compatible with this version of the proto package. | |
950 | ProtoPackageIsVersion2 = true | |
951 | ||
952 | // ProtoPackageIsVersion1 is referenced from generated protocol buffer files | |
953 | // to assert that that code is compatible with this version of the proto package. | |
954 | ProtoPackageIsVersion1 = true | |
955 | ) | |
956 | ||
957 | // InternalMessageInfo is a type used internally by generated .pb.go files. | |
958 | // This type is not intended to be used by non-generated code. | |
959 | // This type is not subject to any compatibility guarantee. | |
960 | type InternalMessageInfo struct { | |
961 | marshal *marshalInfo | |
962 | unmarshal *unmarshalInfo | |
963 | merge *mergeInfo | |
964 | discard *discardInfo | |
965 | } |