feat: 相关操作升级为泛型实现

This commit is contained in:
2025-10-13 17:05:00 +08:00
parent 80b5e4e7cc
commit 50d2d6c7bb
18 changed files with 316 additions and 1354 deletions

View File

@ -0,0 +1,79 @@
// Package op_ternary ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-11-28 16:05
package op_ternary
import (
"git.zhangdeman.cn/zhangdeman/op_type"
"git.zhangdeman.cn/zhangdeman/wrapper/op_any"
"git.zhangdeman.cn/zhangdeman/wrapper/op_map"
)
var (
// TernaryOperator 三元运算符操作实例
TernaryOperator = &ternaryOperator{}
)
// ternaryOperator ...
type ternaryOperator struct {
}
// CondFunc ...
type CondFunc func() bool
// defaultCondFunc ...
func defaultCondFunc() bool {
return false
}
// BaseType ...
func BaseType[Bt op_type.BaseType](cond bool, trueVal Bt, falseVal Bt) Bt {
if cond {
return trueVal
}
return falseVal
}
// BaseTypeWithFunc ...
func BaseTypeWithFunc[Bt op_type.BaseType](condFunc CondFunc, trueVal Bt, falseVal Bt) Bt {
if nil == condFunc {
condFunc = defaultCondFunc
}
return BaseType(condFunc(), trueVal, falseVal)
}
// Map ...
func Map(cond bool, trueVal op_map.Map, falseVal op_map.Map) op_map.Map {
if cond {
return trueVal
}
return falseVal
}
// MapWithFunc ...
func MapWithFunc(condFunc CondFunc, trueVal op_map.Map, falseVal op_map.Map) op_map.Map {
if nil == condFunc {
condFunc = defaultCondFunc
}
return Map(condFunc(), trueVal, falseVal)
}
// Any ...
func Any(cond bool, trueVal *op_any.AnyType, falseVal *op_any.AnyType) *op_any.AnyType {
if cond {
return trueVal
}
return falseVal
}
// AnyWithFunc ...
func AnyWithFunc(condFunc CondFunc, trueVal *op_any.AnyType, falseVal *op_any.AnyType) *op_any.AnyType {
if nil == condFunc {
condFunc = defaultCondFunc
}
return Any(condFunc(), trueVal, falseVal)
}