Skip to content

Commit

Permalink
Fix map decoding error
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickcping committed Nov 29, 2024
1 parent 76030a3 commit 36efe48
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions davinci/codec_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,21 @@ func (d MapCodec) DecodeValue(data []byte, v reflect.Value) error {
return fmt.Errorf("invalid map value to decode")
}

// Get the type of the map
typ := v.Type().Elem()

// Unmarshal the data into a map
var tempMap []interface{}
var tempMap map[string]interface{}
if err := json.Unmarshal(data, &tempMap); err != nil {
return err
}

for _, item := range tempMap {
valType := v.Type()
valKeyType := valType.Key()
valElemType := valType.Elem()
mapType := reflect.MapOf(valKeyType, valElemType)
vMap := reflect.MakeMapWithSize(mapType, 0)

for key, item := range tempMap {
// Create a new value of the map element type
elem := reflect.New(typ).Elem()
elem := reflect.New(valElemType).Elem()

// Convert the map value to a JSON byte map
jsonValueBytes, err := json.Marshal(item)
Expand All @@ -68,9 +71,12 @@ func (d MapCodec) DecodeValue(data []byte, v reflect.Value) error {
}

// Append the new element to the map
v.Set(reflect.Append(v, elem))
vMap.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(elem.Interface()))
}

// Set the map value
v.Set(vMap)

return nil
}

Expand Down

0 comments on commit 36efe48

Please sign in to comment.