ToStringMapString is unable to cast map[string][]interface{} and error is omitted
Open
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 4k
- Forks
- 337
- Avg merge
- 15m
- Merged PRs (30d)
- 1
Description
Here is my sample code. I tried to turn SampleReq into a map[string]string. However,map["array"] turns out to be empty. I checked the code and found ToStringE() is unable to handle a slice and the error is omitted in the call stack.
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/spf13/cast"
)
type SampleReq struct {
SampleString string `json:"string"`
IntArray []int32 `json:"array"`
}
func main() {
req := &SampleReq{
SampleString: "string",
IntArray: append(make([]int32, 0), 123),
}
reqStr, _ := json.Marshal(req)
d := json.NewDecoder(bytes.NewReader(reqStr))
d.UseNumber()
var mapResult map[string]interface{}
_ = d.Decode(&mapResult)
castResult := cast.ToStringMapString(mapResult)
fmt.Printf("before cast %+v, after cast:%+v", mapResult, castResult) // map["array"] is lost
// a temporary fix
b, _ := json.Marshal(mapResult["array"])
castResult["array"] = string(b)
}
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the sample program and inspect ToStringMapString and ToStringE, which the report identifies as the relevant entry points. Determine the expected representation for the slice value and ensure the resulting behavior is covered by a regression test, with conversion errors no longer silently omitted.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100