From 2d30a53c3ab6e19ddb09d968fc9b38aeddd57a94 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 4 Apr 2020 22:46:52 +0800 Subject: [PATCH] remove function From for package gdb; add function g.Table; add ServeHTTP interface implements for ghttp.Server --- database/gdb/gdb.go | 2 +- database/gdb/gdb_model.go | 16 --------------- database/gdb/gdb_unit_z_mysql_model_test.go | 2 +- .../gdb/gdb_unit_z_mysql_transaction_test.go | 2 +- frame/g/g_object.go | 7 +++++++ net/ghttp/ghttp_client_chain.go | 11 ++++++++++ net/ghttp/ghttp_func.go | 8 +++++++- net/ghttp/ghttp_server.go | 2 +- net/ghttp/ghttp_server_handler.go | 10 +--------- net/ghttp/ghttp_unit_client_test.go | 20 +++++++++++++++++++ 10 files changed, 50 insertions(+), 30 deletions(-) diff --git a/database/gdb/gdb.go b/database/gdb/gdb.go index 209857895..c21a91809 100644 --- a/database/gdb/gdb.go +++ b/database/gdb/gdb.go @@ -78,8 +78,8 @@ type DB interface { Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error) // Model creation. - From(tables string) *Model Table(tables string) *Model + Model(tables string) *Model Schema(schema string) *Schema // Configuration methods. diff --git a/database/gdb/gdb_model.go b/database/gdb/gdb_model.go index df9597b0e..7752ac259 100644 --- a/database/gdb/gdb_model.go +++ b/database/gdb/gdb_model.go @@ -69,7 +69,6 @@ func (c *Core) Table(table string) *Model { fields: "*", start: -1, offset: -1, - safe: true, option: OPTION_ALLOWEMPTY, } } @@ -80,13 +79,6 @@ func (c *Core) Model(table string) *Model { return c.DB.Table(table) } -// From is alias of Core.Table. -// See Core.Table. -// Deprecated. -func (c *Core) From(table string) *Model { - return c.DB.Table(table) -} - // Table acts like Core.Table except it operates on transaction. // See Core.Table. func (tx *TX) Table(table string) *Model { @@ -99,7 +91,6 @@ func (tx *TX) Table(table string) *Model { fields: "*", start: -1, offset: -1, - safe: true, option: OPTION_ALLOWEMPTY, } } @@ -110,13 +101,6 @@ func (tx *TX) Model(table string) *Model { return tx.Table(table) } -// From is alias of tx.Table. -// See tx.Table. -// Deprecated. -func (tx *TX) From(table string) *Model { - return tx.Table(table) -} - // As sets an alias name for current table. func (m *Model) As(as string) *Model { if m.tables != "" { diff --git a/database/gdb/gdb_unit_z_mysql_model_test.go b/database/gdb/gdb_unit_z_mysql_model_test.go index 7f2032acf..e0e541f2d 100644 --- a/database/gdb/gdb_unit_z_mysql_model_test.go +++ b/database/gdb/gdb_unit_z_mysql_model_test.go @@ -26,7 +26,7 @@ func Test_Model_Insert(t *testing.T) { table := createTable() defer dropTable(table) gtest.C(t, func(t *gtest.T) { - user := db.From(table) + user := db.Table(table) result, err := user.Filter().Data(g.Map{ "id": 1, "uid": 1, diff --git a/database/gdb/gdb_unit_z_mysql_transaction_test.go b/database/gdb/gdb_unit_z_mysql_transaction_test.go index 3dd776fbf..28d817fb2 100644 --- a/database/gdb/gdb_unit_z_mysql_transaction_test.go +++ b/database/gdb/gdb_unit_z_mysql_transaction_test.go @@ -120,7 +120,7 @@ func Test_TX_Insert(t *testing.T) { if err != nil { gtest.Error(err) } - user := tx.From(table) + user := tx.Table(table) if _, err := user.Data(g.Map{ "id": 1, "passport": "t1", diff --git a/frame/g/g_object.go b/frame/g/g_object.go index da846e2ef..404fd7abc 100644 --- a/frame/g/g_object.go +++ b/frame/g/g_object.go @@ -91,6 +91,13 @@ func DB(name ...string) gdb.DB { return gins.Database(name...) } +// Table creates and returns a model from specified database or default database configuration. +// The optional parameter specifies the configuration group name of the database, +// which is "default" in default. +func Table(tables string, db ...string) *gdb.Model { + return DB(db...).Table(tables) +} + // Redis returns an instance of redis client with specified configuration group name. func Redis(name ...string) *gredis.Redis { return gins.Redis(name...) diff --git a/net/ghttp/ghttp_client_chain.go b/net/ghttp/ghttp_client_chain.go index b5d6e8699..55671bf83 100644 --- a/net/ghttp/ghttp_client_chain.go +++ b/net/ghttp/ghttp_client_chain.go @@ -11,6 +11,17 @@ import ( "time" ) +// Prefix is a chaining function, +// which sets the URL prefix for next request of this client. +func (c *Client) Prefix(prefix string) *Client { + newClient := c + if c.parent == nil { + newClient = c.Clone() + } + newClient.SetPrefix(prefix) + return newClient +} + // Header is a chaining function, // which sets custom HTTP headers with map for next request. func (c *Client) Header(m map[string]string) *Client { diff --git a/net/ghttp/ghttp_func.go b/net/ghttp/ghttp_func.go index e2d46a8ef..3a55296fb 100644 --- a/net/ghttp/ghttp_func.go +++ b/net/ghttp/ghttp_func.go @@ -19,9 +19,15 @@ import ( // The optional parameter specifies whether ignore the url encoding for the data. func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr string) { // If given string/[]byte, converts and returns it directly as string. - switch params.(type) { + switch v := params.(type) { case string, []byte: return gconv.String(params) + case []interface{}: + if len(v) > 0 { + params = v[0] + } else { + params = nil + } } // Else converts it to map and does the url encoding. m, urlEncode := gconv.Map(params), true diff --git a/net/ghttp/ghttp_server.go b/net/ghttp/ghttp_server.go index deb6a8d19..c989942e0 100644 --- a/net/ghttp/ghttp_server.go +++ b/net/ghttp/ghttp_server.go @@ -306,7 +306,7 @@ func (s *Server) Start() error { // Default HTTP handler. if s.config.Handler == nil { - s.config.Handler = http.HandlerFunc(s.defaultHandler) + s.config.Handler = s } // Install external plugins. diff --git a/net/ghttp/ghttp_server_handler.go b/net/ghttp/ghttp_server_handler.go index 5974d0d8f..6a788fe41 100644 --- a/net/ghttp/ghttp_server_handler.go +++ b/net/ghttp/ghttp_server_handler.go @@ -25,15 +25,7 @@ import ( ) // 默认HTTP Server处理入口,http包底层默认使用了gorutine异步处理请求,所以这里不再异步执行 -func (s *Server) defaultHandler(w http.ResponseWriter, r *http.Request) { - s.handleRequest(w, r) -} - -// 执行处理HTTP请求, -// 首先,查找是否有对应域名的处理接口配置; -// 其次,如果没有对应的自定义处理接口配置,那么走默认的域名处理接口配置; -// 最后,如果以上都没有找到处理接口,那么进行文件处理; -func (s *Server) handleRequest(w http.ResponseWriter, r *http.Request) { +func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { // 重写规则判断 if len(s.config.Rewrites) > 0 { if rewrite, ok := s.config.Rewrites[r.URL.Path]; ok { diff --git a/net/ghttp/ghttp_unit_client_test.go b/net/ghttp/ghttp_unit_client_test.go index f66c62a54..98ce0b53d 100644 --- a/net/ghttp/ghttp_unit_client_test.go +++ b/net/ghttp/ghttp_unit_client_test.go @@ -87,6 +87,26 @@ func Test_Client_Cookie(t *testing.T) { }) } +func Test_Client_MapParam(t *testing.T) { + p, _ := ports.PopRand() + s := g.Server(p) + s.BindHandler("/map", func(r *ghttp.Request) { + r.Response.Write(r.Get("test")) + }) + s.SetPort(p) + s.SetDumpRouterMap(false) + s.Start() + defer s.Shutdown() + + time.Sleep(100 * time.Millisecond) + gtest.C(t, func(t *gtest.T) { + c := ghttp.NewClient() + c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) + + t.Assert(c.GetContent("/map", g.Map{"test": "1234567890"}), "1234567890") + }) +} + func Test_Client_Cookies(t *testing.T) { p, _ := ports.PopRand() s := g.Server(p)