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

util/gconv: After conversion, the interface{} type becomes *interface {} #3731

Closed
aundis opened this issue Aug 15, 2024 · 11 comments · Fixed by #3732
Closed

util/gconv: After conversion, the interface{} type becomes *interface {} #3731

aundis opened this issue Aug 15, 2024 · 11 comments · Fixed by #3732
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@aundis
Copy link

aundis commented Aug 15, 2024

Go version

go version go1.18 windows/amd64

GoFrame version

v2.7.2

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	req := map[string]any{
		"id": "123",
		"doc": map[string]any{
			"craft": nil,
		},
		"fields": []string{"_id"},
	}

	var args *UpdateByIdReq
	err := gconv.Struct(req, &args)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%T", args.Doc["craft"])
}

type UpdateByIdReq struct {
	Id     string                 `json:"id" v:"required|length:24,24#id长度错误"`
	Doc    map[string]interface{} `json:"doc" v:"required"`
	Fields []string               `json:"fields"`
}

What did you see happen?

输出 *interface {}

What did you expect to see?

应该是 interface{}

@aundis aundis added the bug It is confirmed a bug, but don't worry, we'll handle it. label Aug 15, 2024
@Issues-translate-bot Issues-translate-bot changed the title util/gconv: 转换之后interface{}类型变为 *interface {} util/gconv: After conversion, the interface{} type becomes *interface {} Aug 15, 2024
@wlynxg
Copy link
Contributor

wlynxg commented Aug 15, 2024

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	dataMap := map[string]any{
		"doc": map[string]any{
			"craft": nil,
		},
	}

	var args *Data
	err := gconv.Struct(dataMap, &args)
	if err != nil {
		panic(err)
	}
	fmt.Printf("args: %T\n", args.Doc["craft"])
	fmt.Printf("dataMap: %T\n", dataMap["doc"].(map[string]any)["craft"])
}

type Data struct {
	Doc map[string]interface{} `json:"doc"`
}

Shouldn't your expected output here be nil?

@AZ000000
Copy link

In Go, when you assign nil to a variable of type interface{}, that variable is actually an interface{} pointer to nil.
So, when you try to print ARGS. Doc["craft"], it appears as *interface{}, indicating that the value is nil, and the interface{} type holds a pointer to nil.

@aundis
Copy link
Author

aundis commented Aug 22, 2024

func main() {
	req := map[string]any{
		"id": "123",
		"doc": map[string]any{
			"craft": nil,
		},
		"fields": []string{"_id"},
	}

	var args *UpdateByIdReq
	err := gconv.Struct(req, &args)
	if err != nil {
		panic(err)
	}
	var abc interface{}
	abc = nil
	fmt.Printf("%T, %T", args.Doc["craft"], abc)
}

output

 *interface {}, <nil>

If I check args.Doc["craft"] == nil here, it returns false. Isn't this counterintuitive?

@wlynxg
Copy link
Contributor

wlynxg commented Aug 22, 2024

You should expect the output to be <nil>, right?

@aundis
Copy link
Author

aundis commented Aug 22, 2024

您应该期望输出是 ,对吧?<nil>

yes

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


You should expect the output to be , right? <nil>

yes

@wlynxg
Copy link
Contributor

wlynxg commented Aug 22, 2024

my fix should be correct

@aundis
Copy link
Author

aundis commented Aug 22, 2024

my fix should be correct

Is this a bug?

@wlynxg
Copy link
Contributor

wlynxg commented Aug 22, 2024

yes

@aundis
Copy link
Author

aundis commented Aug 22, 2024

In Go, when you assign nil to a variable of type interface{}, that variable is actually an interface{} pointer to nil. So, when you try to print ARGS. Doc["craft"], it appears as *interface{}, indicating that the value is nil, and the interface{} type holds a pointer to nil.

Thank you. My expected type is <nil>, so that it's easier to make subsequent checks, such as value == nil.

@aundis
Copy link
Author

aundis commented Aug 22, 2024

yes

Thank you

@gqcn gqcn closed this as completed in #3732 Sep 9, 2024
houseme added a commit that referenced this issue Sep 12, 2024
…hub.com:gogf/gf into feature/action-1.23

* 'feature/action-1.23' of github.com:gogf/gf:
  feat: version v2.7.3 (#3763)
  perf(util/gconv):  add cache logic to enhance performance (#3673)
  fix(contrib/drivers/pgsql): fix insert error when data struct field has nil in PgSQL (#3679)
  fix(util/gconv): #3731 map type name mismatch in switch case statement (#3732)
  fix(contrib/drivers/pgsql): #3671 fix invalid pgsql insert json type (#3742)
  refactor(nacos-registry): use official nacos sdk instead of the third-party nacos sdk (#3745)

* 'feature/action-1.23' of github.com:gogf/gf:
  feat: version v2.7.3 (#3763)
  perf(util/gconv):  add cache logic to enhance performance (#3673)
  fix(contrib/drivers/pgsql): fix insert error when data struct field has nil in PgSQL (#3679)
  fix(util/gconv): #3731 map type name mismatch in switch case statement (#3732)
  fix(contrib/drivers/pgsql): #3671 fix invalid pgsql insert json type (#3742)
  refactor(nacos-registry): use official nacos sdk instead of the third-party nacos sdk (#3745)
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants