wp-go/app/theme/wp/fn.go

75 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-05-03 15:57:49 +00:00
package wp
import (
"errors"
"github.com/fthvgb1/wp-go/safety"
2023-05-03 15:57:49 +00:00
)
var fnMap = safety.NewMap[string, map[string]any]()
var fnHook = safety.NewMap[string, map[string]any]()
2023-05-03 15:57:49 +00:00
func GetFn[T any](fnType string, name string) []T {
v, ok := fnMap.Load(fnType)
2023-05-03 15:57:49 +00:00
if !ok {
return nil
}
vv, ok := v[name]
if !ok {
return nil
}
return vv.([]T)
}
func GetFnHook[T any](fnType string, name string) []T {
v, ok := fnHook.Load(fnType)
2023-05-03 15:57:49 +00:00
if !ok {
return nil
}
vv, ok := v[name]
if !ok {
return nil
}
return vv.([]T)
}
func PushFn[T any](fnType string, name string, fns ...T) error {
v, ok := fnMap.Load(fnType)
2023-05-03 15:57:49 +00:00
if !ok {
v = make(map[string]any)
fnMap.Store(fnType, v)
2023-05-03 15:57:49 +00:00
v[name] = fns
return nil
}
vv, ok := v[name]
if !ok || vv == nil {
v[name] = fns
return nil
}
s, ok := vv.([]T)
if ok {
s = append(s, fns...)
v[name] = s
}
return errors.New("error fn type")
}
func PushFnHook[T any](fnType string, name string, fns ...T) error {
v, ok := fnHook.Load(fnType)
2023-05-03 15:57:49 +00:00
if !ok {
v = make(map[string]any)
fnHook.Store(fnType, v)
2023-05-03 15:57:49 +00:00
v[name] = fns
return nil
}
vv, ok := v[name]
if !ok || vv == nil {
v[name] = fns
return nil
}
s, ok := vv.([]T)
if ok {
s = append(s, fns...)
v[name] = s
}
return errors.New("error fn type")
}