Skip to content

Commit

Permalink
other: v2 部分内容修正
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Apr 28, 2024
1 parent e372b71 commit 51af0a9
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 48 deletions.
19 changes: 19 additions & 0 deletions modular/application_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package modular_test

import (
"github.com/kercylan98/minotaur/modular"
"testing"
)

func TestNewApplication(t *testing.T) {
if modular.NewApplication() == nil {
t.Fail()
}
}

func TestRun(t *testing.T) {
application := modular.NewApplication()
modular.RegisterService[*AccountService, AccountServiceExposer](application)
modular.RegisterService[*ConfigService, ConfigServiceExposer](application)
modular.Run(application)
}
13 changes: 11 additions & 2 deletions modular/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (

// startLifecycle 开始生命周期
func startLifecycle(services []GlobalService) *lifecycle {
return &lifecycle{services: services, wait: new(sync.WaitGroup)}
l := &lifecycle{services: services, wait: new(sync.WaitGroup)}
l.root = l
return l
}

// lifecycle 生命周期函数
Expand All @@ -18,17 +20,24 @@ type lifecycle struct {
n *lifecycle // 下一个生命周期
wait *sync.WaitGroup // 等待组
last bool // 是否是最后一个生命周期
root *lifecycle // 根生命周期
running bool // 是否正在运行
}

// next 设置下一个生命周期
func (l *lifecycle) next(name string, handler func(service GlobalService) bool) *lifecycle {
l.last = false
l.n = &lifecycle{name: name, services: l.services, handler: handler, wait: l.wait, last: true}
l.n = &lifecycle{name: name, services: l.services, handler: handler, wait: l.wait, last: true, root: l.root}
return l.n
}

// run 运行生命周期
func (l *lifecycle) run() {
if !l.root.running {
l.root.running = true
l.root.run()
return
}
var num int
if l.handler != nil {
for _, service := range l.services {
Expand Down
2 changes: 1 addition & 1 deletion modular/modular.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Run(application *Application) {
//
// 通过该函数注册的服务将会在应用程序启动时被实例化,并且在整个应用程序的生命周期中只存在一个实例
func RegisterService[Instance GlobalService, Exposer any](application *Application) {
tof := reflect.TypeOf((*Instance)(nil)).Elem()
tof := reflect.TypeOf(new(Instance)).Elem().Elem()
vof := reflect.New(tof).Interface()
service := vof.(GlobalService)
exposer := vof.(Exposer)
Expand Down
61 changes: 61 additions & 0 deletions modular/services_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package modular_test

import (
"github.com/kercylan98/minotaur/modular"
)

type AccountService struct {
config ConfigServiceExposer
}

func (a *AccountService) OnInit(application *modular.Application) {
}

func (a *AccountService) OnPreload(application *modular.Application) {
a.config = modular.InvokeService[ConfigServiceExposer](application)
}

func (a *AccountService) OnMount(application *modular.Application) {
}

func (a *AccountService) OnStart(application *modular.Application) {
// 假设需要使用配置
a.config.Get("key")
}

func (a *AccountService) Login() {

}

type AccountServiceExposer interface {
Login()
}

type ConfigService struct {
kv map[string]string
}

func (c *ConfigService) OnInit(application *modular.Application) {
}

func (c *ConfigService) OnPreload(application *modular.Application) {
}

func (c *ConfigService) OnMount(application *modular.Application) {
// 假设从数据库加载
c.kv = make(map[string]string)
}

func (c *ConfigService) OnStart(application *modular.Application) {
}

func (c *ConfigService) Get(key string) string {
if c.kv == nil {
panic("config service not initialized")
}
return key
}

type ConfigServiceExposer interface {
Get(key string) string
}
6 changes: 4 additions & 2 deletions toolkit/collection/collection.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package collection

import "github.com/kercylan98/minotaur/utils/generic"
import (
"github.com/kercylan98/minotaur/toolkit/constraints"
)

// ComparisonHandler 用于比较 `source` 和 `target` 两个值是否相同的比较函数
// - 该函数接受两个参数,分别是源值和目标值,返回 true 的情况下即表示两者相同
type ComparisonHandler[V any] func(source, target V) bool

// OrderedValueGetter 用于获取 v 的可排序字段值的函数
type OrderedValueGetter[V any, N generic.Ordered] func(v V) N
type OrderedValueGetter[V any, N constraints.Ordered] func(v V) N
26 changes: 13 additions & 13 deletions toolkit/collection/find.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package collection

import (
"github.com/kercylan98/minotaur/utils/generic"
"github.com/kercylan98/minotaur/toolkit/constraints"
)

// FindLoopedNextInSlice 返回 i 的下一个数组成员,当 i 达到数组长度时从 0 开始
Expand Down Expand Up @@ -146,7 +146,7 @@ func FindIndexInComparableSlice[S ~[]V, V comparable](slice S, v V) int {
}

// FindMinimumInComparableSlice 获取切片中的最小值
func FindMinimumInComparableSlice[S ~[]V, V generic.Ordered](slice S) (result V) {
func FindMinimumInComparableSlice[S ~[]V, V constraints.Ordered](slice S) (result V) {
if len(slice) == 0 {
return
}
Expand All @@ -160,7 +160,7 @@ func FindMinimumInComparableSlice[S ~[]V, V generic.Ordered](slice S) (result V)
}

// FindMinimumInSlice 获取切片中的最小值
func FindMinimumInSlice[S ~[]V, V any, N generic.Ordered](slice S, handler OrderedValueGetter[V, N]) (result V) {
func FindMinimumInSlice[S ~[]V, V any, N constraints.Ordered](slice S, handler OrderedValueGetter[V, N]) (result V) {
if len(slice) == 0 {
return
}
Expand All @@ -174,7 +174,7 @@ func FindMinimumInSlice[S ~[]V, V any, N generic.Ordered](slice S, handler Order
}

// FindMaximumInComparableSlice 获取切片中的最大值
func FindMaximumInComparableSlice[S ~[]V, V generic.Ordered](slice S) (result V) {
func FindMaximumInComparableSlice[S ~[]V, V constraints.Ordered](slice S) (result V) {
if len(slice) == 0 {
return
}
Expand All @@ -188,7 +188,7 @@ func FindMaximumInComparableSlice[S ~[]V, V generic.Ordered](slice S) (result V)
}

// FindMaximumInSlice 获取切片中的最大值
func FindMaximumInSlice[S ~[]V, V any, N generic.Ordered](slice S, handler OrderedValueGetter[V, N]) (result V) {
func FindMaximumInSlice[S ~[]V, V any, N constraints.Ordered](slice S, handler OrderedValueGetter[V, N]) (result V) {
if len(slice) == 0 {
return
}
Expand All @@ -202,7 +202,7 @@ func FindMaximumInSlice[S ~[]V, V any, N generic.Ordered](slice S, handler Order
}

// FindMin2MaxInComparableSlice 获取切片中的最小值和最大值
func FindMin2MaxInComparableSlice[S ~[]V, V generic.Ordered](slice S) (min, max V) {
func FindMin2MaxInComparableSlice[S ~[]V, V constraints.Ordered](slice S) (min, max V) {
if len(slice) == 0 {
return
}
Expand All @@ -220,7 +220,7 @@ func FindMin2MaxInComparableSlice[S ~[]V, V generic.Ordered](slice S) (min, max
}

// FindMin2MaxInSlice 获取切片中的最小值和最大值
func FindMin2MaxInSlice[S ~[]V, V any, N generic.Ordered](slice S, handler OrderedValueGetter[V, N]) (min, max V) {
func FindMin2MaxInSlice[S ~[]V, V any, N constraints.Ordered](slice S, handler OrderedValueGetter[V, N]) (min, max V) {
if len(slice) == 0 {
return
}
Expand All @@ -238,7 +238,7 @@ func FindMin2MaxInSlice[S ~[]V, V any, N generic.Ordered](slice S, handler Order
}

// FindMinFromComparableMap 获取 map 中的最小值
func FindMinFromComparableMap[M ~map[K]V, K comparable, V generic.Ordered](m M) (result V) {
func FindMinFromComparableMap[M ~map[K]V, K comparable, V constraints.Ordered](m M) (result V) {
if m == nil {
return
}
Expand All @@ -257,7 +257,7 @@ func FindMinFromComparableMap[M ~map[K]V, K comparable, V generic.Ordered](m M)
}

// FindMinFromMap 获取 map 中的最小值
func FindMinFromMap[M ~map[K]V, K comparable, V any, N generic.Ordered](m M, handler OrderedValueGetter[V, N]) (result V) {
func FindMinFromMap[M ~map[K]V, K comparable, V any, N constraints.Ordered](m M, handler OrderedValueGetter[V, N]) (result V) {
if m == nil {
return
}
Expand All @@ -276,7 +276,7 @@ func FindMinFromMap[M ~map[K]V, K comparable, V any, N generic.Ordered](m M, han
}

// FindMaxFromComparableMap 获取 map 中的最大值
func FindMaxFromComparableMap[M ~map[K]V, K comparable, V generic.Ordered](m M) (result V) {
func FindMaxFromComparableMap[M ~map[K]V, K comparable, V constraints.Ordered](m M) (result V) {
if m == nil {
return
}
Expand All @@ -289,7 +289,7 @@ func FindMaxFromComparableMap[M ~map[K]V, K comparable, V generic.Ordered](m M)
}

// FindMaxFromMap 获取 map 中的最大值
func FindMaxFromMap[M ~map[K]V, K comparable, V any, N generic.Ordered](m M, handler OrderedValueGetter[V, N]) (result V) {
func FindMaxFromMap[M ~map[K]V, K comparable, V any, N constraints.Ordered](m M, handler OrderedValueGetter[V, N]) (result V) {
if m == nil {
return
}
Expand All @@ -302,7 +302,7 @@ func FindMaxFromMap[M ~map[K]V, K comparable, V any, N generic.Ordered](m M, han
}

// FindMin2MaxFromComparableMap 获取 map 中的最小值和最大值
func FindMin2MaxFromComparableMap[M ~map[K]V, K comparable, V generic.Ordered](m M) (min, max V) {
func FindMin2MaxFromComparableMap[M ~map[K]V, K comparable, V constraints.Ordered](m M) (min, max V) {
if m == nil {
return
}
Expand All @@ -325,7 +325,7 @@ func FindMin2MaxFromComparableMap[M ~map[K]V, K comparable, V generic.Ordered](m
}

// FindMin2MaxFromMap 获取 map 中的最小值和最大值
func FindMin2MaxFromMap[M ~map[K]V, K comparable, V generic.Ordered](m M) (min, max V) {
func FindMin2MaxFromMap[M ~map[K]V, K comparable, V constraints.Ordered](m M) (min, max V) {
if m == nil {
return
}
Expand Down
18 changes: 9 additions & 9 deletions toolkit/collection/loop.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package collection

import (
"github.com/kercylan98/minotaur/utils/generic"
"github.com/kercylan98/minotaur/toolkit/constraints"
"sort"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ func LoopMap[M ~map[K]V, K comparable, V any](m M, f func(i int, key K, val V) b
// LoopMapByOrderedKeyAsc 按照键的升序迭代 m 中的每一个函数,并将键和值传递给 f 函数
// - 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
// - 迭代过程将在 f 函数返回 false 时中断
func LoopMapByOrderedKeyAsc[M ~map[K]V, K generic.Ordered, V any](m M, f func(i int, key K, val V) bool) {
func LoopMapByOrderedKeyAsc[M ~map[K]V, K constraints.Ordered, V any](m M, f func(i int, key K, val V) bool) {
var keys []K
for k := range m {
keys = append(keys, k)
Expand All @@ -60,7 +60,7 @@ func LoopMapByOrderedKeyAsc[M ~map[K]V, K generic.Ordered, V any](m M, f func(i
// LoopMapByOrderedKeyDesc 按照键的降序迭代 m 中的每一个函数,并将键和值传递给 f 函数
// - 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
// - 迭代过程将在 f 函数返回 false 时中断
func LoopMapByOrderedKeyDesc[M ~map[K]V, K generic.Ordered, V any](m M, f func(i int, key K, val V) bool) {
func LoopMapByOrderedKeyDesc[M ~map[K]V, K constraints.Ordered, V any](m M, f func(i int, key K, val V) bool) {
var keys []K
for k := range m {
keys = append(keys, k)
Expand All @@ -78,7 +78,7 @@ func LoopMapByOrderedKeyDesc[M ~map[K]V, K generic.Ordered, V any](m M, f func(i
// LoopMapByOrderedValueAsc 按照值的升序迭代 m 中的每一个函数,并将键和值传递给 f 函数
// - 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
// - 迭代过程将在 f 函数返回 false 时中断
func LoopMapByOrderedValueAsc[M ~map[K]V, K comparable, V generic.Ordered](m M, f func(i int, key K, val V) bool) {
func LoopMapByOrderedValueAsc[M ~map[K]V, K comparable, V constraints.Ordered](m M, f func(i int, key K, val V) bool) {
var keys []K
var values []V
for k, v := range m {
Expand All @@ -98,7 +98,7 @@ func LoopMapByOrderedValueAsc[M ~map[K]V, K comparable, V generic.Ordered](m M,
// LoopMapByOrderedValueDesc 按照值的降序迭代 m 中的每一个函数,并将键和值传递给 f 函数
// - 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
// - 迭代过程将在 f 函数返回 false 时中断
func LoopMapByOrderedValueDesc[M ~map[K]V, K comparable, V generic.Ordered](m M, f func(i int, key K, val V) bool) {
func LoopMapByOrderedValueDesc[M ~map[K]V, K comparable, V constraints.Ordered](m M, f func(i int, key K, val V) bool) {
var keys []K
var values []V
for k, v := range m {
Expand All @@ -118,7 +118,7 @@ func LoopMapByOrderedValueDesc[M ~map[K]V, K comparable, V generic.Ordered](m M,
// LoopMapByKeyGetterAsc 按照键的升序迭代 m 中的每一个函数,并将键和值传递给 f 函数
// - 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
// - 迭代过程将在 f 函数返回 false 时中断
func LoopMapByKeyGetterAsc[M ~map[K]V, K comparable, V comparable, N generic.Ordered](m M, getter func(k K) N, f func(i int, key K, val V) bool) {
func LoopMapByKeyGetterAsc[M ~map[K]V, K comparable, V comparable, N constraints.Ordered](m M, getter func(k K) N, f func(i int, key K, val V) bool) {
var keys []K
for k := range m {
keys = append(keys, k)
Expand All @@ -136,7 +136,7 @@ func LoopMapByKeyGetterAsc[M ~map[K]V, K comparable, V comparable, N generic.Ord
// LoopMapByValueGetterAsc 按照值的升序迭代 m 中的每一个函数,并将键和值传递给 f 函数
// - 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
// - 迭代过程将在 f 函数返回 false 时中断
func LoopMapByValueGetterAsc[M ~map[K]V, K comparable, V any, N generic.Ordered](m M, getter func(v V) N, f func(i int, key K, val V) bool) {
func LoopMapByValueGetterAsc[M ~map[K]V, K comparable, V any, N constraints.Ordered](m M, getter func(v V) N, f func(i int, key K, val V) bool) {
var keys []K
for k := range m {
keys = append(keys, k)
Expand All @@ -154,7 +154,7 @@ func LoopMapByValueGetterAsc[M ~map[K]V, K comparable, V any, N generic.Ordered]
// LoopMapByKeyGetterDesc 按照键的降序迭代 m 中的每一个函数,并将键和值传递给 f 函数
// - 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
// - 迭代过程将在 f 函数返回 false 时中断
func LoopMapByKeyGetterDesc[M ~map[K]V, K comparable, V comparable, N generic.Ordered](m M, getter func(k K) N, f func(i int, key K, val V) bool) {
func LoopMapByKeyGetterDesc[M ~map[K]V, K comparable, V comparable, N constraints.Ordered](m M, getter func(k K) N, f func(i int, key K, val V) bool) {
var keys []K
for k := range m {
keys = append(keys, k)
Expand All @@ -172,7 +172,7 @@ func LoopMapByKeyGetterDesc[M ~map[K]V, K comparable, V comparable, N generic.Or
// LoopMapByValueGetterDesc 按照值的降序迭代 m 中的每一个函数,并将键和值传递给 f 函数
// - 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
// - 迭代过程将在 f 函数返回 false 时中断
func LoopMapByValueGetterDesc[M ~map[K]V, K comparable, V any, N generic.Ordered](m M, getter func(v V) N, f func(i int, key K, val V) bool) {
func LoopMapByValueGetterDesc[M ~map[K]V, K comparable, V any, N constraints.Ordered](m M, getter func(v V) N, f func(i int, key K, val V) bool) {
var keys []K
for k := range m {
keys = append(keys, k)
Expand Down
2 changes: 1 addition & 1 deletion toolkit/collection/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package collection

import (
"fmt"
"github.com/kercylan98/minotaur/utils/random"
"github.com/kercylan98/minotaur/toolkit/random"
)

// ChooseRandomSliceElementRepeatN 返回 slice 中的 n 个可重复随机元素
Expand Down
Loading

0 comments on commit 51af0a9

Please sign in to comment.