Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

Commit

Permalink
Fix find bit (#712)
Browse files Browse the repository at this point in the history
* fix find bit

* fix error on mysql
  • Loading branch information
lunny authored Sep 9, 2017
1 parent 8439b76 commit d9eb2f5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
12 changes: 12 additions & 0 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,15 @@ func convertInt(v interface{}) (int64, error) {
}
return 0, fmt.Errorf("unsupported type: %v", v)
}

func asBool(bs []byte) (bool, error) {
if len(bs) == 0 {
return false, nil
}
if bs[0] == 0x00 {
return false, nil
} else if bs[0] == 0x01 {
return true, nil
}
return strconv.ParseBool(string(bs))
}
2 changes: 1 addition & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func (session *Session) rows2Beans(rows *core.Rows, fields []string, fieldsCount
var newValue = newElemFunc(fields)
bean := newValue.Interface()
dataStruct := rValue(bean)

// handle beforeClosures
scanResults, err := session.row2Slice(rows, fields, fieldsCount, bean)
if err != nil {
Expand All @@ -312,7 +313,6 @@ func (session *Session) rows2Beans(rows *core.Rows, fields []string, fieldsCount
if err != nil {
return err
}

err = sliceValueSetFunc(&newValue, pk)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions session_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value,
case reflect.String:
fieldValue.SetString(string(data))
case reflect.Bool:
d := string(data)
v, err := strconv.ParseBool(d)
v, err := asBool(data)
if err != nil {
return fmt.Errorf("arg %v as bool: %s", key, err.Error())
}
Expand Down
30 changes: 27 additions & 3 deletions session_find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,34 @@ func TestFindMapPtrString(t *testing.T) {
userinfo := testEngine.TableMapper.Obj2Table("Userinfo")
var ids []map[string]*string
err := testEngine.Table(userinfo).Desc("id").Find(&ids)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
for _, record := range ids {
fmt.Println(record)
}
}

func TestFindBit(t *testing.T) {
type FindBitStruct struct {
Id int64
Msg bool `xorm:"bit"`
}

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

cnt, err := testEngine.Insert([]FindBitStruct{
{
Msg: false,
},
{
Msg: true,
},
})
assert.NoError(t, err)
assert.EqualValues(t, 2, cnt)

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

0 comments on commit d9eb2f5

Please sign in to comment.