gf icon indicating copy to clipboard operation
gf copied to clipboard

util/gconv: gconv.Scan is not able to convert two maps with different types.

Open StrangeYear opened this issue 3 weeks ago • 2 comments

Go version

1.25

GoFrame version

2.9.6

Can this bug be reproduced with the latest release?

Option Yes

What did you do?


func TestGconv(t *testing.T) {
	extraStr := `
{
  "USD": {
    "CNY": 7
  }
}`
	extra := map[string]any{}
	err := json.Unmarshal([]byte(extraStr), &extra)
	if err != nil {
		t.Fatal(err)
	}
	fmt.Printf("extra type: %T\n", extra)
	fmt.Printf("extra USD type: %T\n", extra["USD"])
	fmt.Printf("extra: %v\n", extra)

	exchangeRate := make(ExchangeRate)
	err = gconv.Scan(extra, &exchangeRate)
	if err != nil {
		t.Fatal(err)
	}
	fmt.Printf("exchangeRate type: %T\n", exchangeRate)
	fmt.Printf("exchangeRate USD type: %T\n", exchangeRate["USD"])
	fmt.Printf("exchangeRate: %v\n", exchangeRate)
}

What did you see happen?

=== RUN   TestGconv
extra type: map[string]interface {}
extra USD type: map[string]interface {}
extra: map[USD:map[CNY:7]]
exchangeRate type: ExchangeRate
exchangeRate USD type: map[string]float64
exchangeRate: map[USD:map[]]
--- PASS: TestGconv (0.00s)
PASS

exchangeRate.USD.CNY is lose

What did you expect to see?

exchangeRate.USD.CNY = 7

StrangeYear avatar Dec 07 '25 09:12 StrangeYear

@StrangeYear

exchangeRate := make(ExchangeRate)
// ExchangeRate 类型的定义是什么?

wln32 avatar Dec 09 '25 00:12 wln32

@StrangeYear

exchangeRate := make(ExchangeRate) // ExchangeRate 类型的定义是什么?

map[string]map[string]float64

StrangeYear avatar Dec 09 '25 00:12 StrangeYear

@StrangeYear 暂时先用结构体代替吧,目前还不支持这种map套map的转换

type ExchangeRate struct {
	USD map[string]any
}

wln32 avatar Dec 14 '25 22:12 wln32