Skip to content

Commit

Permalink
Merge pull request #342 from tmm1/sort-map-bool
Browse files Browse the repository at this point in the history
Support (*Encoder).SetSortMapKeys(true) on map[string]bool
  • Loading branch information
vmihailenco committed Oct 1, 2023
2 parents 96ec1ea + 690c1fa commit 2f6f7d2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions decode_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var errArrayStruct = errors.New("msgpack: number of fields in array-encoded stru
var (
mapStringStringPtrType = reflect.TypeOf((*map[string]string)(nil))
mapStringStringType = mapStringStringPtrType.Elem()
mapStringBoolPtrType = reflect.TypeOf((*map[string]bool)(nil))
mapStringBoolType = mapStringBoolPtrType.Elem()
)

var (
Expand Down
1 change: 1 addition & 0 deletions decode_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
var (
interfaceType = reflect.TypeOf((*interface{})(nil)).Elem()
stringType = reflect.TypeOf((*string)(nil)).Elem()
boolType = reflect.TypeOf((*bool)(nil)).Elem()
)

var valueDecoders []decoderFunc
Expand Down
1 change: 1 addition & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func (e *Encoder) resetWriter(w io.Writer) {
// SetSortMapKeys causes the Encoder to encode map keys in increasing order.
// Supported map types are:
// - map[string]string
// - map[string]bool
// - map[string]interface{}
func (e *Encoder) SetSortMapKeys(on bool) *Encoder {
if on {
Expand Down
46 changes: 46 additions & 0 deletions encode_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ func encodeMapValue(e *Encoder, v reflect.Value) error {
return nil
}

func encodeMapStringBoolValue(e *Encoder, v reflect.Value) error {
if v.IsNil() {
return e.EncodeNil()
}

if err := e.EncodeMapLen(v.Len()); err != nil {
return err
}

m := v.Convert(mapStringBoolType).Interface().(map[string]bool)
if e.flags&sortMapKeysFlag != 0 {
return e.encodeSortedMapStringBool(m)
}

for mk, mv := range m {
if err := e.EncodeString(mk); err != nil {
return err
}
if err := e.EncodeBool(mv); err != nil {
return err
}
}

return nil
}

func encodeMapStringStringValue(e *Encoder, v reflect.Value) error {
if v.IsNil() {
return e.EncodeNil()
Expand Down Expand Up @@ -113,6 +139,26 @@ func (e *Encoder) EncodeMapSorted(m map[string]interface{}) error {
return nil
}

func (e *Encoder) encodeSortedMapStringBool(m map[string]bool) error {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
err := e.EncodeString(k)
if err != nil {
return err
}
if err = e.EncodeBool(m[k]); err != nil {
return err
}
}

return nil
}

func (e *Encoder) encodeSortedMapStringString(m map[string]string) error {
keys := make([]string, 0, len(m))
for k := range m {
Expand Down
2 changes: 2 additions & 0 deletions encode_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ func _getEncoder(typ reflect.Type) encoderFunc {
switch typ.Elem() {
case stringType:
return encodeMapStringStringValue
case boolType:
return encodeMapStringBoolValue
case interfaceType:
return encodeMapStringInterfaceValue
}
Expand Down

0 comments on commit 2f6f7d2

Please sign in to comment.