diff --git a/g/container/gvar/gvar.go b/g/container/gvar/gvar.go new file mode 100644 index 000000000..a375e1b1b --- /dev/null +++ b/g/container/gvar/gvar.go @@ -0,0 +1,50 @@ +// 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" + "gitee.com/johng/gf/g/util/gconv" +) + +type Var struct { + value interface{} +} + +func New(value...interface{}) *Var { + v := &Var{} + if len(value) > 0 { + v.value = value[0] + } + return v +} + +func (v *Var) IsNil() bool { return v.value == nil } +func (v *Var) Bytes() []byte { return gconv.Bytes(v.value) } +func (v *Var) String() string { return gconv.String(v.value) } +func (v *Var) Bool() bool { return gconv.Bool(v.value) } + +func (v *Var) Int() int { return gconv.Int(v.value) } +func (v *Var) Int8() int8 { return gconv.Int8(v.value) } +func (v *Var) Int16() int16 { return gconv.Int16(v.value) } +func (v *Var) Int32() int32 { return gconv.Int32(v.value) } +func (v *Var) Int64() int64 { return gconv.Int64(v.value) } + +func (v *Var) Uint() uint { return gconv.Uint(v.value) } +func (v *Var) Uint8() uint8 { return gconv.Uint8(v.value) } +func (v *Var) Uint16() uint16 { return gconv.Uint16(v.value) } +func (v *Var) Uint32() uint32 { return gconv.Uint32(v.value) } +func (v *Var) Uint64() uint64 { return gconv.Uint64(v.value) } + +func (v *Var) Float32() float32 { return gconv.Float32(v.value) } +func (v *Var) Float64() float64 { return gconv.Float64(v.value) } + +func (v *Var) Strings() []string { return gconv.Strings(v.value) } + +func (v *Var) Time(format...string) time.Time { return gconv.Time(v.value, format...) } +func (v *Var) TimeDuration() time.Duration { return gconv.TimeDuration(v.value) } \ No newline at end of file diff --git a/g/g.go b/g/g.go index 552778979..c39f16268 100644 --- a/g/g.go +++ b/g/g.go @@ -7,26 +7,6 @@ package g -import ( - "gitee.com/johng/gf/g/os/gcfg" - "gitee.com/johng/gf/g/os/gview" - "gitee.com/johng/gf/g/util/gconv" - "gitee.com/johng/gf/g/frame/gins" - "gitee.com/johng/gf/g/os/gcache" - "gitee.com/johng/gf/g/os/gfsnotify" - "gitee.com/johng/gf/g/database/gdb" - "gitee.com/johng/gf/g/database/gredis" - "gitee.com/johng/gf/g/net/ghttp" - "gitee.com/johng/gf/g/net/gtcp" - "gitee.com/johng/gf/g/net/gudp" - "gitee.com/johng/gf/g/util/gregex" - "gitee.com/johng/gf/g/util/gutil" -) - -const ( - gIS_DATABASE_CONFIG_CACHED = "gf.core.component.database.cached" -) - // 常用map数据结构(使用别名) type Map = map[string]interface{} @@ -37,139 +17,3 @@ type List = []Map type Slice = []interface{} type Array = Slice -// 阻塞等待HTTPServer执行完成(同一进程多HTTPServer情况下) -func Wait() { - ghttp.Wait() -} - -// HTTPServer单例对象 -func Server(name...interface{}) *ghttp.Server { - return ghttp.GetServer(name...) -} - -// TCPServer单例对象 -func TcpServer(name...interface{}) *gtcp.Server { - return gtcp.GetServer(name...) -} - -// UDPServer单例对象 -func UdpServer(name...interface{}) *gudp.Server { - return gudp.GetServer(name...) -} - -// 核心对象:View -func View() *gview.View { - return gins.View() -} - -// Config配置管理对象 -// 配置文件目录查找依次为:启动参数cfgpath、当前程序运行目录 -func Config() *gcfg.Config { - return gins.Config() -} - -// 数据库操作对象,使用了连接池 -func Database(name...string) *gdb.Db { - config := gins.Config() - if config == nil { - return nil - } - // 数据库配置是否已经设置 - if gcache.Get(gIS_DATABASE_CONFIG_CACHED) == nil { - if m := config.GetMap("database"); m != nil { - c := gdb.Config{} - for group, v := range m { - cg := gdb.ConfigGroup{} - if list, ok := v.([]interface{}); ok { - for _, nodev := range list { - node := gdb.ConfigNode{} - nodem := nodev.(map[string]interface{}) - if value, ok := nodem["host"]; ok { - node.Host = gconv.String(value) - } - if value, ok := nodem["port"]; ok { - node.Port = gconv.String(value) - } - if value, ok := nodem["user"]; ok { - node.User = gconv.String(value) - } - if value, ok := nodem["pass"]; ok { - node.Pass = gconv.String(value) - } - if value, ok := nodem["name"]; ok { - node.Name = gconv.String(value) - } - if value, ok := nodem["type"]; ok { - node.Type = gconv.String(value) - } - if value, ok := nodem["role"]; ok { - node.Role = gconv.String(value) - } - if value, ok := nodem["charset"]; ok { - node.Charset = gconv.String(value) - } - if value, ok := nodem["priority"]; ok { - node.Priority = gconv.Int(value) - } - if value, ok := nodem["max-idle"]; ok { - node.MaxIdleConnCount = gconv.Int(value) - } - if value, ok := nodem["max-open"]; ok { - node.MaxOpenConnCount = gconv.Int(value) - } - if value, ok := nodem["max-lifetime"]; ok { - node.MaxConnLifetime = gconv.Int(value) - } - cg = append(cg, node) - } - } - c[group] = cg - } - gdb.SetConfig(c) - gcache.Set(gIS_DATABASE_CONFIG_CACHED, struct{}{}, 0) - // 使用gfsnotify进行文件监控,当配置文件有任何变化时,清空数据库配置缓存 - gfsnotify.Add(Config().GetFilePath(), func(event *gfsnotify.Event) { - gcache.Remove(gIS_DATABASE_CONFIG_CACHED) - }) - } - } - if db, err := gdb.New(name...); err == nil { - return db - } else { - return nil - } -} - -// Redis操作对象,使用了连接池 -func Redis(name...string) *gredis.Redis { - group := "default" - if len(name) > 0 { - group = name[0] - } - config := gins.Config() - if config == nil { - return nil - } - if m := config.GetMap("redis"); m != nil { - // host:port[,db[,pass]] - if v, ok := m[group]; ok { - array, err := gregex.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)`, gconv.String(v)) - if err == nil { - return gredis.New(gredis.Config{ - Host : array[1], - Port : gconv.Int(array[2]), - Db : gconv.Int(array[3]), - Pass : array[4], - }) - } - } - } - return nil -} - -// 打印变量 -func Dump(i...interface{}) { - gutil.Dump(i...) -} - - diff --git a/g/g_func.go b/g/g_func.go new file mode 100644 index 000000000..3882bf213 --- /dev/null +++ b/g/g_func.go @@ -0,0 +1,43 @@ +// Copyright 2017 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 g + +import ( + "gitee.com/johng/gf/g/net/ghttp" + "gitee.com/johng/gf/g/util/gutil" + "gitee.com/johng/gf/g/os/glog" +) + +const ( + LOG_LEVEL_ALL = glog.LEVEL_ALL + LOG_LEVEL_DEBU = glog.LEVEL_DEBU + LOG_LEVEL_INFO = glog.LEVEL_INFO + LOG_LEVEL_NOTI = glog.LEVEL_NOTI + LOG_LEVEL_WARN = glog.LEVEL_WARN + LOG_LEVEL_ERRO = glog.LEVEL_ERRO + LOG_LEVEL_CRIT = glog.LEVEL_CRIT +) + +// 阻塞等待HTTPServer执行完成(同一进程多HTTPServer情况下) +func Wait() { + ghttp.Wait() +} + +// 是否显示调试信息 +func SetDebug(debug bool) { + glog.SetDebug(debug) +} + +// 设置日志的显示等级 +func SetLogLevel(level int) { + glog.SetLevel(level) +} + +// 打印变量 +func Dump(i...interface{}) { + gutil.Dump(i...) +} diff --git a/g/g_logger.go b/g/g_logger.go new file mode 100644 index 000000000..6532bc601 --- /dev/null +++ b/g/g_logger.go @@ -0,0 +1,8 @@ +// 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 g + diff --git a/g/g_object.go b/g/g_object.go new file mode 100644 index 000000000..42cb4a351 --- /dev/null +++ b/g/g_object.go @@ -0,0 +1,151 @@ +// 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 g + +import ( + "gitee.com/johng/gf/g/util/gconv" + "gitee.com/johng/gf/g/database/gdb" + "gitee.com/johng/gf/g/os/gcache" + "gitee.com/johng/gf/g/os/gfsnotify" + "gitee.com/johng/gf/g/database/gredis" + "gitee.com/johng/gf/g/frame/gins" + "gitee.com/johng/gf/g/net/ghttp" + "gitee.com/johng/gf/g/net/gtcp" + "gitee.com/johng/gf/g/net/gudp" + "gitee.com/johng/gf/g/os/gview" + "gitee.com/johng/gf/g/os/gcfg" + "gitee.com/johng/gf/g/util/gregex" +) +const ( + gIS_DATABASE_CONFIG_CACHED = "gf.core.component.database.cached" +) + + +// HTTPServer单例对象 +func Server(name...interface{}) *ghttp.Server { + return ghttp.GetServer(name...) +} + +// TCPServer单例对象 +func TcpServer(name...interface{}) *gtcp.Server { + return gtcp.GetServer(name...) +} + +// UDPServer单例对象 +func UdpServer(name...interface{}) *gudp.Server { + return gudp.GetServer(name...) +} + +// 核心对象:View +func View() *gview.View { + return gins.View() +} + +// Config配置管理对象 +// 配置文件目录查找依次为:启动参数cfgpath、当前程序运行目录 +func Config() *gcfg.Config { + return gins.Config() +} + +// 数据库操作对象,使用了连接池 +func Database(name...string) *gdb.Db { + config := gins.Config() + if config == nil { + return nil + } + // 数据库配置是否已经设置 + if gcache.Get(gIS_DATABASE_CONFIG_CACHED) == nil { + if m := config.GetMap("database"); m != nil { + c := gdb.Config{} + for group, v := range m { + cg := gdb.ConfigGroup{} + if list, ok := v.([]interface{}); ok { + for _, nodev := range list { + node := gdb.ConfigNode{} + nodem := nodev.(map[string]interface{}) + if value, ok := nodem["host"]; ok { + node.Host = gconv.String(value) + } + if value, ok := nodem["port"]; ok { + node.Port = gconv.String(value) + } + if value, ok := nodem["user"]; ok { + node.User = gconv.String(value) + } + if value, ok := nodem["pass"]; ok { + node.Pass = gconv.String(value) + } + if value, ok := nodem["name"]; ok { + node.Name = gconv.String(value) + } + if value, ok := nodem["type"]; ok { + node.Type = gconv.String(value) + } + if value, ok := nodem["role"]; ok { + node.Role = gconv.String(value) + } + if value, ok := nodem["charset"]; ok { + node.Charset = gconv.String(value) + } + if value, ok := nodem["priority"]; ok { + node.Priority = gconv.Int(value) + } + if value, ok := nodem["max-idle"]; ok { + node.MaxIdleConnCount = gconv.Int(value) + } + if value, ok := nodem["max-open"]; ok { + node.MaxOpenConnCount = gconv.Int(value) + } + if value, ok := nodem["max-lifetime"]; ok { + node.MaxConnLifetime = gconv.Int(value) + } + cg = append(cg, node) + } + } + c[group] = cg + } + gdb.SetConfig(c) + gcache.Set(gIS_DATABASE_CONFIG_CACHED, struct{}{}, 0) + // 使用gfsnotify进行文件监控,当配置文件有任何变化时,清空数据库配置缓存 + gfsnotify.Add(Config().GetFilePath(), func(event *gfsnotify.Event) { + gcache.Remove(gIS_DATABASE_CONFIG_CACHED) + }) + } + } + if db, err := gdb.New(name...); err == nil { + return db + } else { + return nil + } +} + +// Redis操作对象,使用了连接池 +func Redis(name...string) *gredis.Redis { + group := "default" + if len(name) > 0 { + group = name[0] + } + config := gins.Config() + if config == nil { + return nil + } + if m := config.GetMap("redis"); m != nil { + // host:port[,db[,pass]] + if v, ok := m[group]; ok { + array, err := gregex.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)`, gconv.String(v)) + if err == nil { + return gredis.New(gredis.Config{ + Host : array[1], + Port : gconv.Int(array[2]), + Db : gconv.Int(array[3]), + Pass : array[4], + }) + } + } + } + return nil +} \ No newline at end of file diff --git a/g/os/glog/glog_logger.go b/g/os/glog/glog_logger.go index cad0ad2f0..922a450f7 100644 --- a/g/os/glog/glog_logger.go +++ b/g/os/glog/glog_logger.go @@ -74,9 +74,9 @@ func (l *Logger) GetLevel() int { // 快捷方法,打开或关闭DEBU日志信息 func (l *Logger) SetDebug(debug bool) { if debug { - l.level.Set(l.level.Val()|LEVEL_DEBU) + l.level.Set(l.level.Val() | LEVEL_DEBU) } else { - l.level.Set(l.level.Val()&^LEVEL_DEBU) + l.level.Set(l.level.Val() & ^LEVEL_DEBU) } } diff --git a/g/os/gtime/gtime.go b/g/os/gtime/gtime.go index e643968c2..863efd213 100644 --- a/g/os/gtime/gtime.go +++ b/g/os/gtime/gtime.go @@ -27,7 +27,6 @@ var ( func init() { // 使用正则判断会比直接使用ParseInLocation挨个轮训判断要快很多 timeRegex, _ = regexp.Compile(TIME_REAGEX_PATTERN) - } // 类似与js中的SetTimeout,一段时间后执行回调函数 diff --git a/g/os/gview/gview.go b/g/os/gview/gview.go index 4ec2e835f..8f9e0d592 100644 --- a/g/os/gview/gview.go +++ b/g/os/gview/gview.go @@ -41,10 +41,18 @@ type FuncMap = map[string]interface{} var viewMap = gmap.NewStringInterfaceMap() // 默认的视图对象 -var viewObj = Get(".") +var viewObj *View + +// 初始化默认的视图对象 +func checkAndInitDefaultView() { + if viewObj == nil { + viewObj = Get(".") + } +} // 直接解析模板内容,返回解析后的内容 func ParseContent(content string, params map[string]interface{}) ([]byte, error) { + checkAndInitDefaultView() return viewObj.ParseContent(content, params) } diff --git a/geg/util/gconv/gconv_struct.go b/geg/util/gconv/gconv_struct1.go similarity index 100% rename from geg/util/gconv/gconv_struct.go rename to geg/util/gconv/gconv_struct1.go diff --git a/geg/util/gconv/gconv_struct2.go b/geg/util/gconv/gconv_struct2.go new file mode 100644 index 000000000..bcb40d688 --- /dev/null +++ b/geg/util/gconv/gconv_struct2.go @@ -0,0 +1,26 @@ +package main + +import ( + "gitee.com/johng/gf/g/util/gconv" + "gitee.com/johng/gf/g" + "fmt" +) + +// 演示slice类型属性的赋值 +func main() { + type User struct { + Scores []int + } + + user := new(User) + scores := []int{99, 100, 60, 140} + + err := gconv.MapToStruct(g.Map{ + "Scores" : scores, + }, user) + if err != nil { + fmt.Println(err) + } else { + g.Dump(user) + } +} \ No newline at end of file