feat: 相关操作升级为泛型实现
This commit is contained in:
79
op_ternary/ternary_operator.go
Normal file
79
op_ternary/ternary_operator.go
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user