增加gvar包,并改进默认的g包

This commit is contained in:
john
2018-09-26 09:58:49 +08:00
parent 8ece3b79a6
commit 350f92f4a0
10 changed files with 289 additions and 160 deletions

50
g/container/gvar/gvar.go Normal file
View File

@ -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) }

156
g/g.go
View File

@ -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...)
}

43
g/g_func.go Normal file
View File

@ -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...)
}

8
g/g_logger.go Normal file
View File

@ -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

151
g/g_object.go Normal file
View File

@ -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
}

View File

@ -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)
}
}

View File

@ -27,7 +27,6 @@ var (
func init() {
// 使用正则判断会比直接使用ParseInLocation挨个轮训判断要快很多
timeRegex, _ = regexp.Compile(TIME_REAGEX_PATTERN)
}
// 类似与js中的SetTimeout一段时间后执行回调函数

View File

@ -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)
}

View File

@ -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)
}
}