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
2 changes: 1 addition & 1 deletion contrib/drivers/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion contrib/drivers/mysql/mysql_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion contrib/drivers/sqlite/sqlite_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion contrib/drivers/sqlitecgo/sqlite_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions database/gdb/gdb_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions database/gdb/gdb_core_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand Down
2 changes: 1 addition & 1 deletion database/gdb/gdb_model_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions database/gdb/gdb_model_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions database/gdb/gdb_type_result_scanlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"`,
Expand Down
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
7 changes: 6 additions & 1 deletion os/gcmd/gcmd_command_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down 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 @@ -414,6 +417,8 @@ func mergeDefaultStructValue(data map[string]interface{}, pointer interface{}) e
} else {
if utils.IsEmpty(foundValue) {
data[foundKey] = field.TagValue
} else {
data[field.Name()] = foundValue
}
}
}
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
6 changes: 3 additions & 3 deletions util/gconv/gconv_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"`,
Expand Down
Loading