Merge pull request #4668 from Loyalsoldier/fix-code

Chore: fix code
This commit is contained in:
Ming Deng 2021-06-23 23:40:53 +08:00 committed by GitHub
commit 2207201e38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 79 additions and 83 deletions

View File

@ -60,6 +60,7 @@
- [4474](https://github.com/beego/beego/pull/4474) - [4474](https://github.com/beego/beego/pull/4474)
- [4479](https://github.com/beego/beego/pull/4479) - [4479](https://github.com/beego/beego/pull/4479)
- [4639](https://github.com/beego/beego/pull/4639) - [4639](https://github.com/beego/beego/pull/4639)
- [4668](https://github.com/beego/beego/pull/4668)
## Fix lint and format code ## Fix lint and format code

View File

@ -20,6 +20,6 @@ import (
// NewFakeConfig return a fake Configer // NewFakeConfig return a fake Configer
func NewFakeConfig() Configer { func NewFakeConfig() Configer {
new := config.NewFakeConfig() config := config.NewFakeConfig()
return &newToOldConfigerAdapter{delegate: new} return &newToOldConfigerAdapter{delegate: config}
} }

View File

@ -80,13 +80,10 @@ func (d *DefaultValueFilterChainBuilder) FilterChain(next orm.Filter) orm.Filter
switch inv.Method { switch inv.Method {
case "Insert", "InsertWithCtx": case "Insert", "InsertWithCtx":
d.handleInsert(ctx, inv) d.handleInsert(ctx, inv)
break
case "InsertOrUpdate", "InsertOrUpdateWithCtx": case "InsertOrUpdate", "InsertOrUpdateWithCtx":
d.handleInsertOrUpdate(ctx, inv) d.handleInsertOrUpdate(ctx, inv)
break
case "InsertMulti", "InsertMultiWithCtx": case "InsertMulti", "InsertMultiWithCtx":
d.handleInsertMulti(ctx, inv) d.handleInsertMulti(ctx, inv)
break
} }
return next(ctx, inv) return next(ctx, inv)
} }

View File

@ -308,7 +308,6 @@ func TestDataTypes(t *testing.T) {
case "DateTime": case "DateTime":
case "Time": case "Time":
assert.True(t, vu.(time.Time).In(DefaultTimeLoc).Sub(value.(time.Time).In(DefaultTimeLoc)) <= time.Second) assert.True(t, vu.(time.Time).In(DefaultTimeLoc).Sub(value.(time.Time).In(DefaultTimeLoc)) <= time.Second)
break
default: default:
assert.Equal(t, value, vu) assert.Equal(t, value, vu)
} }
@ -1842,12 +1841,10 @@ func TestRawQueryRow(t *testing.T) {
switch col { switch col {
case "id": case "id":
throwFail(t, AssertIs(id, 1)) throwFail(t, AssertIs(id, 1))
break
case "time", "datetime": case "time", "datetime":
v = v.(time.Time).In(DefaultTimeLoc) v = v.(time.Time).In(DefaultTimeLoc)
value := dataValues[col].(time.Time).In(DefaultTimeLoc) value := dataValues[col].(time.Time).In(DefaultTimeLoc)
assert.True(t, v.(time.Time).Sub(value) <= time.Second) assert.True(t, v.(time.Time).Sub(value) <= time.Second)
break
case "date": case "date":
default: default:
throwFail(t, AssertIs(v, dataValues[col])) throwFail(t, AssertIs(v, dataValues[col]))
@ -1942,7 +1939,6 @@ func TestQueryRows(t *testing.T) {
case "Date": case "Date":
case "DateTime": case "DateTime":
assert.True(t, vu.(time.Time).In(DefaultTimeLoc).Sub(value.(time.Time).In(DefaultTimeLoc)) <= time.Second) assert.True(t, vu.(time.Time).In(DefaultTimeLoc).Sub(value.(time.Time).In(DefaultTimeLoc)) <= time.Second)
break
default: default:
assert.Equal(t, value, vu) assert.Equal(t, value, vu)
} }
@ -1966,7 +1962,6 @@ func TestQueryRows(t *testing.T) {
case "Date": case "Date":
case "DateTime": case "DateTime":
assert.True(t, vu.(time.Time).In(DefaultTimeLoc).Sub(value.(time.Time).In(DefaultTimeLoc)) <= time.Second) assert.True(t, vu.(time.Time).In(DefaultTimeLoc).Sub(value.(time.Time).In(DefaultTimeLoc)) <= time.Second)
break
default: default:
assert.Equal(t, value, vu) assert.Equal(t, value, vu)
} }

View File

@ -45,9 +45,9 @@ func CfgCookieName(cookieName string) ManagerConfigOpt {
} }
// CfgCookieName set len of session id // CfgCookieName set len of session id
func CfgSessionIdLength(len int64) ManagerConfigOpt { func CfgSessionIdLength(length int64) ManagerConfigOpt {
return func(config *ManagerConfig) { return func(config *ManagerConfig) {
config.SessionIDLength = len config.SessionIDLength = length
} }
} }

View File

@ -50,80 +50,83 @@ func notMatchTestInfo(pattern, url string) testInfo {
} }
func init() { func init() {
routers = make([]testInfo, 0, 128) const (
// match example abcHTML = "/suffix/abc.html"
routers = append(routers, matchTestInfo("/topic/?:auth:int", "/topic", nil)) abcSuffix = "/abc/suffix/*"
routers = append(routers, matchTestInfo("/topic/?:auth:int", "/topic/123", map[string]string{":auth": "123"})) )
routers = append(routers, matchTestInfo("/topic/:id/?:auth", "/topic/1", map[string]string{":id": "1"}))
routers = append(routers, matchTestInfo("/topic/:id/?:auth", "/topic/1/2", map[string]string{":id": "1", ":auth": "2"}))
routers = append(routers, matchTestInfo("/topic/:id/?:auth:int", "/topic/1", map[string]string{":id": "1"}))
routers = append(routers, matchTestInfo("/topic/:id/?:auth:int", "/topic/1/123", map[string]string{":id": "1", ":auth": "123"}))
routers = append(routers, matchTestInfo("/:id", "/123", map[string]string{":id": "123"}))
routers = append(routers, matchTestInfo("/hello/?:id", "/hello", map[string]string{":id": ""}))
routers = append(routers, matchTestInfo("/", "/", nil))
routers = append(routers, matchTestInfo("/customer/login", "/customer/login", nil))
routers = append(routers, matchTestInfo("/customer/login", "/customer/login.json", map[string]string{":ext": "json"}))
routers = append(routers, matchTestInfo("/*", "/http://customer/123/", map[string]string{":splat": "http://customer/123/"}))
routers = append(routers, matchTestInfo("/*", "/customer/2009/12/11", map[string]string{":splat": "customer/2009/12/11"}))
routers = append(routers, matchTestInfo("/aa/*/bb", "/aa/2009/bb", map[string]string{":splat": "2009"}))
routers = append(routers, matchTestInfo("/cc/*/dd", "/cc/2009/11/dd", map[string]string{":splat": "2009/11"}))
routers = append(routers, matchTestInfo("/cc/:id/*", "/cc/2009/11/dd", map[string]string{":id": "2009", ":splat": "11/dd"}))
routers = append(routers, matchTestInfo("/ee/:year/*/ff", "/ee/2009/11/ff", map[string]string{":year": "2009", ":splat": "11"}))
routers = append(routers, matchTestInfo("/thumbnail/:size/uploads/*", "/thumbnail/100x100/uploads/items/2014/04/20/dPRCdChkUd651t1Hvs18.jpg", map[string]string{":size": "100x100", ":splat": "items/2014/04/20/dPRCdChkUd651t1Hvs18.jpg"}))
routers = append(routers, matchTestInfo("/*.*", "/nice/api.json", map[string]string{":path": "nice/api", ":ext": "json"}))
routers = append(routers, matchTestInfo("/:name/*.*", "/nice/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}))
routers = append(routers, matchTestInfo("/:name/test/*.*", "/nice/test/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}))
routers = append(routers, matchTestInfo("/dl/:width:int/:height:int/*.*", "/dl/48/48/05ac66d9bda00a3acf948c43e306fc9a.jpg", map[string]string{":width": "48", ":height": "48", ":ext": "jpg", ":path": "05ac66d9bda00a3acf948c43e306fc9a"}))
routers = append(routers, matchTestInfo("/v1/shop/:id:int", "/v1/shop/123", map[string]string{":id": "123"}))
routers = append(routers, matchTestInfo("/v1/shop/:id\\((a|b|c)\\)", "/v1/shop/123(a)", map[string]string{":id": "123"}))
routers = append(routers, matchTestInfo("/v1/shop/:id\\((a|b|c)\\)", "/v1/shop/123(b)", map[string]string{":id": "123"}))
routers = append(routers, matchTestInfo("/v1/shop/:id\\((a|b|c)\\)", "/v1/shop/123(c)", map[string]string{":id": "123"}))
routers = append(routers, matchTestInfo("/:year:int/:month:int/:id/:endid", "/1111/111/aaa/aaa", map[string]string{":year": "1111", ":month": "111", ":id": "aaa", ":endid": "aaa"}))
routers = append(routers, matchTestInfo("/v1/shop/:id/:name", "/v1/shop/123/nike", map[string]string{":id": "123", ":name": "nike"}))
routers = append(routers, matchTestInfo("/v1/shop/:id/account", "/v1/shop/123/account", map[string]string{":id": "123"}))
routers = append(routers, matchTestInfo("/v1/shop/:name:string", "/v1/shop/nike", map[string]string{":name": "nike"}))
routers = append(routers, matchTestInfo("/v1/shop/:id([0-9]+)", "/v1/shop//123", map[string]string{":id": "123"}))
routers = append(routers, matchTestInfo("/v1/shop/:id([0-9]+)_:name", "/v1/shop/123_nike", map[string]string{":id": "123", ":name": "nike"}))
routers = append(routers, matchTestInfo("/v1/shop/:id(.+)_cms.html", "/v1/shop/123_cms.html", map[string]string{":id": "123"}))
routers = append(routers, matchTestInfo("/v1/shop/cms_:id(.+)_:page(.+).html", "/v1/shop/cms_123_1.html", map[string]string{":id": "123", ":page": "1"}))
routers = append(routers, matchTestInfo("/v1/:v/cms/aaa_:id(.+)_:page(.+).html", "/v1/2/cms/aaa_123_1.html", map[string]string{":v": "2", ":id": "123", ":page": "1"}))
routers = append(routers, matchTestInfo("/v1/:v/cms_:id(.+)_:page(.+).html", "/v1/2/cms_123_1.html", map[string]string{":v": "2", ":id": "123", ":page": "1"}))
routers = append(routers, matchTestInfo("/v1/:v(.+)_cms/ttt_:id(.+)_:page(.+).html", "/v1/2_cms/ttt_123_1.html", map[string]string{":v": "2", ":id": "123", ":page": "1"}))
routers = append(routers, matchTestInfo("/api/projects/:pid/members/?:mid", "/api/projects/1/members", map[string]string{":pid": "1"}))
routers = append(routers, matchTestInfo("/api/projects/:pid/members/?:mid", "/api/projects/1/members/2", map[string]string{":pid": "1", ":mid": "2"}))
routers = append(routers, matchTestInfo("/?:year/?:month/?:day", "/2020/11/10", map[string]string{":year": "2020", ":month": "11", ":day": "10"}))
routers = append(routers, matchTestInfo("/?:year/?:month/?:day", "/2020/11", map[string]string{":year": "2020", ":month": "11"}))
routers = append(routers, matchTestInfo("/?:year", "/2020", map[string]string{":year": "2020"}))
routers = append(routers, matchTestInfo("/?:year([0-9]+)/?:month([0-9]+)/mid/?:day([0-9]+)/?:hour([0-9]+)", "/2020/11/mid/10/24", map[string]string{":year": "2020", ":month": "11", ":day": "10", ":hour": "24"}))
routers = append(routers, matchTestInfo("/?:year/?:month/mid/?:day/?:hour", "/2020/mid/10", map[string]string{":year": "2020", ":day": "10"}))
routers = append(routers, matchTestInfo("/?:year/?:month/mid/?:day/?:hour", "/2020/11/mid", map[string]string{":year": "2020", ":month": "11"}))
routers = append(routers, matchTestInfo("/?:year/?:month/mid/?:day/?:hour", "/mid/10/24", map[string]string{":day": "10", ":hour": "24"}))
routers = append(routers, matchTestInfo("/?:year([0-9]+)/:month([0-9]+)/mid/:day([0-9]+)/?:hour([0-9]+)", "/2020/11/mid/10/24", map[string]string{":year": "2020", ":month": "11", ":day": "10", ":hour": "24"}))
routers = append(routers, matchTestInfo("/?:year/:month/mid/:day/?:hour", "/11/mid/10/24", map[string]string{":month": "11", ":day": "10"}))
routers = append(routers, matchTestInfo("/?:year/:month/mid/:day/?:hour", "/2020/11/mid/10", map[string]string{":year": "2020", ":month": "11", ":day": "10"}))
routers = append(routers, matchTestInfo("/?:year/:month/mid/:day/?:hour", "/11/mid/10", map[string]string{":month": "11", ":day": "10"}))
// not match example
// https://github.com/beego/beego/v2/issues/3865 routers = []testInfo{
routers = append(routers, notMatchTestInfo("/read_:id:int\\.htm", "/read_222htm")) // match example
routers = append(routers, notMatchTestInfo("/read_:id:int\\.htm", "/read_222_htm")) matchTestInfo("/topic/?:auth:int", "/topic", nil),
routers = append(routers, notMatchTestInfo("/read_:id:int\\.htm", " /read_262shtm")) matchTestInfo("/topic/?:auth:int", "/topic/123", map[string]string{":auth": "123"}),
matchTestInfo("/topic/:id/?:auth", "/topic/1", map[string]string{":id": "1"}),
matchTestInfo("/topic/:id/?:auth", "/topic/1/2", map[string]string{":id": "1", ":auth": "2"}),
matchTestInfo("/topic/:id/?:auth:int", "/topic/1", map[string]string{":id": "1"}),
matchTestInfo("/topic/:id/?:auth:int", "/topic/1/123", map[string]string{":id": "1", ":auth": "123"}),
matchTestInfo("/:id", "/123", map[string]string{":id": "123"}),
matchTestInfo("/hello/?:id", "/hello", map[string]string{":id": ""}),
matchTestInfo("/", "/", nil),
matchTestInfo("/customer/login", "/customer/login", nil),
matchTestInfo("/customer/login", "/customer/login.json", map[string]string{":ext": "json"}),
matchTestInfo("/*", "/http://customer/123/", map[string]string{":splat": "http://customer/123/"}),
matchTestInfo("/*", "/customer/2009/12/11", map[string]string{":splat": "customer/2009/12/11"}),
matchTestInfo("/aa/*/bb", "/aa/2009/bb", map[string]string{":splat": "2009"}),
matchTestInfo("/cc/*/dd", "/cc/2009/11/dd", map[string]string{":splat": "2009/11"}),
matchTestInfo("/cc/:id/*", "/cc/2009/11/dd", map[string]string{":id": "2009", ":splat": "11/dd"}),
matchTestInfo("/ee/:year/*/ff", "/ee/2009/11/ff", map[string]string{":year": "2009", ":splat": "11"}),
matchTestInfo("/thumbnail/:size/uploads/*", "/thumbnail/100x100/uploads/items/2014/04/20/dPRCdChkUd651t1Hvs18.jpg", map[string]string{":size": "100x100", ":splat": "items/2014/04/20/dPRCdChkUd651t1Hvs18.jpg"}),
matchTestInfo("/*.*", "/nice/api.json", map[string]string{":path": "nice/api", ":ext": "json"}),
matchTestInfo("/:name/*.*", "/nice/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}),
matchTestInfo("/:name/test/*.*", "/nice/test/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}),
matchTestInfo("/dl/:width:int/:height:int/*.*", "/dl/48/48/05ac66d9bda00a3acf948c43e306fc9a.jpg", map[string]string{":width": "48", ":height": "48", ":ext": "jpg", ":path": "05ac66d9bda00a3acf948c43e306fc9a"}),
matchTestInfo("/v1/shop/:id:int", "/v1/shop/123", map[string]string{":id": "123"}),
matchTestInfo("/v1/shop/:id\\((a|b|c)\\)", "/v1/shop/123(a)", map[string]string{":id": "123"}),
matchTestInfo("/v1/shop/:id\\((a|b|c)\\)", "/v1/shop/123(b)", map[string]string{":id": "123"}),
matchTestInfo("/v1/shop/:id\\((a|b|c)\\)", "/v1/shop/123(c)", map[string]string{":id": "123"}),
matchTestInfo("/:year:int/:month:int/:id/:endid", "/1111/111/aaa/aaa", map[string]string{":year": "1111", ":month": "111", ":id": "aaa", ":endid": "aaa"}),
matchTestInfo("/v1/shop/:id/:name", "/v1/shop/123/nike", map[string]string{":id": "123", ":name": "nike"}),
matchTestInfo("/v1/shop/:id/account", "/v1/shop/123/account", map[string]string{":id": "123"}),
matchTestInfo("/v1/shop/:name:string", "/v1/shop/nike", map[string]string{":name": "nike"}),
matchTestInfo("/v1/shop/:id([0-9]+)", "/v1/shop//123", map[string]string{":id": "123"}),
matchTestInfo("/v1/shop/:id([0-9]+)_:name", "/v1/shop/123_nike", map[string]string{":id": "123", ":name": "nike"}),
matchTestInfo("/v1/shop/:id(.+)_cms.html", "/v1/shop/123_cms.html", map[string]string{":id": "123"}),
matchTestInfo("/v1/shop/cms_:id(.+)_:page(.+).html", "/v1/shop/cms_123_1.html", map[string]string{":id": "123", ":page": "1"}),
matchTestInfo("/v1/:v/cms/aaa_:id(.+)_:page(.+).html", "/v1/2/cms/aaa_123_1.html", map[string]string{":v": "2", ":id": "123", ":page": "1"}),
matchTestInfo("/v1/:v/cms_:id(.+)_:page(.+).html", "/v1/2/cms_123_1.html", map[string]string{":v": "2", ":id": "123", ":page": "1"}),
matchTestInfo("/v1/:v(.+)_cms/ttt_:id(.+)_:page(.+).html", "/v1/2_cms/ttt_123_1.html", map[string]string{":v": "2", ":id": "123", ":page": "1"}),
matchTestInfo("/api/projects/:pid/members/?:mid", "/api/projects/1/members", map[string]string{":pid": "1"}),
matchTestInfo("/api/projects/:pid/members/?:mid", "/api/projects/1/members/2", map[string]string{":pid": "1", ":mid": "2"}),
matchTestInfo("/?:year/?:month/?:day", "/2020/11/10", map[string]string{":year": "2020", ":month": "11", ":day": "10"}),
matchTestInfo("/?:year/?:month/?:day", "/2020/11", map[string]string{":year": "2020", ":month": "11"}),
matchTestInfo("/?:year", "/2020", map[string]string{":year": "2020"}),
matchTestInfo("/?:year([0-9]+)/?:month([0-9]+)/mid/?:day([0-9]+)/?:hour([0-9]+)", "/2020/11/mid/10/24", map[string]string{":year": "2020", ":month": "11", ":day": "10", ":hour": "24"}),
matchTestInfo("/?:year/?:month/mid/?:day/?:hour", "/2020/mid/10", map[string]string{":year": "2020", ":day": "10"}),
matchTestInfo("/?:year/?:month/mid/?:day/?:hour", "/2020/11/mid", map[string]string{":year": "2020", ":month": "11"}),
matchTestInfo("/?:year/?:month/mid/?:day/?:hour", "/mid/10/24", map[string]string{":day": "10", ":hour": "24"}),
matchTestInfo("/?:year([0-9]+)/:month([0-9]+)/mid/:day([0-9]+)/?:hour([0-9]+)", "/2020/11/mid/10/24", map[string]string{":year": "2020", ":month": "11", ":day": "10", ":hour": "24"}),
matchTestInfo("/?:year/:month/mid/:day/?:hour", "/11/mid/10/24", map[string]string{":month": "11", ":day": "10"}),
matchTestInfo("/?:year/:month/mid/:day/?:hour", "/2020/11/mid/10", map[string]string{":year": "2020", ":month": "11", ":day": "10"}),
matchTestInfo("/?:year/:month/mid/:day/?:hour", "/11/mid/10", map[string]string{":month": "11", ":day": "10"}),
// test .html, .json not suffix // not match example
const abcHtml = "/suffix/abc.html" // https://github.com/beego/beego/v2/issues/3865
routers = append(routers, notMatchTestInfo(abcHtml, "/suffix.html/abc")) notMatchTestInfo("/read_:id:int\\.htm", "/read_222htm"),
routers = append(routers, matchTestInfo("/suffix/abc", abcHtml, nil)) notMatchTestInfo("/read_:id:int\\.htm", "/read_222_htm"),
routers = append(routers, matchTestInfo("/suffix/*", abcHtml, nil)) notMatchTestInfo("/read_:id:int\\.htm", " /read_262shtm"),
routers = append(routers, notMatchTestInfo("/suffix/*", "/suffix.html/a"))
const abcSuffix = "/abc/suffix/*" // test .html, .json not suffix
routers = append(routers, notMatchTestInfo(abcSuffix, "/abc/suffix.html/a")) notMatchTestInfo(abcHTML, "/suffix.html/abc"),
routers = append(routers, matchTestInfo(abcSuffix, "/abc/suffix/a", nil)) matchTestInfo("/suffix/abc", abcHTML, nil),
routers = append(routers, notMatchTestInfo(abcSuffix, "/abc.j/suffix/a")) matchTestInfo("/suffix/*", abcHTML, nil),
notMatchTestInfo("/suffix/*", "/suffix.html/a"),
notMatchTestInfo(abcSuffix, "/abc/suffix.html/a"),
matchTestInfo(abcSuffix, "/abc/suffix/a", nil),
notMatchTestInfo(abcSuffix, "/abc.j/suffix/a"),
}
} }
func TestTreeRouters(t *testing.T) { func TestTreeRouters(t *testing.T) {
for _, r := range routers { for _, r := range routers {
shouldMatch := r.shouldMatchOrNot shouldMatch := r.shouldMatchOrNot
tr := NewTree() tr := NewTree()
tr.AddRouter(r.pattern, "astaxie") tr.AddRouter(r.pattern, "astaxie")

View File

@ -92,7 +92,7 @@ func viewsBlocksBlockTpl() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "views/blocks/block.tpl", size: 50, mode: os.FileMode(436), modTime: time.Unix(1541431067, 0)} info := bindataFileInfo{name: "views/blocks/block.tpl", size: 50, mode: os.FileMode(0o664), modTime: time.Unix(1541431067, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -112,7 +112,7 @@ func viewsHeaderTpl() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "views/header.tpl", size: 52, mode: os.FileMode(436), modTime: time.Unix(1541431067, 0)} info := bindataFileInfo{name: "views/header.tpl", size: 52, mode: os.FileMode(0o664), modTime: time.Unix(1541431067, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -132,7 +132,7 @@ func viewsIndexTpl() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "views/index.tpl", size: 255, mode: os.FileMode(436), modTime: time.Unix(1541434906, 0)} info := bindataFileInfo{name: "views/index.tpl", size: 255, mode: os.FileMode(0o664), modTime: time.Unix(1541434906, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }