mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
同步master
This commit is contained in:
@ -1,7 +0,0 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
|
||||
|
||||
package container
|
||||
@ -1,7 +0,0 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
|
||||
|
||||
package crypto
|
||||
@ -1,7 +0,0 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
|
||||
|
||||
package database
|
||||
@ -107,7 +107,7 @@ func New(config Config) *Redis {
|
||||
// return redis instance with default group.
|
||||
//
|
||||
// 获取指定分组名称的Redis单例对象,底层根据配置信息公用的连接池(连接池单例)。
|
||||
func Instance(name...string) *Redis {
|
||||
func Instance(name ...string) *Redis {
|
||||
group := DEFAULT_GROUP_NAME
|
||||
if len(name) > 0 {
|
||||
group = name[0]
|
||||
|
||||
@ -1,7 +0,0 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
|
||||
|
||||
package encoding
|
||||
@ -1,7 +0,0 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
|
||||
|
||||
package frame
|
||||
8
g/g.go
8
g/g.go
@ -9,13 +9,9 @@ package g
|
||||
import "github.com/gogf/gf/g/container/gvar"
|
||||
|
||||
// Universal variable type, like generics.
|
||||
//
|
||||
// 动态变量类型,可以用该类型替代interface{}类型
|
||||
type Var = gvar.Var
|
||||
|
||||
// Frequently-used map type alias.
|
||||
//
|
||||
// 常用map数据结构(使用别名)
|
||||
type Map = map[string]interface{}
|
||||
type MapAnyAny = map[interface{}]interface{}
|
||||
type MapAnyStr = map[interface{}]string
|
||||
@ -28,8 +24,6 @@ type MapIntStr = map[int]string
|
||||
type MapIntInt = map[int]int
|
||||
|
||||
// Frequently-used slice type alias.
|
||||
//
|
||||
// 常用list数据结构(使用别名)
|
||||
type List = []Map
|
||||
type ListAnyStr = []map[interface{}]string
|
||||
type ListAnyInt = []map[interface{}]int
|
||||
@ -41,8 +35,6 @@ type ListIntStr = []map[int]string
|
||||
type ListIntInt = []map[int]int
|
||||
|
||||
// Frequently-used slice type alias.
|
||||
//
|
||||
// 常用slice数据结构(使用别名)
|
||||
type Slice = []interface{}
|
||||
type SliceAny = []interface{}
|
||||
type SliceStr = []string
|
||||
|
||||
22
g/g_func.go
22
g/g_func.go
@ -13,38 +13,28 @@ import (
|
||||
"github.com/gogf/gf/g/util/gutil"
|
||||
)
|
||||
|
||||
// NewVar creates a *Var.
|
||||
//
|
||||
// 动态变量
|
||||
// NewVar returns a *gvar.Var.
|
||||
func NewVar(i interface{}, unsafe...bool) *Var {
|
||||
return gvar.New(i, unsafe...)
|
||||
}
|
||||
|
||||
// Wait blocks until all the web servers shutdown.
|
||||
//
|
||||
// 阻塞等待HTTPServer执行完成(同一进程多HTTPServer情况下)
|
||||
func Wait() {
|
||||
ghttp.Wait()
|
||||
}
|
||||
|
||||
// Dump dumps a variable to stdout with more manually readable.
|
||||
//
|
||||
// 格式化打印变量.
|
||||
func Dump(i...interface{}) {
|
||||
gutil.Dump(i...)
|
||||
}
|
||||
|
||||
// Export exports a variable to string with more manually readable.
|
||||
//
|
||||
// 格式化导出变量.
|
||||
func Export(i...interface{}) string {
|
||||
return gutil.Export(i...)
|
||||
}
|
||||
|
||||
// Throw throws a exception, which can be caught by Catch function.
|
||||
// Throw throws a exception, which can be caught by TryCatch function.
|
||||
// It always be used in TryCatch function.
|
||||
//
|
||||
// 抛出一个异常
|
||||
func Throw(exception interface{}) {
|
||||
gutil.Throw(exception)
|
||||
}
|
||||
@ -55,12 +45,8 @@ func TryCatch(try func(), catch ... func(exception interface{})) {
|
||||
}
|
||||
|
||||
// IsEmpty checks given value empty or not.
|
||||
// false: integer(0), bool(false), slice/map(len=0), nil;
|
||||
// true : other.
|
||||
//
|
||||
// 判断给定的变量是否为空。
|
||||
// 整型为0, 布尔为false, slice/map长度为0, 其他为nil的情况,都为空。
|
||||
// 为空时返回true,否则返回false。
|
||||
// It returns false if value is: integer(0), bool(false), slice/map(len=0), nil;
|
||||
// or else true.
|
||||
func IsEmpty(value interface{}) bool {
|
||||
return empty.IsEmpty(value)
|
||||
}
|
||||
@ -10,23 +10,17 @@ import (
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
)
|
||||
|
||||
// Disable/Enabled debug of logging globally.
|
||||
//
|
||||
// 是否显示调试信息
|
||||
// SetDebug disables/enables debug level for logging globally.
|
||||
func SetDebug(debug bool) {
|
||||
glog.SetDebug(debug)
|
||||
}
|
||||
|
||||
// Set the logging level globally.
|
||||
//
|
||||
// 设置日志的显示等级
|
||||
// SetLogLevel sets the logging level globally.
|
||||
func SetLogLevel(level int) {
|
||||
glog.SetLevel(level)
|
||||
}
|
||||
|
||||
// Get the global logging level.
|
||||
//
|
||||
// 获取设置的日志显示等级
|
||||
// GetLogLevel returns the global logging level.
|
||||
func GetLogLevel() int {
|
||||
return glog.GetLevel()
|
||||
}
|
||||
@ -17,59 +17,42 @@ import (
|
||||
"github.com/gogf/gf/g/os/gcfg"
|
||||
)
|
||||
|
||||
// Get an instance of http server with specified name.
|
||||
//
|
||||
// HTTPServer单例对象
|
||||
// Server returns an instance of http server with specified name.
|
||||
func Server(name...interface{}) *ghttp.Server {
|
||||
return ghttp.GetServer(name...)
|
||||
}
|
||||
|
||||
// Get an instance of tcp server with specified name.
|
||||
//
|
||||
// TCPServer单例对象
|
||||
// TCPServer returns an instance of tcp server with specified name.
|
||||
func TCPServer(name...interface{}) *gtcp.Server {
|
||||
return gtcp.GetServer(name...)
|
||||
}
|
||||
|
||||
// Get an instance of udp server with specified name.
|
||||
//
|
||||
// UDPServer单例对象
|
||||
// UDPServer returns an instance of udp server with specified name.
|
||||
func UDPServer(name...interface{}) *gudp.Server {
|
||||
return gudp.GetServer(name...)
|
||||
}
|
||||
|
||||
// Get an instance of template engine object with specified name.
|
||||
//
|
||||
// 核心对象:View
|
||||
// View returns an instance of template engine object with specified name.
|
||||
func View(name...string) *gview.View {
|
||||
return gins.View(name...)
|
||||
}
|
||||
|
||||
// Get an instance of config object with specified default config file name.
|
||||
//
|
||||
// Config配置管理对象,
|
||||
// 配置文件目录查找依次为:启动参数cfgpath、当前程序运行目录
|
||||
// Config returns an instance of config object with specified name.
|
||||
func Config(file...string) *gcfg.Config {
|
||||
return gins.Config(file...)
|
||||
}
|
||||
|
||||
// Get an instance of database ORM object with specified configuration group name.
|
||||
//
|
||||
// 数据库操作对象,使用了连接池
|
||||
// Database returns an instance of database ORM object with specified configuration group name.
|
||||
func Database(name...string) gdb.DB {
|
||||
return gins.Database(name...)
|
||||
}
|
||||
|
||||
// Alias of Database.
|
||||
//
|
||||
// (别名)Database
|
||||
// Alias of Database. See Database.
|
||||
func DB(name...string) gdb.DB {
|
||||
return gins.Database(name...)
|
||||
}
|
||||
|
||||
// Get an instance of redis client with specified configuration group name.
|
||||
//
|
||||
// Redis操作对象,使用了连接池
|
||||
// Redis returns an instance of redis client with specified configuration group name.
|
||||
func Redis(name...string) *gredis.Redis {
|
||||
return gins.Redis(name...)
|
||||
}
|
||||
@ -8,9 +8,8 @@ package g
|
||||
|
||||
import "github.com/gogf/gf/g/net/ghttp"
|
||||
|
||||
// SetServerGraceful enables/disables graceful reload feature of ghttp Web Server.
|
||||
//
|
||||
// 是否开启WebServer的平滑重启特性。
|
||||
// SetServerGraceful enables/disables graceful/hot reload feature of http Web Server.
|
||||
// This feature is disabled in default.
|
||||
func SetServerGraceful(enabled bool) {
|
||||
ghttp.SetGraceful(enabled)
|
||||
}
|
||||
|
||||
@ -1 +0,0 @@
|
||||
package net
|
||||
@ -100,12 +100,13 @@ func (c *Config) filePath(file...string) (path string) {
|
||||
return path
|
||||
}
|
||||
|
||||
// 设置配置管理器的配置文件存放目录绝对路径
|
||||
// SetPath sets the configuration directory path for file search.
|
||||
// The param <path> can be absolute or relative path, but absolute path is suggested.
|
||||
func (c *Config) SetPath(path string) error {
|
||||
// 判断绝对路径(或者工作目录下目录)
|
||||
// Absolute path.
|
||||
realPath := gfile.RealPath(path)
|
||||
if realPath == "" {
|
||||
// 判断相对路径
|
||||
// Relative path.
|
||||
c.paths.RLockFunc(func(array []string) {
|
||||
for _, v := range array {
|
||||
if path, _ := gspath.Search(v, path); path != "" {
|
||||
@ -115,7 +116,7 @@ func (c *Config) SetPath(path string) error {
|
||||
}
|
||||
})
|
||||
}
|
||||
// 目录不存在错误处理
|
||||
// Path not exist.
|
||||
if realPath == "" {
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
if c.paths.Len() > 0 {
|
||||
@ -132,13 +133,13 @@ func (c *Config) SetPath(path string) error {
|
||||
glog.Error(err)
|
||||
return err
|
||||
}
|
||||
// 路径必须为目录类型
|
||||
// Should be a directory.
|
||||
if !gfile.IsDir(realPath) {
|
||||
err := errors.New(fmt.Sprintf(`[gcfg] SetPath failed: path "%s" should be directory type`, path))
|
||||
glog.Error(err)
|
||||
return err
|
||||
}
|
||||
// 重复判断
|
||||
// Repeated path check.
|
||||
if c.paths.Search(realPath) != -1 {
|
||||
return nil
|
||||
}
|
||||
@ -149,19 +150,23 @@ func (c *Config) SetPath(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 设置是否执行层级冲突检查,当键名中存在层级符号时需要开启该特性,默认为关闭。
|
||||
// 开启比较耗性能,也不建议允许键名中存在分隔符,最好在应用端避免这种情况。
|
||||
// SetViolenceCheck sets whether to perform level conflict check.
|
||||
// This feature needs to be enabled when there is a level symbol in the key name.
|
||||
// The default is off.
|
||||
// Turning on this feature is quite expensive,
|
||||
// and it is not recommended to allow separators in the key names.
|
||||
// It is best to avoid this on the application side.
|
||||
func (c *Config) SetViolenceCheck(check bool) {
|
||||
c.vc.Set(check)
|
||||
c.Clear()
|
||||
}
|
||||
|
||||
// 添加配置管理器的配置文件搜索路径
|
||||
// AddPath adds a absolute or relative path to the search paths.
|
||||
func (c *Config) AddPath(path string) error {
|
||||
// 判断绝对路径(或者工作目录下目录)
|
||||
// Absolute path.
|
||||
realPath := gfile.RealPath(path)
|
||||
if realPath == "" {
|
||||
// 判断相对路径
|
||||
// Relative path.
|
||||
c.paths.RLockFunc(func(array []string) {
|
||||
for _, v := range array {
|
||||
if path, _ := gspath.Search(v, path); path != "" {
|
||||
@ -171,7 +176,6 @@ func (c *Config) AddPath(path string) error {
|
||||
}
|
||||
})
|
||||
}
|
||||
// 目录不存在错误处理
|
||||
if realPath == "" {
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
if c.paths.Len() > 0 {
|
||||
@ -188,13 +192,12 @@ func (c *Config) AddPath(path string) error {
|
||||
glog.Error(err)
|
||||
return err
|
||||
}
|
||||
// 路径必须为目录类型
|
||||
if !gfile.IsDir(realPath) {
|
||||
err := errors.New(fmt.Sprintf(`[gcfg] AddPath failed: path "%s" should be directory type`, path))
|
||||
glog.Error(err)
|
||||
return err
|
||||
}
|
||||
// 重复判断
|
||||
// Repeated path check.
|
||||
if c.paths.Search(realPath) != -1 {
|
||||
return nil
|
||||
}
|
||||
@ -203,8 +206,10 @@ func (c *Config) AddPath(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 查找配置文件,获取指定配置文件的绝对路径,默认获取默认的配置文件路径;
|
||||
// 当指定的配置文件不存在时,返回空字符串,并且不会报错。
|
||||
// GetFilePath returns the absolute path of the specified configuration file.
|
||||
// If <file> is not passed, it returns the configuration file path of the default name.
|
||||
// If the specified configuration file does not exist,
|
||||
// an empty string is returned.
|
||||
func (c *Config) GetFilePath(file...string) (path string) {
|
||||
name := c.name.Val()
|
||||
if len(file) > 0 {
|
||||
@ -225,19 +230,20 @@ func (c *Config) GetFilePath(file...string) (path string) {
|
||||
return
|
||||
}
|
||||
|
||||
// 设置配置管理对象的默认文件名称
|
||||
// SetFileName sets the default configuration file name.
|
||||
func (c *Config) SetFileName(name string) {
|
||||
//glog.Debug("[gcfg] SetFileName:", name)
|
||||
c.name.Set(name)
|
||||
}
|
||||
|
||||
// 获取配置管理对象的默认文件名称
|
||||
// GetFileName returns the default configuration file name.
|
||||
func (c *Config) GetFileName() string {
|
||||
return c.name.Val()
|
||||
}
|
||||
|
||||
// 添加配置文件到配置管理器中,第二个参数为非必须,如果不输入表示添加进入默认的配置名称中
|
||||
// 内部带缓存控制功能。
|
||||
// getJson returns a gjson.Json object for the specified <file> content.
|
||||
// It would print error if file reading fails.
|
||||
// If any error occurs, it return nil.
|
||||
func (c *Config) getJson(file...string) *gjson.Json {
|
||||
name := c.name.Val()
|
||||
if len(file) > 0 {
|
||||
@ -255,7 +261,8 @@ func (c *Config) getJson(file...string) *gjson.Json {
|
||||
}
|
||||
if j, err := gjson.LoadContent(content); err == nil {
|
||||
j.SetViolenceCheck(c.vc.Val())
|
||||
// 添加配置文件监听,如果有任何变化,删除文件内容缓存,下一次查询会自动更新
|
||||
// Add monitor for this configuration file,
|
||||
// any changes of this file will refresh its cache in Config object.
|
||||
if filePath != "" {
|
||||
gfsnotify.Add(filePath, func(event *gfsnotify.Event) {
|
||||
c.jsons.Remove(name)
|
||||
@ -277,7 +284,6 @@ func (c *Config) getJson(file...string) *gjson.Json {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取配置项,当不存在时返回nil
|
||||
func (c *Config) Get(pattern string, file...string) interface{} {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.Get(pattern)
|
||||
@ -285,7 +291,6 @@ func (c *Config) Get(pattern string, file...string) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获得配置项,返回动态变量
|
||||
func (c *Config) GetVar(pattern string, file...string) gvar.VarRead {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return gvar.New(j.Get(pattern), true)
|
||||
@ -293,7 +298,6 @@ func (c *Config) GetVar(pattern string, file...string) gvar.VarRead {
|
||||
return gvar.New(nil, true)
|
||||
}
|
||||
|
||||
// 判断指定的配置项是否存在
|
||||
func (c *Config) Contains(pattern string, file...string) bool {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.Contains(pattern)
|
||||
@ -301,8 +305,6 @@ func (c *Config) Contains(pattern string, file...string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// 获得一个键值对关联数组/哈希表,方便操作,不需要自己做类型转换
|
||||
// 注意,如果获取的值不存在,或者类型与json类型不匹配,那么将会返回nil
|
||||
func (c *Config) GetMap(pattern string, file...string) map[string]interface{} {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetMap(pattern)
|
||||
@ -310,8 +312,6 @@ func (c *Config) GetMap(pattern string, file...string) map[string]interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获得一个数组[]interface{},方便操作,不需要自己做类型转换
|
||||
// 注意,如果获取的值不存在,或者类型与json类型不匹配,那么将会返回nil
|
||||
func (c *Config) GetArray(pattern string, file...string) []interface{} {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetArray(pattern)
|
||||
@ -319,7 +319,6 @@ func (c *Config) GetArray(pattern string, file...string) []interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 返回指定json中的string
|
||||
func (c *Config) GetString(pattern string, file...string) string {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetString(pattern)
|
||||
@ -341,7 +340,6 @@ func (c *Config) GetInterfaces(pattern string, file...string) []interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 返回指定json中的bool
|
||||
func (c *Config) GetBool(pattern string, file...string) bool {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetBool(pattern)
|
||||
@ -349,7 +347,6 @@ func (c *Config) GetBool(pattern string, file...string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// 返回指定json中的float32
|
||||
func (c *Config) GetFloat32(pattern string, file...string) float32 {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetFloat32(pattern)
|
||||
@ -357,7 +354,6 @@ func (c *Config) GetFloat32(pattern string, file...string) float32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// 返回指定json中的float64
|
||||
func (c *Config) GetFloat64(pattern string, file...string) float64 {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetFloat64(pattern)
|
||||
@ -372,7 +368,6 @@ func (c *Config) GetFloats(pattern string, file...string) []float64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 返回指定json中的float64->int
|
||||
func (c *Config) GetInt(pattern string, file...string) int {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetInt(pattern)
|
||||
@ -416,7 +411,6 @@ func (c *Config) GetInts(pattern string, file...string) []int {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 返回指定json中的float64->uint
|
||||
func (c *Config) GetUint(pattern string, file...string) uint {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetUint(pattern)
|
||||
|
||||
@ -125,6 +125,7 @@ func Datetime() string {
|
||||
|
||||
// 解析日期字符串(日期支持'-'或'/'或'.'连接符号)
|
||||
func parseDateStr(s string) (year, month, day int) {
|
||||
<<<<<<< HEAD
|
||||
array := strings.Split(s, "-")
|
||||
if len(array) < 3 {
|
||||
array = strings.Split(s, "/")
|
||||
@ -155,6 +156,38 @@ func parseDateStr(s string) (year, month, day int) {
|
||||
year = int(time.Now().Year()/100)*100 + year
|
||||
}
|
||||
return
|
||||
=======
|
||||
array := strings.Split(s, "-")
|
||||
if len(array) < 3 {
|
||||
array = strings.Split(s, "/")
|
||||
}
|
||||
if len(array) < 3 {
|
||||
array = strings.Split(s, ".")
|
||||
}
|
||||
// 解析失败
|
||||
if len(array) < 3 {
|
||||
return
|
||||
}
|
||||
// 判断年份在开头还是末尾
|
||||
if isNumeric(array[1]) {
|
||||
year, _ = strconv.Atoi(array[0])
|
||||
month, _ = strconv.Atoi(array[1])
|
||||
day, _ = strconv.Atoi(array[2])
|
||||
} else {
|
||||
if v, ok := monthMap[strings.ToLower(array[1])]; ok {
|
||||
month = v
|
||||
} else {
|
||||
return
|
||||
}
|
||||
year, _ = strconv.Atoi(array[2])
|
||||
day, _ = strconv.Atoi(array[0])
|
||||
}
|
||||
// 年是否为缩写,如果是,那么需要补上前缀
|
||||
if year < 100 {
|
||||
year = int(time.Now().Year()/100)*100 + year
|
||||
}
|
||||
return
|
||||
>>>>>>> 68949b69bc9e9e60a4a131150539239d04715d69
|
||||
}
|
||||
|
||||
// 字符串转换为时间对象,format参数指定格式的format(如: Y-m-d H:i:s),当指定format参数时效果同StrToTimeFormat方法。
|
||||
|
||||
@ -9,7 +9,8 @@ package gtime
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/gogf/gf/g/text/gregex"
|
||||
"strings"
|
||||
"github.com/gogf/gf/g/text/gstr"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -126,8 +127,8 @@ func (t *Time) Format(format string) string {
|
||||
result := t.Time.Format(f)
|
||||
// 有几个转换的符号需要特殊处理
|
||||
switch runes[i] {
|
||||
case 'j': buffer.WriteString(strings.Replace(result, "=j=0", "", -1))
|
||||
case 'G': buffer.WriteString(strings.Replace(result, "=G=0", "", -1))
|
||||
case 'j': buffer.WriteString(gstr.ReplaceByArray(result, []string{"=j=0", "", "=j=", ""}))
|
||||
case 'G': buffer.WriteString(gstr.ReplaceByArray(result, []string{"=G=0", "", "=G=", ""}))
|
||||
case 'u': buffer.WriteString(strings.Replace(result, "=u=.", "", -1))
|
||||
default:
|
||||
buffer.WriteString(result)
|
||||
|
||||
@ -1 +0,0 @@
|
||||
package os
|
||||
@ -4,9 +4,7 @@
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// Package gtest provides simple and useful test utils.
|
||||
//
|
||||
// 测试模块.
|
||||
// Package gtest provides convenient test utils for unit testing.
|
||||
package gtest
|
||||
|
||||
import (
|
||||
@ -19,7 +17,9 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 封装一个测试用例
|
||||
// Case creates an unit test case.
|
||||
// The param <t> is the pointer to testing.T of stdlib (*testing.T).
|
||||
// The param <f> is the callback function for unit test case.
|
||||
func Case(t *testing.T, f func()) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
@ -30,7 +30,7 @@ func Case(t *testing.T, f func()) {
|
||||
f()
|
||||
}
|
||||
|
||||
// 断言判断, 相等
|
||||
// Assert checks <value> and <expect> EQUAL.
|
||||
func Assert(value, expect interface{}) {
|
||||
rvValue := reflect.ValueOf(value)
|
||||
rvExpect := reflect.ValueOf(expect)
|
||||
@ -50,14 +50,9 @@ func Assert(value, expect interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// 断言判断, 相等, 包括数据类型
|
||||
// AssertEQ checks <value> and <expect> EQUAL, including their TYPES.
|
||||
func AssertEQ(value, expect interface{}) {
|
||||
// 类型判断
|
||||
t1 := reflect.TypeOf(value)
|
||||
t2 := reflect.TypeOf(expect)
|
||||
if t1 != t2 {
|
||||
panic(fmt.Sprintf(`[ASSERT] EXPECT TYPE %v == %v`, t1, t2))
|
||||
}
|
||||
// Value assert.
|
||||
rvValue := reflect.ValueOf(value)
|
||||
rvExpect := reflect.ValueOf(expect)
|
||||
if rvValue.Kind() == reflect.Ptr {
|
||||
@ -74,9 +69,15 @@ func AssertEQ(value, expect interface{}) {
|
||||
if fmt.Sprintf("%v", value) != fmt.Sprintf("%v", expect) {
|
||||
panic(fmt.Sprintf(`[ASSERT] EXPECT %v == %v`, value, expect))
|
||||
}
|
||||
// Type assert.
|
||||
t1 := reflect.TypeOf(value)
|
||||
t2 := reflect.TypeOf(expect)
|
||||
if t1 != t2 {
|
||||
panic(fmt.Sprintf(`[ASSERT] EXPECT TYPE %v[%v] == %v[%v]`, value, t1, expect, t2))
|
||||
}
|
||||
}
|
||||
|
||||
// 断言判断, 不相等
|
||||
// AssertNE checks <value> and <expect> NOT EQUAL.
|
||||
func AssertNE(value, expect interface{}) {
|
||||
rvValue := reflect.ValueOf(value)
|
||||
rvExpect := reflect.ValueOf(expect)
|
||||
@ -96,7 +97,9 @@ func AssertNE(value, expect interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// 断言判断, value > expect; 注意: 仅有字符串、整形、浮点型才可以比较
|
||||
// AssertGT checks <value> is GREATER THAN <expect>.
|
||||
// Notice that, only string, integer and float types can be compared by AssertGT,
|
||||
// others are invalid.
|
||||
func AssertGT(value, expect interface{}) {
|
||||
passed := false
|
||||
switch reflect.ValueOf(expect).Kind() {
|
||||
@ -117,7 +120,9 @@ func AssertGT(value, expect interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// 断言判断, value >= expect; 注意: 仅有字符串、整形、浮点型才可以比较
|
||||
// AssertGTE checks <value> is GREATER OR EQUAL THAN <expect>.
|
||||
// Notice that, only string, integer and float types can be compared by AssertGTE,
|
||||
// others are invalid.
|
||||
func AssertGTE(value, expect interface{}) {
|
||||
passed := false
|
||||
switch reflect.ValueOf(expect).Kind() {
|
||||
@ -138,7 +143,9 @@ func AssertGTE(value, expect interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// 断言判断, value < expect; 注意: 仅有字符串、整形、浮点型才可以比较
|
||||
// AssertLT checks <value> is LESS EQUAL THAN <expect>.
|
||||
// Notice that, only string, integer and float types can be compared by AssertLT,
|
||||
// others are invalid.
|
||||
func AssertLT(value, expect interface{}) {
|
||||
passed := false
|
||||
switch reflect.ValueOf(expect).Kind() {
|
||||
@ -159,7 +166,9 @@ func AssertLT(value, expect interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// 断言判断, value <= expect; 注意: 仅有字符串、整形、浮点型才可以比较
|
||||
// AssertLTE checks <value> is LESS OR EQUAL THAN <expect>.
|
||||
// Notice that, only string, integer and float types can be compared by AssertLTE,
|
||||
// others are invalid.
|
||||
func AssertLTE(value, expect interface{}) {
|
||||
passed := false
|
||||
switch reflect.ValueOf(expect).Kind() {
|
||||
@ -180,16 +189,18 @@ func AssertLTE(value, expect interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 断言判断, value IN expect; 注意: expect必须为slice类型。
|
||||
// 注意:value参数可以为普通变量,也可以为slice类型。
|
||||
// AssertIN checks <value> is IN <expect>.
|
||||
// The <expect> should be a slice,
|
||||
// but the <value> can be a slice or a basic type variable.
|
||||
// TODO map support.
|
||||
func AssertIN(value, expect interface{}) {
|
||||
passed := true
|
||||
switch reflect.ValueOf(expect).Kind() {
|
||||
case reflect.Slice, reflect.Array:
|
||||
expectSlice := gconv.Interfaces(expect)
|
||||
for _, v1 := range gconv.Interfaces(value) {
|
||||
result := false
|
||||
for _, v2 := range gconv.Interfaces(expect) {
|
||||
for _, v2 := range expectSlice {
|
||||
if v1 == v2 {
|
||||
result = true
|
||||
break
|
||||
@ -206,7 +217,10 @@ func AssertIN(value, expect interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// 断言判断, value NOT IN expect; 注意: expect必须为slice类型
|
||||
// AssertNI checks <value> is NOT IN <expect>.
|
||||
// The <expect> should be a slice,
|
||||
// but the <value> can be a slice or a basic type variable.
|
||||
// TODO map support.
|
||||
func AssertNI(value, expect interface{}) {
|
||||
passed := true
|
||||
switch reflect.ValueOf(expect).Kind() {
|
||||
@ -230,18 +244,18 @@ func AssertNI(value, expect interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// 提示错误不退出进程执行
|
||||
// Error panics with given <message>.
|
||||
func Error(message...interface{}) {
|
||||
panic(fmt.Sprintf("[ERROR] %s", fmt.Sprint(message...)))
|
||||
}
|
||||
|
||||
// 提示错误并退出进程执行
|
||||
// Fatal prints <message> to stderr and exit the process.
|
||||
func Fatal(message...interface{}) {
|
||||
fmt.Fprintf(os.Stderr, "[FATAL] %s\n%s", fmt.Sprint(message...), getBacktrace())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Map比较,如果相等返回nil,否则返回错误信息.
|
||||
// compareMap compares two maps, returns nil if they are equal, or else returns error.
|
||||
func compareMap(value, expect interface{}) error {
|
||||
rvValue := reflect.ValueOf(value)
|
||||
rvExpect := reflect.ValueOf(expect)
|
||||
@ -253,8 +267,9 @@ func compareMap(value, expect interface{}) error {
|
||||
if rvExpect.Kind() == reflect.Map {
|
||||
if rvValue.Kind() == reflect.Map {
|
||||
if rvExpect.Len() == rvValue.Len() {
|
||||
// 将两个map类型转换为同一个map类型, 才能执行比较,
|
||||
// 直接使用 rvValue.MapIndex(key).Interface() 当key类型不一致时会报错。
|
||||
// Turn two interface maps to the same type for comparison.
|
||||
// Direct use of rvValue.MapIndex(key).Interface() will panic
|
||||
// when the key types are inconsistent.
|
||||
mValue := make(map[string]string)
|
||||
mExpect := make(map[string]string)
|
||||
ksValue := rvValue.MapKeys()
|
||||
@ -280,7 +295,8 @@ func compareMap(value, expect interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取文件调用回溯字符串,参数skip表示调用端往上多少级开始回溯
|
||||
// getBacktrace returns the caller backtrace content from getBacktrace.
|
||||
// The param <skip> indicates the skip count of the caller backtrace from getBacktrace.
|
||||
func getBacktrace(skip...int) string {
|
||||
customSkip := 0
|
||||
if len(skip) > 0 {
|
||||
@ -289,7 +305,7 @@ func getBacktrace(skip...int) string {
|
||||
backtrace := ""
|
||||
index := 1
|
||||
from := 0
|
||||
// 首先定位业务文件开始位置
|
||||
// Ignore current gtest lines and find the beginning index of caller file.
|
||||
for i := 0; i < 10; i++ {
|
||||
if _, file, _, ok := runtime.Caller(i); ok {
|
||||
if reg, _ := regexp.Compile(`gtest\.go$`); !reg.MatchString(file) {
|
||||
@ -298,7 +314,7 @@ func getBacktrace(skip...int) string {
|
||||
}
|
||||
}
|
||||
}
|
||||
// 从业务文件开始位置根据自定义的skip开始backtrace
|
||||
// Get the caller backtrace from business caller file.
|
||||
goRoot := runtime.GOROOT()
|
||||
for i := from + customSkip; i < 10000; i++ {
|
||||
if _, file, cline, ok := runtime.Caller(i); ok && file != "" {
|
||||
|
||||
@ -1 +0,0 @@
|
||||
package util
|
||||
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"time"
|
||||
)
|
||||
@ -19,15 +20,11 @@ func main() {
|
||||
"2018-02-09",
|
||||
"2017/12/14 04:51:34 +0805 LMT",
|
||||
"2018/02/09 12:00:15",
|
||||
"18/02/09 12:16",
|
||||
"18/02/09 12",
|
||||
"18/02/09 +0805 LMT",
|
||||
"01/Nov/2018:13:28:13 +0800",
|
||||
"01-Nov-2018 11:50:28 +0805 LMT",
|
||||
"01-Nov-2018T15:04:05Z07:00",
|
||||
"01-Nov-2018T01:19:15+08:00",
|
||||
"01-Nov-2018 11:50:28 +0805 LMT",
|
||||
"01/Nov/18 11:50:28",
|
||||
"01/Nov/2018 11:50:28",
|
||||
"01/Nov/2018:11:50:28",
|
||||
"01.Nov.2018:11:50:28",
|
||||
@ -35,12 +32,12 @@ func main() {
|
||||
}
|
||||
cstLocal, _ := time.LoadLocation("Asia/Shanghai")
|
||||
for _, s := range array {
|
||||
fmt.Println(s)
|
||||
if t, err := gtime.StrToTime(s); err == nil {
|
||||
fmt.Println(t.String())
|
||||
fmt.Println(s)
|
||||
fmt.Println(t.UTC().String())
|
||||
fmt.Println(t.In(cstLocal).String())
|
||||
} else {
|
||||
panic(err)
|
||||
glog.Error(s, err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
@ -2,10 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/gcfg"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(gcfg.Instance().GetString("viewpath"))
|
||||
fmt.Println(gcfg.Instance().GetString("database.default.0.host"))
|
||||
fmt.Println(gtime.Now().Format(`Y-m-j G:i:su`))
|
||||
}
|
||||
Reference in New Issue
Block a user