Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e0afb55bb2 | ||
|
|
a469692358 | ||
|
|
bce7131d9f | ||
|
|
a53f7b718d | ||
|
|
75624ff0b7 | ||
|
|
743d232c38 | ||
|
|
1d0661ae40 | ||
|
|
69e72283dc |
@@ -1,6 +1,7 @@
|
|||||||
package elasticsearch
|
package elasticsearch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/ayflying/utility_go/internal/boot"
|
||||||
"github.com/gogf/gf/v2/database/gdb"
|
"github.com/gogf/gf/v2/database/gdb"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
)
|
)
|
||||||
@@ -15,16 +16,19 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var (
|
boot.AddFunc(func() {
|
||||||
err error
|
var (
|
||||||
driverObj = New()
|
err error
|
||||||
driverNames = g.SliceStr{"es", "elasticsearch"}
|
driverObj = New()
|
||||||
)
|
driverNames = g.SliceStr{"es", "elasticsearch"}
|
||||||
for _, driverName := range driverNames {
|
)
|
||||||
if err = gdb.Register(driverName, driverObj); err != nil {
|
for _, driverName := range driverNames {
|
||||||
panic(err)
|
if err = gdb.Register(driverName, driverObj); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New create and returns a driver that implements gdb.Driver, which supports operations for MySQL.
|
// New create and returns a driver that implements gdb.Driver, which supports operations for MySQL.
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package found
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"github.com/ayflying/utility_go/internal/boot"
|
||||||
"github.com/gogf/gf/v2/database/gdb"
|
"github.com/gogf/gf/v2/database/gdb"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
)
|
)
|
||||||
@@ -21,16 +22,18 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var (
|
boot.AddFunc(func() {
|
||||||
err error
|
var (
|
||||||
driverObj = New()
|
err error
|
||||||
driverNames = g.SliceStr{"es", "found"}
|
driverObj = New()
|
||||||
)
|
driverNames = g.SliceStr{"es", "found"}
|
||||||
for _, driverName := range driverNames {
|
)
|
||||||
if err = gdb.Register(driverName, driverObj); err != nil {
|
for _, driverName := range driverNames {
|
||||||
panic(err)
|
if err = gdb.Register(driverName, driverObj); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// New create and returns a driver that implements gdb.Driver, which supports operations for MySQL.
|
// New create and returns a driver that implements gdb.Driver, which supports operations for MySQL.
|
||||||
|
|||||||
3
go.mod
3
go.mod
@@ -17,6 +17,7 @@ require (
|
|||||||
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20241220152942-06eb5c6e8230
|
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20241220152942-06eb5c6e8230
|
||||||
github.com/minio/minio-go/v7 v7.0.85
|
github.com/minio/minio-go/v7 v7.0.85
|
||||||
github.com/prometheus/client_golang v1.21.1
|
github.com/prometheus/client_golang v1.21.1
|
||||||
|
github.com/stretchr/testify v1.10.0
|
||||||
github.com/xuri/excelize/v2 v2.9.0
|
github.com/xuri/excelize/v2 v2.9.0
|
||||||
golang.org/x/oauth2 v0.24.0
|
golang.org/x/oauth2 v0.24.0
|
||||||
google.golang.org/api v0.44.0
|
google.golang.org/api v0.44.0
|
||||||
@@ -31,6 +32,7 @@ require (
|
|||||||
github.com/beorn7/perks v1.0.1 // indirect
|
github.com/beorn7/perks v1.0.1 // indirect
|
||||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
github.com/elastic/elastic-transport-go/v8 v8.6.1 // indirect
|
github.com/elastic/elastic-transport-go/v8 v8.6.1 // indirect
|
||||||
github.com/emirpasic/gods v1.18.1 // indirect
|
github.com/emirpasic/gods v1.18.1 // indirect
|
||||||
@@ -60,6 +62,7 @@ require (
|
|||||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||||
github.com/pelletier/go-toml v1.9.3 // indirect
|
github.com/pelletier/go-toml v1.9.3 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/prometheus/client_model v0.6.1 // indirect
|
github.com/prometheus/client_model v0.6.1 // indirect
|
||||||
github.com/prometheus/common v0.62.0 // indirect
|
github.com/prometheus/common v0.62.0 // indirect
|
||||||
github.com/prometheus/procfs v0.15.1 // indirect
|
github.com/prometheus/procfs v0.15.1 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -249,6 +249,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
|||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||||
|
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||||
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20241220152942-06eb5c6e8230 h1:B0oaMTAQKDZd8cwYT0qsAI7+c3KbFeBNA8GhgoBMXWw=
|
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20241220152942-06eb5c6e8230 h1:B0oaMTAQKDZd8cwYT0qsAI7+c3KbFeBNA8GhgoBMXWw=
|
||||||
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20241220152942-06eb5c6e8230/go.mod h1:C5LA5UO2ZXJrLaPLYtE1wUJMiyd/nwWaCO5cw/2pSHs=
|
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20241220152942-06eb5c6e8230/go.mod h1:C5LA5UO2ZXJrLaPLYtE1wUJMiyd/nwWaCO5cw/2pSHs=
|
||||||
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
|
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
|
||||||
|
|||||||
@@ -1,30 +1,33 @@
|
|||||||
package boot
|
package boot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
v1 "github.com/ayflying/utility_go/api/system/v1"
|
v1 "github.com/ayflying/utility_go/api/system/v1"
|
||||||
"github.com/ayflying/utility_go/service"
|
"github.com/ayflying/utility_go/service"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
|
||||||
"github.com/gogf/gf/v2/os/gctx"
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
"github.com/gogf/gf/v2/os/gtimer"
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ctx = gctx.GetInitCtx()
|
ctx = gctx.GetInitCtx()
|
||||||
|
_func = []func(){}
|
||||||
)
|
)
|
||||||
|
|
||||||
func Boot() (err error) {
|
func Boot() (err error) {
|
||||||
err = service.SystemCron().StartCron()
|
err = service.SystemCron().StartCron()
|
||||||
|
|
||||||
//用户活动持久化
|
//用户活动持久化
|
||||||
gtimer.SetTimeout(ctx, time.Minute, func(ctx context.Context) {
|
service.SystemCron().AddCron(v1.CronType_DAILY, func() error {
|
||||||
service.SystemCron().AddCron(v1.CronType_DAILY, func() error {
|
return service.GameAct().Saves()
|
||||||
service.GameAct().Saves()
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
//初始化自启动方法
|
||||||
|
for _, v := range _func {
|
||||||
|
v()
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddFunc 注册方法,在启动时执行
|
||||||
|
func AddFunc(f func()) {
|
||||||
|
_func = append(_func, f)
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/os/gctx"
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
"github.com/gogf/gf/v2/os/gtime"
|
"github.com/gogf/gf/v2/os/gtime"
|
||||||
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -130,7 +131,8 @@ func (s *sGameAct) Save(actId int) (err error) {
|
|||||||
result := strings.Split(cacheKey, ":")
|
result := strings.Split(cacheKey, ":")
|
||||||
actId, err = strconv.Atoi(result[1])
|
actId, err = strconv.Atoi(result[1])
|
||||||
var uid int64
|
var uid int64
|
||||||
uid, err = strconv.ParseInt(result[2], 10, 64)
|
uid = gconv.Int64(result[2])
|
||||||
|
//uid, err = strconv.ParseInt(result[2], 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package ip2region
|
package ip2region
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/ayflying/utility_go/internal/boot"
|
||||||
"github.com/ayflying/utility_go/service"
|
"github.com/ayflying/utility_go/service"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/os/gctx"
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
@@ -19,13 +20,16 @@ type sIp2region struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func New() *sIp2region {
|
func New() *sIp2region {
|
||||||
s := &sIp2region{}
|
|
||||||
s.Load()
|
return &sIp2region{}
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
service.RegisterIp2Region(New())
|
service.RegisterIp2Region(New())
|
||||||
|
|
||||||
|
boot.AddFunc(func() {
|
||||||
|
service.Ip2Region().Load()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load 加载到内存中
|
// Load 加载到内存中
|
||||||
@@ -34,12 +38,14 @@ func init() {
|
|||||||
// @receiver s *sIp2region: sIp2region的实例。
|
// @receiver s *sIp2region: sIp2region的实例。
|
||||||
func (s *sIp2region) Load() {
|
func (s *sIp2region) Load() {
|
||||||
var err error
|
var err error
|
||||||
var dbPath = "/runtime/library/ip2region.xdb"
|
|
||||||
|
var url = "https://github.com/ayflying/resource/raw/refs/heads/main/attachment/ip2region.xdb"
|
||||||
|
var dbPath = "runtime/library/ip2region.xdb"
|
||||||
|
|
||||||
if gfile.IsEmpty(dbPath) {
|
if gfile.IsEmpty(dbPath) {
|
||||||
|
g.Log().Debug(ctx, "等待下载ip库文件")
|
||||||
//下载文件
|
//下载文件
|
||||||
putData, err2 := g.Client().Discovery(nil).
|
putData, err2 := g.Client().Discovery(nil).Get(ctx, url)
|
||||||
Get(ctx, "https://resource.luoe.cn/attachment/ip2region.xdb")
|
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package aycache
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
v1 "github.com/ayflying/utility_go/api/system/v1"
|
v1 "github.com/ayflying/utility_go/api/system/v1"
|
||||||
|
"github.com/ayflying/utility_go/internal/boot"
|
||||||
"github.com/ayflying/utility_go/pkg/aycache/drive"
|
"github.com/ayflying/utility_go/pkg/aycache/drive"
|
||||||
drive2 "github.com/ayflying/utility_go/pkg/aycache/drive"
|
drive2 "github.com/ayflying/utility_go/pkg/aycache/drive"
|
||||||
"github.com/ayflying/utility_go/service"
|
"github.com/ayflying/utility_go/service"
|
||||||
@@ -26,11 +27,13 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
boot.AddFunc(func() {
|
||||||
service.SystemCron().AddCron(v1.CronType_MINUTE, func() error {
|
//初始化指标
|
||||||
QPS.Set(math.Round(float64(QPSCount) / 60))
|
service.SystemCron().AddCron(v1.CronType_MINUTE, func() error {
|
||||||
QPSCount = 0
|
QPS.Set(math.Round(float64(QPSCount) / 60))
|
||||||
return nil
|
QPSCount = 0
|
||||||
|
return nil
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package tools
|
package tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
|
"github.com/gogf/gf/v2/util/grand"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -50,3 +52,73 @@ func (m *randMod) RandomAll(data map[int]int, n int) []int {
|
|||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RandomAll[t Any](data map[t]int, n int) []t {
|
||||||
|
if n > len(data) {
|
||||||
|
n = len(data)
|
||||||
|
}
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
// 复制权重映射,避免修改原始数据
|
||||||
|
remainingWeights := make(map[t]int)
|
||||||
|
for k, v := range data {
|
||||||
|
remainingWeights[k] = v
|
||||||
|
}
|
||||||
|
result := make([]t, 0, n)
|
||||||
|
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
totalWeight := 0
|
||||||
|
// 计算剩余元素的总权重
|
||||||
|
for _, weight := range remainingWeights {
|
||||||
|
totalWeight += weight
|
||||||
|
}
|
||||||
|
if totalWeight == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// 生成一个 0 到总权重之间的随机数
|
||||||
|
randomNum := rand.Intn(totalWeight)
|
||||||
|
currentWeight := 0
|
||||||
|
for key, weight := range remainingWeights {
|
||||||
|
currentWeight += weight
|
||||||
|
if randomNum < currentWeight {
|
||||||
|
// 将选中的元素添加到结果切片中
|
||||||
|
result = append(result, key)
|
||||||
|
// 从剩余权重映射中移除选中的元素
|
||||||
|
delete(remainingWeights, key)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandByArrInt 根据传入的 interface 切片中的整数值按权重随机返回一个索引
|
||||||
|
// 参数 s: 一个包含整数的 interface 切片,切片中的每个元素代表一个权重
|
||||||
|
// 返回值: 随机选中的元素的索引
|
||||||
|
func (m *randMod) RandByArrInt(_s interface{}) int {
|
||||||
|
// 初始化总权重为 0
|
||||||
|
sv := 0
|
||||||
|
s := gconv.Ints(_s)
|
||||||
|
|
||||||
|
// 遍历切片,累加每个元素的权重
|
||||||
|
for i := range s {
|
||||||
|
sv += gconv.Int(s[i])
|
||||||
|
}
|
||||||
|
if sv < 1 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用 grand.Intn 生成一个 0 到总权重之间的随机数
|
||||||
|
r := grand.Intn(sv)
|
||||||
|
// 初始化当前累加的权重为 0
|
||||||
|
var all int
|
||||||
|
// 再次遍历切片,累加权重
|
||||||
|
for i := range s {
|
||||||
|
all += s[i]
|
||||||
|
// 如果当前累加的权重大于随机数,则返回当前索引
|
||||||
|
if all > r {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 如果没有找到符合条件的索引,返回 0
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ type redis struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *redis) Load() {
|
func (r *redis) Load() {
|
||||||
g.Log().Debugf(gctx.New(), "初始化工具类")
|
g.Log().Debugf(gctx.New(), "初始化redis工具类")
|
||||||
if Redis == nil {
|
if Redis == nil {
|
||||||
Redis = &redis{}
|
Redis = &redis{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -121,3 +121,10 @@ func (m *timeMod) GetDailyTimeList(time1 time.Time, time2 time.Time) (timeList [
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExcelTime2Time excel时间转时间 (12/10/24 02:03转为时间)
|
||||||
|
func (m *timeMod) ExcelTime2Time(excelTime string) time.Time {
|
||||||
|
layout := "01/02/06 15:04" // 月/日/年(最后两位) 小时:分钟 (24小时制)
|
||||||
|
timeNew, _ := time.ParseInLocation(layout, excelTime, time.Local)
|
||||||
|
return timeNew
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package tools
|
package tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/ayflying/utility_go/internal/boot"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/os/gctx"
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
"github.com/gogf/gf/v2/os/gtime"
|
"github.com/gogf/gf/v2/os/gtime"
|
||||||
@@ -18,9 +20,9 @@ type Number interface {
|
|||||||
int | int64 | int32 | int16 | uint64 | uint32 | uint16 | float32 | float64
|
int | int64 | int32 | int16 | uint64 | uint32 | uint16 | float32 | float64
|
||||||
}
|
}
|
||||||
|
|
||||||
//type Any interface {
|
type Any interface {
|
||||||
// interface{} | string | int | int64 | int32 | int16 | uint64 | uint32 | uint16 | float32 | float64
|
string | int | int64 | int32 | int16 | uint64 | uint32 | uint16 | float32 | float64
|
||||||
//}
|
}
|
||||||
|
|
||||||
type toolsInterface interface {
|
type toolsInterface interface {
|
||||||
Load()
|
Load()
|
||||||
@@ -30,15 +32,17 @@ type tools struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
g.Log().Debugf(gctx.New(), "初始化工具类")
|
boot.AddFunc(func() {
|
||||||
|
g.Log().Debugf(gctx.New(), "初始化tools工具类")
|
||||||
|
|
||||||
names := []toolsInterface{
|
names := []toolsInterface{
|
||||||
Tools,
|
Tools,
|
||||||
}
|
}
|
||||||
for _, v := range names {
|
for _, v := range names {
|
||||||
v.Load()
|
v.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *tools) Load() {
|
func (m *tools) Load() {
|
||||||
@@ -128,7 +132,7 @@ func RemoveSlice[t Number](slice []t, value ...t) []t {
|
|||||||
// 从后向前遍历切片
|
// 从后向前遍历切片
|
||||||
for i := len(slice) - 1; i >= 0; i-- {
|
for i := len(slice) - 1; i >= 0; i-- {
|
||||||
// 检查当前元素是否等于需要移除的值
|
// 检查当前元素是否等于需要移除的值
|
||||||
if InArray(slice[i], value) {
|
if InArray[t](value, slice[i]) {
|
||||||
// 如果相等,移除该元素
|
// 如果相等,移除该元素
|
||||||
// 使用append和切片操作符来实现移除操作,将i之前和i之后的元素合并到一起
|
// 使用append和切片操作符来实现移除操作,将i之前和i之后的元素合并到一起
|
||||||
slice = append(slice[:i], slice[i+1:]...)
|
slice = append(slice[:i], slice[i+1:]...)
|
||||||
@@ -145,7 +149,7 @@ func RemoveSlice[t Number](slice []t, value ...t) []t {
|
|||||||
// @param value 需要查找的值
|
// @param value 需要查找的值
|
||||||
// @param array 进行查找的切片
|
// @param array 进行查找的切片
|
||||||
// @return bool 返回是否存在
|
// @return bool 返回是否存在
|
||||||
func InArray[t Number](value t, array []t) bool {
|
func InArray[t Number](array []t, value t) bool {
|
||||||
for _, v := range array {
|
for _, v := range array {
|
||||||
if v == value {
|
if v == value {
|
||||||
return true
|
return true
|
||||||
@@ -253,3 +257,33 @@ func (m *tools) ItemsMerge(_items ...[][]int64) [][]int64 {
|
|||||||
}
|
}
|
||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProcessingMap 处理map
|
||||||
|
// 该函数用于递归处理一个键为字符串、值为int64的map。
|
||||||
|
// 如果键是一个JSON字符串,会尝试将其解析为一个新的map,并递归处理这个新的map。
|
||||||
|
// 最终返回一个处理后的map,其中所有键都是非JSON字符串。
|
||||||
|
func (m *tools) ProcessingMap(data map[string]int64) map[string]int64 {
|
||||||
|
// 创建一个临时map,用于存储处理后的键值对
|
||||||
|
var temp = make(map[string]int64)
|
||||||
|
// 遍历输入的map
|
||||||
|
for k, v := range data {
|
||||||
|
// 创建一个新的map,用于存储解析后的JSON数据
|
||||||
|
data_k := make(map[string]int64)
|
||||||
|
// 尝试将键解析为JSON数据
|
||||||
|
err := json.Unmarshal([]byte(k), &data_k)
|
||||||
|
// 如果解析成功
|
||||||
|
if err == nil {
|
||||||
|
// 递归处理解析后的map
|
||||||
|
data_kmap := m.ProcessingMap(data_k)
|
||||||
|
// 返回处理后的map
|
||||||
|
// 如果解析失败,直接将原键值对添加到临时map中
|
||||||
|
// 将递归处理后的结果合并到临时map中
|
||||||
|
for k_k, k_v := range data_kmap {
|
||||||
|
temp[k_k] = k_v
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
temp[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return temp
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package utility_go
|
package utility_go
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/ayflying/utility_go/config"
|
||||||
"github.com/ayflying/utility_go/internal/boot"
|
"github.com/ayflying/utility_go/internal/boot"
|
||||||
_ "github.com/ayflying/utility_go/internal/logic"
|
_ "github.com/ayflying/utility_go/internal/logic"
|
||||||
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/ayflying/utility_go/config"
|
|
||||||
"github.com/gogf/gf/v2/os/gctx"
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,8 +14,11 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
g.Log().Debug(ctx, "utility_go init启动完成")
|
||||||
|
// 初始化配置
|
||||||
var err = boot.Boot()
|
var err = boot.Boot()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
66
utility_test.go
Normal file
66
utility_test.go
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package utility_go_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
//_ "github.com/ayflying/utility_go/internal/logic"
|
||||||
|
|
||||||
|
"github.com/ayflying/utility_go/internal/boot"
|
||||||
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ctx = gctx.GetInitCtx()
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestInit(t *testing.T) {
|
||||||
|
g.Log().Debug(ctx, "开始调试了")
|
||||||
|
// 初始化配置
|
||||||
|
var err = boot.Boot()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
//func TestLoadConfig(t *testing.T) {
|
||||||
|
//
|
||||||
|
// tests := []struct {
|
||||||
|
// name string
|
||||||
|
// filePath string
|
||||||
|
// wantErr bool
|
||||||
|
// }{
|
||||||
|
// {
|
||||||
|
// name: "valid config file",
|
||||||
|
// filePath: "testdata/valid_config.json",
|
||||||
|
// wantErr: false,
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// name: "non-existent file",
|
||||||
|
// filePath: "nonexistent.json",
|
||||||
|
// wantErr: true,
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// name: "invalid config format",
|
||||||
|
// filePath: "testdata/invalid_config.json",
|
||||||
|
// wantErr: true,
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// name: "empty file path",
|
||||||
|
// filePath: "",
|
||||||
|
// wantErr: true,
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// for _, tt := range tests {
|
||||||
|
// t.Run(tt.name, func(t *testing.T) {
|
||||||
|
//
|
||||||
|
// _, err := config.Load(tt.filePath)
|
||||||
|
// if tt.wantErr {
|
||||||
|
// assert.Error(t, err)
|
||||||
|
// } else {
|
||||||
|
// assert.NoError(t, err)
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
//}
|
||||||
Reference in New Issue
Block a user