diff --git a/helpers.go b/helpers.go index 13587f9e..324c5bea 100644 --- a/helpers.go +++ b/helpers.go @@ -194,57 +194,31 @@ func isArrayValueZero(v reflect.Value) bool { return true } -func int64ToIntValue(id int64, tv *reflect.Value) reflect.Value { +func int64ToIntValue(id int64, tp reflect.Type) reflect.Value { var v interface{} - - switch tv.Interface().(type) { - case int16: + switch tp.Kind() { + case reflect.Int16: v = int16(id) - case int32: + case reflect.Int32: v = int32(id) - case int: + case reflect.Int: v = int(id) - case int64: - v = int64(id) - case uint16: + case reflect.Int64: + v = id + case reflect.Uint16: v = uint16(id) - case uint32: + case reflect.Uint32: v = uint32(id) - case uint64: + case reflect.Uint64: v = uint64(id) - case uint: + case reflect.Uint: v = uint(id) - case *int16: - temp := int16(id) - v = &temp - case *int32: - temp := int32(id) - v = &temp - case *int: - temp := int(id) - v = &temp - case *int64: - temp := int64(id) - v = &temp - case *uint16: - temp := uint16(id) - v = &temp - case *uint32: - temp := uint32(id) - v = &temp - case *uint: - temp := uint(id) - v = &temp - case *uint64: - temp := uint64(id) - v = &temp } - - return reflect.ValueOf(v).Convert(tv.Type()) + return reflect.ValueOf(v).Convert(tp) } -func int64ToInt(id int64, tv *reflect.Value) interface{} { - return int64ToIntValue(id, tv).Interface() +func int64ToInt(id int64, tp reflect.Type) interface{} { + return int64ToIntValue(id, tp).Interface() } func isPKZero(pk core.PK) bool { diff --git a/session_insert.go b/session_insert.go index 4b2b157b..2c8ad782 100644 --- a/session_insert.go +++ b/session_insert.go @@ -424,7 +424,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { return 1, nil } - aiValue.Set(int64ToIntValue(id, aiValue)) + aiValue.Set(int64ToIntValue(id, aiValue.Type())) return 1, nil } else if session.Engine.dialect.DBType() == core.POSTGRES && len(table.AutoIncrement) > 0 { @@ -469,7 +469,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { return 1, nil } - aiValue.Set(int64ToIntValue(id, aiValue)) + aiValue.Set(int64ToIntValue(id, aiValue.Type())) return 1, nil } else { @@ -512,7 +512,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { return res.RowsAffected() } - aiValue.Set(int64ToIntValue(id, aiValue)) + aiValue.Set(int64ToIntValue(id, aiValue.Type())) return res.RowsAffected() } diff --git a/session_insert_test.go b/session_insert_test.go index 12126190..b232d3f7 100644 --- a/session_insert_test.go +++ b/session_insert_test.go @@ -26,19 +26,3 @@ func TestInsertOne(t *testing.T) { _, err := testEngine.InsertOne(data) assert.NoError(t, err) } - -func TestInsertOneIfPkIsPoint(t *testing.T) { - assert.NoError(t, prepareEngine()) - - type TestPoint struct { - Id *int64 `xorm:"autoincr pk notnull 'id'"` - Msg *string `xorm:"varchar(255)"` - Created *time.Time `xorm:"created"` - } - - assert.NoError(t, testEngine.Sync2(new(TestPoint))) - msg := "hi" - data := TestPoint{Msg: &msg} - _, err := testEngine.InsertOne(&data) - assert.NoError(t, err) -} \ No newline at end of file