feat(database): 新增命令行参数解析、空值检查和数据库配置管理功能
- 实现了 command 包用于控制台参数和选项解析 - 添加了 empty 包提供变量空值/零值检查功能 - 集成了 Viper 库进行配置文件管理和环境变量支持 - 提供了数据库配置初始化和默认值设置功能 - 实现了配置值获取、设置和结构化解析方法 - 添加了服务器、数据库和JWT配置的便捷获取方法 - 实现了完整的数据库接口定义和事务管理功能
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). 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 instance provides instances management.
|
||||
//
|
||||
// Note that this package is not used for cache, as it has no cache expiration.
|
||||
package instance
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
"github.com/gogf/gf/v2/encoding/ghash"
|
||||
)
|
||||
|
||||
const (
|
||||
groupNumber = 64
|
||||
)
|
||||
|
||||
var (
|
||||
groups = make([]*gmap.StrAnyMap, groupNumber)
|
||||
)
|
||||
|
||||
func init() {
|
||||
for i := 0; i < groupNumber; i++ {
|
||||
groups[i] = gmap.NewStrAnyMap(true)
|
||||
}
|
||||
}
|
||||
|
||||
func getGroup(key string) *gmap.StrAnyMap {
|
||||
return groups[int(ghash.DJB([]byte(key))%groupNumber)]
|
||||
}
|
||||
|
||||
// Get returns the instance by given name.
|
||||
func Get(name string) any {
|
||||
return getGroup(name).Get(name)
|
||||
}
|
||||
|
||||
// Set sets an instance to the instance manager with given name.
|
||||
func Set(name string, instance any) {
|
||||
getGroup(name).Set(name, instance)
|
||||
}
|
||||
|
||||
// GetOrSet returns the instance by name,
|
||||
// or set instance to the instance manager if it does not exist and returns this instance.
|
||||
func GetOrSet(name string, instance any) any {
|
||||
return getGroup(name).GetOrSet(name, instance)
|
||||
}
|
||||
|
||||
// GetOrSetFunc returns the instance by name,
|
||||
// or sets instance with returned value of callback function `f` if it does not exist
|
||||
// and then returns this instance.
|
||||
func GetOrSetFunc(name string, f func() any) any {
|
||||
return getGroup(name).GetOrSetFunc(name, f)
|
||||
}
|
||||
|
||||
// GetOrSetFuncLock returns the instance by name,
|
||||
// or sets instance with returned value of callback function `f` if it does not exist
|
||||
// and then returns this instance.
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
func GetOrSetFuncLock(name string, f func() any) any {
|
||||
return getGroup(name).GetOrSetFuncLock(name, f)
|
||||
}
|
||||
|
||||
// SetIfNotExist sets `instance` to the map if the `name` does not exist, then returns true.
|
||||
// It returns false if `name` exists, and `instance` would be ignored.
|
||||
func SetIfNotExist(name string, instance any) bool {
|
||||
return getGroup(name).SetIfNotExist(name, instance)
|
||||
}
|
||||
|
||||
// Clear deletes all instances stored.
|
||||
func Clear() {
|
||||
for i := 0; i < groupNumber; i++ {
|
||||
groups[i].Clear()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). 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 instance_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/internal/instance"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_SetGet(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
instance.Set("test-user", 1)
|
||||
t.Assert(instance.Get("test-user"), 1)
|
||||
t.Assert(instance.Get("none-exists"), nil)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(instance.GetOrSet("test-1", 1), 1)
|
||||
t.Assert(instance.Get("test-1"), 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(instance.GetOrSetFunc("test-2", func() any {
|
||||
return 2
|
||||
}), 2)
|
||||
t.Assert(instance.Get("test-2"), 2)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(instance.GetOrSetFuncLock("test-3", func() any {
|
||||
return 3
|
||||
}), 3)
|
||||
t.Assert(instance.Get("test-3"), 3)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(instance.SetIfNotExist("test-4", 4), true)
|
||||
t.Assert(instance.Get("test-4"), 4)
|
||||
t.Assert(instance.SetIfNotExist("test-4", 5), false)
|
||||
t.Assert(instance.Get("test-4"), 4)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user