Skip to content

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

@StrangeYear

Description

@StrangeYear

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIt is confirmed a bug, but don't worry, we'll handle it.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions