-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Missing command-line argument. #3390
Comments
这个bug是在解析完参数给结构体字段赋值的时候导致的 |
This bug is caused when the parameters are parsed and assigned to the structure fields. |
我复测了几个BUG出来首先这是我的代码 type Command struct {
*gcmd.Command
}
var (
Test = cTest{}
)
type cTest struct {
g.Meta `name:"index" ad:"test"`
}
type cInput struct {
g.Meta `name:"index"`
Name string `short:"n" name:"age" brief:"我是name字段"`
Age string `short:"a" name:"name" brief:"我是age字段"`
}
type cOutput struct{}
func (c cTest) Index(ctx context.Context, in cInput) (out *cOutput, err error) {
g.Dump(in)
return
}
func main() {
root, err := gcmd.NewFromObject(Test)
if err != nil {
panic(err)
}
command := &Command{root}
command.Run(gctx.GetInitCtx())
} 下面贴测试结果 |
你输入的数据是成功解析了的,问题就是出现在你红线标记哪里,再给字段赋值的时候,由于你tag的值和结构体字段重名了,导致错误解析 |
The data you entered was parsed successfully. The problem is that when you assign a value to the field where the red line is marked, the value of your tag has the same name as the structure field, resulting in an incorrect parsing. |
经过又一轮调试,发现:scan 赋值的部分,顺序可能没有严格按照参数名进行匹配。 |
After another round of debugging, I found that the order of the scan assignment may not be strictly matched according to the parameter names. |
我正在试图重新实现这部分 |
I'm trying to reimplement this part |
以下是刚刚进一步精简出来的重现代码 package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
type cInput struct {
A string `short:"a" name:"aa" brief:"aa"`
B string `short:"b" name:"bb" brief:"bb"`
}
func main() {
data := map[string]interface{}{
"a": "aaa",
"aa": "bbb",
"b": "bbb",
"bb": "aaa",
}
p := cInput{}
err := gconv.Struct(data, &p)
if err != nil {
g.Dump(err)
} else {
g.Dump(p)
}
} 原因可能找到了,(以上代码测试方法一样) |
The following is the reproducible code that has just been further streamlined package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
type cInput struct {
A string `short:"a" name:"aa" brief:"aa"`
B string `short:"b" name:"bb" brief:"bb"`
}
func main() {
data := map[string]interface{}{
"a": "aaa",
"aa": "bbb",
"b": "bbb",
"bb": "aaa",
}
p := cInput{}
err := gconv.Struct(data, &p)
if err != nil {
g.Dump(err)
} else {
g.Dump(p)
}
} The reason may be found, (the above code test method is the same) |
您是框架核心开发者吗?我本想尝试提交 PR,但是对 gogf 的内部结构还不够熟悉,很多地方不清楚为什么那样实现,不敢改动。 |
Are you a framework core developer? I wanted to try submitting a PR, but I was not familiar enough with the internal structure of gogf. I didn’t know why it was implemented that way in many places, and I didn’t dare to change it. |
具体的实现
我跟你一样呢,不是核心开发者,只是无聊学习下而已,我目前已经实现的差不多了已经,测试用例都能跑过 |
specific implementation
I'm just like you. I'm not a core developer. I'm just learning out of boredom. I've implemented almost everything so far, and I can run all the test cases. |
测试用例可能需要再针对新的补充一点,把这次的问题发现都包含进去。感谢您帮助我一起发现了这个问题!👍 |
The test cases may need to be supplemented with new ones to include all the problems discovered this time. Thanks for helping me figure this out! 👍 |
我目前是通过为所有参数附上默认值来避开这个问题的。 |
I currently get around this problem by attaching default values to all parameters. |
我看看呢。 |
Let me take a look. |
What version of
Go
and system type/arch are you using?Go Version: go1.21.4 linux/amd64
What version of
GoFrame
are you using?require github.com/gogf/gf/v2 v2.6.4
Can this bug be re-produced with the latest release?
Yes.
What did you do?
To facilitate comparison, it is recommended to use the command
gf run ./main.go -a "-a aaa -b bbb"
for debugging purposes.为了方便对比,建议使用
gf run ./main.go -a "-a aaa -b bbb"
命令进行调试操作。What did you expect to see?
Modifying
B
incInput
toBe
or any other content will cause the loss of the parameter valuebbb
.Modifying the short options corresponding to
A
andB
incInput
such thatA
corresponds tob
, andB
corresponds toa
, would result in the parameter valueaaa
still being associated withA
, while the parameterbbb
remains withB
.修改 cInput 中的
B
为Be
或其它内容,将丢失参数值bbb
。修改 cInput 中的
A
,B
对应的 short,使其调换 为A
对应b
,B
对应a
,参数值aaa
仍在A
上,参数bbb
仍在B
上。What did you see instead?
After modifying
B
incInput
toBe
or any other content, the original parameter valuebbb
that could be received should still be accepted without being affected.When modifying the short options corresponding to
A
andB
incInput
such thatA
corresponds tob
, andB
corresponds toa
, the parameter valueaaa
would now be associated withB
, while the parameterbbb
is onA
.修改 cInput 中的
B
为Be
或其它内容后,原来可以接收的参数值bbb
,仍然可以顺利接收,不受影响。修改 cInput 中的
A
,B
对应的 short,使其调换 为A
对应b
,B
对应a
,参数值aaa
在B
上,参数bbb
在A
上。The text was updated successfully, but these errors were encountered: