Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert strings to int in intConverter #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ func TestConverters(t *testing.T) {
testValue{value: float64(1442.212), err: nil, columnDesc: "float not null", output: float64(1442.212)},

// bigint

testValue{value: "3230", err: nil, columnDesc: "bigint not null", output: int64(3230)},
testValue{value: int64(3230), err: nil, columnDesc: "bigint not null", output: int64(3230)},
testValue{value: uint64(math.MaxInt64 + 1), err: ErrOverFlow, columnDesc: "bigint not null", output: nil},
testValue{value: int64(math.MaxInt64), err: nil, columnDesc: "bigint not null", output: int64(math.MaxInt64)},
Expand All @@ -216,6 +218,7 @@ func TestConverters(t *testing.T) {
testValue{value: nil, err: errors.New("Msg: 515"), columnDesc: "bigint not null", output: nil},

// tinyint
testValue{value: "200", err: nil, columnDesc: "tinyint not null", output: int64(200)},
testValue{value: int64(200), err: nil, columnDesc: "tinyint not null", output: int64(200)},
testValue{value: int64(math.MaxUint8 + 1), err: ErrOverFlow, columnDesc: "tinyint not null", output: nil},
testValue{value: int64(math.MaxUint8), err: nil, columnDesc: "tinyint not null", output: int64(math.MaxUint8)},
Expand All @@ -224,6 +227,7 @@ func TestConverters(t *testing.T) {
testValue{value: nil, err: errors.New("Msg: 515"), columnDesc: "tinyint", output: nil},

// unsigned int
testValue{value: "3230", err: nil, columnDesc: "unsigned int not null", output: int64(3230)},
testValue{value: int64(3230), err: nil, columnDesc: "unsigned int not null", output: int64(3230)},
testValue{value: math.MaxUint32 + 1, err: ErrOverFlow, columnDesc: "unsigned int not null", output: nil},
testValue{value: -1, err: ErrOverFlow, columnDesc: "unsigned int not null", output: nil},
Expand All @@ -232,6 +236,7 @@ func TestConverters(t *testing.T) {
testValue{value: nil, err: errors.New("Msg: 515"), columnDesc: "unsigned int not null", output: nil},

// unsigned smallint
testValue{value: "3230", err: nil, columnDesc: "unsigned smallint not null", output: int64(3230)},
testValue{value: int64(3230), err: nil, columnDesc: "unsigned smallint not null", output: int64(3230)},
testValue{value: math.MaxUint16 + 1, err: ErrOverFlow, columnDesc: "unsigned smallint null", output: nil},
testValue{value: -1, err: ErrOverFlow, columnDesc: "unsigned smallint null", output: nil},
Expand All @@ -240,13 +245,15 @@ func TestConverters(t *testing.T) {
testValue{value: nil, err: errors.New("Msg: 515"), columnDesc: "unsigned smallint not null", output: nil},

// unsigned bigint
testValue{value: "3230", err: nil, columnDesc: "unsigned bigint not null", output: uint64(3230)},
testValue{value: uint64(3230), err: nil, columnDesc: "unsigned bigint not null", output: uint64(3230)},
testValue{value: -1, err: ErrOverFlow, columnDesc: "unsigned bigint not null", output: nil},
testValue{value: uint64(math.MaxUint64), err: nil, columnDesc: "unsigned bigint not null", output: uint64(math.MaxUint64)},
testValue{value: nil, err: nil, columnDesc: "unsigned bigint null", output: nil},
testValue{value: nil, err: errors.New("Msg: 515"), columnDesc: "unsigned bigint not null", output: nil},

// tinyint
testValue{value: "200", err: nil, columnDesc: "unsigned tinyint not null", output: int64(200)},
testValue{value: int64(200), err: nil, columnDesc: "unsigned tinyint not null", output: int64(200)},
testValue{value: int64(math.MaxUint8 + 1), err: ErrOverFlow, columnDesc: "unsigned tinyint not null", output: nil},
testValue{value: int64(math.MaxUint8), err: nil, columnDesc: "unsigned tinyint not null", output: int64(math.MaxUint8)},
Expand Down
6 changes: 6 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,12 @@ func (i intConverter) ConvertValue(src interface{}) (driver.Value, error) {
return nil, ErrOverFlow
}
return i64, nil
case reflect.String:
i, err := strconv.Atoi(rv.String())
if err != nil {
return nil, ErrBadType
}
return int64(i), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
u64 = rv.Uint()
}
Expand Down