初始化项目

This commit is contained in:
皮蛋13361098506
2025-01-06 16:01:02 +08:00
commit 1b77f62820
575 changed files with 69193 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
package initMgr
import (
"fmt"
"sync"
"time"
. "Framework/startMgr"
"goutil/logUtil"
)
var (
funcMap = make(map[string]*FuncItem)
mutex sync.Mutex
operateName = "Init"
)
// 注册方法(如果名称重复会panic)
// name:方法名称(唯一标识)
// moduleType:模块类型
// definition:方法定义
func Register(name string, moduleType ModuleType, definition func() error) {
mutex.Lock()
defer mutex.Unlock()
if _, exists := funcMap[name]; exists {
panic(fmt.Sprintf("%s已经存在请重新命名", name))
}
funcMap[name] = NewFuncItem(name, moduleType, definition)
}
// 调用一个方法
// name:方法名称(唯一标识)
// 返回值:
// 错误对象
func CallOne(name string) (err error) {
mutex.Lock()
defer mutex.Unlock()
if item, exists := funcMap[name]; !exists {
panic(fmt.Sprintf("%s不存在", name))
} else {
// 调用方法
err = item.Call2(operateName)
}
return
}
// 调用任意数量的方法
// nameList:任意数量的方法名称
// 返回值:
// 错误列表
func CallAny(nameList ...string) (errList []error) {
mutex.Lock()
defer mutex.Unlock()
for _, name := range nameList {
if item, exists := funcMap[name]; !exists {
panic(fmt.Sprintf("%s不存在", name))
} else {
// 调用方法
if err := item.Call2(operateName); err != nil {
errList = append(errList, err)
}
}
}
return
}
// 调用所有方法
// 返回值:
// 错误列表
func CallAll() (errList []error) {
mutex.Lock()
defer mutex.Unlock()
startTime := time.Now()
defer func() {
logUtil.InfoLog(fmt.Sprintf("%s 执行总时间:%s", operateName, time.Since(startTime)))
}()
for _, item := range funcMap {
// 调用方法
if err := item.Call2(operateName); err != nil {
errList = append(errList, err)
}
}
return
}
// 按照模块类型进行调用
// moduleType:模块类型
// 返回值:
// errList:错误列表
func CallType(moduleType ModuleType) (errList []error) {
mutex.Lock()
defer mutex.Unlock()
startTime := time.Now()
defer func() {
logUtil.InfoLog(fmt.Sprintf("%v %s 执行总时间:%s", moduleType, operateName, time.Since(startTime)))
}()
for _, item := range funcMap {
if item.ModuleType != moduleType {
continue
}
// 调用方法
if err := item.Call2(operateName); err != nil {
errList = append(errList, err)
}
}
return
}

View File

@@ -0,0 +1,54 @@
package initMgr
import (
"fmt"
"testing"
)
func TestRegister(t *testing.T) {
Register("first", 1, first)
Register("second", 2, second)
Register("third", 3, third)
Register("fourth", 4, fourth)
}
func TestCallOne(t *testing.T) {
name := "first"
if err := CallOne(name); err != nil {
t.Errorf("there should be no error, but now it has:%s", err)
}
}
func TestCallAny(t *testing.T) {
errList := CallAny("second", "third")
if len(errList) != 1 {
t.Errorf("there should be 1 error, but now:%d", len(errList))
}
}
func TestCallAll(t *testing.T) {
errList := CallAll()
if len(errList) != 2 {
t.Errorf("there should be 1 error, but now:%d", len(errList))
}
}
func first() error {
fmt.Println("first")
return nil
}
func second() error {
fmt.Println("second")
return fmt.Errorf("the second error")
}
func third() error {
fmt.Println("third")
return nil
}
func fourth() error {
fmt.Println("fourth")
return fmt.Errorf("the fourth error")
}