From bfc87d23153e04a02b0a22465bf0e96bb039a83f Mon Sep 17 00:00:00 2001 From: Hunk Zhu Date: Sun, 22 Oct 2023 00:58:06 +0000 Subject: [PATCH 1/7] fix api and ctrl not working well. --- cmd/gf/internal/cmd/genpb/genpb.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/gf/internal/cmd/genpb/genpb.go b/cmd/gf/internal/cmd/genpb/genpb.go index e1c4f8836c..007b5d68bb 100644 --- a/cmd/gf/internal/cmd/genpb/genpb.go +++ b/cmd/gf/internal/cmd/genpb/genpb.go @@ -19,10 +19,10 @@ import ( type ( CGenPb struct{} CGenPbInput struct { - g.Meta `name:"pb" config:"{CGenPbConfig}" brief:"{CGenPbBrief}" eg:"{CGenPbEg}"` - Path string `name:"path" short:"p" dc:"protobuf file folder path" d:"manifest/protobuf"` - OutputApi string `name:"api" short:"a" dc:"output folder path storing generated go files of api" d:"api"` - OutputCtrl string `name:"ctrl" short:"c" dc:"output folder path storing generated go files of controller" d:"internal/controller"` + g.Meta `name:"pb" config:"{CGenPbConfig}" brief:"{CGenPbBrief}" eg:"{CGenPbEg}"` + Path string `name:"path" short:"p" dc:"protobuf file folder path" d:"manifest/protobuf"` + Api string `name:"api" short:"a" dc:"output folder path storing generated go files of api" d:"api"` + Ctrl string `name:"ctrl" short:"c" dc:"output folder path storing generated go files of controller" d:"internal/controller"` } CGenPbOutput struct{} ) @@ -68,20 +68,20 @@ func (c CGenPb) Pb(ctx context.Context, in CGenPbInput) (out *CGenPbOutput, err } } // output path checks. - outputApiPath := gfile.RealPath(in.OutputApi) + outputApiPath := gfile.RealPath(in.Api) if outputApiPath == "" { if isParsingPWD { outputApiPath = protoPath } else { - mlog.Fatalf(`output api folder "%s" does not exist`, in.OutputApi) + mlog.Fatalf(`output api folder "%s" does not exist`, in.Api) } } - outputCtrlPath := gfile.RealPath(in.OutputCtrl) + outputCtrlPath := gfile.RealPath(in.Ctrl) if outputCtrlPath == "" { if isParsingPWD { outputCtrlPath = "" } else { - mlog.Fatalf(`output controller folder "%s" does not exist`, in.OutputCtrl) + mlog.Fatalf(`output controller folder "%s" does not exist`, in.Ctrl) } } From d1a16463b503118a93d1c26a60b85d8a76257dc2 Mon Sep 17 00:00:00 2001 From: Hunk Zhu Date: Wed, 25 Oct 2023 13:34:43 +0000 Subject: [PATCH 2/7] fix a word --- os/gcmd/gcmd_command_object.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os/gcmd/gcmd_command_object.go b/os/gcmd/gcmd_command_object.go index e8bd991770..a558afdb98 100644 --- a/os/gcmd/gcmd_command_object.go +++ b/os/gcmd/gcmd_command_object.go @@ -144,7 +144,7 @@ func newCommandFromObjectMeta(object interface{}, name string) (command *Command if err = gconv.Scan(metaData, &command); err != nil { return } - // Name filed is necessary. + // Name field is necessary. if command.Name == "" { if name == "" { err = gerror.Newf( From ad9409f48d67fc99c2960a0c1665a71d37fea5e7 Mon Sep 17 00:00:00 2001 From: Hunk Zhu Date: Wed, 25 Oct 2023 15:00:58 +0000 Subject: [PATCH 3/7] Revert "fix api and ctrl not working well." This reverts commit bfc87d23153e04a02b0a22465bf0e96bb039a83f. --- cmd/gf/internal/cmd/genpb/genpb.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/gf/internal/cmd/genpb/genpb.go b/cmd/gf/internal/cmd/genpb/genpb.go index 007b5d68bb..e1c4f8836c 100644 --- a/cmd/gf/internal/cmd/genpb/genpb.go +++ b/cmd/gf/internal/cmd/genpb/genpb.go @@ -19,10 +19,10 @@ import ( type ( CGenPb struct{} CGenPbInput struct { - g.Meta `name:"pb" config:"{CGenPbConfig}" brief:"{CGenPbBrief}" eg:"{CGenPbEg}"` - Path string `name:"path" short:"p" dc:"protobuf file folder path" d:"manifest/protobuf"` - Api string `name:"api" short:"a" dc:"output folder path storing generated go files of api" d:"api"` - Ctrl string `name:"ctrl" short:"c" dc:"output folder path storing generated go files of controller" d:"internal/controller"` + g.Meta `name:"pb" config:"{CGenPbConfig}" brief:"{CGenPbBrief}" eg:"{CGenPbEg}"` + Path string `name:"path" short:"p" dc:"protobuf file folder path" d:"manifest/protobuf"` + OutputApi string `name:"api" short:"a" dc:"output folder path storing generated go files of api" d:"api"` + OutputCtrl string `name:"ctrl" short:"c" dc:"output folder path storing generated go files of controller" d:"internal/controller"` } CGenPbOutput struct{} ) @@ -68,20 +68,20 @@ func (c CGenPb) Pb(ctx context.Context, in CGenPbInput) (out *CGenPbOutput, err } } // output path checks. - outputApiPath := gfile.RealPath(in.Api) + outputApiPath := gfile.RealPath(in.OutputApi) if outputApiPath == "" { if isParsingPWD { outputApiPath = protoPath } else { - mlog.Fatalf(`output api folder "%s" does not exist`, in.Api) + mlog.Fatalf(`output api folder "%s" does not exist`, in.OutputApi) } } - outputCtrlPath := gfile.RealPath(in.Ctrl) + outputCtrlPath := gfile.RealPath(in.OutputCtrl) if outputCtrlPath == "" { if isParsingPWD { outputCtrlPath = "" } else { - mlog.Fatalf(`output controller folder "%s" does not exist`, in.Ctrl) + mlog.Fatalf(`output controller folder "%s" does not exist`, in.OutputCtrl) } } From 5b2bbbaf4c404fd74a7ba5d27423affe64df5c78 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Thu, 2 Nov 2023 17:05:27 +0800 Subject: [PATCH 4/7] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=E9=94=99=E8=AF=AFfiled=E6=94=B9=E4=B8=BAfield?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contrib/drivers/clickhouse/clickhouse.go | 2 +- contrib/drivers/mysql/mysql_model_test.go | 2 +- contrib/drivers/sqlite/sqlite_model_test.go | 2 +- contrib/drivers/sqlitecgo/sqlite_model_test.go | 2 +- database/gdb/gdb_core.go | 6 +++--- database/gdb/gdb_core_config.go | 6 +++--- database/gdb/gdb_model_select.go | 2 +- database/gdb/gdb_model_time.go | 12 ++++++------ database/gdb/gdb_type_result_scanlist.go | 6 +++--- util/gconv/gconv_scan.go | 6 +++--- 10 files changed, 23 insertions(+), 23 deletions(-) diff --git a/contrib/drivers/clickhouse/clickhouse.go b/contrib/drivers/clickhouse/clickhouse.go index 059e9afff8..33665c0ebd 100644 --- a/contrib/drivers/clickhouse/clickhouse.go +++ b/contrib/drivers/clickhouse/clickhouse.go @@ -163,7 +163,7 @@ func (d *Driver) TableFields(ctx context.Context, table string, schema ...string isNull = false fieldType = m["type"].String() ) - // in clickhouse , filed type like is Nullable(int) + // in clickhouse , field type like is Nullable(int) fieldsResult, _ := gregex.MatchString(`^Nullable\((.*?)\)`, fieldType) if len(fieldsResult) == 2 { isNull = true diff --git a/contrib/drivers/mysql/mysql_model_test.go b/contrib/drivers/mysql/mysql_model_test.go index 0aa8d494aa..230100f760 100644 --- a/contrib/drivers/mysql/mysql_model_test.go +++ b/contrib/drivers/mysql/mysql_model_test.go @@ -4118,7 +4118,7 @@ func Test_Model_Embedded_Filter(t *testing.T) { // Password string // Nickname string // CreateTime string -// NoneExistFiled string +// NoneExistField string // } // data := User{ // Id: 1, diff --git a/contrib/drivers/sqlite/sqlite_model_test.go b/contrib/drivers/sqlite/sqlite_model_test.go index cd260bc574..f39804f1ef 100644 --- a/contrib/drivers/sqlite/sqlite_model_test.go +++ b/contrib/drivers/sqlite/sqlite_model_test.go @@ -3726,7 +3726,7 @@ func Test_Model_Insert_KeyFieldNameMapping_Error(t *testing.T) { Password string Nickname string CreateTime string - NoneExistFiled string + NoneExistField string } data := User{ Id: 1, diff --git a/contrib/drivers/sqlitecgo/sqlite_model_test.go b/contrib/drivers/sqlitecgo/sqlite_model_test.go index c47bccd12b..8aa297e7a2 100644 --- a/contrib/drivers/sqlitecgo/sqlite_model_test.go +++ b/contrib/drivers/sqlitecgo/sqlite_model_test.go @@ -3765,7 +3765,7 @@ func Test_Model_Insert_KeyFieldNameMapping_Error(t *testing.T) { Password string Nickname string CreateTime string - NoneExistFiled string + NoneExistField string } data := User{ Id: 1, diff --git a/database/gdb/gdb_core.go b/database/gdb/gdb_core.go index 43db34735d..f4ac5f7817 100644 --- a/database/gdb/gdb_core.go +++ b/database/gdb/gdb_core.go @@ -785,7 +785,7 @@ func (c *Core) HasTable(name string) (bool, error) { return result.Bool(), nil } -// isSoftCreatedFieldName checks and returns whether given filed name is an automatic-filled created time. +// isSoftCreatedFieldName checks and returns whether given field name is an automatic-filled created time. func (c *Core) isSoftCreatedFieldName(fieldName string) bool { if fieldName == "" { return false @@ -794,9 +794,9 @@ func (c *Core) isSoftCreatedFieldName(fieldName string) bool { if utils.EqualFoldWithoutChars(fieldName, config.CreatedAt) { return true } - return gstr.InArray(append([]string{config.CreatedAt}, createdFiledNames...), fieldName) + return gstr.InArray(append([]string{config.CreatedAt}, createdFieldNames...), fieldName) } - for _, v := range createdFiledNames { + for _, v := range createdFieldNames { if utils.EqualFoldWithoutChars(fieldName, v) { return true } diff --git a/database/gdb/gdb_core_config.go b/database/gdb/gdb_core_config.go index 1b2edca5a8..149d2afc36 100644 --- a/database/gdb/gdb_core_config.go +++ b/database/gdb/gdb_core_config.go @@ -49,9 +49,9 @@ type ConfigNode struct { ExecTimeout time.Duration `json:"execTimeout"` // (Optional) Max exec time for dml. TranTimeout time.Duration `json:"tranTimeout"` // (Optional) Max exec time for a transaction. PrepareTimeout time.Duration `json:"prepareTimeout"` // (Optional) Max exec time for prepare operation. - CreatedAt string `json:"createdAt"` // (Optional) The filed name of table for automatic-filled created datetime. - UpdatedAt string `json:"updatedAt"` // (Optional) The filed name of table for automatic-filled updated datetime. - DeletedAt string `json:"deletedAt"` // (Optional) The filed name of table for automatic-filled updated datetime. + CreatedAt string `json:"createdAt"` // (Optional) The field name of table for automatic-filled created datetime. + UpdatedAt string `json:"updatedAt"` // (Optional) The field name of table for automatic-filled updated datetime. + DeletedAt string `json:"deletedAt"` // (Optional) The field name of table for automatic-filled updated datetime. TimeMaintainDisabled bool `json:"timeMaintainDisabled"` // (Optional) Disable the automatic time maintaining feature. } diff --git a/database/gdb/gdb_model_select.go b/database/gdb/gdb_model_select.go index c7f7239d3c..de19b8d623 100644 --- a/database/gdb/gdb_model_select.go +++ b/database/gdb/gdb_model_select.go @@ -306,7 +306,7 @@ func (m *Model) Scan(pointer interface{}, where ...interface{}) error { // Where("u1.id<2"). // ScanAndCount(&users, &count, false) func (m *Model) ScanAndCount(pointer interface{}, totalCount *int, useFieldForCount bool) (err error) { - // support Fileds with *, example: .Fileds("a.*, b.name"). Count sql is select count(1) from xxx + // support Fields with *, example: .Fields("a.*, b.name"). Count sql is select count(1) from xxx countModel := m.Clone() // If useFieldForCount is false, set the fields to a constant value of 1 for counting if !useFieldForCount { diff --git a/database/gdb/gdb_model_time.go b/database/gdb/gdb_model_time.go index cd3671fcc4..76333ba655 100644 --- a/database/gdb/gdb_model_time.go +++ b/database/gdb/gdb_model_time.go @@ -17,9 +17,9 @@ import ( ) var ( - createdFiledNames = []string{"created_at", "create_at"} // Default filed names of table for automatic-filled created datetime. - updatedFiledNames = []string{"updated_at", "update_at"} // Default filed names of table for automatic-filled updated datetime. - deletedFiledNames = []string{"deleted_at", "delete_at"} // Default filed names of table for automatic-filled deleted datetime. + createdFieldNames = []string{"created_at", "create_at"} // Default field names of table for automatic-filled created datetime. + updatedFieldNames = []string{"updated_at", "update_at"} // Default field names of table for automatic-filled updated datetime. + deletedFieldNames = []string{"deleted_at", "delete_at"} // Default field names of table for automatic-filled deleted datetime. ) // Unscoped disables the auto-update time feature for insert, update and delete options. @@ -47,7 +47,7 @@ func (m *Model) getSoftFieldNameCreated(schema string, table string) string { if config.CreatedAt != "" { return m.getSoftFieldName(schema, tableName, []string{config.CreatedAt}) } - return m.getSoftFieldName(schema, tableName, createdFiledNames) + return m.getSoftFieldName(schema, tableName, createdFieldNames) } // getSoftFieldNameUpdate checks and returns the field name for record updating time. @@ -68,7 +68,7 @@ func (m *Model) getSoftFieldNameUpdated(schema string, table string) (field stri if config.UpdatedAt != "" { return m.getSoftFieldName(schema, tableName, []string{config.UpdatedAt}) } - return m.getSoftFieldName(schema, tableName, updatedFiledNames) + return m.getSoftFieldName(schema, tableName, updatedFieldNames) } // getSoftFieldNameDelete checks and returns the field name for record deleting time. @@ -89,7 +89,7 @@ func (m *Model) getSoftFieldNameDeleted(schema string, table string) (field stri if config.DeletedAt != "" { return m.getSoftFieldName(schema, tableName, []string{config.DeletedAt}) } - return m.getSoftFieldName(schema, tableName, deletedFiledNames) + return m.getSoftFieldName(schema, tableName, deletedFieldNames) } // getSoftFieldName retrieves and returns the field name of the table for possible key. diff --git a/database/gdb/gdb_type_result_scanlist.go b/database/gdb/gdb_type_result_scanlist.go index 84210c07d7..d2d3228468 100644 --- a/database/gdb/gdb_type_result_scanlist.go +++ b/database/gdb/gdb_type_result_scanlist.go @@ -247,7 +247,7 @@ func doScanList(in doScanListInput) (err error) { relationBindToFieldName string // Eg: relationKV: id:uid -> uid ) if len(in.RelationFields) > 0 { - // The relation key string of table filed name and attribute name + // The relation key string of table field name and attribute name // can be joined with char '=' or ':'. array := gstr.SplitAndTrim(in.RelationFields, "=") if len(array) == 1 { @@ -363,11 +363,11 @@ func doScanList(in doScanListInput) (err error) { if in.RelationFields != "" && !relationBindToFieldNameChecked { relationFromAttrField = relationFromAttrValue.FieldByName(relationBindToFieldName) if !relationFromAttrField.IsValid() { - filedMap, _ := gstructs.FieldMap(gstructs.FieldMapInput{ + fieldMap, _ := gstructs.FieldMap(gstructs.FieldMapInput{ Pointer: relationFromAttrValue, RecursiveOption: gstructs.RecursiveOptionEmbeddedNoTag, }) - if key, _ := gutil.MapPossibleItemByKey(gconv.Map(filedMap), relationBindToFieldName); key == "" { + if key, _ := gutil.MapPossibleItemByKey(gconv.Map(fieldMap), relationBindToFieldName); key == "" { return gerror.NewCodef( gcode.CodeInvalidParameter, `cannot find possible related attribute name "%s" from given relation fields "%s"`, diff --git a/util/gconv/gconv_scan.go b/util/gconv/gconv_scan.go index b31c4a3b89..6187d079de 100644 --- a/util/gconv/gconv_scan.go +++ b/util/gconv/gconv_scan.go @@ -279,7 +279,7 @@ func doScanList( relationBindToFieldName string // Eg: relationKV: id:uid -> uid ) if len(relationFields) > 0 { - // The relation key string of table filed name and attribute name + // The relation key string of table field name and attribute name // can be joined with char '=' or ':'. array := utils.SplitAndTrim(relationFields, "=") if len(array) == 1 { @@ -396,12 +396,12 @@ func doScanList( relationFromAttrField = relationFromAttrValue.FieldByName(relationBindToFieldName) if !relationFromAttrField.IsValid() { var ( - filedMap, _ = gstructs.FieldMap(gstructs.FieldMapInput{ + fieldMap, _ = gstructs.FieldMap(gstructs.FieldMapInput{ Pointer: relationFromAttrValue, RecursiveOption: gstructs.RecursiveOptionEmbeddedNoTag, }) ) - if key, _ := utils.MapPossibleItemByKey(Map(filedMap), relationBindToFieldName); key == "" { + if key, _ := utils.MapPossibleItemByKey(Map(fieldMap), relationBindToFieldName); key == "" { return gerror.NewCodef( gcode.CodeInvalidParameter, `cannot find possible related attribute name "%s" from given relation fields "%s"`, From 3b46a925d956ec207904c7d9a9cae53e2c4e9c76 Mon Sep 17 00:00:00 2001 From: Hunk Zhu Date: Sat, 4 Nov 2023 13:54:49 +0000 Subject: [PATCH 5/7] fix gcmd tag name found --- os/gcmd/gcmd_command_object.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/os/gcmd/gcmd_command_object.go b/os/gcmd/gcmd_command_object.go index a558afdb98..8ad7e9b265 100644 --- a/os/gcmd/gcmd_command_object.go +++ b/os/gcmd/gcmd_command_object.go @@ -408,12 +408,14 @@ func mergeDefaultStructValue(data map[string]interface{}, pointer interface{}) e foundValue interface{} ) for _, field := range tagFields { - foundKey, foundValue = gutil.MapPossibleItemByKey(data, field.Name()) + foundKey, foundValue = gutil.MapPossibleItemByKey(data, field.Tag("name")) if foundKey == "" { data[field.Name()] = field.TagValue } else { if utils.IsEmpty(foundValue) { data[foundKey] = field.TagValue + } else { + data[field.Name()] = foundValue } } } From 8180bb875e98b86504cc78bf7f05da6ad0cb9c67 Mon Sep 17 00:00:00 2001 From: Hunk Zhu Date: Sat, 4 Nov 2023 15:43:27 +0000 Subject: [PATCH 6/7] improved --- os/gcmd/gcmd_command_object.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/os/gcmd/gcmd_command_object.go b/os/gcmd/gcmd_command_object.go index 8ad7e9b265..9c628aada7 100644 --- a/os/gcmd/gcmd_command_object.go +++ b/os/gcmd/gcmd_command_object.go @@ -406,9 +406,13 @@ func mergeDefaultStructValue(data map[string]interface{}, pointer interface{}) e var ( foundKey string foundValue interface{} + fieldName string ) for _, field := range tagFields { - foundKey, foundValue = gutil.MapPossibleItemByKey(data, field.Tag("name")) + if fieldName = field.Tag("name"); fieldName == "" { + fieldName = field.Name() + } + foundKey, foundValue = gutil.MapPossibleItemByKey(data, fieldName) if foundKey == "" { data[field.Name()] = field.TagValue } else { From 652ec0027ec0e848f73ddc86b21cb09b49341c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=B7=E4=BA=AE?= <739476267@qq.com> Date: Tue, 7 Nov 2023 11:17:14 +0000 Subject: [PATCH 7/7] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dgcmd=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E8=AF=BB=E5=8F=96name=20tag=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- os/gcmd/gcmd_command.go | 11 ++++++----- os/gcmd/gcmd_command_object.go | 9 ++++----- os/gcmd/gcmd_command_run.go | 8 +++++--- os/gcmd/gcmd_z_unit_feature_object1_test.go | 15 ++++++++++++++- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/os/gcmd/gcmd_command.go b/os/gcmd/gcmd_command.go index 557a7531c8..d3cfc2b78a 100644 --- a/os/gcmd/gcmd_command.go +++ b/os/gcmd/gcmd_command.go @@ -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 ( diff --git a/os/gcmd/gcmd_command_object.go b/os/gcmd/gcmd_command_object.go index 9c628aada7..ab9d3d7cb0 100644 --- a/os/gcmd/gcmd_command_object.go +++ b/os/gcmd/gcmd_command_object.go @@ -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( @@ -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 { diff --git a/os/gcmd/gcmd_command_run.go b/os/gcmd/gcmd_command_run.go index c3f6fd53cb..7aa61f95df 100644 --- a/os/gcmd/gcmd_command_run.go +++ b/os/gcmd/gcmd_command_run.go @@ -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 } diff --git a/os/gcmd/gcmd_z_unit_feature_object1_test.go b/os/gcmd/gcmd_z_unit_feature_object1_test.go index a80183ce25..a2845b327d 100644 --- a/os/gcmd/gcmd_z_unit_feature_object1_test.go +++ b/os/gcmd/gcmd_z_unit_feature_object1_test.go @@ -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 { @@ -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"}`) }) }