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

This commit is contained in:
Hunk Zhu 2023-11-08 21:26:51 +08:00 committed by GitHub
parent 5f5b82188c
commit 0b407c5e6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 54 additions and 33 deletions

View File

@ -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

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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
}

View File

@ -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.
}

View File

@ -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 {

View File

@ -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.

View File

@ -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"`,

View File

@ -43,6 +43,7 @@ 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.
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.

View File

@ -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(
@ -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(
@ -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
}
}
}

View File

@ -171,10 +171,12 @@ func (c *Command) reParse(ctx context.Context, parser *Parser) (*Parser, error)
if arg.IsArg {
continue
}
if arg.Short != "" {
optionKey = fmt.Sprintf(`%s,%s`, arg.Name, arg.Short)
} else {
optionKey = arg.Name
if arg.FieldName != "" {
optionKey += fmt.Sprintf(`,%s`, arg.FieldName)
}
if arg.Short != "" {
optionKey += fmt.Sprintf(`,%s`, arg.Short)
}
supportedOptions[optionKey] = !arg.Orphan
}

View File

@ -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"}`)
})
}

View File

@ -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"`,