Fix create table with struct missing columns #1938
|
@ -12,6 +12,7 @@ import (
|
|||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"xorm.io/xorm/caches"
|
||||
"xorm.io/xorm/convert"
|
||||
|
@ -143,8 +144,18 @@ func (parser *Parser) Parse(v reflect.Value) (*schemas.Table, error) {
|
|||
var hasCacheTag, hasNoCacheTag bool
|
||||
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
tag := t.Field(i).Tag
|
||||
var isUnexportField bool
|
||||
for _, c := range t.Field(i).Name {
|
||||
if unicode.IsLower(c) {
|
||||
isUnexportField = true
|
||||
}
|
||||
break
|
||||
}
|
||||
if isUnexportField {
|
||||
continue
|
||||
}
|
||||
|
||||
tag := t.Field(i).Tag
|
||||
ormTagStr := tag.Get(parser.identifier)
|
||||
var col *schemas.Column
|
||||
fieldValue := v.Field(i)
|
||||
|
@ -292,7 +303,6 @@ func (parser *Parser) Parse(v reflect.Value) (*schemas.Table, error) {
|
|||
}
|
||||
|
||||
table.AddColumn(col)
|
||||
|
||||
} // end for
|
||||
|
||||
if idFieldColName != "" && len(table.PrimaryKeys) == 0 {
|
||||
|
|
|
@ -53,7 +53,7 @@ func TestUnexportField(t *testing.T) {
|
|||
)
|
||||
|
||||
type VanilaStruct struct {
|
||||
private int
|
||||
private int // unexported fields will be ignored
|
||||
Public int
|
||||
}
|
||||
table, err := parser.Parse(reflect.ValueOf(new(VanilaStruct)))
|
||||
|
@ -67,18 +67,13 @@ func TestUnexportField(t *testing.T) {
|
|||
}
|
||||
|
||||
type TaggedStruct struct {
|
||||
private int `xorm:"private"`
|
||||
private int `xorm:"private"` // unexported fields will be ignored
|
||||
Public int `xorm:"-"`
|
||||
}
|
||||
table, err = parser.Parse(reflect.ValueOf(new(TaggedStruct)))
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "tagged_struct", table.Name)
|
||||
assert.EqualValues(t, 1, len(table.Columns()))
|
||||
|
||||
for _, col := range table.Columns() {
|
||||
assert.EqualValues(t, "private", col.Name)
|
||||
assert.NotEqual(t, "public", col.Name)
|
||||
}
|
||||
assert.EqualValues(t, 0, len(table.Columns()))
|
||||
}
|
||||
|
||||
func TestParseWithOtherIdentifier(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user