Skip to content

Commit

Permalink
support for any maps in msgp.AppendIntf like in (*Writer).writeIntf (#…
Browse files Browse the repository at this point in the history
…340)

Signed-off-by: Eliott Bouhana <[email protected]>
  • Loading branch information
eliottness authored Feb 26, 2024
1 parent db2e247 commit 79cd8c5
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions msgp/write_bytes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package msgp

import (
"errors"
"math"
"reflect"
"time"
Expand Down Expand Up @@ -342,10 +343,10 @@ func AppendMapStrIntf(b []byte, m map[string]interface{}) ([]byte, error) {
// provided []byte. 'i' must be one of the following:
// - 'nil'
// - A bool, float, string, []byte, int, uint, or complex
// - A map[string]interface{} or map[string]string
// - A map[string]T where T is another supported type
// - A []T, where T is another supported type
// - A *T, where T is another supported type
// - A type that satisfieds the msgp.Marshaler interface
// - A type that satisfies the msgp.Marshaler interface
// - A type that satisfies the msgp.Extension interface
func AppendIntf(b []byte, i interface{}) ([]byte, error) {
if i == nil {
Expand Down Expand Up @@ -414,6 +415,21 @@ func AppendIntf(b []byte, i interface{}) ([]byte, error) {
var err error
v := reflect.ValueOf(i)
switch v.Kind() {
case reflect.Map:
if v.Type().Key().Kind() != reflect.String {
return b, errors.New("msgp: map keys must be strings")
}
ks := v.MapKeys()
b = AppendMapHeader(b, uint32(len(ks)))
for _, key := range ks {
val := v.MapIndex(key)
b = AppendString(b, key.String())
b, err = AppendIntf(b, val.Interface())
if err != nil {
return nil, err
}
}
return b, nil
case reflect.Array, reflect.Slice:
l := v.Len()
b = AppendArrayHeader(b, uint32(l))
Expand Down

0 comments on commit 79cd8c5

Please sign in to comment.