gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/LeftRight.go (about) 1 package corestr 2 3 import ( 4 "regexp" 5 "strings" 6 7 "gitlab.com/evatix-go/core/constants" 8 "gitlab.com/evatix-go/core/internal/strutilinternal" 9 ) 10 11 type LeftRight struct { 12 Left, Right string 13 IsValid bool 14 Message string 15 } 16 17 func InvalidLeftRightNoMessage() *LeftRight { 18 return &LeftRight{ 19 Left: constants.EmptyString, 20 Right: constants.EmptyString, 21 IsValid: false, 22 Message: constants.EmptyString, 23 } 24 } 25 26 func InvalidLeftRight(message string) *LeftRight { 27 return &LeftRight{ 28 Left: constants.EmptyString, 29 Right: constants.EmptyString, 30 IsValid: false, 31 Message: message, 32 } 33 } 34 35 func LeftRightUsingSlicePtr(slice *[]string) *LeftRight { 36 if slice == nil || *slice == nil { 37 return LeftRightUsingSlice(nil) 38 } 39 40 return LeftRightUsingSlice(*slice) 41 } 42 43 func LeftRightTrimmedUsingSlice(slice []string) *LeftRight { 44 if slice == nil { 45 return LeftRightUsingSlice(nil) 46 } 47 48 length := len(slice) 49 50 if length == 0 { 51 return InvalidLeftRight( 52 LeftRightExpectingLengthMessager.Message( 53 length)) 54 } 55 56 if length == 1 { 57 return &LeftRight{ 58 Left: slice[constants.Zero], 59 Right: constants.EmptyString, 60 IsValid: length == ExpectingLengthForLeftRight, 61 } 62 } 63 64 return &LeftRight{ 65 Left: strings.TrimSpace(slice[constants.Zero]), 66 Right: strings.TrimSpace(slice[length-1]), 67 IsValid: length == ExpectingLengthForLeftRight, 68 } 69 } 70 71 func LeftRightUsingSlice(slice []string) *LeftRight { 72 length := len(slice) 73 74 if length == 0 { 75 return InvalidLeftRight( 76 LeftRightExpectingLengthMessager.Message( 77 length)) 78 } 79 80 if length == 1 { 81 return &LeftRight{ 82 Left: slice[constants.Zero], 83 Right: constants.EmptyString, 84 IsValid: length == ExpectingLengthForLeftRight, 85 } 86 } 87 88 return &LeftRight{ 89 Left: slice[constants.Zero], 90 Right: slice[length-1], 91 IsValid: length == ExpectingLengthForLeftRight, 92 } 93 } 94 95 func (it *LeftRight) LeftBytes() []byte { 96 return []byte(it.Left) 97 } 98 99 func (it *LeftRight) RightBytes() []byte { 100 return []byte(it.Right) 101 } 102 103 func (it *LeftRight) LeftTrim() string { 104 return strings.TrimSpace(it.Left) 105 } 106 107 func (it *LeftRight) RightTrim() string { 108 return strings.TrimSpace(it.Right) 109 } 110 111 func (it *LeftRight) IsLeftEmpty() bool { 112 return it.Left == "" 113 } 114 115 func (it *LeftRight) IsRightEmpty() bool { 116 return it.Right == "" 117 } 118 119 func (it *LeftRight) IsRightWhitespace() bool { 120 return strutilinternal.IsEmptyOrWhitespace(it.Right) 121 } 122 123 func (it *LeftRight) IsLeftWhitespace() bool { 124 return strutilinternal.IsEmptyOrWhitespace(it.Left) 125 } 126 127 func (it *LeftRight) HasValidNonEmptyLeft() bool { 128 return it.IsValid && !it.IsLeftEmpty() 129 } 130 131 func (it *LeftRight) HasValidNonEmptyRight() bool { 132 return it.IsValid && !it.IsRightEmpty() 133 } 134 135 func (it *LeftRight) HasValidNonWhitespaceLeft() bool { 136 return it.IsValid && !it.IsLeftWhitespace() 137 } 138 139 func (it *LeftRight) HasValidNonWhitespaceRight() bool { 140 return it.IsValid && !it.IsRightWhitespace() 141 } 142 143 // HasSafeNonEmpty receiver.IsValid && 144 // !receiver.IsLeftEmpty() && 145 // !receiver.IsRightEmpty() 146 func (it *LeftRight) HasSafeNonEmpty() bool { 147 return it.IsValid && 148 !it.IsLeftEmpty() && 149 !it.IsRightEmpty() 150 } 151 152 func (it LeftRight) NonPtr() LeftRight { 153 return it 154 } 155 156 func (it *LeftRight) Ptr() *LeftRight { 157 return it 158 } 159 160 func (it *LeftRight) IsLeftRegexMatch(regexp *regexp.Regexp) bool { 161 if regexp == nil { 162 return false 163 } 164 165 return regexp.MatchString(it.Left) 166 } 167 168 func (it *LeftRight) IsRightRegexMatch(regexp *regexp.Regexp) bool { 169 if regexp == nil { 170 return false 171 } 172 173 return regexp.MatchString(it.Right) 174 } 175 176 func (it *LeftRight) IsLeft(left string) bool { 177 return it.Left == left 178 } 179 180 func (it *LeftRight) IsRight(right string) bool { 181 return it.Right == right 182 } 183 184 func (it *LeftRight) Is(left, right string) bool { 185 return it.Left == left && it.Right == right 186 } 187 188 func (it *LeftRight) IsEqual(another *LeftRight) bool { 189 if it == nil && another == nil { 190 return true 191 } 192 193 if it == nil || another == nil { 194 return false 195 } 196 197 return it.IsValid == another.IsValid && 198 it.Is(another.Left, another.Right) 199 } 200 201 func (it *LeftRight) Clone() *LeftRight { 202 return &LeftRight{ 203 Left: it.Left, 204 Right: it.Right, 205 IsValid: it.IsValid, 206 Message: it.Message, 207 } 208 } 209 210 func (it *LeftRight) Clear() { 211 if it == nil { 212 return 213 } 214 215 it.Left = "" 216 it.Right = "" 217 it.IsValid = false 218 it.Message = "" 219 } 220 221 func (it *LeftRight) Dispose() { 222 if it == nil { 223 return 224 } 225 226 it.Clear() 227 }