mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
gvar调用端改进,去掉不必要的并发安全参数;错误提示细节改进
This commit is contained in:
@ -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{}
|
||||
|
||||
38
g/container/gvar/gvar_read.go
Normal file
38
g/container/gvar/gvar_read.go
Normal file
@ -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
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
// 获得一个键值对关联数组/哈希表,方便操作,不需要自己做类型转换
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ import (
|
||||
"gitee.com/johng/gf/g"
|
||||
)
|
||||
|
||||
// 使用第二个参数指定读取的配置文件
|
||||
func main() {
|
||||
c := g.Config()
|
||||
redisConfig := c.GetArray("redis-cache", "redis.toml")
|
||||
|
||||
@ -5,6 +5,7 @@ import (
|
||||
"gitee.com/johng/gf/g"
|
||||
)
|
||||
|
||||
// 使用默认的config.toml配置文件读取配置
|
||||
func main() {
|
||||
c := g.Config()
|
||||
fmt.Println(c.GetArray("memcache"))
|
||||
|
||||
@ -5,6 +5,7 @@ import (
|
||||
"gitee.com/johng/gf/g"
|
||||
)
|
||||
|
||||
// 使用GetVar获取动态变量
|
||||
func main() {
|
||||
fmt.Println(g.Config().GetVar("memcache.0").String())
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ import (
|
||||
"gitee.com/johng/gf/g"
|
||||
)
|
||||
|
||||
// 使用g.Config方法获取配置管理对象,并指定默认的配置文件名称
|
||||
func main() {
|
||||
fmt.Println(g.Config("config.json").Get("viewpath"))
|
||||
}
|
||||
|
||||
@ -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"))
|
||||
|
||||
12
geg/os/gcfg/gcfg_error.go
Normal file
12
geg/os/gcfg/gcfg_error.go
Normal file
@ -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"))
|
||||
}
|
||||
|
||||
@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user