Skip to content

Commit

Permalink
fix some bug on extends find (go-xorm#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored Apr 29, 2018
1 parent d966376 commit e3550c7
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
20 changes: 19 additions & 1 deletion engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,25 @@ func (engine *Engine) idOfV(rv reflect.Value) (core.PK, error) {
pk := make([]interface{}, len(table.PrimaryKeys))
for i, col := range table.PKColumns() {
var err error
pkField := v.FieldByName(col.FieldName)

fieldName := col.FieldName
for {
parts := strings.SplitN(fieldName, ".", 2)
if len(parts) == 1 {
break
}

v = v.FieldByName(parts[0])
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return nil, ErrUnSupportedType
}
fieldName = parts[1]
}
fmt.Println(table.Name, fieldName, v)
pkField := v.FieldByName(fieldName)
switch pkField.Kind() {
case reflect.String:
pk[i], err = engine.idTypeAssertion(col, pkField.String())
Expand Down
72 changes: 72 additions & 0 deletions session_find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,5 +655,77 @@ func TestFindMapStringId(t *testing.T) {
cnt, err = testEngine.ID("1").Delete(new(FindMapDevice))
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
}

func TestFindExtends(t *testing.T) {
type FindExtendsB struct {
ID int64
}

type FindExtendsA struct {
FindExtendsB `xorm:"extends"`
}

assert.NoError(t, prepareEngine())
assertSync(t, new(FindExtendsA))

cnt, err := testEngine.Insert(&FindExtendsA{
FindExtendsB: FindExtendsB{},
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

cnt, err = testEngine.Insert(&FindExtendsA{
FindExtendsB: FindExtendsB{},
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

var results []FindExtendsA
err = testEngine.Find(&results)
assert.NoError(t, err)
assert.EqualValues(t, 2, len(results))
}

func TestFindExtends3(t *testing.T) {
type FindExtendsCC struct {
ID int64
Name string
}

type FindExtendsBB struct {
FindExtendsCC `xorm:"extends"`
}

type FindExtendsAA struct {
FindExtendsBB `xorm:"extends"`
}

assert.NoError(t, prepareEngine())
assertSync(t, new(FindExtendsAA))

cnt, err := testEngine.Insert(&FindExtendsAA{
FindExtendsBB: FindExtendsBB{
FindExtendsCC: FindExtendsCC{
Name: "cc1",
},
},
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

cnt, err = testEngine.Insert(&FindExtendsAA{
FindExtendsBB: FindExtendsBB{
FindExtendsCC: FindExtendsCC{
Name: "cc2",
},
},
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

var results []FindExtendsAA
err = testEngine.Find(&results)
assert.NoError(t, err)
assert.EqualValues(t, 2, len(results))
}

0 comments on commit e3550c7

Please sign in to comment.