1// Copyright 2019 The Go Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5// Package dynamicpb creates protocol buffer messages using runtime type information. 6package dynamicpb 7 8import ( 9 "math" 10 11 "google.golang.org/protobuf/internal/errors" 12 "google.golang.org/protobuf/reflect/protoreflect" 13 "google.golang.org/protobuf/runtime/protoiface" 14 "google.golang.org/protobuf/runtime/protoimpl" 15) 16 17// enum is a dynamic protoreflect.Enum. 18type enum struct { 19 num protoreflect.EnumNumber 20 typ protoreflect.EnumType 21} 22 23func (e enum) Descriptor() protoreflect.EnumDescriptor { return e.typ.Descriptor() } 24func (e enum) Type() protoreflect.EnumType { return e.typ } 25func (e enum) Number() protoreflect.EnumNumber { return e.num } 26 27// enumType is a dynamic protoreflect.EnumType. 28type enumType struct { 29 desc protoreflect.EnumDescriptor 30} 31 32// NewEnumType creates a new EnumType with the provided descriptor. 33// 34// EnumTypes created by this package are equal if their descriptors are equal. 35// That is, if ed1 == ed2, then NewEnumType(ed1) == NewEnumType(ed2). 36// 37// Enum values created by the EnumType are equal if their numbers are equal. 38func NewEnumType(desc protoreflect.EnumDescriptor) protoreflect.EnumType { 39 return enumType{desc} 40} 41 42func (et enumType) New(n protoreflect.EnumNumber) protoreflect.Enum { return enum{n, et} } 43func (et enumType) Descriptor() protoreflect.EnumDescriptor { return et.desc } 44 45// extensionType is a dynamic protoreflect.ExtensionType. 46type extensionType struct { 47 desc extensionTypeDescriptor 48} 49 50// A Message is a dynamically constructed protocol buffer message. 51// 52// Message implements the proto.Message interface, and may be used with all 53// standard proto package functions such as Marshal, Unmarshal, and so forth. 54// 55// Message also implements the protoreflect.Message interface. See the protoreflect 56// package documentation for that interface for how to get and set fields and 57// otherwise interact with the contents of a Message. 58// 59// Reflection API functions which construct messages, such as NewField, 60// return new dynamic messages of the appropriate type. Functions which take 61// messages, such as Set for a message-value field, will accept any message 62// with a compatible type. 63// 64// Operations which modify a Message are not safe for concurrent use. 65type Message struct { 66 typ messageType 67 known map[protoreflect.FieldNumber]protoreflect.Value 68 ext map[protoreflect.FieldNumber]protoreflect.FieldDescriptor 69 unknown protoreflect.RawFields 70} 71 72var ( 73 _ protoreflect.Message = (*Message)(nil) 74 _ protoreflect.ProtoMessage = (*Message)(nil) 75 _ protoiface.MessageV1 = (*Message)(nil) 76) 77 78// NewMessage creates a new message with the provided descriptor. 79func NewMessage(desc protoreflect.MessageDescriptor) *Message { 80 return &Message{ 81 typ: messageType{desc}, 82 known: make(map[protoreflect.FieldNumber]protoreflect.Value), 83 ext: make(map[protoreflect.FieldNumber]protoreflect.FieldDescriptor), 84 } 85} 86 87// ProtoMessage implements the legacy message interface. 88func (m *Message) ProtoMessage() {} 89 90// ProtoReflect implements the protoreflect.ProtoMessage interface. 91func (m *Message) ProtoReflect() protoreflect.Message { 92 return m 93} 94 95// String returns a string representation of a message. 96func (m *Message) String() string { 97 return protoimpl.X.MessageStringOf(m) 98} 99 100// Reset clears the message to be empty, but preserves the dynamic message type. 101func (m *Message) Reset() { 102 m.known = make(map[protoreflect.FieldNumber]protoreflect.Value) 103 m.ext = make(map[protoreflect.FieldNumber]protoreflect.FieldDescriptor) 104 m.unknown = nil 105} 106 107// Descriptor returns the message descriptor. 108func (m *Message) Descriptor() protoreflect.MessageDescriptor { 109 return m.typ.desc 110} 111 112// Type returns the message type. 113func (m *Message) Type() protoreflect.MessageType { 114 return m.typ 115} 116 117// New returns a newly allocated empty message with the same descriptor. 118// See protoreflect.Message for details. 119func (m *Message) New() protoreflect.Message { 120 return m.Type().New() 121} 122 123// Interface returns the message. 124// See protoreflect.Message for details. 125func (m *Message) Interface() protoreflect.ProtoMessage { 126 return m 127} 128 129// ProtoMethods is an internal detail of the protoreflect.Message interface. 130// Users should never call this directly. 131func (m *Message) ProtoMethods() *protoiface.Methods { 132 return nil 133} 134 135// Range visits every populated field in undefined order. 136// See protoreflect.Message for details. 137func (m *Message) Range(f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) { 138 for num, v := range m.known { 139 fd := m.ext[num] 140 if fd == nil { 141 fd = m.Descriptor().Fields().ByNumber(num) 142 } 143 if !isSet(fd, v) { 144 continue 145 } 146 if !f(fd, v) { 147 return 148 } 149 } 150} 151 152// Has reports whether a field is populated. 153// See protoreflect.Message for details. 154func (m *Message) Has(fd protoreflect.FieldDescriptor) bool { 155 m.checkField(fd) 156 if fd.IsExtension() && m.ext[fd.Number()] != fd { 157 return false 158 } 159 v, ok := m.known[fd.Number()] 160 if !ok { 161 return false 162 } 163 return isSet(fd, v) 164} 165 166// Clear clears a field. 167// See protoreflect.Message for details. 168func (m *Message) Clear(fd protoreflect.FieldDescriptor) { 169 m.checkField(fd) 170 num := fd.Number() 171 delete(m.known, num) 172 delete(m.ext, num) 173} 174 175// Get returns the value of a field. 176// See protoreflect.Message for details. 177func (m *Message) Get(fd protoreflect.FieldDescriptor) protoreflect.Value { 178 m.checkField(fd) 179 num := fd.Number() 180 if fd.IsExtension() { 181 if fd != m.ext[num] { 182 return fd.(protoreflect.ExtensionTypeDescriptor).Type().Zero() 183 } 184 return m.known[num] 185 } 186 if v, ok := m.known[num]; ok { 187 switch { 188 case fd.IsMap(): 189 if v.Map().Len() > 0 { 190 return v 191 } 192 case fd.IsList(): 193 if v.List().Len() > 0 { 194 return v 195 } 196 default: 197 return v 198 } 199 } 200 switch { 201 case fd.IsMap(): 202 return protoreflect.ValueOfMap(&dynamicMap{desc: fd}) 203 case fd.IsList(): 204 return protoreflect.ValueOfList(emptyList{desc: fd}) 205 case fd.Message() != nil: 206 return protoreflect.ValueOfMessage(&Message{typ: messageType{fd.Message()}}) 207 case fd.Kind() == protoreflect.BytesKind: 208 return protoreflect.ValueOfBytes(append([]byte(nil), fd.Default().Bytes()...)) 209 default: 210 return fd.Default() 211 } 212} 213 214// Mutable returns a mutable reference to a repeated, map, or message field. 215// See protoreflect.Message for details. 216func (m *Message) Mutable(fd protoreflect.FieldDescriptor) protoreflect.Value { 217 m.checkField(fd) 218 if !fd.IsMap() && !fd.IsList() && fd.Message() == nil { 219 panic(errors.New("%v: getting mutable reference to non-composite type", fd.FullName())) 220 } 221 if m.known == nil { 222 panic(errors.New("%v: modification of read-only message", fd.FullName())) 223 } 224 num := fd.Number() 225 if fd.IsExtension() { 226 if fd != m.ext[num] { 227 m.ext[num] = fd 228 m.known[num] = fd.(protoreflect.ExtensionTypeDescriptor).Type().New() 229 } 230 return m.known[num] 231 } 232 if v, ok := m.known[num]; ok { 233 return v 234 } 235 m.clearOtherOneofFields(fd) 236 m.known[num] = m.NewField(fd) 237 if fd.IsExtension() { 238 m.ext[num] = fd 239 } 240 return m.known[num] 241} 242 243// Set stores a value in a field. 244// See protoreflect.Message for details. 245func (m *Message) Set(fd protoreflect.FieldDescriptor, v protoreflect.Value) { 246 m.checkField(fd) 247 if m.known == nil { 248 panic(errors.New("%v: modification of read-only message", fd.FullName())) 249 } 250 if fd.IsExtension() { 251 isValid := true 252 switch { 253 case !fd.(protoreflect.ExtensionTypeDescriptor).Type().IsValidValue(v): 254 isValid = false 255 case fd.IsList(): 256 isValid = v.List().IsValid() 257 case fd.IsMap(): 258 isValid = v.Map().IsValid() 259 case fd.Message() != nil: 260 isValid = v.Message().IsValid() 261 } 262 if !isValid { 263 panic(errors.New("%v: assigning invalid type %T", fd.FullName(), v.Interface())) 264 } 265 m.ext[fd.Number()] = fd 266 } else { 267 typecheck(fd, v) 268 } 269 m.clearOtherOneofFields(fd) 270 m.known[fd.Number()] = v 271} 272 273func (m *Message) clearOtherOneofFields(fd protoreflect.FieldDescriptor) { 274 od := fd.ContainingOneof() 275 if od == nil { 276 return 277 } 278 num := fd.Number() 279 for i := 0; i < od.Fields().Len(); i++ { 280 if n := od.Fields().Get(i).Number(); n != num { 281 delete(m.known, n) 282 } 283 } 284} 285 286// NewField returns a new value for assignable to the field of a given descriptor. 287// See protoreflect.Message for details. 288func (m *Message) NewField(fd protoreflect.FieldDescriptor) protoreflect.Value { 289 m.checkField(fd) 290 switch { 291 case fd.IsExtension(): 292 return fd.(protoreflect.ExtensionTypeDescriptor).Type().New() 293 case fd.IsMap(): 294 return protoreflect.ValueOfMap(&dynamicMap{ 295 desc: fd, 296 mapv: make(map[interface{}]protoreflect.Value), 297 }) 298 case fd.IsList(): 299 return protoreflect.ValueOfList(&dynamicList{desc: fd}) 300 case fd.Message() != nil: 301 return protoreflect.ValueOfMessage(NewMessage(fd.Message()).ProtoReflect()) 302 default: 303 return fd.Default() 304 } 305} 306 307// WhichOneof reports which field in a oneof is populated, returning nil if none are populated. 308// See protoreflect.Message for details. 309func (m *Message) WhichOneof(od protoreflect.OneofDescriptor) protoreflect.FieldDescriptor { 310 for i := 0; i < od.Fields().Len(); i++ { 311 fd := od.Fields().Get(i) 312 if m.Has(fd) { 313 return fd 314 } 315 } 316 return nil 317} 318 319// GetUnknown returns the raw unknown fields. 320// See protoreflect.Message for details. 321func (m *Message) GetUnknown() protoreflect.RawFields { 322 return m.unknown 323} 324 325// SetUnknown sets the raw unknown fields. 326// See protoreflect.Message for details. 327func (m *Message) SetUnknown(r protoreflect.RawFields) { 328 if m.known == nil { 329 panic(errors.New("%v: modification of read-only message", m.typ.desc.FullName())) 330 } 331 m.unknown = r 332} 333 334// IsValid reports whether the message is valid. 335// See protoreflect.Message for details. 336func (m *Message) IsValid() bool { 337 return m.known != nil 338} 339 340func (m *Message) checkField(fd protoreflect.FieldDescriptor) { 341 if fd.IsExtension() && fd.ContainingMessage().FullName() == m.Descriptor().FullName() { 342 if _, ok := fd.(protoreflect.ExtensionTypeDescriptor); !ok { 343 panic(errors.New("%v: extension field descriptor does not implement ExtensionTypeDescriptor", fd.FullName())) 344 } 345 return 346 } 347 if fd.Parent() == m.Descriptor() { 348 return 349 } 350 fields := m.Descriptor().Fields() 351 index := fd.Index() 352 if index >= fields.Len() || fields.Get(index) != fd { 353 panic(errors.New("%v: field descriptor does not belong to this message", fd.FullName())) 354 } 355} 356 357type messageType struct { 358 desc protoreflect.MessageDescriptor 359} 360 361// NewMessageType creates a new MessageType with the provided descriptor. 362// 363// MessageTypes created by this package are equal if their descriptors are equal. 364// That is, if md1 == md2, then NewMessageType(md1) == NewMessageType(md2). 365func NewMessageType(desc protoreflect.MessageDescriptor) protoreflect.MessageType { 366 return messageType{desc} 367} 368 369func (mt messageType) New() protoreflect.Message { return NewMessage(mt.desc) } 370func (mt messageType) Zero() protoreflect.Message { return &Message{typ: messageType{mt.desc}} } 371func (mt messageType) Descriptor() protoreflect.MessageDescriptor { return mt.desc } 372func (mt messageType) Enum(i int) protoreflect.EnumType { 373 if ed := mt.desc.Fields().Get(i).Enum(); ed != nil { 374 return NewEnumType(ed) 375 } 376 return nil 377} 378func (mt messageType) Message(i int) protoreflect.MessageType { 379 if md := mt.desc.Fields().Get(i).Message(); md != nil { 380 return NewMessageType(md) 381 } 382 return nil 383} 384 385type emptyList struct { 386 desc protoreflect.FieldDescriptor 387} 388 389func (x emptyList) Len() int { return 0 } 390func (x emptyList) Get(n int) protoreflect.Value { panic(errors.New("out of range")) } 391func (x emptyList) Set(n int, v protoreflect.Value) { 392 panic(errors.New("modification of immutable list")) 393} 394func (x emptyList) Append(v protoreflect.Value) { panic(errors.New("modification of immutable list")) } 395func (x emptyList) AppendMutable() protoreflect.Value { 396 panic(errors.New("modification of immutable list")) 397} 398func (x emptyList) Truncate(n int) { panic(errors.New("modification of immutable list")) } 399func (x emptyList) NewElement() protoreflect.Value { return newListEntry(x.desc) } 400func (x emptyList) IsValid() bool { return false } 401 402type dynamicList struct { 403 desc protoreflect.FieldDescriptor 404 list []protoreflect.Value 405} 406 407func (x *dynamicList) Len() int { 408 return len(x.list) 409} 410 411func (x *dynamicList) Get(n int) protoreflect.Value { 412 return x.list[n] 413} 414 415func (x *dynamicList) Set(n int, v protoreflect.Value) { 416 typecheckSingular(x.desc, v) 417 x.list[n] = v 418} 419 420func (x *dynamicList) Append(v protoreflect.Value) { 421 typecheckSingular(x.desc, v) 422 x.list = append(x.list, v) 423} 424 425func (x *dynamicList) AppendMutable() protoreflect.Value { 426 if x.desc.Message() == nil { 427 panic(errors.New("%v: invalid AppendMutable on list with non-message type", x.desc.FullName())) 428 } 429 v := x.NewElement() 430 x.Append(v) 431 return v 432} 433 434func (x *dynamicList) Truncate(n int) { 435 // Zero truncated elements to avoid keeping data live. 436 for i := n; i < len(x.list); i++ { 437 x.list[i] = protoreflect.Value{} 438 } 439 x.list = x.list[:n] 440} 441 442func (x *dynamicList) NewElement() protoreflect.Value { 443 return newListEntry(x.desc) 444} 445 446func (x *dynamicList) IsValid() bool { 447 return true 448} 449 450type dynamicMap struct { 451 desc protoreflect.FieldDescriptor 452 mapv map[interface{}]protoreflect.Value 453} 454 455func (x *dynamicMap) Get(k protoreflect.MapKey) protoreflect.Value { return x.mapv[k.Interface()] } 456func (x *dynamicMap) Set(k protoreflect.MapKey, v protoreflect.Value) { 457 typecheckSingular(x.desc.MapKey(), k.Value()) 458 typecheckSingular(x.desc.MapValue(), v) 459 x.mapv[k.Interface()] = v 460} 461func (x *dynamicMap) Has(k protoreflect.MapKey) bool { return x.Get(k).IsValid() } 462func (x *dynamicMap) Clear(k protoreflect.MapKey) { delete(x.mapv, k.Interface()) } 463func (x *dynamicMap) Mutable(k protoreflect.MapKey) protoreflect.Value { 464 if x.desc.MapValue().Message() == nil { 465 panic(errors.New("%v: invalid Mutable on map with non-message value type", x.desc.FullName())) 466 } 467 v := x.Get(k) 468 if !v.IsValid() { 469 v = x.NewValue() 470 x.Set(k, v) 471 } 472 return v 473} 474func (x *dynamicMap) Len() int { return len(x.mapv) } 475func (x *dynamicMap) NewValue() protoreflect.Value { 476 if md := x.desc.MapValue().Message(); md != nil { 477 return protoreflect.ValueOfMessage(NewMessage(md).ProtoReflect()) 478 } 479 return x.desc.MapValue().Default() 480} 481func (x *dynamicMap) IsValid() bool { 482 return x.mapv != nil 483} 484 485func (x *dynamicMap) Range(f func(protoreflect.MapKey, protoreflect.Value) bool) { 486 for k, v := range x.mapv { 487 if !f(protoreflect.ValueOf(k).MapKey(), v) { 488 return 489 } 490 } 491} 492 493func isSet(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool { 494 switch { 495 case fd.IsMap(): 496 return v.Map().Len() > 0 497 case fd.IsList(): 498 return v.List().Len() > 0 499 case fd.ContainingOneof() != nil: 500 return true 501 case fd.Syntax() == protoreflect.Proto3 && !fd.IsExtension(): 502 switch fd.Kind() { 503 case protoreflect.BoolKind: 504 return v.Bool() 505 case protoreflect.EnumKind: 506 return v.Enum() != 0 507 case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed32Kind, protoreflect.Sfixed64Kind: 508 return v.Int() != 0 509 case protoreflect.Uint32Kind, protoreflect.Uint64Kind, protoreflect.Fixed32Kind, protoreflect.Fixed64Kind: 510 return v.Uint() != 0 511 case protoreflect.FloatKind, protoreflect.DoubleKind: 512 return v.Float() != 0 || math.Signbit(v.Float()) 513 case protoreflect.StringKind: 514 return v.String() != "" 515 case protoreflect.BytesKind: 516 return len(v.Bytes()) > 0 517 } 518 } 519 return true 520} 521 522func typecheck(fd protoreflect.FieldDescriptor, v protoreflect.Value) { 523 if err := typeIsValid(fd, v); err != nil { 524 panic(err) 525 } 526} 527 528func typeIsValid(fd protoreflect.FieldDescriptor, v protoreflect.Value) error { 529 switch { 530 case !v.IsValid(): 531 return errors.New("%v: assigning invalid value", fd.FullName()) 532 case fd.IsMap(): 533 if mapv, ok := v.Interface().(*dynamicMap); !ok || mapv.desc != fd || !mapv.IsValid() { 534 return errors.New("%v: assigning invalid type %T", fd.FullName(), v.Interface()) 535 } 536 return nil 537 case fd.IsList(): 538 switch list := v.Interface().(type) { 539 case *dynamicList: 540 if list.desc == fd && list.IsValid() { 541 return nil 542 } 543 case emptyList: 544 if list.desc == fd && list.IsValid() { 545 return nil 546 } 547 } 548 return errors.New("%v: assigning invalid type %T", fd.FullName(), v.Interface()) 549 default: 550 return singularTypeIsValid(fd, v) 551 } 552} 553 554func typecheckSingular(fd protoreflect.FieldDescriptor, v protoreflect.Value) { 555 if err := singularTypeIsValid(fd, v); err != nil { 556 panic(err) 557 } 558} 559 560func singularTypeIsValid(fd protoreflect.FieldDescriptor, v protoreflect.Value) error { 561 vi := v.Interface() 562 var ok bool 563 switch fd.Kind() { 564 case protoreflect.BoolKind: 565 _, ok = vi.(bool) 566 case protoreflect.EnumKind: 567 // We could check against the valid set of enum values, but do not. 568 _, ok = vi.(protoreflect.EnumNumber) 569 case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: 570 _, ok = vi.(int32) 571 case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: 572 _, ok = vi.(uint32) 573 case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: 574 _, ok = vi.(int64) 575 case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: 576 _, ok = vi.(uint64) 577 case protoreflect.FloatKind: 578 _, ok = vi.(float32) 579 case protoreflect.DoubleKind: 580 _, ok = vi.(float64) 581 case protoreflect.StringKind: 582 _, ok = vi.(string) 583 case protoreflect.BytesKind: 584 _, ok = vi.([]byte) 585 case protoreflect.MessageKind, protoreflect.GroupKind: 586 var m protoreflect.Message 587 m, ok = vi.(protoreflect.Message) 588 if ok && m.Descriptor().FullName() != fd.Message().FullName() { 589 return errors.New("%v: assigning invalid message type %v", fd.FullName(), m.Descriptor().FullName()) 590 } 591 if dm, ok := vi.(*Message); ok && dm.known == nil { 592 return errors.New("%v: assigning invalid zero-value message", fd.FullName()) 593 } 594 } 595 if !ok { 596 return errors.New("%v: assigning invalid type %T", fd.FullName(), v.Interface()) 597 } 598 return nil 599} 600 601func newListEntry(fd protoreflect.FieldDescriptor) protoreflect.Value { 602 switch fd.Kind() { 603 case protoreflect.BoolKind: 604 return protoreflect.ValueOfBool(false) 605 case protoreflect.EnumKind: 606 return protoreflect.ValueOfEnum(fd.Enum().Values().Get(0).Number()) 607 case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: 608 return protoreflect.ValueOfInt32(0) 609 case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: 610 return protoreflect.ValueOfUint32(0) 611 case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: 612 return protoreflect.ValueOfInt64(0) 613 case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: 614 return protoreflect.ValueOfUint64(0) 615 case protoreflect.FloatKind: 616 return protoreflect.ValueOfFloat32(0) 617 case protoreflect.DoubleKind: 618 return protoreflect.ValueOfFloat64(0) 619 case protoreflect.StringKind: 620 return protoreflect.ValueOfString("") 621 case protoreflect.BytesKind: 622 return protoreflect.ValueOfBytes(nil) 623 case protoreflect.MessageKind, protoreflect.GroupKind: 624 return protoreflect.ValueOfMessage(NewMessage(fd.Message()).ProtoReflect()) 625 } 626 panic(errors.New("%v: unknown kind %v", fd.FullName(), fd.Kind())) 627} 628 629// NewExtensionType creates a new ExtensionType with the provided descriptor. 630// 631// Dynamic ExtensionTypes with the same descriptor compare as equal. That is, 632// if xd1 == xd2, then NewExtensionType(xd1) == NewExtensionType(xd2). 633// 634// The InterfaceOf and ValueOf methods of the extension type are defined as: 635// 636// func (xt extensionType) ValueOf(iv interface{}) protoreflect.Value { 637// return protoreflect.ValueOf(iv) 638// } 639// 640// func (xt extensionType) InterfaceOf(v protoreflect.Value) interface{} { 641// return v.Interface() 642// } 643// 644// The Go type used by the proto.GetExtension and proto.SetExtension functions 645// is determined by these methods, and is therefore equivalent to the Go type 646// used to represent a protoreflect.Value. See the protoreflect.Value 647// documentation for more details. 648func NewExtensionType(desc protoreflect.ExtensionDescriptor) protoreflect.ExtensionType { 649 if xt, ok := desc.(protoreflect.ExtensionTypeDescriptor); ok { 650 desc = xt.Descriptor() 651 } 652 return extensionType{extensionTypeDescriptor{desc}} 653} 654 655func (xt extensionType) New() protoreflect.Value { 656 switch { 657 case xt.desc.IsMap(): 658 return protoreflect.ValueOfMap(&dynamicMap{ 659 desc: xt.desc, 660 mapv: make(map[interface{}]protoreflect.Value), 661 }) 662 case xt.desc.IsList(): 663 return protoreflect.ValueOfList(&dynamicList{desc: xt.desc}) 664 case xt.desc.Message() != nil: 665 return protoreflect.ValueOfMessage(NewMessage(xt.desc.Message())) 666 default: 667 return xt.desc.Default() 668 } 669} 670 671func (xt extensionType) Zero() protoreflect.Value { 672 switch { 673 case xt.desc.IsMap(): 674 return protoreflect.ValueOfMap(&dynamicMap{desc: xt.desc}) 675 case xt.desc.Cardinality() == protoreflect.Repeated: 676 return protoreflect.ValueOfList(emptyList{desc: xt.desc}) 677 case xt.desc.Message() != nil: 678 return protoreflect.ValueOfMessage(&Message{typ: messageType{xt.desc.Message()}}) 679 default: 680 return xt.desc.Default() 681 } 682} 683 684func (xt extensionType) TypeDescriptor() protoreflect.ExtensionTypeDescriptor { 685 return xt.desc 686} 687 688func (xt extensionType) ValueOf(iv interface{}) protoreflect.Value { 689 v := protoreflect.ValueOf(iv) 690 typecheck(xt.desc, v) 691 return v 692} 693 694func (xt extensionType) InterfaceOf(v protoreflect.Value) interface{} { 695 typecheck(xt.desc, v) 696 return v.Interface() 697} 698 699func (xt extensionType) IsValidInterface(iv interface{}) bool { 700 return typeIsValid(xt.desc, protoreflect.ValueOf(iv)) == nil 701} 702 703func (xt extensionType) IsValidValue(v protoreflect.Value) bool { 704 return typeIsValid(xt.desc, v) == nil 705} 706 707type extensionTypeDescriptor struct { 708 protoreflect.ExtensionDescriptor 709} 710 711func (xt extensionTypeDescriptor) Type() protoreflect.ExtensionType { 712 return extensionType{xt} 713} 714 715func (xt extensionTypeDescriptor) Descriptor() protoreflect.ExtensionDescriptor { 716 return xt.ExtensionDescriptor 717} 718