From b965dbff70fbf6a8b2278651bb2e62fef83a8b06 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 25 Nov 2018 22:18:36 +0800 Subject: [PATCH] =?UTF-8?q?gvar=E8=B0=83=E7=94=A8=E7=AB=AF=E6=94=B9?= =?UTF-8?q?=E8=BF=9B=EF=BC=8C=E5=8E=BB=E6=8E=89=E4=B8=8D=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E7=9A=84=E5=B9=B6=E5=8F=91=E5=AE=89=E5=85=A8=E5=8F=82=E6=95=B0?= =?UTF-8?q?=EF=BC=9B=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA=E7=BB=86=E8=8A=82?= =?UTF-8?q?=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/container/gvar/gvar.go | 29 ------------------ g/container/gvar/gvar_read.go | 38 +++++++++++++++++++++++ g/database/gdb/gdb_base.go | 2 +- g/database/gdb/gdb_transaction.go | 2 +- g/frame/gins/gins.go | 6 +--- g/net/ghttp/ghttp_request_params.go | 4 +-- g/net/ghttp/ghttp_request_request.go | 6 ++-- g/net/ghttp/ghttp_server_session.go | 5 +-- g/os/gcfg/gcfg.go | 46 ++++++++++++++++------------ g/os/gspath/gspath.go | 5 +++ g/os/gview/gview.go | 8 ++--- geg/os/gcfg/gcfg1.go | 1 + geg/os/gcfg/gcfg2.go | 1 + geg/os/gcfg/gcfg3.go | 1 + geg/os/gcfg/gcfg4.go | 1 + geg/os/gcfg/gcfg_auto_update.go | 7 +++-- geg/os/gcfg/gcfg_error.go | 12 ++++++++ geg/other/test.go | 16 +--------- 18 files changed, 104 insertions(+), 86 deletions(-) create mode 100644 g/container/gvar/gvar_read.go create mode 100644 geg/os/gcfg/gcfg_error.go diff --git a/g/container/gvar/gvar.go b/g/container/gvar/gvar.go index 119e69cf7..1ed19c326 100644 --- a/g/container/gvar/gvar.go +++ b/g/container/gvar/gvar.go @@ -18,35 +18,6 @@ type Var struct { safe bool // 当为true时,value为 *gtype.Interface 类型 } -// 只读变量接口 -type VarRead interface { - Val() interface{} - IsNil() bool - Bytes() []byte - String() string - Bool() bool - Int() int - Int8() int8 - Int16() int16 - Int32() int32 - Int64() int64 - Uint() uint - Uint8() uint8 - Uint16() uint16 - Uint32() uint32 - Uint64() uint64 - Float32() float32 - Float64() float64 - Interface() interface{} - Ints() []int - Floats() []float64 - Strings() []string - Interfaces() []interface{} - Time(format ...string) time.Time - TimeDuration() time.Duration - Struct(objPointer interface{}, attrMapping ...map[string]string) error -} - // 创建一个动态变量,value参数可以为nil func New(value interface{}, safe...bool) *Var { v := &Var{} diff --git a/g/container/gvar/gvar_read.go b/g/container/gvar/gvar_read.go new file mode 100644 index 000000000..bd4b7f8ef --- /dev/null +++ b/g/container/gvar/gvar_read.go @@ -0,0 +1,38 @@ +// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://gitee.com/johng/gf. + +package gvar + +import "time" + +// 只读变量接口 +type VarRead interface { + Val() interface{} + IsNil() bool + Bytes() []byte + String() string + Bool() bool + Int() int + Int8() int8 + Int16() int16 + Int32() int32 + Int64() int64 + Uint() uint + Uint8() uint8 + Uint16() uint16 + Uint32() uint32 + Uint64() uint64 + Float32() float32 + Float64() float64 + Interface() interface{} + Ints() []int + Floats() []float64 + Strings() []string + Interfaces() []interface{} + Time(format ...string) time.Time + TimeDuration() time.Duration + Struct(objPointer interface{}, attrMapping ...map[string]string) error +} \ No newline at end of file diff --git a/g/database/gdb/gdb_base.go b/g/database/gdb/gdb_base.go index f875dc9fe..1cfc1b76b 100644 --- a/g/database/gdb/gdb_base.go +++ b/g/database/gdb/gdb_base.go @@ -190,7 +190,7 @@ func (db *Db) GetAll(query string, args ...interface{}) (Result, error) { for i, col := range values { v := make([]byte, len(col)) copy(v, col) - row[columns[i]] = gvar.New(v) + row[columns[i]] = gvar.New(v, false) } records = append(records, row) } diff --git a/g/database/gdb/gdb_transaction.go b/g/database/gdb/gdb_transaction.go index 1db80ebfa..a65cffa87 100644 --- a/g/database/gdb/gdb_transaction.go +++ b/g/database/gdb/gdb_transaction.go @@ -123,7 +123,7 @@ func (tx *Tx) GetAll(query string, args ...interface{}) (Result, error) { for i, col := range values { v := make([]byte, len(col)) copy(v, col) - row[columns[i]] = gvar.New(v) + row[columns[i]] = gvar.New(v, false) } //fmt.Printf("%p\n", row["typeid"]) records = append(records, row) diff --git a/g/frame/gins/gins.go b/g/frame/gins/gins.go index 51a6405e8..0c8ba867c 100644 --- a/g/frame/gins/gins.go +++ b/g/frame/gins/gins.go @@ -130,11 +130,7 @@ func Database(name...string) *gdb.Db { db := instances.GetOrSetFuncLock(key, func() interface{} { m := config.GetMap("database") if m == nil { - if path := config.GetFilePath(); path == "" { - glog.Error(`incomplete configuration for database: config file not found`) - } else { - glog.Errorfln(`incomplete configuration for database: "database" node not found in config file "%s"`, path) - } + glog.Error(`database init failed: "database" node not found, is config file or configuration missing?`) return nil } for group, v := range m { diff --git a/g/net/ghttp/ghttp_request_params.go b/g/net/ghttp/ghttp_request_params.go index ccdc3c24a..c944aabc1 100644 --- a/g/net/ghttp/ghttp_request_params.go +++ b/g/net/ghttp/ghttp_request_params.go @@ -20,9 +20,9 @@ func (r *Request) SetParam(key string, value interface{}) { func (r *Request) GetParam(key string) gvar.VarRead { if r.params != nil { if v, ok := r.params[key]; ok { - return gvar.New(v, false).ReadOnly() + return gvar.New(v, false) } } - return nil + return gvar.New(nil, false) } diff --git a/g/net/ghttp/ghttp_request_request.go b/g/net/ghttp/ghttp_request_request.go index 71730b00f..95343213d 100644 --- a/g/net/ghttp/ghttp_request_request.go +++ b/g/net/ghttp/ghttp_request_request.go @@ -29,12 +29,12 @@ func (r *Request) GetRequest(key string, def ... []string) []string { func (r *Request) GetRequestVar(key string, def ... interface{}) gvar.VarRead { value := r.GetRequest(key) if value != nil { - return gvar.New(value) + return gvar.New(value, false) } if len(def) > 0 { - return gvar.New(def[0]) + return gvar.New(def[0], false) } - return nil + return gvar.New(nil, false) } func (r *Request) GetRequestString(key string, def ... string) string { diff --git a/g/net/ghttp/ghttp_server_session.go b/g/net/ghttp/ghttp_server_session.go index abd698934..dc08c680b 100644 --- a/g/net/ghttp/ghttp_server_session.go +++ b/g/net/ghttp/ghttp_server_session.go @@ -97,10 +97,7 @@ func (s *Session) Get (key string) interface{} { // 获取SESSION,建议都用该方法获取参数 func (s *Session) GetVar(key string) gvar.VarRead { s.init() - if v := s.data.Get(key); v != nil { - return gvar.NewRead(v, false) - } - return nil + return gvar.NewRead(s.data.Get(key), false) } diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index 585f10be8..f30fdc6dc 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -9,7 +9,9 @@ package gcfg import ( + "bytes" "errors" + "fmt" "gitee.com/johng/gf/g/container/gmap" "gitee.com/johng/gf/g/container/gtype" "gitee.com/johng/gf/g/container/gvar" @@ -56,17 +58,25 @@ func (c *Config) filePath(file...string) string { name = file[0] } path, _ := c.paths.Search(name) + if path == "" { + buffer := bytes.NewBuffer(nil) + buffer.WriteString(fmt.Sprintf("[gcfg] cannot find config file \"%s\" in following paths:", name)) + for k, v := range c.paths.Paths() { + buffer.WriteString(fmt.Sprintf("\n%d. %s",k + 1, v)) + } + glog.Error(buffer.String()) + } return path } // 设置配置管理器的配置文件存放目录绝对路径 func (c *Config) SetPath(path string) error { if rp, err := c.paths.Set(path); err != nil { - glog.Error("gcfg.SetPath failed:", err.Error()) + glog.Error("[gcfg] SetPath failed:", err.Error()) return err } else { c.jsons.Clear() - glog.Debug("gcfg.SetPath:", rp) + glog.Debug("[gcfg] SetPath:", rp) } return nil } @@ -81,43 +91,41 @@ func (c *Config) SetViolenceCheck(check bool) { // 添加配置管理器的配置文件搜索路径 func (c *Config) AddPath(path string) error { if rp, err := c.paths.Add(path); err != nil { - glog.Debug("gcfg.AddPath failed:", err.Error()) + glog.Debug("[gcfg] AddPath failed:", err.Error()) return err } else { - glog.Debug("gcfg.AddPath:", rp) + glog.Debug("[gcfg] AddPath:", rp) } return nil } // 获取指定文件的绝对路径,默认获取默认的配置文件路径 func (c *Config) GetFilePath(file...string) string { - name := c.name.Val() - if len(file) > 0 { - name = file[0] - } - path, _ := c.paths.Search(name) - return path + return c.filePath(file...) } // 设置配置管理对象的默认文件名称 func (c *Config) SetFileName(name string) { - glog.Debug("gcfg.SetFileName:", name) + glog.Debug("[gcfg] SetFileName:", name) c.name.Set(name) } // 添加配置文件到配置管理器中,第二个参数为非必须,如果不输入表示添加进入默认的配置名称中 func (c *Config) getJson(file...string) *gjson.Json { - fpath := c.filePath(file...) - if r := c.jsons.Get(fpath); r != nil { + filePath := c.filePath(file...) + if filePath == "" { + return nil + } + if r := c.jsons.Get(filePath); r != nil { return r.(*gjson.Json) } - if j, err := gjson.Load(fpath); err == nil { + if j, err := gjson.Load(filePath); err == nil { j.SetViolenceCheck(c.vc.Val()) - c.addMonitor(fpath) - c.jsons.Set(fpath, j) + c.addMonitor(filePath) + c.jsons.Set(filePath, j) return j } else { - glog.Errorfln(`gcfg.Load config file "%s" failed: %s`, fpath, err.Error()) + glog.Errorfln(`[gcfg] Load config file "%s" failed: %s`, filePath, err.Error()) } return nil } @@ -133,9 +141,9 @@ func (c *Config) Get(pattern string, file...string) interface{} { // 获得配置项,返回动态变量 func (c *Config) GetVar(pattern string, file...string) gvar.VarRead { if j := c.getJson(file...); j != nil { - return gvar.New(j.Get(pattern)) + return gvar.New(j.Get(pattern), false) } - return nil + return gvar.New(nil, false) } // 获得一个键值对关联数组/哈希表,方便操作,不需要自己做类型转换 diff --git a/g/os/gspath/gspath.go b/g/os/gspath/gspath.go index ac7d8ed54..f32bd52b5 100644 --- a/g/os/gspath/gspath.go +++ b/g/os/gspath/gspath.go @@ -141,6 +141,11 @@ func (sp *SPath) Remove(path string) { } } +// 返回当前对象搜索目录路径列表 +func (sp *SPath) Paths() []string { + return sp.paths.Slice() +} + // 返回当前对象缓存的所有路径列表 func (sp *SPath) AllPaths() []string { paths := sp.cache.Keys() diff --git a/g/os/gview/gview.go b/g/os/gview/gview.go index 0cced9d5c..d61e1dbb8 100644 --- a/g/os/gview/gview.go +++ b/g/os/gview/gview.go @@ -97,10 +97,10 @@ func New(path...string) *View { // 设置模板目录绝对路径 func (view *View) SetPath(path string) error { if rp, err := view.paths.Set(path); err != nil { - glog.Error("gview.SetPath failed:", err.Error()) + glog.Error("[gview] SetPath failed:", err.Error()) return err } else { - glog.Debug("gview.SetPath:", rp) + glog.Debug("[gview] SetPath:", rp) } return nil } @@ -108,10 +108,10 @@ func (view *View) SetPath(path string) error { // 添加模板目录搜索路径 func (view *View) AddPath(path string) error { if rp, err := view.paths.Add(path); err != nil { - glog.Error("gview.AddPath failed:", err.Error()) + glog.Error("[gview] AddPath failed:", err.Error()) return err } else { - glog.Debug("gview.AddPath:", rp) + glog.Debug("[gview] AddPath:", rp) } return nil } diff --git a/geg/os/gcfg/gcfg1.go b/geg/os/gcfg/gcfg1.go index 05fbb6ba2..6fef32fbe 100644 --- a/geg/os/gcfg/gcfg1.go +++ b/geg/os/gcfg/gcfg1.go @@ -5,6 +5,7 @@ import ( "gitee.com/johng/gf/g" ) +// 使用第二个参数指定读取的配置文件 func main() { c := g.Config() redisConfig := c.GetArray("redis-cache", "redis.toml") diff --git a/geg/os/gcfg/gcfg2.go b/geg/os/gcfg/gcfg2.go index c985508f1..4512692b8 100644 --- a/geg/os/gcfg/gcfg2.go +++ b/geg/os/gcfg/gcfg2.go @@ -5,6 +5,7 @@ import ( "gitee.com/johng/gf/g" ) +// 使用默认的config.toml配置文件读取配置 func main() { c := g.Config() fmt.Println(c.GetArray("memcache")) diff --git a/geg/os/gcfg/gcfg3.go b/geg/os/gcfg/gcfg3.go index 6b8522504..37b4720d8 100644 --- a/geg/os/gcfg/gcfg3.go +++ b/geg/os/gcfg/gcfg3.go @@ -5,6 +5,7 @@ import ( "gitee.com/johng/gf/g" ) +// 使用GetVar获取动态变量 func main() { fmt.Println(g.Config().GetVar("memcache.0").String()) } diff --git a/geg/os/gcfg/gcfg4.go b/geg/os/gcfg/gcfg4.go index 75013413c..169f8385b 100644 --- a/geg/os/gcfg/gcfg4.go +++ b/geg/os/gcfg/gcfg4.go @@ -5,6 +5,7 @@ import ( "gitee.com/johng/gf/g" ) +// 使用g.Config方法获取配置管理对象,并指定默认的配置文件名称 func main() { fmt.Println(g.Config("config.json").Get("viewpath")) } diff --git a/geg/os/gcfg/gcfg_auto_update.go b/geg/os/gcfg/gcfg_auto_update.go index 130f1a79e..2ce1481cb 100644 --- a/geg/os/gcfg/gcfg_auto_update.go +++ b/geg/os/gcfg/gcfg_auto_update.go @@ -2,13 +2,14 @@ package main import ( "fmt" - "time" - "gitee.com/johng/gf/g/os/gcfg" + "gitee.com/johng/gf/g" "gitee.com/johng/gf/g/os/gtime" + "time" ) +// 配置文件热更新示例 func main() { - c := gcfg.New("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/os/gcfg") + c := g.Config() // 每隔1秒打印当前配置项值,用户可手动在外部修改文件内容,gcfg读取到的配置项值会即时得到更新 gtime.SetInterval(time.Second, func() bool { fmt.Println(c.Get("viewpath")) diff --git a/geg/os/gcfg/gcfg_error.go b/geg/os/gcfg/gcfg_error.go new file mode 100644 index 000000000..717cebb09 --- /dev/null +++ b/geg/os/gcfg/gcfg_error.go @@ -0,0 +1,12 @@ +package main + +import ( + "fmt" + "gitee.com/johng/gf/g" +) + +// 演示在找不到配置文件时的错误提示 +func main() { + fmt.Println(g.Config("none-exist-config.toml").Get("none")) +} + diff --git a/geg/other/test.go b/geg/other/test.go index 9283f9a25..fc3151fcf 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -2,22 +2,8 @@ package main import ( "gitee.com/johng/gf/g" - "gitee.com/johng/gf/g/net/ghttp" ) func main() { - s := g.Server() - s.BindHandler("/", func(r *ghttp.Request) { - - }) - s.BindHandler("/user", func(r *ghttp.Request) { - - }) - s.BindHandler("/user/:id", func(r *ghttp.Request) { - r.Response.Header().Set("Content-Type", "text/plain; charset=utf-8") - r.Response.Write(r.Get("id")) - }) - s.SetFileServerEnabled(false) - s.SetPort(3000) - s.Run() + g.Database() }