github.com/gogo/protobuf@v1.3.2/jsonpb/jsonpb.go (about) 1 // Go support for Protocol Buffers - Google's data interchange format 2 // 3 // Copyright 2015 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 jsonpb provides marshaling and unmarshaling between protocol buffers and JSON. 34 It follows the specification at https://developers.google.com/protocol-buffers/docs/proto3#json. 35 36 This package produces a different output than the standard "encoding/json" package, 37 which does not operate correctly on protocol buffers. 38 */ 39 package jsonpb 40 41 import ( 42 "bytes" 43 "encoding/json" 44 "errors" 45 "fmt" 46 "io" 47 "math" 48 "reflect" 49 "sort" 50 "strconv" 51 "strings" 52 "time" 53 54 "github.com/gogo/protobuf/proto" 55 "github.com/gogo/protobuf/types" 56 ) 57 58 const secondInNanos = int64(time.Second / time.Nanosecond) 59 const maxSecondsInDuration = 315576000000 60 61 // Marshaler is a configurable object for converting between 62 // protocol buffer objects and a JSON representation for them. 63 type Marshaler struct { 64 // Whether to render enum values as integers, as opposed to string values. 65 EnumsAsInts bool 66 67 // Whether to render fields with zero values. 68 EmitDefaults bool 69 70 // A string to indent each level by. The presence of this field will 71 // also cause a space to appear between the field separator and 72 // value, and for newlines to be appear between fields and array 73 // elements. 74 Indent string 75 76 // Whether to use the original (.proto) name for fields. 77 OrigName bool 78 79 // A custom URL resolver to use when marshaling Any messages to JSON. 80 // If unset, the default resolution strategy is to extract the 81 // fully-qualified type name from the type URL and pass that to 82 // proto.MessageType(string). 83 AnyResolver AnyResolver 84 } 85 86 // AnyResolver takes a type URL, present in an Any message, and resolves it into 87 // an instance of the associated message. 88 type AnyResolver interface { 89 Resolve(typeUrl string) (proto.Message, error) 90 } 91 92 func defaultResolveAny(typeUrl string) (proto.Message, error) { 93 // Only the part of typeUrl after the last slash is relevant. 94 mname := typeUrl 95 if slash := strings.LastIndex(mname, "/"); slash >= 0 { 96 mname = mname[slash+1:] 97 } 98 mt := proto.MessageType(mname) 99 if mt == nil { 100 return nil, fmt.Errorf("unknown message type %q", mname) 101 } 102 return reflect.New(mt.Elem()).Interface().(proto.Message), nil 103 } 104 105 // JSONPBMarshaler is implemented by protobuf messages that customize the 106 // way they are marshaled to JSON. Messages that implement this should 107 // also implement JSONPBUnmarshaler so that the custom format can be 108 // parsed. 109 // 110 // The JSON marshaling must follow the proto to JSON specification: 111 // https://developers.google.com/protocol-buffers/docs/proto3#json 112 type JSONPBMarshaler interface { 113 MarshalJSONPB(*Marshaler) ([]byte, error) 114 } 115 116 // JSONPBUnmarshaler is implemented by protobuf messages that customize 117 // the way they are unmarshaled from JSON. Messages that implement this 118 // should also implement JSONPBMarshaler so that the custom format can be 119 // produced. 120 // 121 // The JSON unmarshaling must follow the JSON to proto specification: 122 // https://developers.google.com/protocol-buffers/docs/proto3#json 123 type JSONPBUnmarshaler interface { 124 UnmarshalJSONPB(*Unmarshaler, []byte) error 125 } 126 127 // Marshal marshals a protocol buffer into JSON. 128 func (m *Marshaler) Marshal(out io.Writer, pb proto.Message) error { 129 v := reflect.ValueOf(pb) 130 if pb == nil || (v.Kind() == reflect.Ptr && v.IsNil()) { 131 return errors.New("Marshal called with nil") 132 } 133 // Check for unset required fields first. 134 if err := checkRequiredFields(pb); err != nil { 135 return err 136 } 137 writer := &errWriter{writer: out} 138 return m.marshalObject(writer, pb, "", "") 139 } 140 141 // MarshalToString converts a protocol buffer object to JSON string. 142 func (m *Marshaler) MarshalToString(pb proto.Message) (string, error) { 143 var buf bytes.Buffer 144 if err := m.Marshal(&buf, pb); err != nil { 145 return "", err 146 } 147 return buf.String(), nil 148 } 149 150 type int32Slice []int32 151 152 var nonFinite = map[string]float64{ 153 `"NaN"`: math.NaN(), 154 `"Infinity"`: math.Inf(1), 155 `"-Infinity"`: math.Inf(-1), 156 } 157 158 // For sorting extensions ids to ensure stable output. 159 func (s int32Slice) Len() int { return len(s) } 160 func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] } 161 func (s int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 162 163 type isWkt interface { 164 XXX_WellKnownType() string 165 } 166 167 var ( 168 wktType = reflect.TypeOf((*isWkt)(nil)).Elem() 169 messageType = reflect.TypeOf((*proto.Message)(nil)).Elem() 170 ) 171 172 // marshalObject writes a struct to the Writer. 173 func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeURL string) error { 174 if jsm, ok := v.(JSONPBMarshaler); ok { 175 b, err := jsm.MarshalJSONPB(m) 176 if err != nil { 177 return err 178 } 179 if typeURL != "" { 180 // we are marshaling this object to an Any type 181 var js map[string]*json.RawMessage 182 if err = json.Unmarshal(b, &js); err != nil { 183 return fmt.Errorf("type %T produced invalid JSON: %v", v, err) 184 } 185 turl, err := json.Marshal(typeURL) 186 if err != nil { 187 return fmt.Errorf("failed to marshal type URL %q to JSON: %v", typeURL, err) 188 } 189 js["@type"] = (*json.RawMessage)(&turl) 190 if m.Indent != "" { 191 b, err = json.MarshalIndent(js, indent, m.Indent) 192 } else { 193 b, err = json.Marshal(js) 194 } 195 if err != nil { 196 return err 197 } 198 } 199 200 out.write(string(b)) 201 return out.err 202 } 203 204 s := reflect.ValueOf(v).Elem() 205 206 // Handle well-known types. 207 if wkt, ok := v.(isWkt); ok { 208 switch wkt.XXX_WellKnownType() { 209 case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value", 210 "Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue": 211 // "Wrappers use the same representation in JSON 212 // as the wrapped primitive type, ..." 213 sprop := proto.GetProperties(s.Type()) 214 return m.marshalValue(out, sprop.Prop[0], s.Field(0), indent) 215 case "Any": 216 // Any is a bit more involved. 217 return m.marshalAny(out, v, indent) 218 case "Duration": 219 s, ns := s.Field(0).Int(), s.Field(1).Int() 220 if s < -maxSecondsInDuration || s > maxSecondsInDuration { 221 return fmt.Errorf("seconds out of range %v", s) 222 } 223 if ns <= -secondInNanos || ns >= secondInNanos { 224 return fmt.Errorf("ns out of range (%v, %v)", -secondInNanos, secondInNanos) 225 } 226 if (s > 0 && ns < 0) || (s < 0 && ns > 0) { 227 return errors.New("signs of seconds and nanos do not match") 228 } 229 // Generated output always contains 0, 3, 6, or 9 fractional digits, 230 // depending on required precision, followed by the suffix "s". 231 f := "%d.%09d" 232 if ns < 0 { 233 ns = -ns 234 if s == 0 { 235 f = "-%d.%09d" 236 } 237 } 238 x := fmt.Sprintf(f, s, ns) 239 x = strings.TrimSuffix(x, "000") 240 x = strings.TrimSuffix(x, "000") 241 x = strings.TrimSuffix(x, ".000") 242 out.write(`"`) 243 out.write(x) 244 out.write(`s"`) 245 return out.err 246 case "Struct", "ListValue": 247 // Let marshalValue handle the `Struct.fields` map or the `ListValue.values` slice. 248 // TODO: pass the correct Properties if needed. 249 return m.marshalValue(out, &proto.Properties{}, s.Field(0), indent) 250 case "Timestamp": 251 // "RFC 3339, where generated output will always be Z-normalized 252 // and uses 0, 3, 6 or 9 fractional digits." 253 s, ns := s.Field(0).Int(), s.Field(1).Int() 254 if ns < 0 || ns >= secondInNanos { 255 return fmt.Errorf("ns out of range [0, %v)", secondInNanos) 256 } 257 t := time.Unix(s, ns).UTC() 258 // time.RFC3339Nano isn't exactly right (we need to get 3/6/9 fractional digits). 259 x := t.Format("2006-01-02T15:04:05.000000000") 260 x = strings.TrimSuffix(x, "000") 261 x = strings.TrimSuffix(x, "000") 262 x = strings.TrimSuffix(x, ".000") 263 out.write(`"`) 264 out.write(x) 265 out.write(`Z"`) 266 return out.err 267 case "Value": 268 // Value has a single oneof. 269 kind := s.Field(0) 270 if kind.IsNil() { 271 // "absence of any variant indicates an error" 272 return errors.New("nil Value") 273 } 274 // oneof -> *T -> T -> T.F 275 x := kind.Elem().Elem().Field(0) 276 // TODO: pass the correct Properties if needed. 277 return m.marshalValue(out, &proto.Properties{}, x, indent) 278 } 279 } 280 281 out.write("{") 282 if m.Indent != "" { 283 out.write("\n") 284 } 285 286 firstField := true 287 288 if typeURL != "" { 289 if err := m.marshalTypeURL(out, indent, typeURL); err != nil { 290 return err 291 } 292 firstField = false 293 } 294 295 for i := 0; i < s.NumField(); i++ { 296 value := s.Field(i) 297 valueField := s.Type().Field(i) 298 if strings.HasPrefix(valueField.Name, "XXX_") { 299 continue 300 } 301 302 //this is not a protobuf field 303 if valueField.Tag.Get("protobuf") == "" && valueField.Tag.Get("protobuf_oneof") == "" { 304 continue 305 } 306 307 // IsNil will panic on most value kinds. 308 switch value.Kind() { 309 case reflect.Chan, reflect.Func, reflect.Interface: 310 if value.IsNil() { 311 continue 312 } 313 } 314 315 if !m.EmitDefaults { 316 switch value.Kind() { 317 case reflect.Bool: 318 if !value.Bool() { 319 continue 320 } 321 case reflect.Int32, reflect.Int64: 322 if value.Int() == 0 { 323 continue 324 } 325 case reflect.Uint32, reflect.Uint64: 326 if value.Uint() == 0 { 327 continue 328 } 329 case reflect.Float32, reflect.Float64: 330 if value.Float() == 0 { 331 continue 332 } 333 case reflect.String: 334 if value.Len() == 0 { 335 continue 336 } 337 case reflect.Map, reflect.Ptr, reflect.Slice: 338 if value.IsNil() { 339 continue 340 } 341 } 342 } 343 344 // Oneof fields need special handling. 345 if valueField.Tag.Get("protobuf_oneof") != "" { 346 // value is an interface containing &T{real_value}. 347 sv := value.Elem().Elem() // interface -> *T -> T 348 value = sv.Field(0) 349 valueField = sv.Type().Field(0) 350 } 351 prop := jsonProperties(valueField, m.OrigName) 352 if !firstField { 353 m.writeSep(out) 354 } 355 // If the map value is a cast type, it may not implement proto.Message, therefore 356 // allow the struct tag to declare the underlying message type. Change the property 357 // of the child types, use CustomType as a passer. CastType currently property is 358 // not used in json encoding. 359 if value.Kind() == reflect.Map { 360 if tag := valueField.Tag.Get("protobuf"); tag != "" { 361 for _, v := range strings.Split(tag, ",") { 362 if !strings.HasPrefix(v, "castvaluetype=") { 363 continue 364 } 365 v = strings.TrimPrefix(v, "castvaluetype=") 366 prop.MapValProp.CustomType = v 367 break 368 } 369 } 370 } 371 if err := m.marshalField(out, prop, value, indent); err != nil { 372 return err 373 } 374 firstField = false 375 } 376 377 // Handle proto2 extensions. 378 if ep, ok := v.(proto.Message); ok { 379 extensions := proto.RegisteredExtensions(v) 380 // Sort extensions for stable output. 381 ids := make([]int32, 0, len(extensions)) 382 for id, desc := range extensions { 383 if !proto.HasExtension(ep, desc) { 384 continue 385 } 386 ids = append(ids, id) 387 } 388 sort.Sort(int32Slice(ids)) 389 for _, id := range ids { 390 desc := extensions[id] 391 if desc == nil { 392 // unknown extension 393 continue 394 } 395 ext, extErr := proto.GetExtension(ep, desc) 396 if extErr != nil { 397 return extErr 398 } 399 value := reflect.ValueOf(ext) 400 var prop proto.Properties 401 prop.Parse(desc.Tag) 402 prop.JSONName = fmt.Sprintf("[%s]", desc.Name) 403 if !firstField { 404 m.writeSep(out) 405 } 406 if err := m.marshalField(out, &prop, value, indent); err != nil { 407 return err 408 } 409 firstField = false 410 } 411 412 } 413 414 if m.Indent != "" { 415 out.write("\n") 416 out.write(indent) 417 } 418 out.write("}") 419 return out.err 420 } 421 422 func (m *Marshaler) writeSep(out *errWriter) { 423 if m.Indent != "" { 424 out.write(",\n") 425 } else { 426 out.write(",") 427 } 428 } 429 430 func (m *Marshaler) marshalAny(out *errWriter, any proto.Message, indent string) error { 431 // "If the Any contains a value that has a special JSON mapping, 432 // it will be converted as follows: {"@type": xxx, "value": yyy}. 433 // Otherwise, the value will be converted into a JSON object, 434 // and the "@type" field will be inserted to indicate the actual data type." 435 v := reflect.ValueOf(any).Elem() 436 turl := v.Field(0).String() 437 val := v.Field(1).Bytes() 438 439 var msg proto.Message 440 var err error 441 if m.AnyResolver != nil { 442 msg, err = m.AnyResolver.Resolve(turl) 443 } else { 444 msg, err = defaultResolveAny(turl) 445 } 446 if err != nil { 447 return err 448 } 449 450 if err := proto.Unmarshal(val, msg); err != nil { 451 return err 452 } 453 454 if _, ok := msg.(isWkt); ok { 455 out.write("{") 456 if m.Indent != "" { 457 out.write("\n") 458 } 459 if err := m.marshalTypeURL(out, indent, turl); err != nil { 460 return err 461 } 462 m.writeSep(out) 463 if m.Indent != "" { 464 out.write(indent) 465 out.write(m.Indent) 466 out.write(`"value": `) 467 } else { 468 out.write(`"value":`) 469 } 470 if err := m.marshalObject(out, msg, indent+m.Indent, ""); err != nil { 471 return err 472 } 473 if m.Indent != "" { 474 out.write("\n") 475 out.write(indent) 476 } 477 out.write("}") 478 return out.err 479 } 480 481 return m.marshalObject(out, msg, indent, turl) 482 } 483 484 func (m *Marshaler) marshalTypeURL(out *errWriter, indent, typeURL string) error { 485 if m.Indent != "" { 486 out.write(indent) 487 out.write(m.Indent) 488 } 489 out.write(`"@type":`) 490 if m.Indent != "" { 491 out.write(" ") 492 } 493 b, err := json.Marshal(typeURL) 494 if err != nil { 495 return err 496 } 497 out.write(string(b)) 498 return out.err 499 } 500 501 // marshalField writes field description and value to the Writer. 502 func (m *Marshaler) marshalField(out *errWriter, prop *proto.Properties, v reflect.Value, indent string) error { 503 if m.Indent != "" { 504 out.write(indent) 505 out.write(m.Indent) 506 } 507 out.write(`"`) 508 out.write(prop.JSONName) 509 out.write(`":`) 510 if m.Indent != "" { 511 out.write(" ") 512 } 513 if err := m.marshalValue(out, prop, v, indent); err != nil { 514 return err 515 } 516 return nil 517 } 518 519 // marshalValue writes the value to the Writer. 520 func (m *Marshaler) marshalValue(out *errWriter, prop *proto.Properties, v reflect.Value, indent string) error { 521 522 v = reflect.Indirect(v) 523 524 // Handle nil pointer 525 if v.Kind() == reflect.Invalid { 526 out.write("null") 527 return out.err 528 } 529 530 // Handle repeated elements. 531 if v.Kind() == reflect.Slice && v.Type().Elem().Kind() != reflect.Uint8 { 532 out.write("[") 533 comma := "" 534 for i := 0; i < v.Len(); i++ { 535 sliceVal := v.Index(i) 536 out.write(comma) 537 if m.Indent != "" { 538 out.write("\n") 539 out.write(indent) 540 out.write(m.Indent) 541 out.write(m.Indent) 542 } 543 if err := m.marshalValue(out, prop, sliceVal, indent+m.Indent); err != nil { 544 return err 545 } 546 comma = "," 547 } 548 if m.Indent != "" { 549 out.write("\n") 550 out.write(indent) 551 out.write(m.Indent) 552 } 553 out.write("]") 554 return out.err 555 } 556 557 // Handle well-known types. 558 // Most are handled up in marshalObject (because 99% are messages). 559 if v.Type().Implements(wktType) { 560 wkt := v.Interface().(isWkt) 561 switch wkt.XXX_WellKnownType() { 562 case "NullValue": 563 out.write("null") 564 return out.err 565 } 566 } 567 568 if t, ok := v.Interface().(time.Time); ok { 569 ts, err := types.TimestampProto(t) 570 if err != nil { 571 return err 572 } 573 return m.marshalValue(out, prop, reflect.ValueOf(ts), indent) 574 } 575 576 if d, ok := v.Interface().(time.Duration); ok { 577 dur := types.DurationProto(d) 578 return m.marshalValue(out, prop, reflect.ValueOf(dur), indent) 579 } 580 581 // Handle enumerations. 582 if !m.EnumsAsInts && prop.Enum != "" { 583 // Unknown enum values will are stringified by the proto library as their 584 // value. Such values should _not_ be quoted or they will be interpreted 585 // as an enum string instead of their value. 586 enumStr := v.Interface().(fmt.Stringer).String() 587 var valStr string 588 if v.Kind() == reflect.Ptr { 589 valStr = strconv.Itoa(int(v.Elem().Int())) 590 } else { 591 valStr = strconv.Itoa(int(v.Int())) 592 } 593 594 if m, ok := v.Interface().(interface { 595 MarshalJSON() ([]byte, error) 596 }); ok { 597 data, err := m.MarshalJSON() 598 if err != nil { 599 return err 600 } 601 enumStr = string(data) 602 enumStr, err = strconv.Unquote(enumStr) 603 if err != nil { 604 return err 605 } 606 } 607 608 isKnownEnum := enumStr != valStr 609 610 if isKnownEnum { 611 out.write(`"`) 612 } 613 out.write(enumStr) 614 if isKnownEnum { 615 out.write(`"`) 616 } 617 return out.err 618 } 619 620 // Handle nested messages. 621 if v.Kind() == reflect.Struct { 622 i := v 623 if v.CanAddr() { 624 i = v.Addr() 625 } else { 626 i = reflect.New(v.Type()) 627 i.Elem().Set(v) 628 } 629 iface := i.Interface() 630 if iface == nil { 631 out.write(`null`) 632 return out.err 633 } 634 635 if m, ok := v.Interface().(interface { 636 MarshalJSON() ([]byte, error) 637 }); ok { 638 data, err := m.MarshalJSON() 639 if err != nil { 640 return err 641 } 642 out.write(string(data)) 643 return nil 644 } 645 646 pm, ok := iface.(proto.Message) 647 if !ok { 648 if prop.CustomType == "" { 649 return fmt.Errorf("%v does not implement proto.Message", v.Type()) 650 } 651 t := proto.MessageType(prop.CustomType) 652 if t == nil || !i.Type().ConvertibleTo(t) { 653 return fmt.Errorf("%v declared custom type %s but it is not convertible to %v", v.Type(), prop.CustomType, t) 654 } 655 pm = i.Convert(t).Interface().(proto.Message) 656 } 657 return m.marshalObject(out, pm, indent+m.Indent, "") 658 } 659 660 // Handle maps. 661 // Since Go randomizes map iteration, we sort keys for stable output. 662 if v.Kind() == reflect.Map { 663 out.write(`{`) 664 keys := v.MapKeys() 665 sort.Sort(mapKeys(keys)) 666 for i, k := range keys { 667 if i > 0 { 668 out.write(`,`) 669 } 670 if m.Indent != "" { 671 out.write("\n") 672 out.write(indent) 673 out.write(m.Indent) 674 out.write(m.Indent) 675 } 676 677 // TODO handle map key prop properly 678 b, err := json.Marshal(k.Interface()) 679 if err != nil { 680 return err 681 } 682 s := string(b) 683 684 // If the JSON is not a string value, encode it again to make it one. 685 if !strings.HasPrefix(s, `"`) { 686 b, err := json.Marshal(s) 687 if err != nil { 688 return err 689 } 690 s = string(b) 691 } 692 693 out.write(s) 694 out.write(`:`) 695 if m.Indent != "" { 696 out.write(` `) 697 } 698 699 vprop := prop 700 if prop != nil && prop.MapValProp != nil { 701 vprop = prop.MapValProp 702 } 703 if err := m.marshalValue(out, vprop, v.MapIndex(k), indent+m.Indent); err != nil { 704 return err 705 } 706 } 707 if m.Indent != "" { 708 out.write("\n") 709 out.write(indent) 710 out.write(m.Indent) 711 } 712 out.write(`}`) 713 return out.err 714 } 715 716 // Handle non-finite floats, e.g. NaN, Infinity and -Infinity. 717 if v.Kind() == reflect.Float32 || v.Kind() == reflect.Float64 { 718 f := v.Float() 719 var sval string 720 switch { 721 case math.IsInf(f, 1): 722 sval = `"Infinity"` 723 case math.IsInf(f, -1): 724 sval = `"-Infinity"` 725 case math.IsNaN(f): 726 sval = `"NaN"` 727 } 728 if sval != "" { 729 out.write(sval) 730 return out.err 731 } 732 } 733 734 // Default handling defers to the encoding/json library. 735 b, err := json.Marshal(v.Interface()) 736 if err != nil { 737 return err 738 } 739 needToQuote := string(b[0]) != `"` && (v.Kind() == reflect.Int64 || v.Kind() == reflect.Uint64) 740 if needToQuote { 741 out.write(`"`) 742 } 743 out.write(string(b)) 744 if needToQuote { 745 out.write(`"`) 746 } 747 return out.err 748 } 749 750 // Unmarshaler is a configurable object for converting from a JSON 751 // representation to a protocol buffer object. 752 type Unmarshaler struct { 753 // Whether to allow messages to contain unknown fields, as opposed to 754 // failing to unmarshal. 755 AllowUnknownFields bool 756 757 // A custom URL resolver to use when unmarshaling Any messages from JSON. 758 // If unset, the default resolution strategy is to extract the 759 // fully-qualified type name from the type URL and pass that to 760 // proto.MessageType(string). 761 AnyResolver AnyResolver 762 } 763 764 // UnmarshalNext unmarshals the next protocol buffer from a JSON object stream. 765 // This function is lenient and will decode any options permutations of the 766 // related Marshaler. 767 func (u *Unmarshaler) UnmarshalNext(dec *json.Decoder, pb proto.Message) error { 768 inputValue := json.RawMessage{} 769 if err := dec.Decode(&inputValue); err != nil { 770 return err 771 } 772 if err := u.unmarshalValue(reflect.ValueOf(pb).Elem(), inputValue, nil); err != nil { 773 return err 774 } 775 return checkRequiredFields(pb) 776 } 777 778 // Unmarshal unmarshals a JSON object stream into a protocol 779 // buffer. This function is lenient and will decode any options 780 // permutations of the related Marshaler. 781 func (u *Unmarshaler) Unmarshal(r io.Reader, pb proto.Message) error { 782 dec := json.NewDecoder(r) 783 return u.UnmarshalNext(dec, pb) 784 } 785 786 // UnmarshalNext unmarshals the next protocol buffer from a JSON object stream. 787 // This function is lenient and will decode any options permutations of the 788 // related Marshaler. 789 func UnmarshalNext(dec *json.Decoder, pb proto.Message) error { 790 return new(Unmarshaler).UnmarshalNext(dec, pb) 791 } 792 793 // Unmarshal unmarshals a JSON object stream into a protocol 794 // buffer. This function is lenient and will decode any options 795 // permutations of the related Marshaler. 796 func Unmarshal(r io.Reader, pb proto.Message) error { 797 return new(Unmarshaler).Unmarshal(r, pb) 798 } 799 800 // UnmarshalString will populate the fields of a protocol buffer based 801 // on a JSON string. This function is lenient and will decode any options 802 // permutations of the related Marshaler. 803 func UnmarshalString(str string, pb proto.Message) error { 804 return new(Unmarshaler).Unmarshal(strings.NewReader(str), pb) 805 } 806 807 // unmarshalValue converts/copies a value into the target. 808 // prop may be nil. 809 func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMessage, prop *proto.Properties) error { 810 targetType := target.Type() 811 812 // Allocate memory for pointer fields. 813 if targetType.Kind() == reflect.Ptr { 814 // If input value is "null" and target is a pointer type, then the field should be treated as not set 815 // UNLESS the target is structpb.Value, in which case it should be set to structpb.NullValue. 816 _, isJSONPBUnmarshaler := target.Interface().(JSONPBUnmarshaler) 817 if string(inputValue) == "null" && targetType != reflect.TypeOf(&types.Value{}) && !isJSONPBUnmarshaler { 818 return nil 819 } 820 target.Set(reflect.New(targetType.Elem())) 821 822 return u.unmarshalValue(target.Elem(), inputValue, prop) 823 } 824 825 if jsu, ok := target.Addr().Interface().(JSONPBUnmarshaler); ok { 826 return jsu.UnmarshalJSONPB(u, []byte(inputValue)) 827 } 828 829 // Handle well-known types that are not pointers. 830 if w, ok := target.Addr().Interface().(isWkt); ok { 831 switch w.XXX_WellKnownType() { 832 case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value", 833 "Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue": 834 return u.unmarshalValue(target.Field(0), inputValue, prop) 835 case "Any": 836 // Use json.RawMessage pointer type instead of value to support pre-1.8 version. 837 // 1.8 changed RawMessage.MarshalJSON from pointer type to value type, see 838 // https://github.com/golang/go/issues/14493 839 var jsonFields map[string]*json.RawMessage 840 if err := json.Unmarshal(inputValue, &jsonFields); err != nil { 841 return err 842 } 843 844 val, ok := jsonFields["@type"] 845 if !ok || val == nil { 846 return errors.New("Any JSON doesn't have '@type'") 847 } 848 849 var turl string 850 if err := json.Unmarshal([]byte(*val), &turl); err != nil { 851 return fmt.Errorf("can't unmarshal Any's '@type': %q", *val) 852 } 853 target.Field(0).SetString(turl) 854 855 var m proto.Message 856 var err error 857 if u.AnyResolver != nil { 858 m, err = u.AnyResolver.Resolve(turl) 859 } else { 860 m, err = defaultResolveAny(turl) 861 } 862 if err != nil { 863 return err 864 } 865 866 if _, ok := m.(isWkt); ok { 867 val, ok := jsonFields["value"] 868 if !ok { 869 return errors.New("Any JSON doesn't have 'value'") 870 } 871 872 if err = u.unmarshalValue(reflect.ValueOf(m).Elem(), *val, nil); err != nil { 873 return fmt.Errorf("can't unmarshal Any nested proto %T: %v", m, err) 874 } 875 } else { 876 delete(jsonFields, "@type") 877 nestedProto, uerr := json.Marshal(jsonFields) 878 if uerr != nil { 879 return fmt.Errorf("can't generate JSON for Any's nested proto to be unmarshaled: %v", uerr) 880 } 881 882 if err = u.unmarshalValue(reflect.ValueOf(m).Elem(), nestedProto, nil); err != nil { 883 return fmt.Errorf("can't unmarshal Any nested proto %T: %v", m, err) 884 } 885 } 886 887 b, err := proto.Marshal(m) 888 if err != nil { 889 return fmt.Errorf("can't marshal proto %T into Any.Value: %v", m, err) 890 } 891 target.Field(1).SetBytes(b) 892 893 return nil 894 case "Duration": 895 unq, err := unquote(string(inputValue)) 896 if err != nil { 897 return err 898 } 899 900 d, err := time.ParseDuration(unq) 901 if err != nil { 902 return fmt.Errorf("bad Duration: %v", err) 903 } 904 905 ns := d.Nanoseconds() 906 s := ns / 1e9 907 ns %= 1e9 908 target.Field(0).SetInt(s) 909 target.Field(1).SetInt(ns) 910 return nil 911 case "Timestamp": 912 unq, err := unquote(string(inputValue)) 913 if err != nil { 914 return err 915 } 916 917 t, err := time.Parse(time.RFC3339Nano, unq) 918 if err != nil { 919 return fmt.Errorf("bad Timestamp: %v", err) 920 } 921 922 target.Field(0).SetInt(t.Unix()) 923 target.Field(1).SetInt(int64(t.Nanosecond())) 924 return nil 925 case "Struct": 926 var m map[string]json.RawMessage 927 if err := json.Unmarshal(inputValue, &m); err != nil { 928 return fmt.Errorf("bad StructValue: %v", err) 929 } 930 target.Field(0).Set(reflect.ValueOf(map[string]*types.Value{})) 931 for k, jv := range m { 932 pv := &types.Value{} 933 if err := u.unmarshalValue(reflect.ValueOf(pv).Elem(), jv, prop); err != nil { 934 return fmt.Errorf("bad value in StructValue for key %q: %v", k, err) 935 } 936 target.Field(0).SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(pv)) 937 } 938 return nil 939 case "ListValue": 940 var s []json.RawMessage 941 if err := json.Unmarshal(inputValue, &s); err != nil { 942 return fmt.Errorf("bad ListValue: %v", err) 943 } 944 945 target.Field(0).Set(reflect.ValueOf(make([]*types.Value, len(s)))) 946 for i, sv := range s { 947 if err := u.unmarshalValue(target.Field(0).Index(i), sv, prop); err != nil { 948 return err 949 } 950 } 951 return nil 952 case "Value": 953 ivStr := string(inputValue) 954 if ivStr == "null" { 955 target.Field(0).Set(reflect.ValueOf(&types.Value_NullValue{})) 956 } else if v, err := strconv.ParseFloat(ivStr, 0); err == nil { 957 target.Field(0).Set(reflect.ValueOf(&types.Value_NumberValue{NumberValue: v})) 958 } else if v, err := unquote(ivStr); err == nil { 959 target.Field(0).Set(reflect.ValueOf(&types.Value_StringValue{StringValue: v})) 960 } else if v, err := strconv.ParseBool(ivStr); err == nil { 961 target.Field(0).Set(reflect.ValueOf(&types.Value_BoolValue{BoolValue: v})) 962 } else if err := json.Unmarshal(inputValue, &[]json.RawMessage{}); err == nil { 963 lv := &types.ListValue{} 964 target.Field(0).Set(reflect.ValueOf(&types.Value_ListValue{ListValue: lv})) 965 return u.unmarshalValue(reflect.ValueOf(lv).Elem(), inputValue, prop) 966 } else if err := json.Unmarshal(inputValue, &map[string]json.RawMessage{}); err == nil { 967 sv := &types.Struct{} 968 target.Field(0).Set(reflect.ValueOf(&types.Value_StructValue{StructValue: sv})) 969 return u.unmarshalValue(reflect.ValueOf(sv).Elem(), inputValue, prop) 970 } else { 971 return fmt.Errorf("unrecognized type for Value %q", ivStr) 972 } 973 return nil 974 } 975 } 976 977 if t, ok := target.Addr().Interface().(*time.Time); ok { 978 ts := &types.Timestamp{} 979 if err := u.unmarshalValue(reflect.ValueOf(ts).Elem(), inputValue, prop); err != nil { 980 return err 981 } 982 tt, err := types.TimestampFromProto(ts) 983 if err != nil { 984 return err 985 } 986 *t = tt 987 return nil 988 } 989 990 if d, ok := target.Addr().Interface().(*time.Duration); ok { 991 dur := &types.Duration{} 992 if err := u.unmarshalValue(reflect.ValueOf(dur).Elem(), inputValue, prop); err != nil { 993 return err 994 } 995 dd, err := types.DurationFromProto(dur) 996 if err != nil { 997 return err 998 } 999 *d = dd 1000 return nil 1001 } 1002 1003 // Handle enums, which have an underlying type of int32, 1004 // and may appear as strings. 1005 // The case of an enum appearing as a number is handled 1006 // at the bottom of this function. 1007 if inputValue[0] == '"' && prop != nil && prop.Enum != "" { 1008 vmap := proto.EnumValueMap(prop.Enum) 1009 // Don't need to do unquoting; valid enum names 1010 // are from a limited character set. 1011 s := inputValue[1 : len(inputValue)-1] 1012 n, ok := vmap[string(s)] 1013 if !ok { 1014 return fmt.Errorf("unknown value %q for enum %s", s, prop.Enum) 1015 } 1016 if target.Kind() == reflect.Ptr { // proto2 1017 target.Set(reflect.New(targetType.Elem())) 1018 target = target.Elem() 1019 } 1020 if targetType.Kind() != reflect.Int32 { 1021 return fmt.Errorf("invalid target %q for enum %s", targetType.Kind(), prop.Enum) 1022 } 1023 target.SetInt(int64(n)) 1024 return nil 1025 } 1026 1027 if prop != nil && len(prop.CustomType) > 0 && target.CanAddr() { 1028 if m, ok := target.Addr().Interface().(interface { 1029 UnmarshalJSON([]byte) error 1030 }); ok { 1031 return json.Unmarshal(inputValue, m) 1032 } 1033 } 1034 1035 // Handle nested messages. 1036 if targetType.Kind() == reflect.Struct { 1037 var jsonFields map[string]json.RawMessage 1038 if err := json.Unmarshal(inputValue, &jsonFields); err != nil { 1039 return err 1040 } 1041 1042 consumeField := func(prop *proto.Properties) (json.RawMessage, bool) { 1043 // Be liberal in what names we accept; both orig_name and camelName are okay. 1044 fieldNames := acceptedJSONFieldNames(prop) 1045 1046 vOrig, okOrig := jsonFields[fieldNames.orig] 1047 vCamel, okCamel := jsonFields[fieldNames.camel] 1048 if !okOrig && !okCamel { 1049 return nil, false 1050 } 1051 // If, for some reason, both are present in the data, favour the camelName. 1052 var raw json.RawMessage 1053 if okOrig { 1054 raw = vOrig 1055 delete(jsonFields, fieldNames.orig) 1056 } 1057 if okCamel { 1058 raw = vCamel 1059 delete(jsonFields, fieldNames.camel) 1060 } 1061 return raw, true 1062 } 1063 1064 sprops := proto.GetProperties(targetType) 1065 for i := 0; i < target.NumField(); i++ { 1066 ft := target.Type().Field(i) 1067 if strings.HasPrefix(ft.Name, "XXX_") { 1068 continue 1069 } 1070 valueForField, ok := consumeField(sprops.Prop[i]) 1071 if !ok { 1072 continue 1073 } 1074 1075 if err := u.unmarshalValue(target.Field(i), valueForField, sprops.Prop[i]); err != nil { 1076 return err 1077 } 1078 } 1079 // Check for any oneof fields. 1080 if len(jsonFields) > 0 { 1081 for _, oop := range sprops.OneofTypes { 1082 raw, ok := consumeField(oop.Prop) 1083 if !ok { 1084 continue 1085 } 1086 nv := reflect.New(oop.Type.Elem()) 1087 target.Field(oop.Field).Set(nv) 1088 if err := u.unmarshalValue(nv.Elem().Field(0), raw, oop.Prop); err != nil { 1089 return err 1090 } 1091 } 1092 } 1093 // Handle proto2 extensions. 1094 if len(jsonFields) > 0 { 1095 if ep, ok := target.Addr().Interface().(proto.Message); ok { 1096 for _, ext := range proto.RegisteredExtensions(ep) { 1097 name := fmt.Sprintf("[%s]", ext.Name) 1098 raw, ok := jsonFields[name] 1099 if !ok { 1100 continue 1101 } 1102 delete(jsonFields, name) 1103 nv := reflect.New(reflect.TypeOf(ext.ExtensionType).Elem()) 1104 if err := u.unmarshalValue(nv.Elem(), raw, nil); err != nil { 1105 return err 1106 } 1107 if err := proto.SetExtension(ep, ext, nv.Interface()); err != nil { 1108 return err 1109 } 1110 } 1111 } 1112 } 1113 if !u.AllowUnknownFields && len(jsonFields) > 0 { 1114 // Pick any field to be the scapegoat. 1115 var f string 1116 for fname := range jsonFields { 1117 f = fname 1118 break 1119 } 1120 return fmt.Errorf("unknown field %q in %v", f, targetType) 1121 } 1122 return nil 1123 } 1124 1125 // Handle arrays 1126 if targetType.Kind() == reflect.Slice { 1127 if targetType.Elem().Kind() == reflect.Uint8 { 1128 outRef := reflect.New(targetType) 1129 outVal := outRef.Interface() 1130 //CustomType with underlying type []byte 1131 if _, ok := outVal.(interface { 1132 UnmarshalJSON([]byte) error 1133 }); ok { 1134 if err := json.Unmarshal(inputValue, outVal); err != nil { 1135 return err 1136 } 1137 target.Set(outRef.Elem()) 1138 return nil 1139 } 1140 // Special case for encoded bytes. Pre-go1.5 doesn't support unmarshalling 1141 // strings into aliased []byte types. 1142 // https://github.com/golang/go/commit/4302fd0409da5e4f1d71471a6770dacdc3301197 1143 // https://github.com/golang/go/commit/c60707b14d6be26bf4213114d13070bff00d0b0a 1144 var out []byte 1145 if err := json.Unmarshal(inputValue, &out); err != nil { 1146 return err 1147 } 1148 target.SetBytes(out) 1149 return nil 1150 } 1151 1152 var slc []json.RawMessage 1153 if err := json.Unmarshal(inputValue, &slc); err != nil { 1154 return err 1155 } 1156 if slc != nil { 1157 l := len(slc) 1158 target.Set(reflect.MakeSlice(targetType, l, l)) 1159 for i := 0; i < l; i++ { 1160 if err := u.unmarshalValue(target.Index(i), slc[i], prop); err != nil { 1161 return err 1162 } 1163 } 1164 } 1165 return nil 1166 } 1167 1168 // Handle maps (whose keys are always strings) 1169 if targetType.Kind() == reflect.Map { 1170 var mp map[string]json.RawMessage 1171 if err := json.Unmarshal(inputValue, &mp); err != nil { 1172 return err 1173 } 1174 if mp != nil { 1175 target.Set(reflect.MakeMap(targetType)) 1176 for ks, raw := range mp { 1177 // Unmarshal map key. The core json library already decoded the key into a 1178 // string, so we handle that specially. Other types were quoted post-serialization. 1179 var k reflect.Value 1180 if targetType.Key().Kind() == reflect.String { 1181 k = reflect.ValueOf(ks) 1182 } else { 1183 k = reflect.New(targetType.Key()).Elem() 1184 var kprop *proto.Properties 1185 if prop != nil && prop.MapKeyProp != nil { 1186 kprop = prop.MapKeyProp 1187 } 1188 if err := u.unmarshalValue(k, json.RawMessage(ks), kprop); err != nil { 1189 return err 1190 } 1191 } 1192 1193 if !k.Type().AssignableTo(targetType.Key()) { 1194 k = k.Convert(targetType.Key()) 1195 } 1196 1197 // Unmarshal map value. 1198 v := reflect.New(targetType.Elem()).Elem() 1199 var vprop *proto.Properties 1200 if prop != nil && prop.MapValProp != nil { 1201 vprop = prop.MapValProp 1202 } 1203 if err := u.unmarshalValue(v, raw, vprop); err != nil { 1204 return err 1205 } 1206 target.SetMapIndex(k, v) 1207 } 1208 } 1209 return nil 1210 } 1211 1212 // Non-finite numbers can be encoded as strings. 1213 isFloat := targetType.Kind() == reflect.Float32 || targetType.Kind() == reflect.Float64 1214 if isFloat { 1215 if num, ok := nonFinite[string(inputValue)]; ok { 1216 target.SetFloat(num) 1217 return nil 1218 } 1219 } 1220 1221 // integers & floats can be encoded as strings. In this case we drop 1222 // the quotes and proceed as normal. 1223 isNum := targetType.Kind() == reflect.Int64 || targetType.Kind() == reflect.Uint64 || 1224 targetType.Kind() == reflect.Int32 || targetType.Kind() == reflect.Uint32 || 1225 targetType.Kind() == reflect.Float32 || targetType.Kind() == reflect.Float64 1226 if isNum && strings.HasPrefix(string(inputValue), `"`) { 1227 inputValue = inputValue[1 : len(inputValue)-1] 1228 } 1229 1230 // Use the encoding/json for parsing other value types. 1231 return json.Unmarshal(inputValue, target.Addr().Interface()) 1232 } 1233 1234 func unquote(s string) (string, error) { 1235 var ret string 1236 err := json.Unmarshal([]byte(s), &ret) 1237 return ret, err 1238 } 1239 1240 // jsonProperties returns parsed proto.Properties for the field and corrects JSONName attribute. 1241 func jsonProperties(f reflect.StructField, origName bool) *proto.Properties { 1242 var prop proto.Properties 1243 prop.Init(f.Type, f.Name, f.Tag.Get("protobuf"), &f) 1244 if origName || prop.JSONName == "" { 1245 prop.JSONName = prop.OrigName 1246 } 1247 return &prop 1248 } 1249 1250 type fieldNames struct { 1251 orig, camel string 1252 } 1253 1254 func acceptedJSONFieldNames(prop *proto.Properties) fieldNames { 1255 opts := fieldNames{orig: prop.OrigName, camel: prop.OrigName} 1256 if prop.JSONName != "" { 1257 opts.camel = prop.JSONName 1258 } 1259 return opts 1260 } 1261 1262 // Writer wrapper inspired by https://blog.golang.org/errors-are-values 1263 type errWriter struct { 1264 writer io.Writer 1265 err error 1266 } 1267 1268 func (w *errWriter) write(str string) { 1269 if w.err != nil { 1270 return 1271 } 1272 _, w.err = w.writer.Write([]byte(str)) 1273 } 1274 1275 // Map fields may have key types of non-float scalars, strings and enums. 1276 // The easiest way to sort them in some deterministic order is to use fmt. 1277 // If this turns out to be inefficient we can always consider other options, 1278 // such as doing a Schwartzian transform. 1279 // 1280 // Numeric keys are sorted in numeric order per 1281 // https://developers.google.com/protocol-buffers/docs/proto#maps. 1282 type mapKeys []reflect.Value 1283 1284 func (s mapKeys) Len() int { return len(s) } 1285 func (s mapKeys) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 1286 func (s mapKeys) Less(i, j int) bool { 1287 if k := s[i].Kind(); k == s[j].Kind() { 1288 switch k { 1289 case reflect.String: 1290 return s[i].String() < s[j].String() 1291 case reflect.Int32, reflect.Int64: 1292 return s[i].Int() < s[j].Int() 1293 case reflect.Uint32, reflect.Uint64: 1294 return s[i].Uint() < s[j].Uint() 1295 } 1296 } 1297 return fmt.Sprint(s[i].Interface()) < fmt.Sprint(s[j].Interface()) 1298 } 1299 1300 // checkRequiredFields returns an error if any required field in the given proto message is not set. 1301 // This function is used by both Marshal and Unmarshal. While required fields only exist in a 1302 // proto2 message, a proto3 message can contain proto2 message(s). 1303 func checkRequiredFields(pb proto.Message) error { 1304 // Most well-known type messages do not contain required fields. The "Any" type may contain 1305 // a message that has required fields. 1306 // 1307 // When an Any message is being marshaled, the code will invoked proto.Unmarshal on Any.Value 1308 // field in order to transform that into JSON, and that should have returned an error if a 1309 // required field is not set in the embedded message. 1310 // 1311 // When an Any message is being unmarshaled, the code will have invoked proto.Marshal on the 1312 // embedded message to store the serialized message in Any.Value field, and that should have 1313 // returned an error if a required field is not set. 1314 if _, ok := pb.(isWkt); ok { 1315 return nil 1316 } 1317 1318 v := reflect.ValueOf(pb) 1319 // Skip message if it is not a struct pointer. 1320 if v.Kind() != reflect.Ptr { 1321 return nil 1322 } 1323 v = v.Elem() 1324 if v.Kind() != reflect.Struct { 1325 return nil 1326 } 1327 1328 for i := 0; i < v.NumField(); i++ { 1329 field := v.Field(i) 1330 sfield := v.Type().Field(i) 1331 1332 if sfield.PkgPath != "" { 1333 // blank PkgPath means the field is exported; skip if not exported 1334 continue 1335 } 1336 1337 if strings.HasPrefix(sfield.Name, "XXX_") { 1338 continue 1339 } 1340 1341 // Oneof field is an interface implemented by wrapper structs containing the actual oneof 1342 // field, i.e. an interface containing &T{real_value}. 1343 if sfield.Tag.Get("protobuf_oneof") != "" { 1344 if field.Kind() != reflect.Interface { 1345 continue 1346 } 1347 v := field.Elem() 1348 if v.Kind() != reflect.Ptr || v.IsNil() { 1349 continue 1350 } 1351 v = v.Elem() 1352 if v.Kind() != reflect.Struct || v.NumField() < 1 { 1353 continue 1354 } 1355 field = v.Field(0) 1356 sfield = v.Type().Field(0) 1357 } 1358 1359 protoTag := sfield.Tag.Get("protobuf") 1360 if protoTag == "" { 1361 continue 1362 } 1363 var prop proto.Properties 1364 prop.Init(sfield.Type, sfield.Name, protoTag, &sfield) 1365 1366 switch field.Kind() { 1367 case reflect.Map: 1368 if field.IsNil() { 1369 continue 1370 } 1371 // Check each map value. 1372 keys := field.MapKeys() 1373 for _, k := range keys { 1374 v := field.MapIndex(k) 1375 if err := checkRequiredFieldsInValue(v); err != nil { 1376 return err 1377 } 1378 } 1379 case reflect.Slice: 1380 // Handle non-repeated type, e.g. bytes. 1381 if !prop.Repeated { 1382 if prop.Required && field.IsNil() { 1383 return fmt.Errorf("required field %q is not set", prop.Name) 1384 } 1385 continue 1386 } 1387 1388 // Handle repeated type. 1389 if field.IsNil() { 1390 continue 1391 } 1392 // Check each slice item. 1393 for i := 0; i < field.Len(); i++ { 1394 v := field.Index(i) 1395 if err := checkRequiredFieldsInValue(v); err != nil { 1396 return err 1397 } 1398 } 1399 case reflect.Ptr: 1400 if field.IsNil() { 1401 if prop.Required { 1402 return fmt.Errorf("required field %q is not set", prop.Name) 1403 } 1404 continue 1405 } 1406 if err := checkRequiredFieldsInValue(field); err != nil { 1407 return err 1408 } 1409 } 1410 } 1411 1412 // Handle proto2 extensions. 1413 for _, ext := range proto.RegisteredExtensions(pb) { 1414 if !proto.HasExtension(pb, ext) { 1415 continue 1416 } 1417 ep, err := proto.GetExtension(pb, ext) 1418 if err != nil { 1419 return err 1420 } 1421 err = checkRequiredFieldsInValue(reflect.ValueOf(ep)) 1422 if err != nil { 1423 return err 1424 } 1425 } 1426 1427 return nil 1428 } 1429 1430 func checkRequiredFieldsInValue(v reflect.Value) error { 1431 if v.Type().Implements(messageType) { 1432 return checkRequiredFields(v.Interface().(proto.Message)) 1433 } 1434 return nil 1435 }