khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 1 | package dynamic |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "compress/gzip" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "reflect" |
| 9 | "sort" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/golang/protobuf/proto" |
| 13 | "github.com/golang/protobuf/protoc-gen-go/descriptor" |
| 14 | |
| 15 | "github.com/jhump/protoreflect/codec" |
| 16 | "github.com/jhump/protoreflect/desc" |
| 17 | "github.com/jhump/protoreflect/internal" |
| 18 | ) |
| 19 | |
| 20 | // ErrUnknownTagNumber is an error that is returned when an operation refers |
| 21 | // to an unknown tag number. |
| 22 | var ErrUnknownTagNumber = errors.New("unknown tag number") |
| 23 | |
| 24 | // UnknownTagNumberError is the same as ErrUnknownTagNumber. |
| 25 | // Deprecated: use ErrUnknownTagNumber |
| 26 | var UnknownTagNumberError = ErrUnknownTagNumber |
| 27 | |
| 28 | // ErrUnknownFieldName is an error that is returned when an operation refers |
| 29 | // to an unknown field name. |
| 30 | var ErrUnknownFieldName = errors.New("unknown field name") |
| 31 | |
| 32 | // UnknownFieldNameError is the same as ErrUnknownFieldName. |
| 33 | // Deprecated: use ErrUnknownFieldName |
| 34 | var UnknownFieldNameError = ErrUnknownFieldName |
| 35 | |
| 36 | // ErrFieldIsNotMap is an error that is returned when map-related operations |
| 37 | // are attempted with fields that are not maps. |
| 38 | var ErrFieldIsNotMap = errors.New("field is not a map type") |
| 39 | |
| 40 | // FieldIsNotMapError is the same as ErrFieldIsNotMap. |
| 41 | // Deprecated: use ErrFieldIsNotMap |
| 42 | var FieldIsNotMapError = ErrFieldIsNotMap |
| 43 | |
| 44 | // ErrFieldIsNotRepeated is an error that is returned when repeated field |
| 45 | // operations are attempted with fields that are not repeated. |
| 46 | var ErrFieldIsNotRepeated = errors.New("field is not repeated") |
| 47 | |
| 48 | // FieldIsNotRepeatedError is the same as ErrFieldIsNotRepeated. |
| 49 | // Deprecated: use ErrFieldIsNotRepeated |
| 50 | var FieldIsNotRepeatedError = ErrFieldIsNotRepeated |
| 51 | |
| 52 | // ErrIndexOutOfRange is an error that is returned when an invalid index is |
| 53 | // provided when access a single element of a repeated field. |
| 54 | var ErrIndexOutOfRange = errors.New("index is out of range") |
| 55 | |
| 56 | // IndexOutOfRangeError is the same as ErrIndexOutOfRange. |
| 57 | // Deprecated: use ErrIndexOutOfRange |
| 58 | var IndexOutOfRangeError = ErrIndexOutOfRange |
| 59 | |
| 60 | // ErrNumericOverflow is an error returned by operations that encounter a |
| 61 | // numeric value that is too large, for example de-serializing a value into an |
| 62 | // int32 field when the value is larger that can fit into a 32-bit value. |
| 63 | var ErrNumericOverflow = errors.New("numeric value is out of range") |
| 64 | |
| 65 | // NumericOverflowError is the same as ErrNumericOverflow. |
| 66 | // Deprecated: use ErrNumericOverflow |
| 67 | var NumericOverflowError = ErrNumericOverflow |
| 68 | |
| 69 | var typeOfProtoMessage = reflect.TypeOf((*proto.Message)(nil)).Elem() |
| 70 | var typeOfDynamicMessage = reflect.TypeOf((*Message)(nil)) |
| 71 | var typeOfBytes = reflect.TypeOf(([]byte)(nil)) |
| 72 | |
| 73 | // Message is a dynamic protobuf message. Instead of a generated struct, |
| 74 | // like most protobuf messages, this is a map of field number to values and |
| 75 | // a message descriptor, which is used to validate the field values and |
| 76 | // also to de-serialize messages (from the standard binary format, as well |
| 77 | // as from the text format and from JSON). |
| 78 | type Message struct { |
| 79 | md *desc.MessageDescriptor |
| 80 | er *ExtensionRegistry |
| 81 | mf *MessageFactory |
| 82 | extraFields map[int32]*desc.FieldDescriptor |
| 83 | values map[int32]interface{} |
| 84 | unknownFields map[int32][]UnknownField |
| 85 | } |
| 86 | |
| 87 | // UnknownField represents a field that was parsed from the binary wire |
| 88 | // format for a message, but was not a recognized field number. Enough |
| 89 | // information is preserved so that re-serializing the message won't lose |
| 90 | // any of the unrecognized data. |
| 91 | type UnknownField struct { |
| 92 | // Encoding indicates how the unknown field was encoded on the wire. If it |
| 93 | // is proto.WireBytes or proto.WireGroupStart then Contents will be set to |
| 94 | // the raw bytes. If it is proto.WireTypeFixed32 then the data is in the least |
| 95 | // significant 32 bits of Value. Otherwise, the data is in all 64 bits of |
| 96 | // Value. |
| 97 | Encoding int8 |
| 98 | Contents []byte |
| 99 | Value uint64 |
| 100 | } |
| 101 | |
| 102 | // NewMessage creates a new dynamic message for the type represented by the given |
| 103 | // message descriptor. During de-serialization, a default MessageFactory is used to |
| 104 | // instantiate any nested message fields and no extension fields will be parsed. To |
| 105 | // use a custom MessageFactory or ExtensionRegistry, use MessageFactory.NewMessage. |
| 106 | func NewMessage(md *desc.MessageDescriptor) *Message { |
| 107 | return NewMessageWithMessageFactory(md, nil) |
| 108 | } |
| 109 | |
| 110 | // NewMessageWithExtensionRegistry creates a new dynamic message for the type |
| 111 | // represented by the given message descriptor. During de-serialization, the given |
| 112 | // ExtensionRegistry is used to parse extension fields and nested messages will be |
| 113 | // instantiated using dynamic.NewMessageFactoryWithExtensionRegistry(er). |
| 114 | func NewMessageWithExtensionRegistry(md *desc.MessageDescriptor, er *ExtensionRegistry) *Message { |
| 115 | mf := NewMessageFactoryWithExtensionRegistry(er) |
| 116 | return NewMessageWithMessageFactory(md, mf) |
| 117 | } |
| 118 | |
| 119 | // NewMessageWithMessageFactory creates a new dynamic message for the type |
| 120 | // represented by the given message descriptor. During de-serialization, the given |
| 121 | // MessageFactory is used to instantiate nested messages. |
| 122 | func NewMessageWithMessageFactory(md *desc.MessageDescriptor, mf *MessageFactory) *Message { |
| 123 | var er *ExtensionRegistry |
| 124 | if mf != nil { |
| 125 | er = mf.er |
| 126 | } |
| 127 | return &Message{ |
| 128 | md: md, |
| 129 | mf: mf, |
| 130 | er: er, |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // AsDynamicMessage converts the given message to a dynamic message. If the |
| 135 | // given message is dynamic, it is returned. Otherwise, a dynamic message is |
| 136 | // created using NewMessage. |
| 137 | func AsDynamicMessage(msg proto.Message) (*Message, error) { |
| 138 | return AsDynamicMessageWithMessageFactory(msg, nil) |
| 139 | } |
| 140 | |
| 141 | // AsDynamicMessageWithExtensionRegistry converts the given message to a dynamic |
| 142 | // message. If the given message is dynamic, it is returned. Otherwise, a |
| 143 | // dynamic message is created using NewMessageWithExtensionRegistry. |
| 144 | func AsDynamicMessageWithExtensionRegistry(msg proto.Message, er *ExtensionRegistry) (*Message, error) { |
| 145 | mf := NewMessageFactoryWithExtensionRegistry(er) |
| 146 | return AsDynamicMessageWithMessageFactory(msg, mf) |
| 147 | } |
| 148 | |
| 149 | // AsDynamicMessageWithMessageFactory converts the given message to a dynamic |
| 150 | // message. If the given message is dynamic, it is returned. Otherwise, a |
| 151 | // dynamic message is created using NewMessageWithMessageFactory. |
| 152 | func AsDynamicMessageWithMessageFactory(msg proto.Message, mf *MessageFactory) (*Message, error) { |
| 153 | if dm, ok := msg.(*Message); ok { |
| 154 | return dm, nil |
| 155 | } |
| 156 | md, err := desc.LoadMessageDescriptorForMessage(msg) |
| 157 | if err != nil { |
| 158 | return nil, err |
| 159 | } |
| 160 | dm := NewMessageWithMessageFactory(md, mf) |
| 161 | err = dm.mergeFrom(msg) |
| 162 | if err != nil { |
| 163 | return nil, err |
| 164 | } |
| 165 | return dm, nil |
| 166 | } |
| 167 | |
| 168 | // GetMessageDescriptor returns a descriptor for this message's type. |
| 169 | func (m *Message) GetMessageDescriptor() *desc.MessageDescriptor { |
| 170 | return m.md |
| 171 | } |
| 172 | |
| 173 | // GetKnownFields returns a slice of descriptors for all known fields. The |
| 174 | // fields will not be in any defined order. |
| 175 | func (m *Message) GetKnownFields() []*desc.FieldDescriptor { |
| 176 | if len(m.extraFields) == 0 { |
| 177 | return m.md.GetFields() |
| 178 | } |
| 179 | flds := make([]*desc.FieldDescriptor, len(m.md.GetFields()), len(m.md.GetFields())+len(m.extraFields)) |
| 180 | copy(flds, m.md.GetFields()) |
| 181 | for _, fld := range m.extraFields { |
| 182 | if !fld.IsExtension() { |
| 183 | flds = append(flds, fld) |
| 184 | } |
| 185 | } |
| 186 | return flds |
| 187 | } |
| 188 | |
| 189 | // GetKnownExtensions returns a slice of descriptors for all extensions known by |
| 190 | // the message's extension registry. The fields will not be in any defined order. |
| 191 | func (m *Message) GetKnownExtensions() []*desc.FieldDescriptor { |
| 192 | if !m.md.IsExtendable() { |
| 193 | return nil |
| 194 | } |
| 195 | exts := m.er.AllExtensionsForType(m.md.GetFullyQualifiedName()) |
| 196 | for _, fld := range m.extraFields { |
| 197 | if fld.IsExtension() { |
| 198 | exts = append(exts, fld) |
| 199 | } |
| 200 | } |
| 201 | return exts |
| 202 | } |
| 203 | |
| 204 | // GetUnknownFields returns a slice of tag numbers for all unknown fields that |
| 205 | // this message contains. The tags will not be in any defined order. |
| 206 | func (m *Message) GetUnknownFields() []int32 { |
| 207 | flds := make([]int32, 0, len(m.unknownFields)) |
| 208 | for tag := range m.unknownFields { |
| 209 | flds = append(flds, tag) |
| 210 | } |
| 211 | return flds |
| 212 | } |
| 213 | |
| 214 | // Descriptor returns the serialized form of the file descriptor in which the |
| 215 | // message was defined and a path to the message type therein. This mimics the |
| 216 | // method of the same name on message types generated by protoc. |
| 217 | func (m *Message) Descriptor() ([]byte, []int) { |
| 218 | // get encoded file descriptor |
| 219 | b, err := proto.Marshal(m.md.GetFile().AsProto()) |
| 220 | if err != nil { |
| 221 | panic(fmt.Sprintf("failed to get encoded descriptor for %s: %v", m.md.GetFile().GetName(), err)) |
| 222 | } |
| 223 | var zippedBytes bytes.Buffer |
| 224 | w := gzip.NewWriter(&zippedBytes) |
| 225 | if _, err := w.Write(b); err != nil { |
| 226 | panic(fmt.Sprintf("failed to get encoded descriptor for %s: %v", m.md.GetFile().GetName(), err)) |
| 227 | } |
| 228 | if err := w.Close(); err != nil { |
| 229 | panic(fmt.Sprintf("failed to get an encoded descriptor for %s: %v", m.md.GetFile().GetName(), err)) |
| 230 | } |
| 231 | |
| 232 | // and path to message |
| 233 | path := []int{} |
| 234 | var d desc.Descriptor |
| 235 | name := m.md.GetFullyQualifiedName() |
| 236 | for d = m.md.GetParent(); d != nil; name, d = d.GetFullyQualifiedName(), d.GetParent() { |
| 237 | found := false |
| 238 | switch d := d.(type) { |
| 239 | case (*desc.FileDescriptor): |
| 240 | for i, md := range d.GetMessageTypes() { |
| 241 | if md.GetFullyQualifiedName() == name { |
| 242 | found = true |
| 243 | path = append(path, i) |
| 244 | } |
| 245 | } |
| 246 | case (*desc.MessageDescriptor): |
| 247 | for i, md := range d.GetNestedMessageTypes() { |
| 248 | if md.GetFullyQualifiedName() == name { |
| 249 | found = true |
| 250 | path = append(path, i) |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | if !found { |
| 255 | panic(fmt.Sprintf("failed to compute descriptor path for %s", m.md.GetFullyQualifiedName())) |
| 256 | } |
| 257 | } |
| 258 | // reverse the path |
| 259 | i := 0 |
| 260 | j := len(path) - 1 |
| 261 | for i < j { |
| 262 | path[i], path[j] = path[j], path[i] |
| 263 | i++ |
| 264 | j-- |
| 265 | } |
| 266 | |
| 267 | return zippedBytes.Bytes(), path |
| 268 | } |
| 269 | |
| 270 | // XXX_MessageName returns the fully qualified name of this message's type. This |
| 271 | // allows dynamic messages to be used with proto.MessageName. |
| 272 | func (m *Message) XXX_MessageName() string { |
| 273 | return m.md.GetFullyQualifiedName() |
| 274 | } |
| 275 | |
| 276 | // FindFieldDescriptor returns a field descriptor for the given tag number. This |
| 277 | // searches known fields in the descriptor, known fields discovered during calls |
| 278 | // to GetField or SetField, and extension fields known by the message's extension |
| 279 | // registry. It returns nil if the tag is unknown. |
| 280 | func (m *Message) FindFieldDescriptor(tagNumber int32) *desc.FieldDescriptor { |
| 281 | fd := m.md.FindFieldByNumber(tagNumber) |
| 282 | if fd != nil { |
| 283 | return fd |
| 284 | } |
| 285 | fd = m.er.FindExtension(m.md.GetFullyQualifiedName(), tagNumber) |
| 286 | if fd != nil { |
| 287 | return fd |
| 288 | } |
| 289 | return m.extraFields[tagNumber] |
| 290 | } |
| 291 | |
| 292 | // FindFieldDescriptorByName returns a field descriptor for the given field |
| 293 | // name. This searches known fields in the descriptor, known fields discovered |
| 294 | // during calls to GetField or SetField, and extension fields known by the |
| 295 | // message's extension registry. It returns nil if the name is unknown. If the |
| 296 | // given name refers to an extension, it should be fully qualified and may be |
| 297 | // optionally enclosed in parentheses or brackets. |
| 298 | func (m *Message) FindFieldDescriptorByName(name string) *desc.FieldDescriptor { |
| 299 | if name == "" { |
| 300 | return nil |
| 301 | } |
| 302 | fd := m.md.FindFieldByName(name) |
| 303 | if fd != nil { |
| 304 | return fd |
| 305 | } |
| 306 | mustBeExt := false |
| 307 | if name[0] == '(' { |
| 308 | if name[len(name)-1] != ')' { |
| 309 | // malformed name |
| 310 | return nil |
| 311 | } |
| 312 | mustBeExt = true |
| 313 | name = name[1 : len(name)-1] |
| 314 | } else if name[0] == '[' { |
| 315 | if name[len(name)-1] != ']' { |
| 316 | // malformed name |
| 317 | return nil |
| 318 | } |
| 319 | mustBeExt = true |
| 320 | name = name[1 : len(name)-1] |
| 321 | } |
| 322 | fd = m.er.FindExtensionByName(m.md.GetFullyQualifiedName(), name) |
| 323 | if fd != nil { |
| 324 | return fd |
| 325 | } |
| 326 | for _, fd := range m.extraFields { |
| 327 | if fd.IsExtension() && name == fd.GetFullyQualifiedName() { |
| 328 | return fd |
| 329 | } else if !mustBeExt && !fd.IsExtension() && name == fd.GetName() { |
| 330 | return fd |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | return nil |
| 335 | } |
| 336 | |
| 337 | // FindFieldDescriptorByJSONName returns a field descriptor for the given JSON |
| 338 | // name. This searches known fields in the descriptor, known fields discovered |
| 339 | // during calls to GetField or SetField, and extension fields known by the |
| 340 | // message's extension registry. If no field matches the given JSON name, it |
| 341 | // will fall back to searching field names (e.g. FindFieldDescriptorByName). If |
| 342 | // this also yields no match, nil is returned. |
| 343 | func (m *Message) FindFieldDescriptorByJSONName(name string) *desc.FieldDescriptor { |
| 344 | if name == "" { |
| 345 | return nil |
| 346 | } |
| 347 | fd := m.md.FindFieldByJSONName(name) |
| 348 | if fd != nil { |
| 349 | return fd |
| 350 | } |
| 351 | mustBeExt := false |
| 352 | if name[0] == '(' { |
| 353 | if name[len(name)-1] != ')' { |
| 354 | // malformed name |
| 355 | return nil |
| 356 | } |
| 357 | mustBeExt = true |
| 358 | name = name[1 : len(name)-1] |
| 359 | } else if name[0] == '[' { |
| 360 | if name[len(name)-1] != ']' { |
| 361 | // malformed name |
| 362 | return nil |
| 363 | } |
| 364 | mustBeExt = true |
| 365 | name = name[1 : len(name)-1] |
| 366 | } |
| 367 | fd = m.er.FindExtensionByJSONName(m.md.GetFullyQualifiedName(), name) |
| 368 | if fd != nil { |
| 369 | return fd |
| 370 | } |
| 371 | for _, fd := range m.extraFields { |
| 372 | if fd.IsExtension() && name == fd.GetFullyQualifiedJSONName() { |
| 373 | return fd |
| 374 | } else if !mustBeExt && !fd.IsExtension() && name == fd.GetJSONName() { |
| 375 | return fd |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // try non-JSON names |
| 380 | return m.FindFieldDescriptorByName(name) |
| 381 | } |
| 382 | |
| 383 | func (m *Message) checkField(fd *desc.FieldDescriptor) error { |
| 384 | return checkField(fd, m.md) |
| 385 | } |
| 386 | |
| 387 | func checkField(fd *desc.FieldDescriptor, md *desc.MessageDescriptor) error { |
| 388 | if fd.GetOwner().GetFullyQualifiedName() != md.GetFullyQualifiedName() { |
| 389 | return fmt.Errorf("given field, %s, is for wrong message type: %s; expecting %s", fd.GetName(), fd.GetOwner().GetFullyQualifiedName(), md.GetFullyQualifiedName()) |
| 390 | } |
| 391 | if fd.IsExtension() && !md.IsExtension(fd.GetNumber()) { |
| 392 | return fmt.Errorf("given field, %s, is an extension but is not in message extension range: %v", fd.GetFullyQualifiedName(), md.GetExtensionRanges()) |
| 393 | } |
| 394 | return nil |
| 395 | } |
| 396 | |
| 397 | // GetField returns the value for the given field descriptor. It panics if an |
| 398 | // error is encountered. See TryGetField. |
| 399 | func (m *Message) GetField(fd *desc.FieldDescriptor) interface{} { |
| 400 | if v, err := m.TryGetField(fd); err != nil { |
| 401 | panic(err.Error()) |
| 402 | } else { |
| 403 | return v |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // TryGetField returns the value for the given field descriptor. An error is |
| 408 | // returned if the given field descriptor does not belong to the right message |
| 409 | // type. |
| 410 | // |
| 411 | // The Go type of the returned value, for scalar fields, is the same as protoc |
| 412 | // would generate for the field (in a non-dynamic message). The table below |
| 413 | // lists the scalar types and the corresponding Go types. |
| 414 | // +-------------------------+-----------+ |
| 415 | // | Declared Type | Go Type | |
| 416 | // +-------------------------+-----------+ |
| 417 | // | int32, sint32, sfixed32 | int32 | |
| 418 | // | int64, sint64, sfixed64 | int64 | |
| 419 | // | uint32, fixed32 | uint32 | |
| 420 | // | uint64, fixed64 | uint64 | |
| 421 | // | float | float32 | |
| 422 | // | double | double32 | |
| 423 | // | bool | bool | |
| 424 | // | string | string | |
| 425 | // | bytes | []byte | |
| 426 | // +-------------------------+-----------+ |
| 427 | // |
| 428 | // Values for enum fields will always be int32 values. You can use the enum |
| 429 | // descriptor associated with the field to lookup value names with those values. |
| 430 | // Values for message type fields may be an instance of the generated type *or* |
| 431 | // may be another *dynamic.Message that represents the type. |
| 432 | // |
| 433 | // If the given field is a map field, the returned type will be |
| 434 | // map[interface{}]interface{}. The actual concrete types of keys and values is |
| 435 | // as described above. If the given field is a (non-map) repeated field, the |
| 436 | // returned type is always []interface{}; the type of the actual elements is as |
| 437 | // described above. |
| 438 | // |
| 439 | // If this message has no value for the given field, its default value is |
| 440 | // returned. If the message is defined in a file with "proto3" syntax, the |
| 441 | // default is always the zero value for the field. The default value for map and |
| 442 | // repeated fields is a nil map or slice (respectively). For field's whose types |
| 443 | // is a message, the default value is an empty message for "proto2" syntax or a |
| 444 | // nil message for "proto3" syntax. Note that the in the latter case, a non-nil |
| 445 | // interface with a nil pointer is returned, not a nil interface. Also note that |
| 446 | // whether the returned value is an empty message or nil depends on if *this* |
| 447 | // message was defined as "proto3" syntax, not the message type referred to by |
| 448 | // the field's type. |
| 449 | // |
| 450 | // If the given field descriptor is not known (e.g. not present in the message |
| 451 | // descriptor) but corresponds to an unknown field, the unknown value will be |
| 452 | // parsed and become known. The parsed value will be returned, or an error will |
| 453 | // be returned if the unknown value cannot be parsed according to the field |
| 454 | // descriptor's type information. |
| 455 | func (m *Message) TryGetField(fd *desc.FieldDescriptor) (interface{}, error) { |
| 456 | if err := m.checkField(fd); err != nil { |
| 457 | return nil, err |
| 458 | } |
| 459 | return m.getField(fd) |
| 460 | } |
| 461 | |
| 462 | // GetFieldByName returns the value for the field with the given name. It panics |
| 463 | // if an error is encountered. See TryGetFieldByName. |
| 464 | func (m *Message) GetFieldByName(name string) interface{} { |
| 465 | if v, err := m.TryGetFieldByName(name); err != nil { |
| 466 | panic(err.Error()) |
| 467 | } else { |
| 468 | return v |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | // TryGetFieldByName returns the value for the field with the given name. An |
| 473 | // error is returned if the given name is unknown. If the given name refers to |
| 474 | // an extension field, it should be fully qualified and optionally enclosed in |
| 475 | // parenthesis or brackets. |
| 476 | // |
| 477 | // If this message has no value for the given field, its default value is |
| 478 | // returned. (See TryGetField for more info on types and default field values.) |
| 479 | func (m *Message) TryGetFieldByName(name string) (interface{}, error) { |
| 480 | fd := m.FindFieldDescriptorByName(name) |
| 481 | if fd == nil { |
| 482 | return nil, UnknownFieldNameError |
| 483 | } |
| 484 | return m.getField(fd) |
| 485 | } |
| 486 | |
| 487 | // GetFieldByNumber returns the value for the field with the given tag number. |
| 488 | // It panics if an error is encountered. See TryGetFieldByNumber. |
| 489 | func (m *Message) GetFieldByNumber(tagNumber int) interface{} { |
| 490 | if v, err := m.TryGetFieldByNumber(tagNumber); err != nil { |
| 491 | panic(err.Error()) |
| 492 | } else { |
| 493 | return v |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | // TryGetFieldByNumber returns the value for the field with the given tag |
| 498 | // number. An error is returned if the given tag is unknown. |
| 499 | // |
| 500 | // If this message has no value for the given field, its default value is |
| 501 | // returned. (See TryGetField for more info on types and default field values.) |
| 502 | func (m *Message) TryGetFieldByNumber(tagNumber int) (interface{}, error) { |
| 503 | fd := m.FindFieldDescriptor(int32(tagNumber)) |
| 504 | if fd == nil { |
| 505 | return nil, UnknownTagNumberError |
| 506 | } |
| 507 | return m.getField(fd) |
| 508 | } |
| 509 | |
| 510 | func (m *Message) getField(fd *desc.FieldDescriptor) (interface{}, error) { |
| 511 | return m.doGetField(fd, false) |
| 512 | } |
| 513 | |
| 514 | func (m *Message) doGetField(fd *desc.FieldDescriptor, nilIfAbsent bool) (interface{}, error) { |
| 515 | res := m.values[fd.GetNumber()] |
| 516 | if res == nil { |
| 517 | var err error |
| 518 | if res, err = m.parseUnknownField(fd); err != nil { |
| 519 | return nil, err |
| 520 | } |
| 521 | if res == nil { |
| 522 | if nilIfAbsent { |
| 523 | return nil, nil |
| 524 | } else { |
| 525 | def := fd.GetDefaultValue() |
| 526 | if def != nil { |
| 527 | return def, nil |
| 528 | } |
| 529 | // GetDefaultValue only returns nil for message types |
| 530 | md := fd.GetMessageType() |
| 531 | if m.md.IsProto3() { |
| 532 | return nilMessage(md), nil |
| 533 | } else { |
| 534 | // for proto2, return default instance of message |
| 535 | return m.mf.NewMessage(md), nil |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | rt := reflect.TypeOf(res) |
| 541 | if rt.Kind() == reflect.Map { |
| 542 | // make defensive copies to prevent caller from storing illegal keys and values |
| 543 | m := res.(map[interface{}]interface{}) |
| 544 | res := map[interface{}]interface{}{} |
| 545 | for k, v := range m { |
| 546 | res[k] = v |
| 547 | } |
| 548 | return res, nil |
| 549 | } else if rt.Kind() == reflect.Slice && rt != typeOfBytes { |
| 550 | // make defensive copies to prevent caller from storing illegal elements |
| 551 | sl := res.([]interface{}) |
| 552 | res := make([]interface{}, len(sl)) |
| 553 | copy(res, sl) |
| 554 | return res, nil |
| 555 | } |
| 556 | return res, nil |
| 557 | } |
| 558 | |
| 559 | func nilMessage(md *desc.MessageDescriptor) interface{} { |
| 560 | // try to return a proper nil pointer |
| 561 | msgType := proto.MessageType(md.GetFullyQualifiedName()) |
| 562 | if msgType != nil && msgType.Implements(typeOfProtoMessage) { |
| 563 | return reflect.Zero(msgType).Interface().(proto.Message) |
| 564 | } |
| 565 | // fallback to nil dynamic message pointer |
| 566 | return (*Message)(nil) |
| 567 | } |
| 568 | |
| 569 | // HasField returns true if this message has a value for the given field. If the |
| 570 | // given field is not valid (e.g. belongs to a different message type), false is |
| 571 | // returned. If this message is defined in a file with "proto3" syntax, this |
| 572 | // will return false even if a field was explicitly assigned its zero value (the |
| 573 | // zero values for a field are intentionally indistinguishable from absent). |
| 574 | func (m *Message) HasField(fd *desc.FieldDescriptor) bool { |
| 575 | if err := m.checkField(fd); err != nil { |
| 576 | return false |
| 577 | } |
| 578 | return m.HasFieldNumber(int(fd.GetNumber())) |
| 579 | } |
| 580 | |
| 581 | // HasFieldName returns true if this message has a value for a field with the |
| 582 | // given name. If the given name is unknown, this returns false. |
| 583 | func (m *Message) HasFieldName(name string) bool { |
| 584 | fd := m.FindFieldDescriptorByName(name) |
| 585 | if fd == nil { |
| 586 | return false |
| 587 | } |
| 588 | return m.HasFieldNumber(int(fd.GetNumber())) |
| 589 | } |
| 590 | |
| 591 | // HasFieldNumber returns true if this message has a value for a field with the |
| 592 | // given tag number. If the given tag is unknown, this returns false. |
| 593 | func (m *Message) HasFieldNumber(tagNumber int) bool { |
| 594 | if _, ok := m.values[int32(tagNumber)]; ok { |
| 595 | return true |
| 596 | } |
| 597 | _, ok := m.unknownFields[int32(tagNumber)] |
| 598 | return ok |
| 599 | } |
| 600 | |
| 601 | // SetField sets the value for the given field descriptor to the given value. It |
| 602 | // panics if an error is encountered. See TrySetField. |
| 603 | func (m *Message) SetField(fd *desc.FieldDescriptor, val interface{}) { |
| 604 | if err := m.TrySetField(fd, val); err != nil { |
| 605 | panic(err.Error()) |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | // TrySetField sets the value for the given field descriptor to the given value. |
| 610 | // An error is returned if the given field descriptor does not belong to the |
| 611 | // right message type or if the given value is not a correct/compatible type for |
| 612 | // the given field. |
| 613 | // |
| 614 | // The Go type expected for a field is the same as TryGetField would return for |
| 615 | // the field. So message values can be supplied as either the correct generated |
| 616 | // message type or as a *dynamic.Message. |
| 617 | // |
| 618 | // Since it is cumbersome to work with dynamic messages, some concessions are |
| 619 | // made to simplify usage regarding types: |
| 620 | // |
| 621 | // 1. If a numeric type is provided that can be converted *without loss or |
| 622 | // overflow*, it is accepted. This allows for setting int64 fields using int |
| 623 | // or int32 values. Similarly for uint64 with uint and uint32 values and for |
| 624 | // float64 fields with float32 values. |
| 625 | // 2. The value can be a named type, as long as its underlying type is correct. |
| 626 | // 3. Map and repeated fields can be set using any kind of concrete map or |
| 627 | // slice type, as long as the values within are all of the correct type. So |
| 628 | // a field defined as a 'map<string, int32>` can be set using a |
| 629 | // map[string]int32, a map[string]interface{}, or even a |
| 630 | // map[interface{}]interface{}. |
| 631 | // 4. Finally, dynamic code that chooses to not treat maps as a special-case |
| 632 | // find that they can set map fields using a slice where each element is a |
| 633 | // message that matches the implicit map-entry field message type. |
| 634 | // |
| 635 | // If the given field descriptor is not known (e.g. not present in the message |
| 636 | // descriptor) it will become known. Subsequent operations using tag numbers or |
| 637 | // names will be able to resolve the newly-known type. If the message has a |
| 638 | // value for the unknown value, it is cleared, replaced by the given known |
| 639 | // value. |
| 640 | func (m *Message) TrySetField(fd *desc.FieldDescriptor, val interface{}) error { |
| 641 | if err := m.checkField(fd); err != nil { |
| 642 | return err |
| 643 | } |
| 644 | return m.setField(fd, val) |
| 645 | } |
| 646 | |
| 647 | // SetFieldByName sets the value for the field with the given name to the given |
| 648 | // value. It panics if an error is encountered. See TrySetFieldByName. |
| 649 | func (m *Message) SetFieldByName(name string, val interface{}) { |
| 650 | if err := m.TrySetFieldByName(name, val); err != nil { |
| 651 | panic(err.Error()) |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | // TrySetFieldByName sets the value for the field with the given name to the |
| 656 | // given value. An error is returned if the given name is unknown or if the |
| 657 | // given value has an incorrect type. If the given name refers to an extension |
| 658 | // field, it should be fully qualified and optionally enclosed in parenthesis or |
| 659 | // brackets. |
| 660 | // |
| 661 | // (See TrySetField for more info on types.) |
| 662 | func (m *Message) TrySetFieldByName(name string, val interface{}) error { |
| 663 | fd := m.FindFieldDescriptorByName(name) |
| 664 | if fd == nil { |
| 665 | return UnknownFieldNameError |
| 666 | } |
| 667 | return m.setField(fd, val) |
| 668 | } |
| 669 | |
| 670 | // SetFieldByNumber sets the value for the field with the given tag number to |
| 671 | // the given value. It panics if an error is encountered. See |
| 672 | // TrySetFieldByNumber. |
| 673 | func (m *Message) SetFieldByNumber(tagNumber int, val interface{}) { |
| 674 | if err := m.TrySetFieldByNumber(tagNumber, val); err != nil { |
| 675 | panic(err.Error()) |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | // TrySetFieldByNumber sets the value for the field with the given tag number to |
| 680 | // the given value. An error is returned if the given tag is unknown or if the |
| 681 | // given value has an incorrect type. |
| 682 | // |
| 683 | // (See TrySetField for more info on types.) |
| 684 | func (m *Message) TrySetFieldByNumber(tagNumber int, val interface{}) error { |
| 685 | fd := m.FindFieldDescriptor(int32(tagNumber)) |
| 686 | if fd == nil { |
| 687 | return UnknownTagNumberError |
| 688 | } |
| 689 | return m.setField(fd, val) |
| 690 | } |
| 691 | |
| 692 | func (m *Message) setField(fd *desc.FieldDescriptor, val interface{}) error { |
| 693 | var err error |
| 694 | if val, err = validFieldValue(fd, val); err != nil { |
| 695 | return err |
| 696 | } |
| 697 | m.internalSetField(fd, val) |
| 698 | return nil |
| 699 | } |
| 700 | |
| 701 | func (m *Message) internalSetField(fd *desc.FieldDescriptor, val interface{}) { |
| 702 | if fd.IsRepeated() { |
| 703 | // Unset fields and zero-length fields are indistinguishable, in both |
| 704 | // proto2 and proto3 syntax |
| 705 | if reflect.ValueOf(val).Len() == 0 { |
| 706 | if m.values != nil { |
| 707 | delete(m.values, fd.GetNumber()) |
| 708 | } |
| 709 | return |
| 710 | } |
| 711 | } else if m.md.IsProto3() && fd.GetOneOf() == nil { |
| 712 | // proto3 considers fields that are set to their zero value as unset |
| 713 | // (we already handled repeated fields above) |
| 714 | var equal bool |
| 715 | if b, ok := val.([]byte); ok { |
| 716 | // can't compare slices, so we have to special-case []byte values |
| 717 | equal = ok && bytes.Equal(b, fd.GetDefaultValue().([]byte)) |
| 718 | } else { |
| 719 | defVal := fd.GetDefaultValue() |
| 720 | equal = defVal == val |
| 721 | if !equal && defVal == nil { |
| 722 | // above just checks if value is the nil interface, |
| 723 | // but we should also test if the given value is a |
| 724 | // nil pointer |
| 725 | rv := reflect.ValueOf(val) |
| 726 | if rv.Kind() == reflect.Ptr && rv.IsNil() { |
| 727 | equal = true |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | if equal { |
| 732 | if m.values != nil { |
| 733 | delete(m.values, fd.GetNumber()) |
| 734 | } |
| 735 | return |
| 736 | } |
| 737 | } |
| 738 | if m.values == nil { |
| 739 | m.values = map[int32]interface{}{} |
| 740 | } |
| 741 | m.values[fd.GetNumber()] = val |
| 742 | // if this field is part of a one-of, make sure all other one-of choices are cleared |
| 743 | od := fd.GetOneOf() |
| 744 | if od != nil { |
| 745 | for _, other := range od.GetChoices() { |
| 746 | if other.GetNumber() != fd.GetNumber() { |
| 747 | delete(m.values, other.GetNumber()) |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | // also clear any unknown fields |
| 752 | if m.unknownFields != nil { |
| 753 | delete(m.unknownFields, fd.GetNumber()) |
| 754 | } |
| 755 | // and add this field if it was previously unknown |
| 756 | if existing := m.FindFieldDescriptor(fd.GetNumber()); existing == nil { |
| 757 | m.addField(fd) |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | func (m *Message) addField(fd *desc.FieldDescriptor) { |
| 762 | if m.extraFields == nil { |
| 763 | m.extraFields = map[int32]*desc.FieldDescriptor{} |
| 764 | } |
| 765 | m.extraFields[fd.GetNumber()] = fd |
| 766 | } |
| 767 | |
| 768 | // ClearField removes any value for the given field. It panics if an error is |
| 769 | // encountered. See TryClearField. |
| 770 | func (m *Message) ClearField(fd *desc.FieldDescriptor) { |
| 771 | if err := m.TryClearField(fd); err != nil { |
| 772 | panic(err.Error()) |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | // TryClearField removes any value for the given field. An error is returned if |
| 777 | // the given field descriptor does not belong to the right message type. |
| 778 | func (m *Message) TryClearField(fd *desc.FieldDescriptor) error { |
| 779 | if err := m.checkField(fd); err != nil { |
| 780 | return err |
| 781 | } |
| 782 | m.clearField(fd) |
| 783 | return nil |
| 784 | } |
| 785 | |
| 786 | // ClearFieldByName removes any value for the field with the given name. It |
| 787 | // panics if an error is encountered. See TryClearFieldByName. |
| 788 | func (m *Message) ClearFieldByName(name string) { |
| 789 | if err := m.TryClearFieldByName(name); err != nil { |
| 790 | panic(err.Error()) |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | // TryClearFieldByName removes any value for the field with the given name. An |
| 795 | // error is returned if the given name is unknown. If the given name refers to |
| 796 | // an extension field, it should be fully qualified and optionally enclosed in |
| 797 | // parenthesis or brackets. |
| 798 | func (m *Message) TryClearFieldByName(name string) error { |
| 799 | fd := m.FindFieldDescriptorByName(name) |
| 800 | if fd == nil { |
| 801 | return UnknownFieldNameError |
| 802 | } |
| 803 | m.clearField(fd) |
| 804 | return nil |
| 805 | } |
| 806 | |
| 807 | // ClearFieldByNumber removes any value for the field with the given tag number. |
| 808 | // It panics if an error is encountered. See TryClearFieldByNumber. |
| 809 | func (m *Message) ClearFieldByNumber(tagNumber int) { |
| 810 | if err := m.TryClearFieldByNumber(tagNumber); err != nil { |
| 811 | panic(err.Error()) |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | // TryClearFieldByNumber removes any value for the field with the given tag |
| 816 | // number. An error is returned if the given tag is unknown. |
| 817 | func (m *Message) TryClearFieldByNumber(tagNumber int) error { |
| 818 | fd := m.FindFieldDescriptor(int32(tagNumber)) |
| 819 | if fd == nil { |
| 820 | return UnknownTagNumberError |
| 821 | } |
| 822 | m.clearField(fd) |
| 823 | return nil |
| 824 | } |
| 825 | |
| 826 | func (m *Message) clearField(fd *desc.FieldDescriptor) { |
| 827 | // clear value |
| 828 | if m.values != nil { |
| 829 | delete(m.values, fd.GetNumber()) |
| 830 | } |
| 831 | // also clear any unknown fields |
| 832 | if m.unknownFields != nil { |
| 833 | delete(m.unknownFields, fd.GetNumber()) |
| 834 | } |
| 835 | // and add this field if it was previously unknown |
| 836 | if existing := m.FindFieldDescriptor(fd.GetNumber()); existing == nil { |
| 837 | m.addField(fd) |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | // GetOneOfField returns which of the given one-of's fields is set and the |
| 842 | // corresponding value. It panics if an error is encountered. See |
| 843 | // TryGetOneOfField. |
| 844 | func (m *Message) GetOneOfField(od *desc.OneOfDescriptor) (*desc.FieldDescriptor, interface{}) { |
| 845 | if fd, val, err := m.TryGetOneOfField(od); err != nil { |
| 846 | panic(err.Error()) |
| 847 | } else { |
| 848 | return fd, val |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | // TryGetOneOfField returns which of the given one-of's fields is set and the |
| 853 | // corresponding value. An error is returned if the given one-of belongs to the |
| 854 | // wrong message type. If the given one-of has no field set, this method will |
| 855 | // return nil, nil. |
| 856 | // |
| 857 | // The type of the value, if one is set, is the same as would be returned by |
| 858 | // TryGetField using the returned field descriptor. |
| 859 | // |
| 860 | // Like with TryGetField, if the given one-of contains any fields that are not |
| 861 | // known (e.g. not present in this message's descriptor), they will become known |
| 862 | // and any unknown value will be parsed (and become a known value on success). |
| 863 | func (m *Message) TryGetOneOfField(od *desc.OneOfDescriptor) (*desc.FieldDescriptor, interface{}, error) { |
| 864 | if od.GetOwner().GetFullyQualifiedName() != m.md.GetFullyQualifiedName() { |
| 865 | return nil, nil, fmt.Errorf("given one-of, %s, is for wrong message type: %s; expecting %s", od.GetName(), od.GetOwner().GetFullyQualifiedName(), m.md.GetFullyQualifiedName()) |
| 866 | } |
| 867 | for _, fd := range od.GetChoices() { |
| 868 | val, err := m.doGetField(fd, true) |
| 869 | if err != nil { |
| 870 | return nil, nil, err |
| 871 | } |
| 872 | if val != nil { |
| 873 | return fd, val, nil |
| 874 | } |
| 875 | } |
| 876 | return nil, nil, nil |
| 877 | } |
| 878 | |
| 879 | // ClearOneOfField removes any value for any of the given one-of's fields. It |
| 880 | // panics if an error is encountered. See TryClearOneOfField. |
| 881 | func (m *Message) ClearOneOfField(od *desc.OneOfDescriptor) { |
| 882 | if err := m.TryClearOneOfField(od); err != nil { |
| 883 | panic(err.Error()) |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | // TryClearOneOfField removes any value for any of the given one-of's fields. An |
| 888 | // error is returned if the given one-of descriptor does not belong to the right |
| 889 | // message type. |
| 890 | func (m *Message) TryClearOneOfField(od *desc.OneOfDescriptor) error { |
| 891 | if od.GetOwner().GetFullyQualifiedName() != m.md.GetFullyQualifiedName() { |
| 892 | return fmt.Errorf("given one-of, %s, is for wrong message type: %s; expecting %s", od.GetName(), od.GetOwner().GetFullyQualifiedName(), m.md.GetFullyQualifiedName()) |
| 893 | } |
| 894 | for _, fd := range od.GetChoices() { |
| 895 | m.clearField(fd) |
| 896 | } |
| 897 | return nil |
| 898 | } |
| 899 | |
| 900 | // GetMapField returns the value for the given map field descriptor and given |
| 901 | // key. It panics if an error is encountered. See TryGetMapField. |
| 902 | func (m *Message) GetMapField(fd *desc.FieldDescriptor, key interface{}) interface{} { |
| 903 | if v, err := m.TryGetMapField(fd, key); err != nil { |
| 904 | panic(err.Error()) |
| 905 | } else { |
| 906 | return v |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | // TryGetMapField returns the value for the given map field descriptor and given |
| 911 | // key. An error is returned if the given field descriptor does not belong to |
| 912 | // the right message type or if it is not a map field. |
| 913 | // |
| 914 | // If the map field does not contain the requested key, this method returns |
| 915 | // nil, nil. The Go type of the value returned mirrors the type that protoc |
| 916 | // would generate for the field. (See TryGetField for more details on types). |
| 917 | // |
| 918 | // If the given field descriptor is not known (e.g. not present in the message |
| 919 | // descriptor) but corresponds to an unknown field, the unknown value will be |
| 920 | // parsed and become known. The parsed value will be searched for the requested |
| 921 | // key and any value returned. An error will be returned if the unknown value |
| 922 | // cannot be parsed according to the field descriptor's type information. |
| 923 | func (m *Message) TryGetMapField(fd *desc.FieldDescriptor, key interface{}) (interface{}, error) { |
| 924 | if err := m.checkField(fd); err != nil { |
| 925 | return nil, err |
| 926 | } |
| 927 | return m.getMapField(fd, key) |
| 928 | } |
| 929 | |
| 930 | // GetMapFieldByName returns the value for the map field with the given name and |
| 931 | // given key. It panics if an error is encountered. See TryGetMapFieldByName. |
| 932 | func (m *Message) GetMapFieldByName(name string, key interface{}) interface{} { |
| 933 | if v, err := m.TryGetMapFieldByName(name, key); err != nil { |
| 934 | panic(err.Error()) |
| 935 | } else { |
| 936 | return v |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | // TryGetMapFieldByName returns the value for the map field with the given name |
| 941 | // and given key. An error is returned if the given name is unknown or if it |
| 942 | // names a field that is not a map field. |
| 943 | // |
| 944 | // If this message has no value for the given field or the value has no value |
| 945 | // for the requested key, then this method returns nil, nil. |
| 946 | // |
| 947 | // (See TryGetField for more info on types.) |
| 948 | func (m *Message) TryGetMapFieldByName(name string, key interface{}) (interface{}, error) { |
| 949 | fd := m.FindFieldDescriptorByName(name) |
| 950 | if fd == nil { |
| 951 | return nil, UnknownFieldNameError |
| 952 | } |
| 953 | return m.getMapField(fd, key) |
| 954 | } |
| 955 | |
| 956 | // GetMapFieldByNumber returns the value for the map field with the given tag |
| 957 | // number and given key. It panics if an error is encountered. See |
| 958 | // TryGetMapFieldByNumber. |
| 959 | func (m *Message) GetMapFieldByNumber(tagNumber int, key interface{}) interface{} { |
| 960 | if v, err := m.TryGetMapFieldByNumber(tagNumber, key); err != nil { |
| 961 | panic(err.Error()) |
| 962 | } else { |
| 963 | return v |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | // TryGetMapFieldByNumber returns the value for the map field with the given tag |
| 968 | // number and given key. An error is returned if the given tag is unknown or if |
| 969 | // it indicates a field that is not a map field. |
| 970 | // |
| 971 | // If this message has no value for the given field or the value has no value |
| 972 | // for the requested key, then this method returns nil, nil. |
| 973 | // |
| 974 | // (See TryGetField for more info on types.) |
| 975 | func (m *Message) TryGetMapFieldByNumber(tagNumber int, key interface{}) (interface{}, error) { |
| 976 | fd := m.FindFieldDescriptor(int32(tagNumber)) |
| 977 | if fd == nil { |
| 978 | return nil, UnknownTagNumberError |
| 979 | } |
| 980 | return m.getMapField(fd, key) |
| 981 | } |
| 982 | |
| 983 | func (m *Message) getMapField(fd *desc.FieldDescriptor, key interface{}) (interface{}, error) { |
| 984 | if !fd.IsMap() { |
| 985 | return nil, FieldIsNotMapError |
| 986 | } |
| 987 | kfd := fd.GetMessageType().GetFields()[0] |
| 988 | ki, err := validElementFieldValue(kfd, key, false) |
| 989 | if err != nil { |
| 990 | return nil, err |
| 991 | } |
| 992 | mp := m.values[fd.GetNumber()] |
| 993 | if mp == nil { |
| 994 | if mp, err = m.parseUnknownField(fd); err != nil { |
| 995 | return nil, err |
| 996 | } else if mp == nil { |
| 997 | return nil, nil |
| 998 | } |
| 999 | } |
| 1000 | return mp.(map[interface{}]interface{})[ki], nil |
| 1001 | } |
| 1002 | |
| 1003 | // ForEachMapFieldEntry executes the given function for each entry in the map |
| 1004 | // value for the given field descriptor. It stops iteration if the function |
| 1005 | // returns false. It panics if an error is encountered. See |
| 1006 | // TryForEachMapFieldEntry. |
| 1007 | func (m *Message) ForEachMapFieldEntry(fd *desc.FieldDescriptor, fn func(key, val interface{}) bool) { |
| 1008 | if err := m.TryForEachMapFieldEntry(fd, fn); err != nil { |
| 1009 | panic(err.Error()) |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | // TryForEachMapFieldEntry executes the given function for each entry in the map |
| 1014 | // value for the given field descriptor. An error is returned if the given field |
| 1015 | // descriptor does not belong to the right message type or if it is not a map |
| 1016 | // field. |
| 1017 | // |
| 1018 | // Iteration ends either when all entries have been examined or when the given |
| 1019 | // function returns false. So the function is expected to return true for normal |
| 1020 | // iteration and false to break out. If this message has no value for the given |
| 1021 | // field, it returns without invoking the given function. |
| 1022 | // |
| 1023 | // The Go type of the key and value supplied to the function mirrors the type |
| 1024 | // that protoc would generate for the field. (See TryGetField for more details |
| 1025 | // on types). |
| 1026 | // |
| 1027 | // If the given field descriptor is not known (e.g. not present in the message |
| 1028 | // descriptor) but corresponds to an unknown field, the unknown value will be |
| 1029 | // parsed and become known. The parsed value will be searched for the requested |
| 1030 | // key and any value returned. An error will be returned if the unknown value |
| 1031 | // cannot be parsed according to the field descriptor's type information. |
| 1032 | func (m *Message) TryForEachMapFieldEntry(fd *desc.FieldDescriptor, fn func(key, val interface{}) bool) error { |
| 1033 | if err := m.checkField(fd); err != nil { |
| 1034 | return err |
| 1035 | } |
| 1036 | return m.forEachMapFieldEntry(fd, fn) |
| 1037 | } |
| 1038 | |
| 1039 | // ForEachMapFieldEntryByName executes the given function for each entry in the |
| 1040 | // map value for the field with the given name. It stops iteration if the |
| 1041 | // function returns false. It panics if an error is encountered. See |
| 1042 | // TryForEachMapFieldEntryByName. |
| 1043 | func (m *Message) ForEachMapFieldEntryByName(name string, fn func(key, val interface{}) bool) { |
| 1044 | if err := m.TryForEachMapFieldEntryByName(name, fn); err != nil { |
| 1045 | panic(err.Error()) |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | // TryForEachMapFieldEntryByName executes the given function for each entry in |
| 1050 | // the map value for the field with the given name. It stops iteration if the |
| 1051 | // function returns false. An error is returned if the given name is unknown or |
| 1052 | // if it names a field that is not a map field. |
| 1053 | // |
| 1054 | // If this message has no value for the given field, it returns without ever |
| 1055 | // invoking the given function. |
| 1056 | // |
| 1057 | // (See TryGetField for more info on types supplied to the function.) |
| 1058 | func (m *Message) TryForEachMapFieldEntryByName(name string, fn func(key, val interface{}) bool) error { |
| 1059 | fd := m.FindFieldDescriptorByName(name) |
| 1060 | if fd == nil { |
| 1061 | return UnknownFieldNameError |
| 1062 | } |
| 1063 | return m.forEachMapFieldEntry(fd, fn) |
| 1064 | } |
| 1065 | |
| 1066 | // ForEachMapFieldEntryByNumber executes the given function for each entry in |
| 1067 | // the map value for the field with the given tag number. It stops iteration if |
| 1068 | // the function returns false. It panics if an error is encountered. See |
| 1069 | // TryForEachMapFieldEntryByNumber. |
| 1070 | func (m *Message) ForEachMapFieldEntryByNumber(tagNumber int, fn func(key, val interface{}) bool) { |
| 1071 | if err := m.TryForEachMapFieldEntryByNumber(tagNumber, fn); err != nil { |
| 1072 | panic(err.Error()) |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | // TryForEachMapFieldEntryByNumber executes the given function for each entry in |
| 1077 | // the map value for the field with the given tag number. It stops iteration if |
| 1078 | // the function returns false. An error is returned if the given tag is unknown |
| 1079 | // or if it indicates a field that is not a map field. |
| 1080 | // |
| 1081 | // If this message has no value for the given field, it returns without ever |
| 1082 | // invoking the given function. |
| 1083 | // |
| 1084 | // (See TryGetField for more info on types supplied to the function.) |
| 1085 | func (m *Message) TryForEachMapFieldEntryByNumber(tagNumber int, fn func(key, val interface{}) bool) error { |
| 1086 | fd := m.FindFieldDescriptor(int32(tagNumber)) |
| 1087 | if fd == nil { |
| 1088 | return UnknownTagNumberError |
| 1089 | } |
| 1090 | return m.forEachMapFieldEntry(fd, fn) |
| 1091 | } |
| 1092 | |
| 1093 | func (m *Message) forEachMapFieldEntry(fd *desc.FieldDescriptor, fn func(key, val interface{}) bool) error { |
| 1094 | if !fd.IsMap() { |
| 1095 | return FieldIsNotMapError |
| 1096 | } |
| 1097 | mp := m.values[fd.GetNumber()] |
| 1098 | if mp == nil { |
| 1099 | if mp, err := m.parseUnknownField(fd); err != nil { |
| 1100 | return err |
| 1101 | } else if mp == nil { |
| 1102 | return nil |
| 1103 | } |
| 1104 | } |
| 1105 | for k, v := range mp.(map[interface{}]interface{}) { |
| 1106 | if !fn(k, v) { |
| 1107 | break |
| 1108 | } |
| 1109 | } |
| 1110 | return nil |
| 1111 | } |
| 1112 | |
| 1113 | // PutMapField sets the value for the given map field descriptor and given key |
| 1114 | // to the given value. It panics if an error is encountered. See TryPutMapField. |
| 1115 | func (m *Message) PutMapField(fd *desc.FieldDescriptor, key interface{}, val interface{}) { |
| 1116 | if err := m.TryPutMapField(fd, key, val); err != nil { |
| 1117 | panic(err.Error()) |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | // TryPutMapField sets the value for the given map field descriptor and given |
| 1122 | // key to the given value. An error is returned if the given field descriptor |
| 1123 | // does not belong to the right message type, if the given field is not a map |
| 1124 | // field, or if the given value is not a correct/compatible type for the given |
| 1125 | // field. |
| 1126 | // |
| 1127 | // The Go type expected for a field is the same as required by TrySetField for |
| 1128 | // a field with the same type as the map's value type. |
| 1129 | // |
| 1130 | // If the given field descriptor is not known (e.g. not present in the message |
| 1131 | // descriptor) it will become known. Subsequent operations using tag numbers or |
| 1132 | // names will be able to resolve the newly-known type. If the message has a |
| 1133 | // value for the unknown value, it is cleared, replaced by the given known |
| 1134 | // value. |
| 1135 | func (m *Message) TryPutMapField(fd *desc.FieldDescriptor, key interface{}, val interface{}) error { |
| 1136 | if err := m.checkField(fd); err != nil { |
| 1137 | return err |
| 1138 | } |
| 1139 | return m.putMapField(fd, key, val) |
| 1140 | } |
| 1141 | |
| 1142 | // PutMapFieldByName sets the value for the map field with the given name and |
| 1143 | // given key to the given value. It panics if an error is encountered. See |
| 1144 | // TryPutMapFieldByName. |
| 1145 | func (m *Message) PutMapFieldByName(name string, key interface{}, val interface{}) { |
| 1146 | if err := m.TryPutMapFieldByName(name, key, val); err != nil { |
| 1147 | panic(err.Error()) |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | // TryPutMapFieldByName sets the value for the map field with the given name and |
| 1152 | // the given key to the given value. An error is returned if the given name is |
| 1153 | // unknown, if it names a field that is not a map, or if the given value has an |
| 1154 | // incorrect type. |
| 1155 | // |
| 1156 | // (See TrySetField for more info on types.) |
| 1157 | func (m *Message) TryPutMapFieldByName(name string, key interface{}, val interface{}) error { |
| 1158 | fd := m.FindFieldDescriptorByName(name) |
| 1159 | if fd == nil { |
| 1160 | return UnknownFieldNameError |
| 1161 | } |
| 1162 | return m.putMapField(fd, key, val) |
| 1163 | } |
| 1164 | |
| 1165 | // PutMapFieldByNumber sets the value for the map field with the given tag |
| 1166 | // number and given key to the given value. It panics if an error is |
| 1167 | // encountered. See TryPutMapFieldByNumber. |
| 1168 | func (m *Message) PutMapFieldByNumber(tagNumber int, key interface{}, val interface{}) { |
| 1169 | if err := m.TryPutMapFieldByNumber(tagNumber, key, val); err != nil { |
| 1170 | panic(err.Error()) |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | // TryPutMapFieldByNumber sets the value for the map field with the given tag |
| 1175 | // number and the given key to the given value. An error is returned if the |
| 1176 | // given tag is unknown, if it indicates a field that is not a map, or if the |
| 1177 | // given value has an incorrect type. |
| 1178 | // |
| 1179 | // (See TrySetField for more info on types.) |
| 1180 | func (m *Message) TryPutMapFieldByNumber(tagNumber int, key interface{}, val interface{}) error { |
| 1181 | fd := m.FindFieldDescriptor(int32(tagNumber)) |
| 1182 | if fd == nil { |
| 1183 | return UnknownTagNumberError |
| 1184 | } |
| 1185 | return m.putMapField(fd, key, val) |
| 1186 | } |
| 1187 | |
| 1188 | func (m *Message) putMapField(fd *desc.FieldDescriptor, key interface{}, val interface{}) error { |
| 1189 | if !fd.IsMap() { |
| 1190 | return FieldIsNotMapError |
| 1191 | } |
| 1192 | kfd := fd.GetMessageType().GetFields()[0] |
| 1193 | ki, err := validElementFieldValue(kfd, key, false) |
| 1194 | if err != nil { |
| 1195 | return err |
| 1196 | } |
| 1197 | vfd := fd.GetMessageType().GetFields()[1] |
| 1198 | vi, err := validElementFieldValue(vfd, val, true) |
| 1199 | if err != nil { |
| 1200 | return err |
| 1201 | } |
| 1202 | mp := m.values[fd.GetNumber()] |
| 1203 | if mp == nil { |
| 1204 | if mp, err = m.parseUnknownField(fd); err != nil { |
| 1205 | return err |
| 1206 | } else if mp == nil { |
| 1207 | m.internalSetField(fd, map[interface{}]interface{}{ki: vi}) |
| 1208 | return nil |
| 1209 | } |
| 1210 | } |
| 1211 | mp.(map[interface{}]interface{})[ki] = vi |
| 1212 | return nil |
| 1213 | } |
| 1214 | |
| 1215 | // RemoveMapField changes the value for the given field descriptor by removing |
| 1216 | // any value associated with the given key. It panics if an error is |
| 1217 | // encountered. See TryRemoveMapField. |
| 1218 | func (m *Message) RemoveMapField(fd *desc.FieldDescriptor, key interface{}) { |
| 1219 | if err := m.TryRemoveMapField(fd, key); err != nil { |
| 1220 | panic(err.Error()) |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | // TryRemoveMapField changes the value for the given field descriptor by |
| 1225 | // removing any value associated with the given key. An error is returned if the |
| 1226 | // given field descriptor does not belong to the right message type or if the |
| 1227 | // given field is not a map field. |
| 1228 | // |
| 1229 | // If the given field descriptor is not known (e.g. not present in the message |
| 1230 | // descriptor) it will become known. Subsequent operations using tag numbers or |
| 1231 | // names will be able to resolve the newly-known type. If the message has a |
| 1232 | // value for the unknown value, it is parsed and any value for the given key |
| 1233 | // removed. |
| 1234 | func (m *Message) TryRemoveMapField(fd *desc.FieldDescriptor, key interface{}) error { |
| 1235 | if err := m.checkField(fd); err != nil { |
| 1236 | return err |
| 1237 | } |
| 1238 | return m.removeMapField(fd, key) |
| 1239 | } |
| 1240 | |
| 1241 | // RemoveMapFieldByName changes the value for the field with the given name by |
| 1242 | // removing any value associated with the given key. It panics if an error is |
| 1243 | // encountered. See TryRemoveMapFieldByName. |
| 1244 | func (m *Message) RemoveMapFieldByName(name string, key interface{}) { |
| 1245 | if err := m.TryRemoveMapFieldByName(name, key); err != nil { |
| 1246 | panic(err.Error()) |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | // TryRemoveMapFieldByName changes the value for the field with the given name |
| 1251 | // by removing any value associated with the given key. An error is returned if |
| 1252 | // the given name is unknown or if it names a field that is not a map. |
| 1253 | func (m *Message) TryRemoveMapFieldByName(name string, key interface{}) error { |
| 1254 | fd := m.FindFieldDescriptorByName(name) |
| 1255 | if fd == nil { |
| 1256 | return UnknownFieldNameError |
| 1257 | } |
| 1258 | return m.removeMapField(fd, key) |
| 1259 | } |
| 1260 | |
| 1261 | // RemoveMapFieldByNumber changes the value for the field with the given tag |
| 1262 | // number by removing any value associated with the given key. It panics if an |
| 1263 | // error is encountered. See TryRemoveMapFieldByNumber. |
| 1264 | func (m *Message) RemoveMapFieldByNumber(tagNumber int, key interface{}) { |
| 1265 | if err := m.TryRemoveMapFieldByNumber(tagNumber, key); err != nil { |
| 1266 | panic(err.Error()) |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | // TryRemoveMapFieldByNumber changes the value for the field with the given tag |
| 1271 | // number by removing any value associated with the given key. An error is |
| 1272 | // returned if the given tag is unknown or if it indicates a field that is not |
| 1273 | // a map. |
| 1274 | func (m *Message) TryRemoveMapFieldByNumber(tagNumber int, key interface{}) error { |
| 1275 | fd := m.FindFieldDescriptor(int32(tagNumber)) |
| 1276 | if fd == nil { |
| 1277 | return UnknownTagNumberError |
| 1278 | } |
| 1279 | return m.removeMapField(fd, key) |
| 1280 | } |
| 1281 | |
| 1282 | func (m *Message) removeMapField(fd *desc.FieldDescriptor, key interface{}) error { |
| 1283 | if !fd.IsMap() { |
| 1284 | return FieldIsNotMapError |
| 1285 | } |
| 1286 | kfd := fd.GetMessageType().GetFields()[0] |
| 1287 | ki, err := validElementFieldValue(kfd, key, false) |
| 1288 | if err != nil { |
| 1289 | return err |
| 1290 | } |
| 1291 | mp := m.values[fd.GetNumber()] |
| 1292 | if mp == nil { |
| 1293 | if mp, err = m.parseUnknownField(fd); err != nil { |
| 1294 | return err |
| 1295 | } else if mp == nil { |
| 1296 | return nil |
| 1297 | } |
| 1298 | } |
| 1299 | res := mp.(map[interface{}]interface{}) |
| 1300 | delete(res, ki) |
| 1301 | if len(res) == 0 { |
| 1302 | delete(m.values, fd.GetNumber()) |
| 1303 | } |
| 1304 | return nil |
| 1305 | } |
| 1306 | |
| 1307 | // FieldLength returns the number of elements in this message for the given |
| 1308 | // field descriptor. It panics if an error is encountered. See TryFieldLength. |
| 1309 | func (m *Message) FieldLength(fd *desc.FieldDescriptor) int { |
| 1310 | l, err := m.TryFieldLength(fd) |
| 1311 | if err != nil { |
| 1312 | panic(err.Error()) |
| 1313 | } |
| 1314 | return l |
| 1315 | } |
| 1316 | |
| 1317 | // TryFieldLength returns the number of elements in this message for the given |
| 1318 | // field descriptor. An error is returned if the given field descriptor does not |
| 1319 | // belong to the right message type or if it is neither a map field nor a |
| 1320 | // repeated field. |
| 1321 | func (m *Message) TryFieldLength(fd *desc.FieldDescriptor) (int, error) { |
| 1322 | if err := m.checkField(fd); err != nil { |
| 1323 | return 0, err |
| 1324 | } |
| 1325 | return m.fieldLength(fd) |
| 1326 | } |
| 1327 | |
| 1328 | // FieldLengthByName returns the number of elements in this message for the |
| 1329 | // field with the given name. It panics if an error is encountered. See |
| 1330 | // TryFieldLengthByName. |
| 1331 | func (m *Message) FieldLengthByName(name string) int { |
| 1332 | l, err := m.TryFieldLengthByName(name) |
| 1333 | if err != nil { |
| 1334 | panic(err.Error()) |
| 1335 | } |
| 1336 | return l |
| 1337 | } |
| 1338 | |
| 1339 | // TryFieldLengthByName returns the number of elements in this message for the |
| 1340 | // field with the given name. An error is returned if the given name is unknown |
| 1341 | // or if the named field is neither a map field nor a repeated field. |
| 1342 | func (m *Message) TryFieldLengthByName(name string) (int, error) { |
| 1343 | fd := m.FindFieldDescriptorByName(name) |
| 1344 | if fd == nil { |
| 1345 | return 0, UnknownFieldNameError |
| 1346 | } |
| 1347 | return m.fieldLength(fd) |
| 1348 | } |
| 1349 | |
| 1350 | // FieldLengthByNumber returns the number of elements in this message for the |
| 1351 | // field with the given tag number. It panics if an error is encountered. See |
| 1352 | // TryFieldLengthByNumber. |
| 1353 | func (m *Message) FieldLengthByNumber(tagNumber int32) int { |
| 1354 | l, err := m.TryFieldLengthByNumber(tagNumber) |
| 1355 | if err != nil { |
| 1356 | panic(err.Error()) |
| 1357 | } |
| 1358 | return l |
| 1359 | } |
| 1360 | |
| 1361 | // TryFieldLengthByNumber returns the number of elements in this message for the |
| 1362 | // field with the given tag number. An error is returned if the given tag is |
| 1363 | // unknown or if the named field is neither a map field nor a repeated field. |
| 1364 | func (m *Message) TryFieldLengthByNumber(tagNumber int32) (int, error) { |
| 1365 | fd := m.FindFieldDescriptor(int32(tagNumber)) |
| 1366 | if fd == nil { |
| 1367 | return 0, UnknownTagNumberError |
| 1368 | } |
| 1369 | return m.fieldLength(fd) |
| 1370 | } |
| 1371 | |
| 1372 | func (m *Message) fieldLength(fd *desc.FieldDescriptor) (int, error) { |
| 1373 | if !fd.IsRepeated() { |
| 1374 | return 0, FieldIsNotRepeatedError |
| 1375 | } |
| 1376 | val := m.values[fd.GetNumber()] |
| 1377 | if val == nil { |
| 1378 | var err error |
| 1379 | if val, err = m.parseUnknownField(fd); err != nil { |
| 1380 | return 0, err |
| 1381 | } else if val == nil { |
| 1382 | return 0, nil |
| 1383 | } |
| 1384 | } |
| 1385 | if sl, ok := val.([]interface{}); ok { |
| 1386 | return len(sl), nil |
| 1387 | } else if mp, ok := val.(map[interface{}]interface{}); ok { |
| 1388 | return len(mp), nil |
| 1389 | } |
| 1390 | return 0, nil |
| 1391 | } |
| 1392 | |
| 1393 | // GetRepeatedField returns the value for the given repeated field descriptor at |
| 1394 | // the given index. It panics if an error is encountered. See |
| 1395 | // TryGetRepeatedField. |
| 1396 | func (m *Message) GetRepeatedField(fd *desc.FieldDescriptor, index int) interface{} { |
| 1397 | if v, err := m.TryGetRepeatedField(fd, index); err != nil { |
| 1398 | panic(err.Error()) |
| 1399 | } else { |
| 1400 | return v |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | // TryGetRepeatedField returns the value for the given repeated field descriptor |
| 1405 | // at the given index. An error is returned if the given field descriptor does |
| 1406 | // not belong to the right message type, if it is not a repeated field, or if |
| 1407 | // the given index is out of range (less than zero or greater than or equal to |
| 1408 | // the length of the repeated field). Also, even though map fields technically |
| 1409 | // are repeated fields, if the given field is a map field an error will result: |
| 1410 | // map representation does not lend itself to random access by index. |
| 1411 | // |
| 1412 | // The Go type of the value returned mirrors the type that protoc would generate |
| 1413 | // for the field's element type. (See TryGetField for more details on types). |
| 1414 | // |
| 1415 | // If the given field descriptor is not known (e.g. not present in the message |
| 1416 | // descriptor) but corresponds to an unknown field, the unknown value will be |
| 1417 | // parsed and become known. The value at the given index in the parsed value |
| 1418 | // will be returned. An error will be returned if the unknown value cannot be |
| 1419 | // parsed according to the field descriptor's type information. |
| 1420 | func (m *Message) TryGetRepeatedField(fd *desc.FieldDescriptor, index int) (interface{}, error) { |
| 1421 | if index < 0 { |
| 1422 | return nil, IndexOutOfRangeError |
| 1423 | } |
| 1424 | if err := m.checkField(fd); err != nil { |
| 1425 | return nil, err |
| 1426 | } |
| 1427 | return m.getRepeatedField(fd, index) |
| 1428 | } |
| 1429 | |
| 1430 | // GetRepeatedFieldByName returns the value for the repeated field with the |
| 1431 | // given name at the given index. It panics if an error is encountered. See |
| 1432 | // TryGetRepeatedFieldByName. |
| 1433 | func (m *Message) GetRepeatedFieldByName(name string, index int) interface{} { |
| 1434 | if v, err := m.TryGetRepeatedFieldByName(name, index); err != nil { |
| 1435 | panic(err.Error()) |
| 1436 | } else { |
| 1437 | return v |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | // TryGetRepeatedFieldByName returns the value for the repeated field with the |
| 1442 | // given name at the given index. An error is returned if the given name is |
| 1443 | // unknown, if it names a field that is not a repeated field (or is a map |
| 1444 | // field), or if the given index is out of range (less than zero or greater |
| 1445 | // than or equal to the length of the repeated field). |
| 1446 | // |
| 1447 | // (See TryGetField for more info on types.) |
| 1448 | func (m *Message) TryGetRepeatedFieldByName(name string, index int) (interface{}, error) { |
| 1449 | if index < 0 { |
| 1450 | return nil, IndexOutOfRangeError |
| 1451 | } |
| 1452 | fd := m.FindFieldDescriptorByName(name) |
| 1453 | if fd == nil { |
| 1454 | return nil, UnknownFieldNameError |
| 1455 | } |
| 1456 | return m.getRepeatedField(fd, index) |
| 1457 | } |
| 1458 | |
| 1459 | // GetRepeatedFieldByNumber returns the value for the repeated field with the |
| 1460 | // given tag number at the given index. It panics if an error is encountered. |
| 1461 | // See TryGetRepeatedFieldByNumber. |
| 1462 | func (m *Message) GetRepeatedFieldByNumber(tagNumber int, index int) interface{} { |
| 1463 | if v, err := m.TryGetRepeatedFieldByNumber(tagNumber, index); err != nil { |
| 1464 | panic(err.Error()) |
| 1465 | } else { |
| 1466 | return v |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | // TryGetRepeatedFieldByNumber returns the value for the repeated field with the |
| 1471 | // given tag number at the given index. An error is returned if the given tag is |
| 1472 | // unknown, if it indicates a field that is not a repeated field (or is a map |
| 1473 | // field), or if the given index is out of range (less than zero or greater than |
| 1474 | // or equal to the length of the repeated field). |
| 1475 | // |
| 1476 | // (See TryGetField for more info on types.) |
| 1477 | func (m *Message) TryGetRepeatedFieldByNumber(tagNumber int, index int) (interface{}, error) { |
| 1478 | if index < 0 { |
| 1479 | return nil, IndexOutOfRangeError |
| 1480 | } |
| 1481 | fd := m.FindFieldDescriptor(int32(tagNumber)) |
| 1482 | if fd == nil { |
| 1483 | return nil, UnknownTagNumberError |
| 1484 | } |
| 1485 | return m.getRepeatedField(fd, index) |
| 1486 | } |
| 1487 | |
| 1488 | func (m *Message) getRepeatedField(fd *desc.FieldDescriptor, index int) (interface{}, error) { |
| 1489 | if fd.IsMap() || !fd.IsRepeated() { |
| 1490 | return nil, FieldIsNotRepeatedError |
| 1491 | } |
| 1492 | sl := m.values[fd.GetNumber()] |
| 1493 | if sl == nil { |
| 1494 | var err error |
| 1495 | if sl, err = m.parseUnknownField(fd); err != nil { |
| 1496 | return nil, err |
| 1497 | } else if sl == nil { |
| 1498 | return nil, IndexOutOfRangeError |
| 1499 | } |
| 1500 | } |
| 1501 | res := sl.([]interface{}) |
| 1502 | if index >= len(res) { |
| 1503 | return nil, IndexOutOfRangeError |
| 1504 | } |
| 1505 | return res[index], nil |
| 1506 | } |
| 1507 | |
| 1508 | // AddRepeatedField appends the given value to the given repeated field. It |
| 1509 | // panics if an error is encountered. See TryAddRepeatedField. |
| 1510 | func (m *Message) AddRepeatedField(fd *desc.FieldDescriptor, val interface{}) { |
| 1511 | if err := m.TryAddRepeatedField(fd, val); err != nil { |
| 1512 | panic(err.Error()) |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | // TryAddRepeatedField appends the given value to the given repeated field. An |
| 1517 | // error is returned if the given field descriptor does not belong to the right |
| 1518 | // message type, if the given field is not repeated, or if the given value is |
| 1519 | // not a correct/compatible type for the given field. If the given field is a |
| 1520 | // map field, the call will succeed if the given value is an instance of the |
| 1521 | // map's entry message type. |
| 1522 | // |
| 1523 | // The Go type expected for a field is the same as required by TrySetField for |
| 1524 | // a non-repeated field of the same type. |
| 1525 | // |
| 1526 | // If the given field descriptor is not known (e.g. not present in the message |
| 1527 | // descriptor) it will become known. Subsequent operations using tag numbers or |
| 1528 | // names will be able to resolve the newly-known type. If the message has a |
| 1529 | // value for the unknown value, it is parsed and the given value is appended to |
| 1530 | // it. |
| 1531 | func (m *Message) TryAddRepeatedField(fd *desc.FieldDescriptor, val interface{}) error { |
| 1532 | if err := m.checkField(fd); err != nil { |
| 1533 | return err |
| 1534 | } |
| 1535 | return m.addRepeatedField(fd, val) |
| 1536 | } |
| 1537 | |
| 1538 | // AddRepeatedFieldByName appends the given value to the repeated field with the |
| 1539 | // given name. It panics if an error is encountered. See |
| 1540 | // TryAddRepeatedFieldByName. |
| 1541 | func (m *Message) AddRepeatedFieldByName(name string, val interface{}) { |
| 1542 | if err := m.TryAddRepeatedFieldByName(name, val); err != nil { |
| 1543 | panic(err.Error()) |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | // TryAddRepeatedFieldByName appends the given value to the repeated field with |
| 1548 | // the given name. An error is returned if the given name is unknown, if it |
| 1549 | // names a field that is not repeated, or if the given value has an incorrect |
| 1550 | // type. |
| 1551 | // |
| 1552 | // (See TrySetField for more info on types.) |
| 1553 | func (m *Message) TryAddRepeatedFieldByName(name string, val interface{}) error { |
| 1554 | fd := m.FindFieldDescriptorByName(name) |
| 1555 | if fd == nil { |
| 1556 | return UnknownFieldNameError |
| 1557 | } |
| 1558 | return m.addRepeatedField(fd, val) |
| 1559 | } |
| 1560 | |
| 1561 | // AddRepeatedFieldByNumber appends the given value to the repeated field with |
| 1562 | // the given tag number. It panics if an error is encountered. See |
| 1563 | // TryAddRepeatedFieldByNumber. |
| 1564 | func (m *Message) AddRepeatedFieldByNumber(tagNumber int, val interface{}) { |
| 1565 | if err := m.TryAddRepeatedFieldByNumber(tagNumber, val); err != nil { |
| 1566 | panic(err.Error()) |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | // TryAddRepeatedFieldByNumber appends the given value to the repeated field |
| 1571 | // with the given tag number. An error is returned if the given tag is unknown, |
| 1572 | // if it indicates a field that is not repeated, or if the given value has an |
| 1573 | // incorrect type. |
| 1574 | // |
| 1575 | // (See TrySetField for more info on types.) |
| 1576 | func (m *Message) TryAddRepeatedFieldByNumber(tagNumber int, val interface{}) error { |
| 1577 | fd := m.FindFieldDescriptor(int32(tagNumber)) |
| 1578 | if fd == nil { |
| 1579 | return UnknownTagNumberError |
| 1580 | } |
| 1581 | return m.addRepeatedField(fd, val) |
| 1582 | } |
| 1583 | |
| 1584 | func (m *Message) addRepeatedField(fd *desc.FieldDescriptor, val interface{}) error { |
| 1585 | if !fd.IsRepeated() { |
| 1586 | return FieldIsNotRepeatedError |
| 1587 | } |
| 1588 | val, err := validElementFieldValue(fd, val, false) |
| 1589 | if err != nil { |
| 1590 | return err |
| 1591 | } |
| 1592 | |
| 1593 | if fd.IsMap() { |
| 1594 | // We're lenient. Just as we allow setting a map field to a slice of entry messages, we also allow |
| 1595 | // adding entries one at a time (as if the field were a normal repeated field). |
| 1596 | msg := val.(proto.Message) |
| 1597 | dm, err := asDynamicMessage(msg, fd.GetMessageType(), m.mf) |
| 1598 | if err != nil { |
| 1599 | return err |
| 1600 | } |
| 1601 | k, err := dm.TryGetFieldByNumber(1) |
| 1602 | if err != nil { |
| 1603 | return err |
| 1604 | } |
| 1605 | v, err := dm.TryGetFieldByNumber(2) |
| 1606 | if err != nil { |
| 1607 | return err |
| 1608 | } |
| 1609 | return m.putMapField(fd, k, v) |
| 1610 | } |
| 1611 | |
| 1612 | sl := m.values[fd.GetNumber()] |
| 1613 | if sl == nil { |
| 1614 | if sl, err = m.parseUnknownField(fd); err != nil { |
| 1615 | return err |
| 1616 | } else if sl == nil { |
| 1617 | sl = []interface{}{} |
| 1618 | } |
| 1619 | } |
| 1620 | res := sl.([]interface{}) |
| 1621 | res = append(res, val) |
| 1622 | m.internalSetField(fd, res) |
| 1623 | return nil |
| 1624 | } |
| 1625 | |
| 1626 | // SetRepeatedField sets the value for the given repeated field descriptor and |
| 1627 | // given index to the given value. It panics if an error is encountered. See |
| 1628 | // SetRepeatedField. |
| 1629 | func (m *Message) SetRepeatedField(fd *desc.FieldDescriptor, index int, val interface{}) { |
| 1630 | if err := m.TrySetRepeatedField(fd, index, val); err != nil { |
| 1631 | panic(err.Error()) |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | // TrySetRepeatedField sets the value for the given repeated field descriptor |
| 1636 | // and given index to the given value. An error is returned if the given field |
| 1637 | // descriptor does not belong to the right message type, if the given field is |
| 1638 | // not repeated, or if the given value is not a correct/compatible type for the |
| 1639 | // given field. Also, even though map fields technically are repeated fields, if |
| 1640 | // the given field is a map field an error will result: map representation does |
| 1641 | // not lend itself to random access by index. |
| 1642 | // |
| 1643 | // The Go type expected for a field is the same as required by TrySetField for |
| 1644 | // a non-repeated field of the same type. |
| 1645 | // |
| 1646 | // If the given field descriptor is not known (e.g. not present in the message |
| 1647 | // descriptor) it will become known. Subsequent operations using tag numbers or |
| 1648 | // names will be able to resolve the newly-known type. If the message has a |
| 1649 | // value for the unknown value, it is parsed and the element at the given index |
| 1650 | // is replaced with the given value. |
| 1651 | func (m *Message) TrySetRepeatedField(fd *desc.FieldDescriptor, index int, val interface{}) error { |
| 1652 | if index < 0 { |
| 1653 | return IndexOutOfRangeError |
| 1654 | } |
| 1655 | if err := m.checkField(fd); err != nil { |
| 1656 | return err |
| 1657 | } |
| 1658 | return m.setRepeatedField(fd, index, val) |
| 1659 | } |
| 1660 | |
| 1661 | // SetRepeatedFieldByName sets the value for the repeated field with the given |
| 1662 | // name and given index to the given value. It panics if an error is |
| 1663 | // encountered. See TrySetRepeatedFieldByName. |
| 1664 | func (m *Message) SetRepeatedFieldByName(name string, index int, val interface{}) { |
| 1665 | if err := m.TrySetRepeatedFieldByName(name, index, val); err != nil { |
| 1666 | panic(err.Error()) |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | // TrySetRepeatedFieldByName sets the value for the repeated field with the |
| 1671 | // given name and the given index to the given value. An error is returned if |
| 1672 | // the given name is unknown, if it names a field that is not repeated (or is a |
| 1673 | // map field), or if the given value has an incorrect type. |
| 1674 | // |
| 1675 | // (See TrySetField for more info on types.) |
| 1676 | func (m *Message) TrySetRepeatedFieldByName(name string, index int, val interface{}) error { |
| 1677 | if index < 0 { |
| 1678 | return IndexOutOfRangeError |
| 1679 | } |
| 1680 | fd := m.FindFieldDescriptorByName(name) |
| 1681 | if fd == nil { |
| 1682 | return UnknownFieldNameError |
| 1683 | } |
| 1684 | return m.setRepeatedField(fd, index, val) |
| 1685 | } |
| 1686 | |
| 1687 | // SetRepeatedFieldByNumber sets the value for the repeated field with the given |
| 1688 | // tag number and given index to the given value. It panics if an error is |
| 1689 | // encountered. See TrySetRepeatedFieldByNumber. |
| 1690 | func (m *Message) SetRepeatedFieldByNumber(tagNumber int, index int, val interface{}) { |
| 1691 | if err := m.TrySetRepeatedFieldByNumber(tagNumber, index, val); err != nil { |
| 1692 | panic(err.Error()) |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | // TrySetRepeatedFieldByNumber sets the value for the repeated field with the |
| 1697 | // given tag number and the given index to the given value. An error is returned |
| 1698 | // if the given tag is unknown, if it indicates a field that is not repeated (or |
| 1699 | // is a map field), or if the given value has an incorrect type. |
| 1700 | // |
| 1701 | // (See TrySetField for more info on types.) |
| 1702 | func (m *Message) TrySetRepeatedFieldByNumber(tagNumber int, index int, val interface{}) error { |
| 1703 | if index < 0 { |
| 1704 | return IndexOutOfRangeError |
| 1705 | } |
| 1706 | fd := m.FindFieldDescriptor(int32(tagNumber)) |
| 1707 | if fd == nil { |
| 1708 | return UnknownTagNumberError |
| 1709 | } |
| 1710 | return m.setRepeatedField(fd, index, val) |
| 1711 | } |
| 1712 | |
| 1713 | func (m *Message) setRepeatedField(fd *desc.FieldDescriptor, index int, val interface{}) error { |
| 1714 | if fd.IsMap() || !fd.IsRepeated() { |
| 1715 | return FieldIsNotRepeatedError |
| 1716 | } |
| 1717 | val, err := validElementFieldValue(fd, val, false) |
| 1718 | if err != nil { |
| 1719 | return err |
| 1720 | } |
| 1721 | sl := m.values[fd.GetNumber()] |
| 1722 | if sl == nil { |
| 1723 | if sl, err = m.parseUnknownField(fd); err != nil { |
| 1724 | return err |
| 1725 | } else if sl == nil { |
| 1726 | return IndexOutOfRangeError |
| 1727 | } |
| 1728 | } |
| 1729 | res := sl.([]interface{}) |
| 1730 | if index >= len(res) { |
| 1731 | return IndexOutOfRangeError |
| 1732 | } |
| 1733 | res[index] = val |
| 1734 | return nil |
| 1735 | } |
| 1736 | |
| 1737 | // GetUnknownField gets the value(s) for the given unknown tag number. If this |
| 1738 | // message has no unknown fields with the given tag, nil is returned. |
| 1739 | func (m *Message) GetUnknownField(tagNumber int32) []UnknownField { |
| 1740 | if u, ok := m.unknownFields[tagNumber]; ok { |
| 1741 | return u |
| 1742 | } else { |
| 1743 | return nil |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | func (m *Message) parseUnknownField(fd *desc.FieldDescriptor) (interface{}, error) { |
| 1748 | unks, ok := m.unknownFields[fd.GetNumber()] |
| 1749 | if !ok { |
| 1750 | return nil, nil |
| 1751 | } |
| 1752 | var v interface{} |
| 1753 | var sl []interface{} |
| 1754 | var mp map[interface{}]interface{} |
| 1755 | if fd.IsMap() { |
| 1756 | mp = map[interface{}]interface{}{} |
| 1757 | } |
| 1758 | var err error |
| 1759 | for _, unk := range unks { |
| 1760 | var val interface{} |
| 1761 | if unk.Encoding == proto.WireBytes || unk.Encoding == proto.WireStartGroup { |
| 1762 | val, err = codec.DecodeLengthDelimitedField(fd, unk.Contents, m.mf) |
| 1763 | } else { |
| 1764 | val, err = codec.DecodeScalarField(fd, unk.Value) |
| 1765 | } |
| 1766 | if err != nil { |
| 1767 | return nil, err |
| 1768 | } |
| 1769 | if fd.IsMap() { |
| 1770 | newEntry := val.(*Message) |
| 1771 | kk, err := newEntry.TryGetFieldByNumber(1) |
| 1772 | if err != nil { |
| 1773 | return nil, err |
| 1774 | } |
| 1775 | vv, err := newEntry.TryGetFieldByNumber(2) |
| 1776 | if err != nil { |
| 1777 | return nil, err |
| 1778 | } |
| 1779 | mp[kk] = vv |
| 1780 | v = mp |
| 1781 | } else if fd.IsRepeated() { |
| 1782 | t := reflect.TypeOf(val) |
| 1783 | if t.Kind() == reflect.Slice && t != typeOfBytes { |
| 1784 | // append slices if we unmarshalled a packed repeated field |
| 1785 | newVals := val.([]interface{}) |
| 1786 | sl = append(sl, newVals...) |
| 1787 | } else { |
| 1788 | sl = append(sl, val) |
| 1789 | } |
| 1790 | v = sl |
| 1791 | } else { |
| 1792 | v = val |
| 1793 | } |
| 1794 | } |
| 1795 | m.internalSetField(fd, v) |
| 1796 | return v, nil |
| 1797 | } |
| 1798 | |
| 1799 | func validFieldValue(fd *desc.FieldDescriptor, val interface{}) (interface{}, error) { |
| 1800 | return validFieldValueForRv(fd, reflect.ValueOf(val)) |
| 1801 | } |
| 1802 | |
| 1803 | func validFieldValueForRv(fd *desc.FieldDescriptor, val reflect.Value) (interface{}, error) { |
| 1804 | if fd.IsMap() && val.Kind() == reflect.Map { |
| 1805 | return validFieldValueForMapField(fd, val) |
| 1806 | } |
| 1807 | |
| 1808 | if fd.IsRepeated() { // this will also catch map fields where given value was not a map |
| 1809 | if val.Kind() != reflect.Array && val.Kind() != reflect.Slice { |
| 1810 | if fd.IsMap() { |
| 1811 | return nil, fmt.Errorf("value for map field must be a map; instead was %v", val.Type()) |
| 1812 | } else { |
| 1813 | return nil, fmt.Errorf("value for repeated field must be a slice; instead was %v", val.Type()) |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | if fd.IsMap() { |
| 1818 | // value should be a slice of entry messages that we need convert into a map[interface{}]interface{} |
| 1819 | m := map[interface{}]interface{}{} |
| 1820 | for i := 0; i < val.Len(); i++ { |
| 1821 | e, err := validElementFieldValue(fd, val.Index(i).Interface(), false) |
| 1822 | if err != nil { |
| 1823 | return nil, err |
| 1824 | } |
| 1825 | msg := e.(proto.Message) |
| 1826 | dm, err := asDynamicMessage(msg, fd.GetMessageType(), nil) |
| 1827 | if err != nil { |
| 1828 | return nil, err |
| 1829 | } |
| 1830 | k, err := dm.TryGetFieldByNumber(1) |
| 1831 | if err != nil { |
| 1832 | return nil, err |
| 1833 | } |
| 1834 | v, err := dm.TryGetFieldByNumber(2) |
| 1835 | if err != nil { |
| 1836 | return nil, err |
| 1837 | } |
| 1838 | m[k] = v |
| 1839 | } |
| 1840 | return m, nil |
| 1841 | } |
| 1842 | |
| 1843 | // make a defensive copy while checking contents (also converts to []interface{}) |
| 1844 | s := make([]interface{}, val.Len()) |
| 1845 | for i := 0; i < val.Len(); i++ { |
| 1846 | ev := val.Index(i) |
| 1847 | if ev.Kind() == reflect.Interface { |
| 1848 | // unwrap it |
| 1849 | ev = reflect.ValueOf(ev.Interface()) |
| 1850 | } |
| 1851 | e, err := validElementFieldValueForRv(fd, ev, false) |
| 1852 | if err != nil { |
| 1853 | return nil, err |
| 1854 | } |
| 1855 | s[i] = e |
| 1856 | } |
| 1857 | |
| 1858 | return s, nil |
| 1859 | } |
| 1860 | |
| 1861 | return validElementFieldValueForRv(fd, val, false) |
| 1862 | } |
| 1863 | |
| 1864 | func asDynamicMessage(m proto.Message, md *desc.MessageDescriptor, mf *MessageFactory) (*Message, error) { |
| 1865 | if dm, ok := m.(*Message); ok { |
| 1866 | return dm, nil |
| 1867 | } |
| 1868 | dm := NewMessageWithMessageFactory(md, mf) |
| 1869 | if err := dm.mergeFrom(m); err != nil { |
| 1870 | return nil, err |
| 1871 | } |
| 1872 | return dm, nil |
| 1873 | } |
| 1874 | |
| 1875 | func validElementFieldValue(fd *desc.FieldDescriptor, val interface{}, allowNilMessage bool) (interface{}, error) { |
| 1876 | return validElementFieldValueForRv(fd, reflect.ValueOf(val), allowNilMessage) |
| 1877 | } |
| 1878 | |
| 1879 | func validElementFieldValueForRv(fd *desc.FieldDescriptor, val reflect.Value, allowNilMessage bool) (interface{}, error) { |
| 1880 | t := fd.GetType() |
| 1881 | if !val.IsValid() { |
| 1882 | return nil, typeError(fd, nil) |
| 1883 | } |
| 1884 | |
| 1885 | switch t { |
| 1886 | case descriptor.FieldDescriptorProto_TYPE_SFIXED32, |
| 1887 | descriptor.FieldDescriptorProto_TYPE_INT32, |
| 1888 | descriptor.FieldDescriptorProto_TYPE_SINT32, |
| 1889 | descriptor.FieldDescriptorProto_TYPE_ENUM: |
| 1890 | return toInt32(reflect.Indirect(val), fd) |
| 1891 | |
| 1892 | case descriptor.FieldDescriptorProto_TYPE_SFIXED64, |
| 1893 | descriptor.FieldDescriptorProto_TYPE_INT64, |
| 1894 | descriptor.FieldDescriptorProto_TYPE_SINT64: |
| 1895 | return toInt64(reflect.Indirect(val), fd) |
| 1896 | |
| 1897 | case descriptor.FieldDescriptorProto_TYPE_FIXED32, |
| 1898 | descriptor.FieldDescriptorProto_TYPE_UINT32: |
| 1899 | return toUint32(reflect.Indirect(val), fd) |
| 1900 | |
| 1901 | case descriptor.FieldDescriptorProto_TYPE_FIXED64, |
| 1902 | descriptor.FieldDescriptorProto_TYPE_UINT64: |
| 1903 | return toUint64(reflect.Indirect(val), fd) |
| 1904 | |
| 1905 | case descriptor.FieldDescriptorProto_TYPE_FLOAT: |
| 1906 | return toFloat32(reflect.Indirect(val), fd) |
| 1907 | |
| 1908 | case descriptor.FieldDescriptorProto_TYPE_DOUBLE: |
| 1909 | return toFloat64(reflect.Indirect(val), fd) |
| 1910 | |
| 1911 | case descriptor.FieldDescriptorProto_TYPE_BOOL: |
| 1912 | return toBool(reflect.Indirect(val), fd) |
| 1913 | |
| 1914 | case descriptor.FieldDescriptorProto_TYPE_BYTES: |
| 1915 | return toBytes(reflect.Indirect(val), fd) |
| 1916 | |
| 1917 | case descriptor.FieldDescriptorProto_TYPE_STRING: |
| 1918 | return toString(reflect.Indirect(val), fd) |
| 1919 | |
| 1920 | case descriptor.FieldDescriptorProto_TYPE_MESSAGE, |
| 1921 | descriptor.FieldDescriptorProto_TYPE_GROUP: |
| 1922 | m, err := asMessage(val, fd.GetFullyQualifiedName()) |
| 1923 | // check that message is correct type |
| 1924 | if err != nil { |
| 1925 | return nil, err |
| 1926 | } |
| 1927 | var msgType string |
| 1928 | if dm, ok := m.(*Message); ok { |
| 1929 | if allowNilMessage && dm == nil { |
| 1930 | // if dm == nil, we'll panic below, so early out if that is allowed |
| 1931 | // (only allowed for map values, to indicate an entry w/ no value) |
| 1932 | return m, nil |
| 1933 | } |
| 1934 | msgType = dm.GetMessageDescriptor().GetFullyQualifiedName() |
| 1935 | } else { |
| 1936 | msgType = proto.MessageName(m) |
| 1937 | } |
| 1938 | if msgType != fd.GetMessageType().GetFullyQualifiedName() { |
| 1939 | return nil, fmt.Errorf("message field %s requires value of type %s; received %s", fd.GetFullyQualifiedName(), fd.GetMessageType().GetFullyQualifiedName(), msgType) |
| 1940 | } |
| 1941 | return m, nil |
| 1942 | |
| 1943 | default: |
| 1944 | return nil, fmt.Errorf("unable to handle unrecognized field type: %v", fd.GetType()) |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | func toInt32(v reflect.Value, fd *desc.FieldDescriptor) (int32, error) { |
| 1949 | if v.Kind() == reflect.Int32 { |
| 1950 | return int32(v.Int()), nil |
| 1951 | } |
| 1952 | return 0, typeError(fd, v.Type()) |
| 1953 | } |
| 1954 | |
| 1955 | func toUint32(v reflect.Value, fd *desc.FieldDescriptor) (uint32, error) { |
| 1956 | if v.Kind() == reflect.Uint32 { |
| 1957 | return uint32(v.Uint()), nil |
| 1958 | } |
| 1959 | return 0, typeError(fd, v.Type()) |
| 1960 | } |
| 1961 | |
| 1962 | func toFloat32(v reflect.Value, fd *desc.FieldDescriptor) (float32, error) { |
| 1963 | if v.Kind() == reflect.Float32 { |
| 1964 | return float32(v.Float()), nil |
| 1965 | } |
| 1966 | return 0, typeError(fd, v.Type()) |
| 1967 | } |
| 1968 | |
| 1969 | func toInt64(v reflect.Value, fd *desc.FieldDescriptor) (int64, error) { |
| 1970 | if v.Kind() == reflect.Int64 || v.Kind() == reflect.Int || v.Kind() == reflect.Int32 { |
| 1971 | return v.Int(), nil |
| 1972 | } |
| 1973 | return 0, typeError(fd, v.Type()) |
| 1974 | } |
| 1975 | |
| 1976 | func toUint64(v reflect.Value, fd *desc.FieldDescriptor) (uint64, error) { |
| 1977 | if v.Kind() == reflect.Uint64 || v.Kind() == reflect.Uint || v.Kind() == reflect.Uint32 { |
| 1978 | return v.Uint(), nil |
| 1979 | } |
| 1980 | return 0, typeError(fd, v.Type()) |
| 1981 | } |
| 1982 | |
| 1983 | func toFloat64(v reflect.Value, fd *desc.FieldDescriptor) (float64, error) { |
| 1984 | if v.Kind() == reflect.Float64 || v.Kind() == reflect.Float32 { |
| 1985 | return v.Float(), nil |
| 1986 | } |
| 1987 | return 0, typeError(fd, v.Type()) |
| 1988 | } |
| 1989 | |
| 1990 | func toBool(v reflect.Value, fd *desc.FieldDescriptor) (bool, error) { |
| 1991 | if v.Kind() == reflect.Bool { |
| 1992 | return v.Bool(), nil |
| 1993 | } |
| 1994 | return false, typeError(fd, v.Type()) |
| 1995 | } |
| 1996 | |
| 1997 | func toBytes(v reflect.Value, fd *desc.FieldDescriptor) ([]byte, error) { |
| 1998 | if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 { |
| 1999 | return v.Bytes(), nil |
| 2000 | } |
| 2001 | return nil, typeError(fd, v.Type()) |
| 2002 | } |
| 2003 | |
| 2004 | func toString(v reflect.Value, fd *desc.FieldDescriptor) (string, error) { |
| 2005 | if v.Kind() == reflect.String { |
| 2006 | return v.String(), nil |
| 2007 | } |
| 2008 | return "", typeError(fd, v.Type()) |
| 2009 | } |
| 2010 | |
| 2011 | func typeError(fd *desc.FieldDescriptor, t reflect.Type) error { |
| 2012 | return fmt.Errorf( |
| 2013 | "%s field %s is not compatible with value of type %v", |
| 2014 | getTypeString(fd), fd.GetFullyQualifiedName(), t) |
| 2015 | } |
| 2016 | |
| 2017 | func getTypeString(fd *desc.FieldDescriptor) string { |
| 2018 | return strings.ToLower(fd.GetType().String()) |
| 2019 | } |
| 2020 | |
| 2021 | func asMessage(v reflect.Value, fieldName string) (proto.Message, error) { |
| 2022 | t := v.Type() |
| 2023 | // we need a pointer to a struct that implements proto.Message |
| 2024 | if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct || !t.Implements(typeOfProtoMessage) { |
| 2025 | return nil, fmt.Errorf("message field %s requires is not compatible with value of type %v", fieldName, v.Type()) |
| 2026 | } |
| 2027 | return v.Interface().(proto.Message), nil |
| 2028 | } |
| 2029 | |
| 2030 | // Reset resets this message to an empty message. It removes all values set in |
| 2031 | // the message. |
| 2032 | func (m *Message) Reset() { |
| 2033 | for k := range m.values { |
| 2034 | delete(m.values, k) |
| 2035 | } |
| 2036 | for k := range m.unknownFields { |
| 2037 | delete(m.unknownFields, k) |
| 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | // String returns this message rendered in compact text format. |
| 2042 | func (m *Message) String() string { |
| 2043 | b, err := m.MarshalText() |
| 2044 | if err != nil { |
| 2045 | panic(fmt.Sprintf("Failed to create string representation of message: %s", err.Error())) |
| 2046 | } |
| 2047 | return string(b) |
| 2048 | } |
| 2049 | |
| 2050 | // ProtoMessage is present to satisfy the proto.Message interface. |
| 2051 | func (m *Message) ProtoMessage() { |
| 2052 | } |
| 2053 | |
| 2054 | // ConvertTo converts this dynamic message into the given message. This is |
| 2055 | // shorthand for resetting then merging: |
| 2056 | // target.Reset() |
| 2057 | // m.MergeInto(target) |
| 2058 | func (m *Message) ConvertTo(target proto.Message) error { |
| 2059 | if err := m.checkType(target); err != nil { |
| 2060 | return err |
| 2061 | } |
| 2062 | |
| 2063 | target.Reset() |
| 2064 | return m.mergeInto(target, defaultDeterminism) |
| 2065 | } |
| 2066 | |
| 2067 | // ConvertToDeterministic converts this dynamic message into the given message. |
| 2068 | // It is just like ConvertTo, but it attempts to produce deterministic results. |
| 2069 | // That means that if the target is a generated message (not another dynamic |
| 2070 | // message) and the current runtime is unaware of any fields or extensions that |
| 2071 | // are present in m, they will be serialized into the target's unrecognized |
| 2072 | // fields deterministically. |
| 2073 | func (m *Message) ConvertToDeterministic(target proto.Message) error { |
| 2074 | if err := m.checkType(target); err != nil { |
| 2075 | return err |
| 2076 | } |
| 2077 | |
| 2078 | target.Reset() |
| 2079 | return m.mergeInto(target, true) |
| 2080 | } |
| 2081 | |
| 2082 | // ConvertFrom converts the given message into this dynamic message. This is |
| 2083 | // shorthand for resetting then merging: |
| 2084 | // m.Reset() |
| 2085 | // m.MergeFrom(target) |
| 2086 | func (m *Message) ConvertFrom(target proto.Message) error { |
| 2087 | if err := m.checkType(target); err != nil { |
| 2088 | return err |
| 2089 | } |
| 2090 | |
| 2091 | m.Reset() |
| 2092 | return m.mergeFrom(target) |
| 2093 | } |
| 2094 | |
| 2095 | // MergeInto merges this dynamic message into the given message. All field |
| 2096 | // values in this message will be set on the given message. For map fields, |
| 2097 | // entries are added to the given message (if the given message has existing |
| 2098 | // values for like keys, they are overwritten). For slice fields, elements are |
| 2099 | // added. |
| 2100 | // |
| 2101 | // If the given message has a different set of known fields, it is possible for |
| 2102 | // some known fields in this message to be represented as unknown fields in the |
| 2103 | // given message after merging, and vice versa. |
| 2104 | func (m *Message) MergeInto(target proto.Message) error { |
| 2105 | if err := m.checkType(target); err != nil { |
| 2106 | return err |
| 2107 | } |
| 2108 | return m.mergeInto(target, defaultDeterminism) |
| 2109 | } |
| 2110 | |
| 2111 | // MergeIntoDeterministic merges this dynamic message into the given message. |
| 2112 | // It is just like MergeInto, but it attempts to produce deterministic results. |
| 2113 | // That means that if the target is a generated message (not another dynamic |
| 2114 | // message) and the current runtime is unaware of any fields or extensions that |
| 2115 | // are present in m, they will be serialized into the target's unrecognized |
| 2116 | // fields deterministically. |
| 2117 | func (m *Message) MergeIntoDeterministic(target proto.Message) error { |
| 2118 | if err := m.checkType(target); err != nil { |
| 2119 | return err |
| 2120 | } |
| 2121 | return m.mergeInto(target, true) |
| 2122 | } |
| 2123 | |
| 2124 | // MergeFrom merges the given message into this dynamic message. All field |
| 2125 | // values in the given message will be set on this message. For map fields, |
| 2126 | // entries are added to this message (if this message has existing values for |
| 2127 | // like keys, they are overwritten). For slice fields, elements are added. |
| 2128 | // |
| 2129 | // If the given message has a different set of known fields, it is possible for |
| 2130 | // some known fields in that message to be represented as unknown fields in this |
| 2131 | // message after merging, and vice versa. |
| 2132 | func (m *Message) MergeFrom(source proto.Message) error { |
| 2133 | if err := m.checkType(source); err != nil { |
| 2134 | return err |
| 2135 | } |
| 2136 | return m.mergeFrom(source) |
| 2137 | } |
| 2138 | |
| 2139 | // Merge implements the proto.Merger interface so that dynamic messages are |
| 2140 | // compatible with the proto.Merge function. It delegates to MergeFrom but will |
| 2141 | // panic on error as the proto.Merger interface doesn't allow for returning an |
| 2142 | // error. |
| 2143 | // |
| 2144 | // Unlike nearly all other methods, this method can work if this message's type |
| 2145 | // is not defined (such as instantiating the message without using NewMessage). |
| 2146 | // This is strictly so that dynamic message's are compatible with the |
| 2147 | // proto.Clone function, which instantiates a new message via reflection (thus |
| 2148 | // its message descriptor will not be set) and than calls Merge. |
| 2149 | func (m *Message) Merge(source proto.Message) { |
| 2150 | if m.md == nil { |
| 2151 | // To support proto.Clone, initialize the descriptor from the source. |
| 2152 | if dm, ok := source.(*Message); ok { |
| 2153 | m.md = dm.md |
| 2154 | // also make sure the clone uses the same message factory and |
| 2155 | // extensions and also knows about the same extra fields (if any) |
| 2156 | m.mf = dm.mf |
| 2157 | m.er = dm.er |
| 2158 | m.extraFields = dm.extraFields |
| 2159 | } else if md, err := desc.LoadMessageDescriptorForMessage(source); err != nil { |
| 2160 | panic(err.Error()) |
| 2161 | } else { |
| 2162 | m.md = md |
| 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | if err := m.MergeFrom(source); err != nil { |
| 2167 | panic(err.Error()) |
| 2168 | } |
| 2169 | } |
| 2170 | |
| 2171 | func (m *Message) checkType(target proto.Message) error { |
| 2172 | if dm, ok := target.(*Message); ok { |
| 2173 | if dm.md.GetFullyQualifiedName() != m.md.GetFullyQualifiedName() { |
| 2174 | return fmt.Errorf("given message has wrong type: %q; expecting %q", dm.md.GetFullyQualifiedName(), m.md.GetFullyQualifiedName()) |
| 2175 | } |
| 2176 | return nil |
| 2177 | } |
| 2178 | |
| 2179 | msgName := proto.MessageName(target) |
| 2180 | if msgName != m.md.GetFullyQualifiedName() { |
| 2181 | return fmt.Errorf("given message has wrong type: %q; expecting %q", msgName, m.md.GetFullyQualifiedName()) |
| 2182 | } |
| 2183 | return nil |
| 2184 | } |
| 2185 | |
| 2186 | func (m *Message) mergeInto(pm proto.Message, deterministic bool) error { |
| 2187 | if dm, ok := pm.(*Message); ok { |
| 2188 | return dm.mergeFrom(m) |
| 2189 | } |
| 2190 | |
| 2191 | target := reflect.ValueOf(pm) |
| 2192 | if target.Kind() == reflect.Ptr { |
| 2193 | target = target.Elem() |
| 2194 | } |
| 2195 | |
| 2196 | // track tags for which the dynamic message has data but the given |
| 2197 | // message doesn't know about it |
| 2198 | unknownTags := map[int32]struct{}{} |
| 2199 | for tag := range m.values { |
| 2200 | unknownTags[tag] = struct{}{} |
| 2201 | } |
| 2202 | |
| 2203 | // check that we can successfully do the merge |
| 2204 | structProps := proto.GetProperties(reflect.TypeOf(pm).Elem()) |
| 2205 | for _, prop := range structProps.Prop { |
| 2206 | if prop.Tag == 0 { |
| 2207 | continue // one-of or special field (such as XXX_unrecognized, etc.) |
| 2208 | } |
| 2209 | tag := int32(prop.Tag) |
| 2210 | v, ok := m.values[tag] |
| 2211 | if !ok { |
| 2212 | continue |
| 2213 | } |
| 2214 | if unknownTags != nil { |
| 2215 | delete(unknownTags, tag) |
| 2216 | } |
| 2217 | f := target.FieldByName(prop.Name) |
| 2218 | ft := f.Type() |
| 2219 | val := reflect.ValueOf(v) |
| 2220 | if !canConvert(val, ft) { |
| 2221 | return fmt.Errorf("cannot convert %v to %v", val.Type(), ft) |
| 2222 | } |
| 2223 | } |
| 2224 | // check one-of fields |
| 2225 | for _, oop := range structProps.OneofTypes { |
| 2226 | prop := oop.Prop |
| 2227 | tag := int32(prop.Tag) |
| 2228 | v, ok := m.values[tag] |
| 2229 | if !ok { |
| 2230 | continue |
| 2231 | } |
| 2232 | if unknownTags != nil { |
| 2233 | delete(unknownTags, tag) |
| 2234 | } |
| 2235 | stf, ok := oop.Type.Elem().FieldByName(prop.Name) |
| 2236 | if !ok { |
| 2237 | return fmt.Errorf("one-of field indicates struct field name %s, but type %v has no such field", prop.Name, oop.Type.Elem()) |
| 2238 | } |
| 2239 | ft := stf.Type |
| 2240 | val := reflect.ValueOf(v) |
| 2241 | if !canConvert(val, ft) { |
| 2242 | return fmt.Errorf("cannot convert %v to %v", val.Type(), ft) |
| 2243 | } |
| 2244 | } |
| 2245 | // and check extensions, too |
| 2246 | for tag, ext := range proto.RegisteredExtensions(pm) { |
| 2247 | v, ok := m.values[tag] |
| 2248 | if !ok { |
| 2249 | continue |
| 2250 | } |
| 2251 | if unknownTags != nil { |
| 2252 | delete(unknownTags, tag) |
| 2253 | } |
| 2254 | ft := reflect.TypeOf(ext.ExtensionType) |
| 2255 | val := reflect.ValueOf(v) |
| 2256 | if !canConvert(val, ft) { |
| 2257 | return fmt.Errorf("cannot convert %v to %v", val.Type(), ft) |
| 2258 | } |
| 2259 | } |
| 2260 | |
| 2261 | // now actually perform the merge |
| 2262 | for _, prop := range structProps.Prop { |
| 2263 | v, ok := m.values[int32(prop.Tag)] |
| 2264 | if !ok { |
| 2265 | continue |
| 2266 | } |
| 2267 | f := target.FieldByName(prop.Name) |
| 2268 | if err := mergeVal(reflect.ValueOf(v), f, deterministic); err != nil { |
| 2269 | return err |
| 2270 | } |
| 2271 | } |
| 2272 | // merge one-ofs |
| 2273 | for _, oop := range structProps.OneofTypes { |
| 2274 | prop := oop.Prop |
| 2275 | tag := int32(prop.Tag) |
| 2276 | v, ok := m.values[tag] |
| 2277 | if !ok { |
| 2278 | continue |
| 2279 | } |
| 2280 | oov := reflect.New(oop.Type.Elem()) |
| 2281 | f := oov.Elem().FieldByName(prop.Name) |
| 2282 | if err := mergeVal(reflect.ValueOf(v), f, deterministic); err != nil { |
| 2283 | return err |
| 2284 | } |
| 2285 | target.Field(oop.Field).Set(oov) |
| 2286 | } |
| 2287 | // merge extensions, too |
| 2288 | for tag, ext := range proto.RegisteredExtensions(pm) { |
| 2289 | v, ok := m.values[tag] |
| 2290 | if !ok { |
| 2291 | continue |
| 2292 | } |
| 2293 | e := reflect.New(reflect.TypeOf(ext.ExtensionType)).Elem() |
| 2294 | if err := mergeVal(reflect.ValueOf(v), e, deterministic); err != nil { |
| 2295 | return err |
| 2296 | } |
| 2297 | if err := proto.SetExtension(pm, ext, e.Interface()); err != nil { |
| 2298 | // shouldn't happen since we already checked that the extension type was compatible above |
| 2299 | return err |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | // if we have fields that the given message doesn't know about, add to its unknown fields |
| 2304 | if len(unknownTags) > 0 { |
| 2305 | var b codec.Buffer |
| 2306 | b.SetDeterministic(deterministic) |
| 2307 | if deterministic { |
| 2308 | // if we need to emit things deterministically, sort the |
| 2309 | // extensions by their tag number |
| 2310 | sortedUnknownTags := make([]int32, 0, len(unknownTags)) |
| 2311 | for tag := range unknownTags { |
| 2312 | sortedUnknownTags = append(sortedUnknownTags, tag) |
| 2313 | } |
| 2314 | sort.Slice(sortedUnknownTags, func(i, j int) bool { |
| 2315 | return sortedUnknownTags[i] < sortedUnknownTags[j] |
| 2316 | }) |
| 2317 | for _, tag := range sortedUnknownTags { |
| 2318 | fd := m.FindFieldDescriptor(tag) |
| 2319 | if err := b.EncodeFieldValue(fd, m.values[tag]); err != nil { |
| 2320 | return err |
| 2321 | } |
| 2322 | } |
| 2323 | } else { |
| 2324 | for tag := range unknownTags { |
| 2325 | fd := m.FindFieldDescriptor(tag) |
| 2326 | if err := b.EncodeFieldValue(fd, m.values[tag]); err != nil { |
| 2327 | return err |
| 2328 | } |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | internal.SetUnrecognized(pm, b.Bytes()) |
| 2333 | } |
| 2334 | |
| 2335 | // finally, convey unknown fields into the given message by letting it unmarshal them |
| 2336 | // (this will append to its unknown fields if not known; if somehow the given message recognizes |
| 2337 | // a field even though the dynamic message did not, it will get correctly unmarshalled) |
| 2338 | if unknownTags != nil && len(m.unknownFields) > 0 { |
| 2339 | var b codec.Buffer |
| 2340 | _ = m.marshalUnknownFields(&b) |
| 2341 | _ = proto.UnmarshalMerge(b.Bytes(), pm) |
| 2342 | } |
| 2343 | |
| 2344 | return nil |
| 2345 | } |
| 2346 | |
| 2347 | func canConvert(src reflect.Value, target reflect.Type) bool { |
| 2348 | if src.Kind() == reflect.Interface { |
| 2349 | src = reflect.ValueOf(src.Interface()) |
| 2350 | } |
| 2351 | srcType := src.Type() |
| 2352 | // we allow convertible types instead of requiring exact types so that calling |
| 2353 | // code can, for example, assign an enum constant to an enum field. In that case, |
| 2354 | // one type is the enum type (a sub-type of int32) and the other may be the int32 |
| 2355 | // type. So we automatically do the conversion in that case. |
| 2356 | if srcType.ConvertibleTo(target) { |
| 2357 | return true |
| 2358 | } else if target.Kind() == reflect.Ptr && srcType.ConvertibleTo(target.Elem()) { |
| 2359 | return true |
| 2360 | } else if target.Kind() == reflect.Slice { |
| 2361 | if srcType.Kind() != reflect.Slice { |
| 2362 | return false |
| 2363 | } |
| 2364 | et := target.Elem() |
| 2365 | for i := 0; i < src.Len(); i++ { |
| 2366 | if !canConvert(src.Index(i), et) { |
| 2367 | return false |
| 2368 | } |
| 2369 | } |
| 2370 | return true |
| 2371 | } else if target.Kind() == reflect.Map { |
| 2372 | if srcType.Kind() != reflect.Map { |
| 2373 | return false |
| 2374 | } |
| 2375 | return canConvertMap(src, target) |
| 2376 | } else if srcType == typeOfDynamicMessage && target.Implements(typeOfProtoMessage) { |
| 2377 | z := reflect.Zero(target).Interface() |
| 2378 | msgType := proto.MessageName(z.(proto.Message)) |
| 2379 | return msgType == src.Interface().(*Message).GetMessageDescriptor().GetFullyQualifiedName() |
| 2380 | } else { |
| 2381 | return false |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | func mergeVal(src, target reflect.Value, deterministic bool) error { |
| 2386 | if src.Kind() == reflect.Interface && !src.IsNil() { |
| 2387 | src = src.Elem() |
| 2388 | } |
| 2389 | srcType := src.Type() |
| 2390 | targetType := target.Type() |
| 2391 | if srcType.ConvertibleTo(targetType) { |
| 2392 | if targetType.Implements(typeOfProtoMessage) && !target.IsNil() { |
| 2393 | Merge(target.Interface().(proto.Message), src.Convert(targetType).Interface().(proto.Message)) |
| 2394 | } else { |
| 2395 | target.Set(src.Convert(targetType)) |
| 2396 | } |
| 2397 | } else if targetType.Kind() == reflect.Ptr && srcType.ConvertibleTo(targetType.Elem()) { |
| 2398 | if !src.CanAddr() { |
| 2399 | target.Set(reflect.New(targetType.Elem())) |
| 2400 | target.Elem().Set(src.Convert(targetType.Elem())) |
| 2401 | } else { |
| 2402 | target.Set(src.Addr().Convert(targetType)) |
| 2403 | } |
| 2404 | } else if targetType.Kind() == reflect.Slice { |
| 2405 | l := target.Len() |
| 2406 | newL := l + src.Len() |
| 2407 | if target.Cap() < newL { |
| 2408 | // expand capacity of the slice and copy |
| 2409 | newSl := reflect.MakeSlice(targetType, newL, newL) |
| 2410 | for i := 0; i < target.Len(); i++ { |
| 2411 | newSl.Index(i).Set(target.Index(i)) |
| 2412 | } |
| 2413 | target.Set(newSl) |
| 2414 | } else { |
| 2415 | target.SetLen(newL) |
| 2416 | } |
| 2417 | for i := 0; i < src.Len(); i++ { |
| 2418 | dest := target.Index(l + i) |
| 2419 | if dest.Kind() == reflect.Ptr { |
| 2420 | dest.Set(reflect.New(dest.Type().Elem())) |
| 2421 | } |
| 2422 | if err := mergeVal(src.Index(i), dest, deterministic); err != nil { |
| 2423 | return err |
| 2424 | } |
| 2425 | } |
| 2426 | } else if targetType.Kind() == reflect.Map { |
| 2427 | return mergeMapVal(src, target, targetType, deterministic) |
| 2428 | } else if srcType == typeOfDynamicMessage && targetType.Implements(typeOfProtoMessage) { |
| 2429 | dm := src.Interface().(*Message) |
| 2430 | if target.IsNil() { |
| 2431 | target.Set(reflect.New(targetType.Elem())) |
| 2432 | } |
| 2433 | m := target.Interface().(proto.Message) |
| 2434 | if err := dm.mergeInto(m, deterministic); err != nil { |
| 2435 | return err |
| 2436 | } |
| 2437 | } else { |
| 2438 | return fmt.Errorf("cannot convert %v to %v", srcType, targetType) |
| 2439 | } |
| 2440 | return nil |
| 2441 | } |
| 2442 | |
| 2443 | func (m *Message) mergeFrom(pm proto.Message) error { |
| 2444 | if dm, ok := pm.(*Message); ok { |
| 2445 | // if given message is also a dynamic message, we merge differently |
| 2446 | for tag, v := range dm.values { |
| 2447 | fd := m.FindFieldDescriptor(tag) |
| 2448 | if fd == nil { |
| 2449 | fd = dm.FindFieldDescriptor(tag) |
| 2450 | } |
| 2451 | if err := mergeField(m, fd, v); err != nil { |
| 2452 | return err |
| 2453 | } |
| 2454 | } |
| 2455 | return nil |
| 2456 | } |
| 2457 | |
| 2458 | pmrv := reflect.ValueOf(pm) |
| 2459 | if pmrv.IsNil() { |
| 2460 | // nil is an empty message, so nothing to do |
| 2461 | return nil |
| 2462 | } |
| 2463 | |
| 2464 | // check that we can successfully do the merge |
| 2465 | src := pmrv.Elem() |
| 2466 | values := map[*desc.FieldDescriptor]interface{}{} |
| 2467 | props := proto.GetProperties(reflect.TypeOf(pm).Elem()) |
| 2468 | if props == nil { |
| 2469 | return fmt.Errorf("could not determine message properties to merge for %v", reflect.TypeOf(pm).Elem()) |
| 2470 | } |
| 2471 | |
| 2472 | // regular fields |
| 2473 | for _, prop := range props.Prop { |
| 2474 | if prop.Tag == 0 { |
| 2475 | continue // one-of or special field (such as XXX_unrecognized, etc.) |
| 2476 | } |
| 2477 | fd := m.FindFieldDescriptor(int32(prop.Tag)) |
| 2478 | if fd == nil { |
| 2479 | // Our descriptor has different fields than this message object. So |
| 2480 | // try to reflect on the message object's fields. |
| 2481 | md, err := desc.LoadMessageDescriptorForMessage(pm) |
| 2482 | if err != nil { |
| 2483 | return err |
| 2484 | } |
| 2485 | fd = md.FindFieldByNumber(int32(prop.Tag)) |
| 2486 | if fd == nil { |
| 2487 | return fmt.Errorf("message descriptor %q did not contain field for tag %d (%q)", md.GetFullyQualifiedName(), prop.Tag, prop.Name) |
| 2488 | } |
| 2489 | } |
| 2490 | rv := src.FieldByName(prop.Name) |
| 2491 | if (rv.Kind() == reflect.Ptr || rv.Kind() == reflect.Slice) && rv.IsNil() { |
| 2492 | continue |
| 2493 | } |
| 2494 | if v, err := validFieldValueForRv(fd, rv); err != nil { |
| 2495 | return err |
| 2496 | } else { |
| 2497 | values[fd] = v |
| 2498 | } |
| 2499 | } |
| 2500 | |
| 2501 | // one-of fields |
| 2502 | for _, oop := range props.OneofTypes { |
| 2503 | oov := src.Field(oop.Field).Elem() |
| 2504 | if !oov.IsValid() || oov.Type() != oop.Type { |
| 2505 | // this field is unset (in other words, one-of message field is not currently set to this option) |
| 2506 | continue |
| 2507 | } |
| 2508 | prop := oop.Prop |
| 2509 | rv := oov.Elem().FieldByName(prop.Name) |
| 2510 | fd := m.FindFieldDescriptor(int32(prop.Tag)) |
| 2511 | if fd == nil { |
| 2512 | // Our descriptor has different fields than this message object. So |
| 2513 | // try to reflect on the message object's fields. |
| 2514 | md, err := desc.LoadMessageDescriptorForMessage(pm) |
| 2515 | if err != nil { |
| 2516 | return err |
| 2517 | } |
| 2518 | fd = md.FindFieldByNumber(int32(prop.Tag)) |
| 2519 | if fd == nil { |
| 2520 | return fmt.Errorf("message descriptor %q did not contain field for tag %d (%q in one-of %q)", md.GetFullyQualifiedName(), prop.Tag, prop.Name, src.Type().Field(oop.Field).Name) |
| 2521 | } |
| 2522 | } |
| 2523 | if v, err := validFieldValueForRv(fd, rv); err != nil { |
| 2524 | return err |
| 2525 | } else { |
| 2526 | values[fd] = v |
| 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | // extension fields |
| 2531 | rexts, _ := proto.ExtensionDescs(pm) |
| 2532 | for _, ed := range rexts { |
| 2533 | v, _ := proto.GetExtension(pm, ed) |
| 2534 | if v == nil { |
| 2535 | continue |
| 2536 | } |
| 2537 | if ed.ExtensionType == nil { |
| 2538 | // unrecognized extension: we'll handle that below when we |
| 2539 | // handle other unrecognized fields |
| 2540 | continue |
| 2541 | } |
| 2542 | fd := m.er.FindExtension(m.md.GetFullyQualifiedName(), ed.Field) |
| 2543 | if fd == nil { |
| 2544 | var err error |
| 2545 | if fd, err = desc.LoadFieldDescriptorForExtension(ed); err != nil { |
| 2546 | return err |
| 2547 | } |
| 2548 | } |
| 2549 | if v, err := validFieldValue(fd, v); err != nil { |
| 2550 | return err |
| 2551 | } else { |
| 2552 | values[fd] = v |
| 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | // now actually perform the merge |
| 2557 | for fd, v := range values { |
| 2558 | if err := mergeField(m, fd, v); err != nil { |
| 2559 | return err |
| 2560 | } |
| 2561 | } |
| 2562 | |
| 2563 | data := internal.GetUnrecognized(pm) |
| 2564 | if len(data) > 0 { |
| 2565 | // ignore any error returned: pulling in unknown fields is best-effort |
| 2566 | _ = m.UnmarshalMerge(data) |
| 2567 | } |
| 2568 | |
| 2569 | return nil |
| 2570 | } |
| 2571 | |
| 2572 | // Validate checks that all required fields are present. It returns an error if any are absent. |
| 2573 | func (m *Message) Validate() error { |
| 2574 | missingFields := m.findMissingFields() |
| 2575 | if len(missingFields) == 0 { |
| 2576 | return nil |
| 2577 | } |
| 2578 | return fmt.Errorf("some required fields missing: %v", strings.Join(missingFields, ", ")) |
| 2579 | } |
| 2580 | |
| 2581 | func (m *Message) findMissingFields() []string { |
| 2582 | if m.md.IsProto3() { |
| 2583 | // proto3 does not allow required fields |
| 2584 | return nil |
| 2585 | } |
| 2586 | var missingFields []string |
| 2587 | for _, fd := range m.md.GetFields() { |
| 2588 | if fd.IsRequired() { |
| 2589 | if _, ok := m.values[fd.GetNumber()]; !ok { |
| 2590 | missingFields = append(missingFields, fd.GetName()) |
| 2591 | } |
| 2592 | } |
| 2593 | } |
| 2594 | return missingFields |
| 2595 | } |
| 2596 | |
| 2597 | // ValidateRecursive checks that all required fields are present and also |
| 2598 | // recursively validates all fields who are also messages. It returns an error |
| 2599 | // if any required fields, in this message or nested within, are absent. |
| 2600 | func (m *Message) ValidateRecursive() error { |
| 2601 | return m.validateRecursive("") |
| 2602 | } |
| 2603 | |
| 2604 | func (m *Message) validateRecursive(prefix string) error { |
| 2605 | if missingFields := m.findMissingFields(); len(missingFields) > 0 { |
| 2606 | for i := range missingFields { |
| 2607 | missingFields[i] = fmt.Sprintf("%s%s", prefix, missingFields[i]) |
| 2608 | } |
| 2609 | return fmt.Errorf("some required fields missing: %v", strings.Join(missingFields, ", ")) |
| 2610 | } |
| 2611 | |
| 2612 | for tag, fld := range m.values { |
| 2613 | fd := m.FindFieldDescriptor(tag) |
| 2614 | var chprefix string |
| 2615 | var md *desc.MessageDescriptor |
| 2616 | checkMsg := func(pm proto.Message) error { |
| 2617 | var dm *Message |
| 2618 | if d, ok := pm.(*Message); ok { |
| 2619 | dm = d |
| 2620 | } else if pm != nil { |
| 2621 | dm = m.mf.NewDynamicMessage(md) |
| 2622 | if err := dm.ConvertFrom(pm); err != nil { |
| 2623 | return nil |
| 2624 | } |
| 2625 | } |
| 2626 | if dm == nil { |
| 2627 | return nil |
| 2628 | } |
| 2629 | if err := dm.validateRecursive(chprefix); err != nil { |
| 2630 | return err |
| 2631 | } |
| 2632 | return nil |
| 2633 | } |
| 2634 | isMap := fd.IsMap() |
| 2635 | if isMap && fd.GetMapValueType().GetMessageType() != nil { |
| 2636 | md = fd.GetMapValueType().GetMessageType() |
| 2637 | mp := fld.(map[interface{}]interface{}) |
| 2638 | for k, v := range mp { |
| 2639 | chprefix = fmt.Sprintf("%s%s[%v].", prefix, getName(fd), k) |
| 2640 | if err := checkMsg(v.(proto.Message)); err != nil { |
| 2641 | return err |
| 2642 | } |
| 2643 | } |
| 2644 | } else if !isMap && fd.GetMessageType() != nil { |
| 2645 | md = fd.GetMessageType() |
| 2646 | if fd.IsRepeated() { |
| 2647 | sl := fld.([]interface{}) |
| 2648 | for i, v := range sl { |
| 2649 | chprefix = fmt.Sprintf("%s%s[%d].", prefix, getName(fd), i) |
| 2650 | if err := checkMsg(v.(proto.Message)); err != nil { |
| 2651 | return err |
| 2652 | } |
| 2653 | } |
| 2654 | } else { |
| 2655 | chprefix = fmt.Sprintf("%s%s.", prefix, getName(fd)) |
| 2656 | if err := checkMsg(fld.(proto.Message)); err != nil { |
| 2657 | return err |
| 2658 | } |
| 2659 | } |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | return nil |
| 2664 | } |
| 2665 | |
| 2666 | func getName(fd *desc.FieldDescriptor) string { |
| 2667 | if fd.IsExtension() { |
| 2668 | return fmt.Sprintf("(%s)", fd.GetFullyQualifiedName()) |
| 2669 | } else { |
| 2670 | return fd.GetName() |
| 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | // knownFieldTags return tags of present and recognized fields, in sorted order. |
| 2675 | func (m *Message) knownFieldTags() []int { |
| 2676 | if len(m.values) == 0 { |
| 2677 | return []int(nil) |
| 2678 | } |
| 2679 | |
| 2680 | keys := make([]int, len(m.values)) |
| 2681 | i := 0 |
| 2682 | for k := range m.values { |
| 2683 | keys[i] = int(k) |
| 2684 | i++ |
| 2685 | } |
| 2686 | |
| 2687 | sort.Ints(keys) |
| 2688 | return keys |
| 2689 | } |
| 2690 | |
| 2691 | // allKnownFieldTags return tags of present and recognized fields, including |
| 2692 | // those that are unset, in sorted order. This only includes extensions that are |
| 2693 | // present. Known but not-present extensions are not included in the returned |
| 2694 | // set of tags. |
| 2695 | func (m *Message) allKnownFieldTags() []int { |
| 2696 | fds := m.md.GetFields() |
| 2697 | keys := make([]int, 0, len(fds)+len(m.extraFields)) |
| 2698 | |
| 2699 | for k := range m.values { |
| 2700 | keys = append(keys, int(k)) |
| 2701 | } |
| 2702 | |
| 2703 | // also include known fields that are not present |
| 2704 | for _, fd := range fds { |
| 2705 | if _, ok := m.values[fd.GetNumber()]; !ok { |
| 2706 | keys = append(keys, int(fd.GetNumber())) |
| 2707 | } |
| 2708 | } |
| 2709 | for _, fd := range m.extraFields { |
| 2710 | if !fd.IsExtension() { // skip extensions that are not present |
| 2711 | if _, ok := m.values[fd.GetNumber()]; !ok { |
| 2712 | keys = append(keys, int(fd.GetNumber())) |
| 2713 | } |
| 2714 | } |
| 2715 | } |
| 2716 | |
| 2717 | sort.Ints(keys) |
| 2718 | return keys |
| 2719 | } |
| 2720 | |
| 2721 | // unknownFieldTags return tags of present but unrecognized fields, in sorted order. |
| 2722 | func (m *Message) unknownFieldTags() []int { |
| 2723 | if len(m.unknownFields) == 0 { |
| 2724 | return []int(nil) |
| 2725 | } |
| 2726 | keys := make([]int, len(m.unknownFields)) |
| 2727 | i := 0 |
| 2728 | for k := range m.unknownFields { |
| 2729 | keys[i] = int(k) |
| 2730 | i++ |
| 2731 | } |
| 2732 | sort.Ints(keys) |
| 2733 | return keys |
| 2734 | } |