mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
fix: the new RawFieldNames considers the tag with options. (#1663)
Co-authored-by: JinfaWang <wangjinfa@iie.ac.cn>
This commit is contained in:
parent
5c169f4f49
commit
93d524b797
@ -41,10 +41,25 @@ func RawFieldNames(in interface{}, postgresSql ...bool) []string {
|
|||||||
out = append(out, fmt.Sprintf("`%s`", fi.Name))
|
out = append(out, fmt.Sprintf("`%s`", fi.Name))
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if pg {
|
//get tag name with the tag opton, e.g.:
|
||||||
out = append(out, tagv)
|
//`db:"id"`
|
||||||
|
//`db:"id,type=char,length=16"`
|
||||||
|
//`db:",type=char,length=16"`
|
||||||
|
if strings.Contains(tagv, ",") {
|
||||||
|
tagv = strings.TrimSpace(strings.Split(tagv, ",")[0])
|
||||||
|
}
|
||||||
|
if tagv != "" {
|
||||||
|
if pg {
|
||||||
|
out = append(out, tagv)
|
||||||
|
} else {
|
||||||
|
out = append(out, fmt.Sprintf("`%s`", tagv))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
out = append(out, fmt.Sprintf("`%s`", tagv))
|
if pg {
|
||||||
|
out = append(out, fi.Name)
|
||||||
|
} else {
|
||||||
|
out = append(out, fmt.Sprintf("`%s`", fi.Name))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,3 +22,20 @@ func TestFieldNames(t *testing.T) {
|
|||||||
assert.Equal(t, expected, out)
|
assert.Equal(t, expected, out)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type mockedUserWithOptions struct {
|
||||||
|
ID string `db:"id" json:"id,omitempty"`
|
||||||
|
UserName string `db:"user_name,type=varchar,length=255" json:"userName,omitempty"`
|
||||||
|
Sex int `db:"sex" json:"sex,omitempty"`
|
||||||
|
UUID string `db:",type=varchar,length=16" uuid:"uuid,omitempty"`
|
||||||
|
Age int `db:"age" json:"age"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFieldNamesWithTagOptions(t *testing.T) {
|
||||||
|
t.Run("new", func(t *testing.T) {
|
||||||
|
var u mockedUserWithOptions
|
||||||
|
out := RawFieldNames(&u)
|
||||||
|
expected := []string{"`id`", "`user_name`", "`sex`", "`UUID`", "`age`"}
|
||||||
|
assert.Equal(t, expected, out)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user