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

fix "gf gen pb" api and ctrl not working well. #3076

Merged
merged 11 commits into from
Nov 8, 2023
Prev Previous commit
Next Next commit
fix: 修复gcmd无法读取name tag的问题
  • Loading branch information
hailaz committed Nov 7, 2023
commit 652ec0027ec0e848f73ddc86b21cb09b49341c72
11 changes: 6 additions & 5 deletions os/gcmd/gcmd_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ type FuncWithValue func(ctx context.Context, parser *Parser) (out interface{}, e

// Argument is the command value that are used by certain command.
type Argument struct {
Name string // Option name.
Short string // Option short.
Brief string // Brief info about this Option, which is used in help info.
IsArg bool // IsArg marks this argument taking value from command line argument instead of option.
Orphan bool // Whether this Option having or having no value bound to it.
Name string // Option name.
FieldName string // Option field name.
Short string // Option short.
Brief string // Brief info about this Option, which is used in help info.
IsArg bool // IsArg marks this argument taking value from command line argument instead of option.
Orphan bool // Whether this Option having or having no value bound to it.
}

var (
Expand Down
9 changes: 4 additions & 5 deletions os/gcmd/gcmd_command_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ func newArgumentsFromInput(object interface{}) (args []Argument, err error) {
}
if arg.Name == "" {
arg.Name = field.Name()
} else if arg.Name != field.Name() {
arg.FieldName = field.Name()
nameSet.Add(arg.FieldName)
}
if arg.Name == helpOptionName {
return nil, gerror.Newf(
Expand Down Expand Up @@ -406,13 +409,9 @@ func mergeDefaultStructValue(data map[string]interface{}, pointer interface{}) e
var (
foundKey string
foundValue interface{}
fieldName string
)
for _, field := range tagFields {
if fieldName = field.Tag("name"); fieldName == "" {
fieldName = field.Name()
}
foundKey, foundValue = gutil.MapPossibleItemByKey(data, fieldName)
foundKey, foundValue = gutil.MapPossibleItemByKey(data, field.Name())
if foundKey == "" {
data[field.Name()] = field.TagValue
} else {
Expand Down
8 changes: 5 additions & 3 deletions os/gcmd/gcmd_command_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,12 @@ func (c *Command) reParse(ctx context.Context, parser *Parser) (*Parser, error)
if arg.IsArg {
continue
}
optionKey = arg.Name
if arg.FieldName != "" {
optionKey += fmt.Sprintf(`,%s`, arg.FieldName)
}
if arg.Short != "" {
optionKey = fmt.Sprintf(`%s,%s`, arg.Name, arg.Short)
} else {
optionKey = arg.Name
optionKey += fmt.Sprintf(`,%s`, arg.Short)
}
supportedOptions[optionKey] = !arg.Orphan
}
Expand Down
15 changes: 14 additions & 1 deletion os/gcmd/gcmd_z_unit_feature_object1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type TestCmdObjectEnvOutput struct{}

type TestCmdObjectTestInput struct {
g.Meta `name:"test" usage:"root test" brief:"root test command" dc:"root test command description" ad:"root test command ad"`
Name string `v:"required" short:"n" orphan:"false" brief:"name for test command"`
Name string `name:"yourname" v:"required" short:"n" orphan:"false" brief:"name for test command" d:"tom"`
}

type TestCmdObjectTestOutput struct {
Expand Down Expand Up @@ -89,10 +89,23 @@ func Test_Command_NewFromObject_RunWithValue(t *testing.T) {
t.AssertNil(err)
t.Assert(cmd.Name, "root")

// test short name
os.Args = []string{"root", "test", "-n=john"}
value, err := cmd.RunWithValueError(ctx)
t.AssertNil(err)
t.Assert(value, `{"Content":"john"}`)

// test name tag name
os.Args = []string{"root", "test", "-yourname=hailaz"}
value1, err1 := cmd.RunWithValueError(ctx)
t.AssertNil(err1)
t.Assert(value1, `{"Content":"hailaz"}`)

// test default tag value
os.Args = []string{"root", "test"}
value2, err2 := cmd.RunWithValueError(ctx)
t.AssertNil(err2)
t.Assert(value2, `{"Content":"tom"}`)
})
}

Expand Down
Loading