Skip to content

Commit

Permalink
remove octal number converting for gconv.Int*/Uint* functions; fix is…
Browse files Browse the repository at this point in the history
…sue #1733
  • Loading branch information
gqcn committed May 9, 2022
1 parent 3db97ba commit 583d576
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
31 changes: 31 additions & 0 deletions database/gdb/gdb_z_mysql_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
15 changes: 4 additions & 11 deletions util/gconv/gconv_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 0 additions & 6 deletions util/gconv/gconv_uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions util/gconv/gconv_z_unit_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 583d576

Please sign in to comment.