* * WriteMap * Map bytes format: [Pairtag][Pairlength][keyTag(L)V][valueTag(L)V] [Pairtag][Pairlength][T(L)V][T(L)V]... * Pairtag = MapFieldnumber << 3 | wiretype, wiertype = proto.BytesType * accpet val type: map[string]interface{} or map[int]interface{} or map[interface{}]interface{} */
(desc *proto.TypeDescriptor, val interface{}, cast bool, disallowUnknown bool, useFieldName bool)
| 371 | * accpet val type: map[string]interface{} or map[int]interface{} or map[interface{}]interface{} |
| 372 | */ |
| 373 | func (p *BinaryProtocol) WriteMap(desc *proto.TypeDescriptor, val interface{}, cast bool, disallowUnknown bool, useFieldName bool) error { |
| 374 | baseId := desc.BaseId() |
| 375 | MapKey := desc.Key() |
| 376 | MapValue := desc.Elem() |
| 377 | // check val is map[string]interface{} or map[int]interface{} or map[interface{}]interface{} |
| 378 | var vs map[string]interface{} |
| 379 | var vs2 map[int]interface{} |
| 380 | var vs3 map[interface{}]interface{} |
| 381 | var ok bool |
| 382 | |
| 383 | if vs, ok = val.(map[string]interface{}); !ok { |
| 384 | if vs2, ok = val.(map[int]interface{}); !ok { |
| 385 | if vs3, ok = val.(map[interface{}]interface{}); !ok { |
| 386 | return errDismatchPrimitive |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | NeedMessageLen := true |
| 391 | if vs != nil { |
| 392 | for k, v := range vs { |
| 393 | p.AppendTag(baseId, proto.BytesType) |
| 394 | var pos int |
| 395 | p.Buf, pos = AppendSpeculativeLength(p.Buf) |
| 396 | p.AppendTag(1, MapKey.WireType()) |
| 397 | p.WriteString(k) |
| 398 | p.AppendTag(2, MapValue.WireType()) |
| 399 | p.WriteBaseTypeWithDesc(MapValue, v, cast, NeedMessageLen, disallowUnknown, useFieldName) |
| 400 | p.Buf = FinishSpeculativeLength(p.Buf, pos) |
| 401 | } |
| 402 | } else if vs2 != nil { |
| 403 | for k, v := range vs2 { |
| 404 | p.AppendTag(baseId, proto.BytesType) |
| 405 | var pos int |
| 406 | p.Buf, pos = AppendSpeculativeLength(p.Buf) |
| 407 | p.AppendTag(1, MapKey.WireType()) |
| 408 | // notice: may have problem, when k is sfixed64/fixed64 or sfixed32/fixed32 there is no need to use varint |
| 409 | // we had better add more code to judge the type of k if write fast |
| 410 | // p.WriteInt64(int64(k)) |
| 411 | p.WriteBaseTypeWithDesc(MapKey, k, NeedMessageLen, cast, disallowUnknown, useFieldName) // the gerneral way |
| 412 | p.AppendTag(2, MapValue.WireType()) |
| 413 | p.WriteBaseTypeWithDesc(MapValue, v, NeedMessageLen, cast, disallowUnknown, useFieldName) |
| 414 | p.Buf = FinishSpeculativeLength(p.Buf, pos) |
| 415 | } |
| 416 | } else { |
| 417 | for k, v := range vs3 { |
| 418 | p.AppendTag(baseId, proto.BytesType) |
| 419 | var pos int |
| 420 | p.Buf, pos = AppendSpeculativeLength(p.Buf) |
| 421 | p.AppendTag(1, MapKey.WireType()) |
| 422 | p.WriteBaseTypeWithDesc(MapKey, k, NeedMessageLen, cast, disallowUnknown, useFieldName) // the gerneral way |
| 423 | p.AppendTag(2, MapValue.WireType()) |
| 424 | p.WriteBaseTypeWithDesc(MapValue, v, NeedMessageLen, cast, disallowUnknown, useFieldName) |
| 425 | p.Buf = FinishSpeculativeLength(p.Buf, pos) |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | return nil |
| 430 | } |
no test coverage detected