github.com/Aoi-hosizora/ahlib@v1.5.1-0.20230404072829-241b93cf91c7/xcondition/xcondition.go (about) 1 package xcondition 2 3 import ( 4 "fmt" 5 "github.com/Aoi-hosizora/ahlib/xreflect" 6 ) 7 8 // IfThen returns value if condition is true, otherwise returns nil. 9 func IfThen(condition bool, value interface{}) interface{} { 10 if condition { 11 return value 12 } 13 return nil 14 } 15 16 // IfThenElse returns value1 if condition is true, otherwise returns value2. 17 func IfThenElse(condition bool, value1, value2 interface{}) interface{} { 18 if condition { 19 return value1 20 } 21 return value2 22 } 23 24 // If is the short form of IfThenElse. 25 func If(cond bool, v1, v2 interface{}) interface{} { 26 return IfThenElse(cond, v1, v2) 27 } 28 29 // DefaultIfNil returns value if it is not nil, otherwise returns defaultValue. Note that this also checks the wrapped data of given value. 30 func DefaultIfNil(value, defaultValue interface{}) interface{} { 31 if !xreflect.IsNilValue(value) { 32 return value 33 } 34 return defaultValue 35 } 36 37 // PanicIfNil returns value if it is not nil, otherwise panics with given the first panicValue. Note that this also checks the wrapped data of given value. 38 func PanicIfNil(value interface{}, panicValue ...interface{}) interface{} { 39 if !xreflect.IsNilValue(value) { 40 return value 41 } 42 if len(panicValue) == 0 || panicValue[0] == nil { 43 panic(fmt.Sprintf("xcondition: nil value for %T", value)) 44 } 45 panic(panicValue[0]) 46 } 47 48 // Un is the short form of PanicIfNil without panicValue, which means "unwrap nil with builtin panic value". 49 func Un(v interface{}) interface{} { 50 return PanicIfNil(v) 51 } 52 53 // Unp is the short form of PanicIfNil with panicValue, which means "unwrap nil with custom panic value". 54 func Unp(v, panicV interface{}) interface{} { 55 return PanicIfNil(v, panicV) 56 } 57 58 // PanicIfErr returns value if given err is nil, otherwise panics with given error message. 59 func PanicIfErr(value interface{}, err error) interface{} { 60 if err != nil { 61 panic(err.Error()) 62 } 63 return value 64 } 65 66 // PanicIfErr2 returns value1 and value2 if given err is nil, otherwise panics with given error message. 67 func PanicIfErr2(value1, value2 interface{}, err error) (interface{}, interface{}) { 68 if err != nil { 69 panic(err.Error()) 70 } 71 return value1, value2 72 } 73 74 // PanicIfErr3 returns value1, value2 and value3 if given err is nil, otherwise panics with given error message. 75 func PanicIfErr3(value1, value2, value3 interface{}, err error) (interface{}, interface{}, interface{}) { 76 if err != nil { 77 panic(err.Error()) 78 } 79 return value1, value2, value3 80 } 81 82 // Ue is the short form of PanicIfErr, which means "unwrap error". 83 func Ue(v interface{}, err error) interface{} { 84 return PanicIfErr(v, err) 85 } 86 87 // Ue2 is the short form of PanicIfErr2, which means "unwrap error". 88 func Ue2(v1, v2 interface{}, err error) (interface{}, interface{}) { 89 return PanicIfErr2(v1, v2, err) 90 } 91 92 // Ue3 is the short form of PanicIfErr3, which means "unwrap error". 93 func Ue3(v1, v2, v3 interface{}, err error) (interface{}, interface{}, interface{}) { 94 return PanicIfErr3(v1, v2, v3, err) 95 } 96 97 // Let calls given function on given value and returns the value returned by given function, if given value and function are both not nil, 98 // otherwise returns the zero value of U type, just like kotlin `let` scope function. 99 func Let(value interface{}, f func(interface{}) interface{}) interface{} { 100 if xreflect.IsNilValue(value) || f == nil { 101 return nil 102 } 103 return f(value) 104 } 105 106 // NillableLet calls given function on given value and returns the value returned by given function, if given function is not nil, otherwise returns the zero value of U type. 107 // The only difference between NillableLet and Let is that, as for NillableLet, given function will also be called even if given value is nil. 108 func NillableLet(value interface{}, f func(interface{}) interface{}) interface{} { 109 if f == nil { 110 return nil 111 } 112 return f(value) 113 } 114 115 const ( 116 panicIndexOutOfRange = "xcondition: index out of range" 117 ) 118 119 // First returns the first element of args, panics if it is out of range. 120 func First(args ...interface{}) interface{} { 121 if len(args) <= 0 { 122 panic(panicIndexOutOfRange) 123 } 124 return args[0] 125 } 126 127 // Second returns the second element of args, panics if it is out of range. 128 func Second(args ...interface{}) interface{} { 129 if len(args) <= 1 { 130 panic(panicIndexOutOfRange) 131 } 132 return args[1] 133 } 134 135 // Third returns the third element of args, panics if it is out of range. 136 func Third(args ...interface{}) interface{} { 137 if len(args) <= 2 { 138 panic(panicIndexOutOfRange) 139 } 140 return args[2] 141 } 142 143 // Last returns the last element of args, panics if it is out of range. 144 func Last(args ...interface{}) interface{} { 145 if len(args) <= 0 { 146 panic(panicIndexOutOfRange) 147 } 148 return args[len(args)-1] 149 }