gitlab.com/evatix-go/core@v1.3.55/corecomparator/Compare.go (about) 1 package corecomparator 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "strconv" 8 "strings" 9 10 "gitlab.com/evatix-go/core/constants" 11 ) 12 13 type Compare byte 14 15 const ( 16 Equal Compare = iota 17 LeftGreater 18 LeftGreaterEqual 19 LeftLess 20 LeftLessEqual 21 NotEqual 22 Inconclusive 23 ) 24 25 func (it Compare) Is(other Compare) bool { 26 return it == other 27 } 28 29 func (it Compare) IsLess() bool { 30 return it == LeftLess 31 } 32 33 func (it Compare) IsLessEqual() bool { 34 return it == LeftLess || it == Equal 35 } 36 37 func (it Compare) IsGreater() bool { 38 return it == LeftGreater 39 } 40 41 func (it Compare) IsGreaterEqual() bool { 42 return it == LeftGreater || it == Equal 43 } 44 45 func (it Compare) IsNameEqual(name string) bool { 46 return it.Name() == name 47 } 48 49 func (it Compare) ToNumberString() string { 50 return strconv.Itoa(int(it)) 51 } 52 53 func (it Compare) IsDefined() bool { 54 return it != Inconclusive 55 } 56 57 func (it Compare) IsValid() bool { 58 return it != Inconclusive 59 } 60 61 func (it Compare) IsInvalid() bool { 62 return it == Inconclusive 63 } 64 65 func (it Compare) RangeNamesCsv() string { 66 return RangeNamesCsv() 67 } 68 69 func (it Compare) IsValueEqual(value byte) bool { 70 return byte(it) == value 71 } 72 73 func (it Compare) IsEqual() bool { 74 return it == Equal 75 } 76 77 func (it Compare) IsLeftGreater() bool { 78 return it == LeftGreater 79 } 80 81 func (it Compare) IsLeftGreaterEqual() bool { 82 return it == LeftGreaterEqual 83 } 84 85 func (it Compare) IsLeftLess() bool { 86 return it == LeftLess 87 } 88 89 func (it Compare) IsLeftLessEqual() bool { 90 return it == LeftLessEqual 91 } 92 93 // IsLeftLessOrLessEqualOrEqual 94 // 95 // it == Equal || it == LeftLess || it == LeftLessEqual 96 func (it Compare) IsLeftLessOrLessEqualOrEqual() bool { 97 return it == Equal || it == LeftLess || it == LeftLessEqual 98 } 99 100 // IsLeftLessEqualLogically 101 // 102 // it == Equal || it == LeftLess || it == LeftLessEqual 103 func (it Compare) IsLeftLessEqualLogically() bool { 104 return it == Equal || it == LeftLess || it == LeftLessEqual 105 } 106 107 // IsLeftGreaterOrGreaterEqualOrEqual 108 // 109 // it == Equal || it == LeftGreater || it == LeftGreaterEqual 110 func (it Compare) IsLeftGreaterOrGreaterEqualOrEqual() bool { 111 return it == Equal || it == LeftGreater || it == LeftGreaterEqual 112 } 113 114 // IsLeftGreaterEqualLogically 115 // 116 // it == Equal || it == LeftGreater || it == LeftGreaterEqual 117 func (it Compare) IsLeftGreaterEqualLogically() bool { 118 return it == Equal || it == LeftGreater || it == LeftGreaterEqual 119 } 120 121 func (it Compare) IsNotEqual() bool { 122 return it == NotEqual 123 } 124 125 // IsNotEqualLogically 126 // 127 // return it != Equal 128 func (it Compare) IsNotEqualLogically() bool { 129 return it != Equal 130 } 131 132 // IsDefinedPlus 133 // 134 // return it != Inconclusive && it == right 135 func (it Compare) IsDefinedPlus(right Compare) bool { 136 return it != Inconclusive && it == right 137 } 138 139 func (it Compare) IsInconclusive() bool { 140 return it == Inconclusive 141 } 142 143 func (it Compare) IsNotInconclusive() bool { 144 return it != Inconclusive 145 } 146 147 func (it Compare) IsDefinedProperly() bool { 148 return it != Inconclusive 149 } 150 151 func (it Compare) IsInconclusiveOrNotEqual() bool { 152 return it == Inconclusive || it == NotEqual 153 } 154 155 func (it Compare) IsAnyOf(values ...Compare) bool { 156 if len(values) == 0 { 157 return true 158 } 159 160 for _, value := range values { 161 if it == value { 162 return true 163 } 164 } 165 166 return false 167 } 168 169 func (it Compare) NameValue() string { 170 return fmt.Sprintf( 171 constants.StringWithBracketWrapNumberFormat, 172 it.Name(), 173 it.Value()) 174 } 175 176 func (it Compare) CsvStrings(values ...Compare) []string { 177 if len(values) == 0 { 178 return []string{} 179 } 180 181 slice := make([]string, len(values)) 182 183 for i, value := range values { 184 slice[i] = value.NameValue() 185 } 186 187 return slice 188 } 189 190 func (it Compare) CsvString(values ...Compare) string { 191 if len(values) == 0 { 192 return "" 193 } 194 195 slice := it.CsvStrings(values...) 196 197 return strings.Join(slice, constants.CsvJoiner) 198 } 199 200 func (it Compare) Name() string { 201 return it.String() 202 } 203 204 func (it Compare) MarshalJSON() ([]byte, error) { 205 return json.Marshal(it.String()) 206 } 207 208 func (it *Compare) UnmarshalJSON(data []byte) error { 209 if data == nil { 210 return errors.New("compare unmarshal json error: data nil") 211 } 212 213 name := string(data) 214 215 compare, has := RangesMap[name] 216 217 if has { 218 *it = compare 219 220 return nil 221 } 222 223 return errors.New(string(data) + " failed to convert to core-compare. Must be any of the values.") 224 } 225 226 func (it Compare) Value() byte { 227 return byte(it) 228 } 229 230 func (it Compare) IsCompareEqualLogically( 231 expectedCompare Compare, 232 ) bool { 233 if it == expectedCompare { 234 return true 235 } 236 237 if expectedCompare.IsLeftGreaterEqualLogically() { 238 return it.IsLeftGreaterEqualLogically() 239 } 240 241 if expectedCompare.IsLeftLessEqualLogically() { 242 return it.IsLeftLessEqualLogically() 243 } 244 245 return false 246 } 247 248 func (it Compare) OnlySupportedErr( 249 message string, 250 onlySupportedCompares ...Compare, 251 ) error { 252 if message == "" { 253 return it.OnlySupportedDirectErr(onlySupportedCompares...) 254 } 255 256 if it.IsAnyOf(onlySupportedCompares...) { 257 return nil 258 } 259 260 csv := it.CsvString(onlySupportedCompares...) 261 262 return fmt.Errorf(constants.EnumOnlySupportedWithMessageFormat, 263 it, 264 it.NameValue(), 265 message, 266 csv) 267 } 268 269 func (it Compare) OnlySupportedDirectErr( 270 onlySupportedCompares ...Compare, 271 ) error { 272 if it.IsAnyOf(onlySupportedCompares...) { 273 return nil 274 } 275 276 csv := it.CsvString(onlySupportedCompares...) 277 278 return fmt.Errorf(constants.EnumOnlySupportedFormat, 279 it, 280 it.NameValue(), 281 csv) 282 } 283 284 func (it Compare) OperatorSymbol() string { 285 return CompareOperatorsSymbols[it] 286 } 287 288 func (it Compare) OperatorShortForm() string { 289 return CompareOperatorsShotNames[it] 290 } 291 292 func (it Compare) SqlOperatorSymbol() string { 293 return SqlCompareOperators[it] 294 } 295 296 func (it Compare) NumberString() string { 297 return strconv.Itoa(int(it)) 298 } 299 300 func (it Compare) NumberJsonString() string { 301 return "\"" + strconv.Itoa(int(it)) + "\"" 302 } 303 304 func (it Compare) StringValue() string { 305 return string(it) 306 } 307 308 func (it Compare) String() string { 309 return CompareNames[it] 310 } 311 312 func (it Compare) IsAnyNamesOf(names ...string) bool { 313 for _, name := range names { 314 if it.IsNameEqual(name) { 315 return true 316 } 317 } 318 319 return false 320 } 321 322 func (it Compare) ValueByte() byte { 323 return byte(it) 324 } 325 326 func (it Compare) ValueInt() int { 327 return int(it) 328 } 329 330 func (it Compare) ValueInt8() int8 { 331 return int8(it) 332 } 333 334 func (it Compare) ValueInt16() int16 { 335 return int16(it) 336 } 337 338 func (it Compare) ValueInt32() int32 { 339 return int32(it) 340 } 341 342 func (it Compare) ValueString() string { 343 return it.ToNumberString() 344 } 345 346 func (it Compare) Format(format string) (compiled string) { 347 panic("Not implemented for compare purposefully : " + format) 348 }