diff --git a/frame/gins/gins.go b/frame/gins/gins.go index 452c08d1205..01be3b1e275 100644 --- a/frame/gins/gins.go +++ b/frame/gins/gins.go @@ -11,6 +11,7 @@ import ( "fmt" "github.com/gogf/gf/internal/intlog" "github.com/gogf/gf/net/ghttp" + "github.com/gogf/gf/util/gutil" "github.com/gogf/gf/os/gfile" @@ -242,8 +243,8 @@ func parseDBConfigNode(value interface{}) *gdb.ConfigNode { if err != nil { glog.Panic(err) } - if value, ok := nodeMap["link"]; ok { - node.LinkInfo = gconv.String(value) + if _, v := gutil.MapPossibleItemByKey(nodeMap, "link"); v != nil { + node.LinkInfo = gconv.String(v) } // Parse link syntax. if node.LinkInfo != "" && node.Type == "" { diff --git a/net/ghttp/ghttp_response_view.go b/net/ghttp/ghttp_response_view.go index 90ecba06b3d..e12b3394853 100644 --- a/net/ghttp/ghttp_response_view.go +++ b/net/ghttp/ghttp_response_view.go @@ -91,8 +91,8 @@ func (r *Response) buildInVars(params ...map[string]interface{}) map[string]inte if c := gcfg.Instance(); c.Available() { vars["Config"] = c.GetMap(".") } - vars["Get"] = r.Request.GetQueryMap() - vars["Post"] = r.Request.GetPostMap() + vars["Form"] = r.Request.GetFormMap() + vars["Query"] = r.Request.GetQueryMap() vars["Cookie"] = r.Request.Cookie.Map() vars["Session"] = r.Request.Session.Map() return vars diff --git a/net/ghttp/ghttp_server.go b/net/ghttp/ghttp_server.go index 1a737f1c8cc..98257bbf732 100644 --- a/net/ghttp/ghttp_server.go +++ b/net/ghttp/ghttp_server.go @@ -63,6 +63,7 @@ type ( // Router item just for dumping. RouterItem struct { + Type int Middleware string Domain string Method string @@ -380,6 +381,7 @@ func (s *Server) GetRouterMap() map[string][]RouterItem { array, _ := gregex.MatchString(`(.*?)%([A-Z]+):(.+)@(.+)`, k) for index, registeredItem := range registeredItems { item := RouterItem{ + Type: registeredItem.handler.itemType, Middleware: array[1], Domain: array[4], Method: array[2], diff --git a/net/gipv4/gipv4.go b/net/gipv4/gipv4.go index c18dc8d1a85..0fe36fe34d5 100644 --- a/net/gipv4/gipv4.go +++ b/net/gipv4/gipv4.go @@ -153,7 +153,7 @@ func IntranetIPArray() (ips []string, err error) { continue } ipStr := ip.String() - if ipStr != "127.0.0.1" && IsIntranet(ipStr) { + if IsIntranet(ipStr) { ips = append(ips, ipStr) } } diff --git a/os/gcache/gcache.go b/os/gcache/gcache.go index 6c8554bc793..094aaed6fcd 100644 --- a/os/gcache/gcache.go +++ b/os/gcache/gcache.go @@ -13,29 +13,20 @@ import "time" var cache = New() // Set sets cache with - pair, which is expired after . -// -// The parameter can be either type of int or time.Duration. -// If is type of int, it means milliseconds. -// If <=0 means it does not expire. +// It does not expire if <= 0. func Set(key interface{}, value interface{}, duration time.Duration) { cache.Set(key, value, duration) } // SetIfNotExist sets cache with - pair if does not exist in the cache, -// which is expired after . -// -// The parameter can be either type of int or time.Duration. -// If is type of int, it means milliseconds. -// If <=0 means it does not expire. +// which is expired after . It does not expire if <= 0. func SetIfNotExist(key interface{}, value interface{}, duration time.Duration) bool { return cache.SetIfNotExist(key, value, duration) } // Sets batch sets cache with key-value pairs by , which is expired after . // -// The parameter can be either type of int or time.Duration. -// If is type of int, it means milliseconds. -// If <=0 means it does not expire. +// It does not expire if <= 0. func Sets(data map[interface{}]interface{}, duration time.Duration) { cache.Sets(data, duration) } @@ -50,33 +41,21 @@ func Get(key interface{}) interface{} { // or sets - pair and returns if does not exist in the cache. // The key-value pair expires after . // -// The parameter can be either type of int or time.Duration. -// If is type of int, it means milliseconds. -// If <=0 means it does not expire. +// It does not expire if <= 0. func GetOrSet(key interface{}, value interface{}, duration time.Duration) interface{} { return cache.GetOrSet(key, value, duration) } -// GetOrSetFunc returns the value of , -// or sets with result of function and returns its result -// if does not exist in the cache. -// The key-value pair expires after . -// -// The parameter can be either type of int or time.Duration. -// If is type of int, it means milliseconds. -// If <=0 means it does not expire. +// GetOrSetFunc returns the value of , or sets with result of function +// and returns its result if does not exist in the cache. The key-value pair expires +// after . It does not expire if <= 0. func GetOrSetFunc(key interface{}, f func() interface{}, duration time.Duration) interface{} { return cache.GetOrSetFunc(key, f, duration) } -// GetOrSetFuncLock returns the value of , -// or sets with result of function and returns its result -// if does not exist in the cache. -// The key-value pair expires after . -// -// The parameter can be either type of int or time.Duration. -// If is type of int, it means milliseconds. -// If <=0 means it does not expire. +// GetOrSetFuncLock returns the value of , or sets with result of function +// and returns its result if does not exist in the cache. The key-value pair expires +// after . It does not expire if <= 0. // // Note that the function is executed within writing mutex lock. func GetOrSetFuncLock(key interface{}, f func() interface{}, duration time.Duration) interface{} { diff --git a/os/gcache/gcache_mem_cache.go b/os/gcache/gcache_mem_cache.go index 69929d85e0d..73757c32c75 100644 --- a/os/gcache/gcache_mem_cache.go +++ b/os/gcache/gcache_mem_cache.go @@ -106,9 +106,7 @@ func (c *memCache) getOrNewExpireSet(expire int64) (expireSet *gset.Set) { // Set sets cache with - pair, which is expired after . // -// The parameter can be either type of int or time.Duration. -// If is type of int, it means milliseconds. -// If <=0 means it does not expire. +// It does not expire if <= 0. func (c *memCache) Set(key interface{}, value interface{}, duration time.Duration) { expireTime := c.getInternalExpire(duration.Nanoseconds() / 1000000) c.dataMu.Lock() @@ -120,9 +118,7 @@ func (c *memCache) Set(key interface{}, value interface{}, duration time.Duratio // doSetWithLockCheck sets cache with - pair if does not exist in the cache, // which is expired after . // -// The parameter can be either type of int or time.Duration. -// If is type of int, it means milliseconds. -// If <=0 means it does not expire. +// It does not expire if <= 0. // // It doubly checks the whether exists in the cache using mutex writing lock // before setting it to the cache. @@ -154,11 +150,7 @@ func (c *memCache) getInternalExpire(expire int64) int64 { } // SetIfNotExist sets cache with - pair if does not exist in the cache, -// which is expired after . -// -// The parameter can be either type of int or time.Duration. -// If is type of int, it means milliseconds. -// If <=0 means it does not expire. +// which is expired after . It does not expire if <= 0. func (c *memCache) SetIfNotExist(key interface{}, value interface{}, duration time.Duration) bool { if !c.Contains(key) { c.doSetWithLockCheck(key, value, duration) @@ -169,9 +161,7 @@ func (c *memCache) SetIfNotExist(key interface{}, value interface{}, duration ti // Sets batch sets cache with key-value pairs by , which is expired after . // -// The parameter can be either type of int or time.Duration. -// If is type of int, it means milliseconds. -// If <=0 means it does not expire. +// It does not expire if <= 0. func (c *memCache) Sets(data map[interface{}]interface{}, duration time.Duration) { expireTime := c.getInternalExpire(duration.Nanoseconds() / 1000000) for k, v := range data { @@ -198,13 +188,9 @@ func (c *memCache) Get(key interface{}) interface{} { return nil } -// GetOrSet returns the value of , -// or sets - pair and returns if does not exist in the cache. -// The key-value pair expires after . -// -// The parameter can be either type of int or time.Duration. -// If is type of int, it means milliseconds. -// If <=0 means it does not expire. +// GetOrSet returns the value of , or sets - pair and returns if +// does not exist in the cache. The key-value pair expires after . It does not expire +// if <= 0. func (c *memCache) GetOrSet(key interface{}, value interface{}, duration time.Duration) interface{} { if v := c.Get(key); v == nil { return c.doSetWithLockCheck(key, value, duration) @@ -213,14 +199,9 @@ func (c *memCache) GetOrSet(key interface{}, value interface{}, duration time.Du } } -// GetOrSetFunc returns the value of , -// or sets with result of function and returns its result -// if does not exist in the cache. -// The key-value pair expires after . -// -// The parameter can be either type of int or time.Duration. -// If is type of int, it means milliseconds. -// If <=0 means it does not expire. +// GetOrSetFunc returns the value of , or sets with result of function +// and returns its result if does not exist in the cache. The key-value pair expires +// after . It does not expire if <= 0. func (c *memCache) GetOrSetFunc(key interface{}, f func() interface{}, duration time.Duration) interface{} { if v := c.Get(key); v == nil { return c.doSetWithLockCheck(key, f(), duration) @@ -229,14 +210,9 @@ func (c *memCache) GetOrSetFunc(key interface{}, f func() interface{}, duration } } -// GetOrSetFuncLock returns the value of , -// or sets with result of function and returns its result -// if does not exist in the cache. -// The key-value pair expires after . -// -// The parameter can be either type of int or time.Duration. -// If is type of int, it means milliseconds. -// If <=0 means it does not expire. +// GetOrSetFuncLock returns the value of , or sets with result of function +// and returns its result if does not exist in the cache. The key-value pair expires +// after . It does not expire if <= 0. // // Note that the function is executed within writing mutex lock. func (c *memCache) GetOrSetFuncLock(key interface{}, f func() interface{}, duration time.Duration) interface{} { diff --git a/os/gview/gview.go b/os/gview/gview.go index db23b0b0d9b..8265ae93552 100644 --- a/os/gview/gview.go +++ b/os/gview/gview.go @@ -141,5 +141,6 @@ func New(path ...string) *View { view.BindFunc("tolower", view.funcToLower) view.BindFunc("nl2br", view.funcNl2Br) view.BindFunc("include", view.funcInclude) + view.BindFunc("dump", view.funcDump) return view } diff --git a/os/gview/gview_buildin.go b/os/gview/gview_buildin.go index 6020e44fda3..1a52f66fb30 100644 --- a/os/gview/gview_buildin.go +++ b/os/gview/gview_buildin.go @@ -8,6 +8,7 @@ package gview import ( "fmt" + "github.com/gogf/gf/util/gutil" "strings" "github.com/gogf/gf/encoding/ghtml" @@ -17,6 +18,16 @@ import ( "github.com/gogf/gf/util/gconv" ) +// funcDump implements build-in template function: dump +func (view *View) funcDump(values ...interface{}) (result string) { + result += "\n" + return result +} + // funcEq implements build-in template function: eq func (view *View) funcEq(value interface{}, others ...interface{}) bool { s := gconv.String(value) diff --git a/os/gview/gview_config.go b/os/gview/gview_config.go index 53f415fa9db..aac79be8c92 100644 --- a/os/gview/gview_config.go +++ b/os/gview/gview_config.go @@ -57,10 +57,10 @@ func (view *View) SetConfigWithMap(m map[string]interface{}) error { return errors.New("configuration cannot be empty") } // Most common used configuration support for single view path. - k1, v1 := gutil.MapPossibleItemByKey(m, "paths") + _, v1 := gutil.MapPossibleItemByKey(m, "paths") _, v2 := gutil.MapPossibleItemByKey(m, "path") if v1 == nil && v2 != nil { - m[k1] = []interface{}{v2} + m["paths"] = []interface{}{v2} } config := Config{} err := gconv.Struct(m, &config) diff --git a/os/gview/gview_doparse.go b/os/gview/gview_doparse.go index 811d8d8b8b2..3477a03385a 100644 --- a/os/gview/gview_doparse.go +++ b/os/gview/gview_doparse.go @@ -87,6 +87,10 @@ func (view *View) Parse(file string, params ...Params) (result string, err error return } item := r.(*fileCacheItem) + // It's not necessary continuing parsing if template content is empty. + if item.content == "" { + return "", nil + } // Get the template object instance for . tpl, err = view.getTemplate(item.folder, fmt.Sprintf(`*%s`, gfile.Ext(item.path))) if err != nil { @@ -145,6 +149,10 @@ func (view *View) ParseDefault(params ...Params) (result string, err error) { // ParseContent parses given template content with template variables // and returns the parsed content in []byte. func (view *View) ParseContent(content string, params ...Params) (string, error) { + // It's not necessary continuing parsing if template content is empty. + if content == "" { + return "", nil + } err := (error)(nil) key := fmt.Sprintf("%s_%v", gCONTENT_TEMPLATE_NAME, view.delimiters) tpl := templates.GetOrSetFuncLock(key, func() interface{} { diff --git a/util/gconv/gconv_map.go b/util/gconv/gconv_map.go index 2cfb3e41cac..b34b2931832 100644 --- a/util/gconv/gconv_map.go +++ b/util/gconv/gconv_map.go @@ -201,7 +201,7 @@ func MapToMapDeep(params interface{}, pointer interface{}, mapping ...map[string return doMapToMap(params, pointer, true, mapping...) } -// doMapStruct converts map type variable to another map type variable . +// doMapToMap converts map type variable to another map type variable . // The elements of should be type of *map. func doMapToMap(params interface{}, pointer interface{}, deep bool, mapping ...map[string]string) error { paramsRv := reflect.ValueOf(params)