gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/KeyValueCollection.go (about) 1 package corestr 2 3 import ( 4 "encoding/json" 5 "sort" 6 "strings" 7 8 "gitlab.com/evatix-go/core/constants" 9 "gitlab.com/evatix-go/core/coredata/corejson" 10 "gitlab.com/evatix-go/core/defaultcapacity" 11 "gitlab.com/evatix-go/core/internal/strutilinternal" 12 ) 13 14 type KeyValueCollection struct { 15 KeyValuePairs []*KeyValuePair `json:"KeyValuePairs,omitempty"` 16 } 17 18 func (it *KeyValueCollection) AllKeysSorted() []string { 19 keys := it.AllKeys() 20 21 sort.Strings(keys) 22 23 return keys 24 } 25 26 func (it *KeyValueCollection) KeysHashset() map[string]bool { 27 panic("implement me") 28 } 29 30 func (it *KeyValueCollection) HasKey(key string) bool { 31 for _, pair := range it.KeyValuePairs { 32 if pair.IsKey(key) { 33 return true 34 } 35 } 36 37 return false 38 } 39 40 func (it KeyValueCollection) SerializeMust() (jsonBytes []byte) { 41 return corejson.NewPtr(it).RawMust() 42 } 43 44 func (it *KeyValueCollection) Compile() string { 45 return it.String() 46 } 47 48 func (it *KeyValueCollection) Count() int { 49 return it.Length() 50 } 51 52 func (it *KeyValueCollection) HasAnyItem() bool { 53 return it.Length() > 0 54 } 55 56 func (it *KeyValueCollection) LastIndex() int { 57 return it.Length() - 1 58 } 59 60 func (it *KeyValueCollection) HasIndex( 61 index int, 62 ) bool { 63 return index != constants.InvalidNotFoundCase && it.LastIndex() >= index 64 } 65 66 func (it *KeyValueCollection) First() *KeyValuePair { 67 return it.KeyValuePairs[0] 68 } 69 70 func (it *KeyValueCollection) FirstOrDefault() *KeyValuePair { 71 if it.IsEmpty() { 72 return nil 73 } 74 75 return it.KeyValuePairs[0] 76 } 77 78 func (it *KeyValueCollection) Last() *KeyValuePair { 79 return it.KeyValuePairs[it.LastIndex()] 80 } 81 82 func (it *KeyValueCollection) LastOrDefault() *KeyValuePair { 83 if it.IsEmpty() { 84 return nil 85 } 86 87 return it.Last() 88 } 89 90 func (it *KeyValueCollection) Find( 91 finder func(index int, currentKeyVal *KeyValuePair) (foundItem *KeyValuePair, isFound, isBreak bool), 92 ) []*KeyValuePair { 93 length := it.Length() 94 95 if length == 0 { 96 return []*KeyValuePair{} 97 } 98 99 slice := make( 100 []*KeyValuePair, 101 0, 102 defaultcapacity.OfSearch(length)) 103 104 for i, item := range it.KeyValuePairs { 105 foundItem, isFound, isBreak := finder(i, item) 106 107 if isFound && foundItem != nil { 108 slice = append(slice, foundItem) 109 } 110 111 if isBreak { 112 return slice 113 } 114 } 115 116 return slice 117 } 118 119 func (it *KeyValueCollection) SafeValueAt(index int) string { 120 if it.IsEmpty() { 121 return constants.EmptyString 122 } 123 124 if it.HasIndex(index) { 125 return it.KeyValuePairs[index].Value 126 } 127 128 return constants.EmptyString 129 } 130 131 func (it *KeyValueCollection) SafeValuesAtIndexes(indexes ...int) []string { 132 requestLength := len(indexes) 133 slice := make([]string, requestLength) 134 135 if requestLength == 0 { 136 return slice 137 } 138 139 for i, index := range indexes { 140 slice[i] = it.SafeValueAt(index) 141 } 142 143 return slice 144 } 145 146 func (it *KeyValueCollection) Strings() []string { 147 if it.IsEmpty() { 148 return []string{} 149 } 150 151 slice := make([]string, it.Length()) 152 153 for i, keyVal := range it.KeyValuePairs { 154 slice[i] = keyVal.String() 155 } 156 157 return slice 158 } 159 160 func (it *KeyValueCollection) StringsUsingFormat( 161 format string, 162 ) []string { 163 if it.IsEmpty() { 164 return []string{} 165 } 166 167 slice := make([]string, it.Length()) 168 169 for i, keyVal := range it.KeyValuePairs { 170 slice[i] = keyVal.FormatString(format) 171 } 172 173 return slice 174 } 175 176 func (it *KeyValueCollection) String() string { 177 return strutilinternal.AnyToString(it.Strings()) 178 } 179 180 func (it *KeyValueCollection) Length() int { 181 if it == nil { 182 return 0 183 } 184 185 return len(it.KeyValuePairs) 186 } 187 188 func (it *KeyValueCollection) IsEmpty() bool { 189 return it.Length() == 0 190 } 191 192 func (it *KeyValueCollection) Add(key, val string) *KeyValueCollection { 193 it.KeyValuePairs = append(it.KeyValuePairs, &KeyValuePair{ 194 Key: key, 195 Value: val, 196 }) 197 198 return it 199 } 200 201 func (it *KeyValueCollection) AddIf( 202 isAdd bool, 203 key, val string, 204 ) *KeyValueCollection { 205 if !isAdd { 206 return it 207 } 208 209 it.KeyValuePairs = append(it.KeyValuePairs, &KeyValuePair{ 210 Key: key, 211 Value: val, 212 }) 213 214 return it 215 } 216 217 func (it *KeyValueCollection) AddStringBySplit( 218 splitter, 219 line string, 220 ) *KeyValueCollection { 221 key, val := strutilinternal.SplitLeftRight( 222 splitter, 223 line) 224 225 return it.Add(key, val) 226 } 227 228 func (it *KeyValueCollection) AddStringBySplitTrim( 229 splitter, 230 line string, 231 ) *KeyValueCollection { 232 key, val := strutilinternal.SplitLeftRightTrim( 233 splitter, 234 line) 235 236 return it.Add(key, val) 237 } 238 239 func (it *KeyValueCollection) Adds( 240 keyValues ...KeyValuePair, 241 ) *KeyValueCollection { 242 if len(keyValues) == 0 { 243 return it 244 } 245 246 for _, keyVal := range keyValues { 247 it.KeyValuePairs = append(it.KeyValuePairs, &KeyValuePair{ 248 Key: keyVal.Key, 249 Value: keyVal.Value, 250 }) 251 } 252 253 return it 254 } 255 256 func (it *KeyValueCollection) AddMap( 257 inputMap map[string]string, 258 ) *KeyValueCollection { 259 if inputMap == nil || len(inputMap) == 0 { 260 return it 261 } 262 263 for key, val := range inputMap { 264 it.KeyValuePairs = append(it.KeyValuePairs, &KeyValuePair{ 265 Key: key, 266 Value: val, 267 }) 268 } 269 270 return it 271 } 272 273 func (it *KeyValueCollection) AddHashsetMap( 274 inputMap map[string]bool, 275 ) *KeyValueCollection { 276 if inputMap == nil || len(inputMap) == 0 { 277 return it 278 } 279 280 for key := range inputMap { 281 it.KeyValuePairs = append(it.KeyValuePairs, &KeyValuePair{ 282 Key: key, 283 Value: key, 284 }) 285 } 286 287 return it 288 } 289 290 func (it *KeyValueCollection) AddHashset( 291 inputHashset *Hashset, 292 ) *KeyValueCollection { 293 if inputHashset == nil || inputHashset.IsEmpty() { 294 return it 295 } 296 297 for key := range inputHashset.items { 298 it.KeyValuePairs = append(it.KeyValuePairs, &KeyValuePair{ 299 Key: key, 300 Value: key, 301 }) 302 } 303 304 return it 305 } 306 307 func (it *KeyValueCollection) AddsHashmap( 308 hashmap *Hashmap, 309 ) *KeyValueCollection { 310 if hashmap == nil || hashmap.IsEmpty() { 311 return it 312 } 313 314 for key, val := range hashmap.items { 315 it.KeyValuePairs = append(it.KeyValuePairs, &KeyValuePair{ 316 Key: key, 317 Value: val, 318 }) 319 } 320 321 return it 322 } 323 324 func (it *KeyValueCollection) Hashmap() *Hashmap { 325 length := it.Length() 326 hashmap := New.Hashmap.Cap(length) 327 328 if length == 0 { 329 return hashmap 330 } 331 332 for _, keyVal := range it.KeyValuePairs { 333 hashmap.AddOrUpdate(keyVal.Key, keyVal.Value) 334 } 335 336 return hashmap 337 } 338 339 func (it *KeyValueCollection) Map() map[string]string { 340 hashmap := it.Hashmap() 341 342 return hashmap.items 343 } 344 345 func (it *KeyValueCollection) AddsHashmaps( 346 hashmaps ...*Hashmap, 347 ) *KeyValueCollection { 348 if hashmaps == nil || len(hashmaps) == 0 { 349 return it 350 } 351 352 for _, hashmap := range hashmaps { 353 it.AddsHashmap(hashmap) 354 } 355 356 return it 357 } 358 359 func (it *KeyValueCollection) AllKeys() []string { 360 length := len(it.KeyValuePairs) 361 keys := make([]string, length) 362 363 if length == 0 { 364 return keys 365 } 366 367 i := 0 368 for _, item := range it.KeyValuePairs { 369 keys[i] = item.Key 370 i++ 371 } 372 373 return keys 374 } 375 376 func (it *KeyValueCollection) AllValues() []string { 377 length := len(it.KeyValuePairs) 378 values := make([]string, length) 379 380 if length == 0 { 381 return values 382 } 383 384 i := 0 385 for _, item := range it.KeyValuePairs { 386 values[i] = item.Value 387 i++ 388 } 389 390 return values 391 } 392 393 // Join values 394 func (it *KeyValueCollection) Join( 395 separator string, 396 ) string { 397 return strings.Join(it.Strings(), separator) 398 } 399 400 func (it *KeyValueCollection) JoinKeys( 401 separator string, 402 ) string { 403 return strings.Join(it.AllKeys(), separator) 404 } 405 406 func (it *KeyValueCollection) JoinValues( 407 separator string, 408 ) string { 409 return strings.Join(it.AllValues(), separator) 410 } 411 412 func (it *KeyValueCollection) JsonModel() []*KeyValuePair { 413 return it.KeyValuePairs 414 } 415 416 func (it *KeyValueCollection) JsonModelAny() interface{} { 417 return it.JsonModel() 418 } 419 420 func (it *KeyValueCollection) Serialize() ([]byte, error) { 421 return corejson.Serialize.Raw(it) 422 } 423 424 func (it *KeyValueCollection) MarshalJSON() ([]byte, error) { 425 return json.Marshal(it.JsonModel()) 426 } 427 428 func (it *KeyValueCollection) UnmarshalJSON(data []byte) error { 429 var dataModelItems []*KeyValuePair 430 err := corejson.Deserialize.UsingBytes( 431 data, 432 &dataModelItems) 433 434 if err == nil && len(dataModelItems) > 0 { 435 it.KeyValuePairs = dataModelItems 436 } else if err == nil { 437 it.KeyValuePairs = []*KeyValuePair{} 438 } 439 440 return err 441 } 442 443 func (it KeyValueCollection) Json() corejson.Result { 444 return corejson.New(it) 445 } 446 447 func (it KeyValueCollection) JsonPtr() *corejson.Result { 448 return corejson.NewPtr(it) 449 } 450 451 func (it *KeyValueCollection) ParseInjectUsingJson( 452 jsonResult *corejson.Result, 453 ) (*KeyValueCollection, error) { 454 err := jsonResult.Unmarshal(it) 455 456 if err != nil { 457 return nil, err 458 } 459 460 return it, nil 461 } 462 463 func (it *KeyValueCollection) AsJsonContractsBinder() corejson.JsonContractsBinder { 464 return it 465 } 466 467 func (it *KeyValueCollection) AsJsoner() corejson.Jsoner { 468 return it 469 } 470 471 func (it *KeyValueCollection) JsonParseSelfInject( 472 jsonResult *corejson.Result, 473 ) error { 474 _, err := it.ParseInjectUsingJson( 475 jsonResult, 476 ) 477 478 return err 479 } 480 481 func (it *KeyValueCollection) AsJsonParseSelfInjector() corejson.JsonParseSelfInjector { 482 return it 483 } 484 485 func (it *KeyValueCollection) Clear() { 486 if it == nil { 487 return 488 } 489 490 tempItems := it.KeyValuePairs 491 clearFunc := func() { 492 for i := 0; i < len(tempItems); i++ { 493 tempItems[i].Dispose() 494 tempItems[i] = nil 495 } 496 } 497 498 go clearFunc() 499 it.KeyValuePairs = []*KeyValuePair{} 500 } 501 502 func (it *KeyValueCollection) Dispose() { 503 if it == nil { 504 return 505 } 506 507 it.Clear() 508 } 509 510 func (it *KeyValueCollection) Deserialize(toPtr interface{}) (parsingErr error) { 511 return it.JsonPtr().Deserialize(toPtr) 512 }