remove octal number converting for gconv.Int*/Uint* functions; fix issue #1733

This commit is contained in:
John Guo
2022-05-09 21:26:42 +08:00
parent 3db97ba0dd
commit 583d576cdb
4 changed files with 37 additions and 19 deletions

View File

@ -4286,3 +4286,34 @@ func Test_Model_Issue1701(t *testing.T) {
t.Assert(value.String(), 100)
})
}
// https://github.com/gogf/gf/issues/1733
func Test_Model_Issue1733(t *testing.T) {
table := "user_" + guid.S()
if _, err := db.Exec(ctx, fmt.Sprintf(`
CREATE TABLE %s (
id int(8) unsigned zerofill NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, table,
)); err != nil {
gtest.AssertNil(err)
}
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
for i := 1; i <= 10; i++ {
_, err := db.Model(table).Data(g.Map{
"id": i,
}).Insert()
t.AssertNil(err)
}
all, err := db.Model(table).OrderAsc("id").All()
t.AssertNil(err)
t.Assert(len(all), 10)
for i := 0; i < 10; i++ {
t.Assert(all[i]["id"].Int(), i+1)
}
})
}

View File

@ -97,8 +97,10 @@ func Int64(any interface{}) int64 {
if f, ok := value.(iInt64); ok {
return f.Int64()
}
s := String(value)
isMinus := false
var (
s = String(value)
isMinus = false
)
if len(s) > 0 {
if s[0] == '-' {
isMinus = true
@ -116,15 +118,6 @@ func Int64(any interface{}) int64 {
return v
}
}
// Octal
if len(s) > 1 && s[0] == '0' {
if v, e := strconv.ParseInt(s[1:], 8, 64); e == nil {
if isMinus {
return -v
}
return v
}
}
// Decimal
if v, e := strconv.ParseInt(s, 10, 64); e == nil {
if isMinus {

View File

@ -104,12 +104,6 @@ func Uint64(any interface{}) uint64 {
return v
}
}
// Octal
if len(s) > 1 && s[0] == '0' {
if v, e := strconv.ParseUint(s[1:], 8, 64); e == nil {
return v
}
}
// Decimal
if v, e := strconv.ParseUint(s, 10, 64); e == nil {
return v

View File

@ -308,7 +308,7 @@ func Test_Int64_All(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var any interface{} = nil
t.AssertEQ(gconv.Int64("0x00e"), int64(14))
t.Assert(gconv.Int64("022"), int64(18))
t.Assert(gconv.Int64("022"), int64(22))
t.Assert(gconv.Int64(any), int64(0))
t.Assert(gconv.Int64(true), 1)
@ -502,7 +502,7 @@ func Test_Uint64_All(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var any interface{} = nil
t.AssertEQ(gconv.Uint64("0x00e"), uint64(14))
t.Assert(gconv.Uint64("022"), uint64(18))
t.Assert(gconv.Uint64("022"), uint64(22))
t.AssertEQ(gconv.Uint64(any), uint64(0))
t.AssertEQ(gconv.Uint64(true), uint64(1))