Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gconv.Struct: unexpected behaviour when conv to a string to list #4084

Open
QisFj opened this issue Dec 27, 2024 · 4 comments
Open

gconv.Struct: unexpected behaviour when conv to a string to list #4084

QisFj opened this issue Dec 27, 2024 · 4 comments
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. help wanted

Comments

@QisFj
Copy link

QisFj commented Dec 27, 2024

Go version

go version go1.23.4 darwin/arm64

GoFrame version

v2.8.2

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

func TestConv(t *testing.T) {
	type Param struct {
		List []int `json:"list"`
	}
	data := map[string]any{
		"list": "[a]",
	}
	var p Param
	err := gconv.Struct(data, &p)
	fmt.Println(err)

	fmt.Printf("%#v\n", p)
}

What did you see happen?

I got err==nil and List = [0]

What did you expect to see?

i expected got err != nil ; at least get len(List) == 0.

I guess the reason is gf treat the "[a]" as a object. and when trying to set an object to list, will set it as a element. like []any{"[a]"}; and then trying to conver []any{"[a]"} to a []int, then use the default value 0, since it can't parse "[a]" as a number.

more input output:

  • "[1, 2]" => []int{1, 2}
  • "[1, \"2\"]" => []int{0}

by the way, if the List is not []int, but []*int. It will return an error, which says failed to conv int to *int. I guess its another bug..

@QisFj QisFj added the bug It is confirmed a bug, but don't worry, we'll handle it. label Dec 27, 2024
@QisFj
Copy link
Author

QisFj commented Dec 27, 2024

By the way, i don't found any document says what will happend using gconv on a list.

Hope can add some document about it. Or if i missed out it. and i’m very thankful for anyone can paste a link here..

@gqcn gqcn self-assigned this Jan 22, 2025
@gqcn
Copy link
Member

gqcn commented Jan 22, 2025

Let me have a check.

@gqcn gqcn removed their assignment Jan 22, 2025
@gqcn
Copy link
Member

gqcn commented Jan 22, 2025

@QisFj Hello, I see the problem is that package gconv ignores the converting error and assigns the converting failed value (default type value) for struct attribute. This is the designed and expected behavior in older version of goframe.

We're planning improve such behavior by adding internal do* functions that do the real converting logic and return the error if failed other than current public converting functions that eat the error. The Scan function can call the do* functions to implement its converting. The do* functions were ready implemented in branch feat/v2.9.0.

Here's the code snips for improvement example:

  1. Scan would call Convert function to implement basic types converting:
    func Convert(fromValue interface{}, toTypeName string, extraParams ...interface{}) interface{} {
    )
  2. Convert would call Ints function to implement []int converting:
    return Ints(in.FromValue)
  3. Ints would call Int function to implement int converting:
    return []int{Int(any)}
  4. But the Int function would return default type value 0 if any error occurs during its internal converting, it so should replace Int function here with doInt function that would return any error during converting:
    func doInt64(any any) (int64, error) {
  5. So do the other types converting.

We would be very appreciated if any friend can submit such contribution to improve function Scan.

Copy link

Hello @QisFj. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it!
你好 @QisFj。我们喜欢您的提案/反馈,并希望您或其他社区成员通过拉取请求做出贡献。我们提前感谢您的贡献,并期待对其进行审查。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. help wanted
Projects
None yet
Development

No branches or pull requests

2 participants