diff --git a/object.go b/object.go index d991df4..c090418 100644 --- a/object.go +++ b/object.go @@ -37,7 +37,7 @@ func ObjectData(data interface{}) *ObjectType { case reflect.Struct: ot.byteData, _ = json.Marshal(ot.source) default: - // 数据类型不是 nil / map / struct 质疑 + // 数据类型不是 nil / map / struct 之一 ot.isValid = false } return ot diff --git a/ternary_operator.go b/ternary_operator.go new file mode 100644 index 0000000..f8052c3 --- /dev/null +++ b/ternary_operator.go @@ -0,0 +1,181 @@ +// Package wrapper ... +// +// Description : wrapper ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-11-28 16:05 +package wrapper + +var ( + // TernaryOperator 三元运算符操作实例 + TernaryOperator = &ternaryOperator{} +) + +// ternaryOperator ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:06 2023/11/28 +type ternaryOperator struct { +} + +// CondFunc ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:10 2023/11/28 +type CondFunc func() bool + +// defaultCondFunc ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:11 2023/11/28 +func defaultCondFunc() bool { + return false +} + +// Int ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:10 2023/11/28 +func (to *ternaryOperator) Int(cond bool, trueVal Int, falseVal Int) Int { + if cond { + return trueVal + } + return falseVal +} + +// IntWithFunc ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:11 2023/11/28 +func (to *ternaryOperator) IntWithFunc(condFunc CondFunc, trueVal Int, falseVal Int) Int { + if nil == condFunc { + condFunc = defaultCondFunc + } + return to.Int(condFunc(), trueVal, falseVal) +} + +// Float ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:10 2023/11/28 +func (to *ternaryOperator) Float(cond bool, trueVal Float, falseVal Float) Float { + if cond { + return trueVal + } + return falseVal +} + +// FloatWithFunc ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:13 2023/11/28 +func (to *ternaryOperator) FloatWithFunc(condFunc CondFunc, trueVal Float, falseVal Float) Float { + if nil == condFunc { + condFunc = defaultCondFunc + } + return to.Float(condFunc(), trueVal, falseVal) +} + +// String ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:15 2023/11/28 +func (to *ternaryOperator) String(cond bool, trueVal String, falseVal String) String { + if cond { + return trueVal + } + return falseVal +} + +// StringWithFunc ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:15 2023/11/28 +func (to *ternaryOperator) StringWithFunc(condFunc CondFunc, trueVal String, falseVal String) String { + if nil == condFunc { + condFunc = defaultCondFunc + } + return to.String(condFunc(), trueVal, falseVal) +} + +// Array ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:22 2023/11/28 +func (to *ternaryOperator) Array(cond bool, trueVal *Array, falseVal *Array) *Array { + if cond { + return trueVal + } + return falseVal +} + +// ArrayWithFunc ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:22 2023/11/28 +func (to *ternaryOperator) ArrayWithFunc(condFunc CondFunc, trueVal *Array, falseVal *Array) *Array { + if nil == condFunc { + condFunc = defaultCondFunc + } + return to.Array(condFunc(), trueVal, falseVal) +} + +// Map ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:25 2023/11/28 +func (to *ternaryOperator) Map(cond bool, trueVal Map, falseVal Map) Map { + if cond { + return trueVal + } + return falseVal +} + +// MapWithFunc ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:24 2023/11/28 +func (to *ternaryOperator) MapWithFunc(condFunc CondFunc, trueVal Map, falseVal Map) Map { + if nil == condFunc { + condFunc = defaultCondFunc + } + return falseVal +} + +// Any ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:26 2023/11/28 +func (to *ternaryOperator) Any(cond bool, trueVal *AnyType, falseVal *AnyType) *AnyType { + if cond { + return trueVal + } + return falseVal +} + +// AnyWithFunc ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:24 2023/11/28 +func (to *ternaryOperator) AnyWithFunc(condFunc CondFunc, trueVal *AnyType, falseVal *AnyType) *AnyType { + if nil == condFunc { + condFunc = defaultCondFunc + } + return to.Any(condFunc(), trueVal, falseVal) +}