gitlab.com/evatix-go/core@v1.3.55/namevalue/NameValuesCollection.go (about) 1 package namevalue 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "strings" 8 9 "gitlab.com/evatix-go/core/constants" 10 ) 11 12 type NameValuesCollection struct { 13 Items []Instance `json:"Items,omitempty"` 14 lazyToString *string 15 } 16 17 func NewNameValuesCollection(capacity int) *NameValuesCollection { 18 slice := make([]Instance, 0, capacity) 19 20 return &NameValuesCollection{ 21 Items: slice, 22 } 23 } 24 25 func NewCollection() *NameValuesCollection { 26 return NewNameValuesCollection(constants.Capacity5) 27 } 28 29 func NewNewNameValuesCollectionUsing( 30 isClone bool, 31 items ...Instance, 32 ) *NameValuesCollection { 33 if items == nil { 34 return EmptyNameValuesCollection() 35 } 36 37 if !isClone { 38 return &NameValuesCollection{ 39 Items: items, 40 } 41 } 42 43 slice := NewNameValuesCollection(len(items)) 44 45 return slice.Adds(items...) 46 } 47 48 func EmptyNameValuesCollection() *NameValuesCollection { 49 return NewNameValuesCollection(0) 50 } 51 52 func (it *NameValuesCollection) Add( 53 item Instance, 54 ) *NameValuesCollection { 55 it.InvalidateLazyString() 56 it.Items = append(it.Items, item) 57 58 return it 59 } 60 61 func (it *NameValuesCollection) Adds( 62 items ...Instance, 63 ) *NameValuesCollection { 64 if len(items) == 0 { 65 return it 66 } 67 68 it.InvalidateLazyString() 69 it.Items = append( 70 it.Items, 71 items...) 72 73 return it 74 } 75 76 func (it *NameValuesCollection) Append( 77 items ...Instance, 78 ) *NameValuesCollection { 79 if len(items) == 0 { 80 return it 81 } 82 83 it.InvalidateLazyString() 84 it.Items = append( 85 it.Items, 86 items...) 87 88 return it 89 } 90 91 func (it *NameValuesCollection) AppendIf( 92 isAppend bool, 93 items ...Instance, 94 ) *NameValuesCollection { 95 if !isAppend || len(items) == 0 { 96 return it 97 } 98 99 it.InvalidateLazyString() 100 it.Items = append( 101 it.Items, 102 items...) 103 104 return it 105 } 106 107 func (it *NameValuesCollection) Prepend( 108 items ...Instance, 109 ) *NameValuesCollection { 110 if len(items) == 0 { 111 return it 112 } 113 114 it.InvalidateLazyString() 115 it.Items = append( 116 items, 117 it.Items...) 118 119 return it 120 } 121 122 func (it *NameValuesCollection) PrependIf( 123 isPrepend bool, 124 items ...Instance, 125 ) *NameValuesCollection { 126 if !isPrepend || len(items) == 0 { 127 return it 128 } 129 130 it.InvalidateLazyString() 131 it.Items = append( 132 items, 133 it.Items...) 134 135 return it 136 } 137 138 func (it *NameValuesCollection) PrependUsingFuncIf( 139 isPrepend bool, 140 itemsGetterFunc func() []Instance, 141 ) *NameValuesCollection { 142 if !isPrepend || itemsGetterFunc == nil { 143 return it 144 } 145 146 it.InvalidateLazyString() 147 it.Items = append( 148 itemsGetterFunc(), 149 it.Items...) 150 151 return it 152 } 153 154 func (it *NameValuesCollection) AppendUsingFuncIf( 155 isAppend bool, 156 itemsGetterFunc func() []Instance, 157 ) *NameValuesCollection { 158 if !isAppend || itemsGetterFunc == nil { 159 return it 160 } 161 162 it.InvalidateLazyString() 163 it.Items = append( 164 it.Items, 165 itemsGetterFunc()..., 166 ) 167 168 return it 169 } 170 171 func (it *NameValuesCollection) AppendPrependIf( 172 isAppendOrPrepend bool, 173 prependItems []Instance, 174 appendItems []Instance, 175 ) *NameValuesCollection { 176 if !isAppendOrPrepend { 177 return it 178 } 179 180 if len(prependItems) > 0 { 181 it.InvalidateLazyString() 182 it.Items = append( 183 prependItems, 184 it.Items...) 185 } 186 187 if len(appendItems) > 0 { 188 it.InvalidateLazyString() 189 it.Items = append( 190 it.Items, 191 appendItems...) 192 } 193 194 return it 195 } 196 197 func (it *NameValuesCollection) AddsPtr( 198 items ...*Instance, 199 ) *NameValuesCollection { 200 if len(items) == 0 { 201 return it 202 } 203 204 for _, item := range items { 205 if item == nil { 206 continue 207 } 208 209 it.Items = append( 210 it.Items, 211 *item) 212 } 213 214 return it 215 } 216 217 func (it *NameValuesCollection) HasCompiledString() bool { 218 return it != nil && it.lazyToString != nil 219 } 220 221 func (it *NameValuesCollection) InvalidateLazyString() { 222 if it == nil { 223 return 224 } 225 226 it.lazyToString = nil 227 } 228 229 func (it *NameValuesCollection) CompiledLazyString() string { 230 if it == nil { 231 return constants.EmptyString 232 } 233 234 if it.lazyToString != nil { 235 return *it.lazyToString 236 } 237 238 toString := it.String() 239 it.lazyToString = &toString 240 241 return toString 242 } 243 244 func (it *NameValuesCollection) ConcatNew( 245 additionalItems ...Instance, 246 ) *NameValuesCollection { 247 cloned := it.Clone() 248 249 return cloned.Adds(additionalItems...) 250 } 251 252 func (it *NameValuesCollection) ConcatNewPtr( 253 additionalItems ...*Instance, 254 ) *NameValuesCollection { 255 cloned := it.Clone() 256 257 return cloned.AddsPtr( 258 additionalItems...) 259 } 260 261 func (it *NameValuesCollection) AddsIf( 262 isAdd bool, 263 items ...Instance, 264 ) *NameValuesCollection { 265 if !isAdd { 266 return it 267 } 268 269 it.InvalidateLazyString() 270 271 return it.Adds(items...) 272 } 273 274 func (it *NameValuesCollection) Length() int { 275 if it == nil { 276 return 0 277 } 278 279 return len(it.Items) 280 } 281 282 func (it *NameValuesCollection) Count() int { 283 return it.Length() 284 } 285 286 func (it *NameValuesCollection) IsEmpty() bool { 287 return it.Length() == 0 288 } 289 290 func (it *NameValuesCollection) HasAnyItem() bool { 291 return !it.IsEmpty() 292 } 293 294 func (it *NameValuesCollection) LastIndex() int { 295 return it.Length() - 1 296 } 297 298 func (it *NameValuesCollection) HasIndex(index int) bool { 299 return it.LastIndex() >= index 300 } 301 302 func (it *NameValuesCollection) Strings() []string { 303 list := make([]string, it.Length()) 304 305 for i, item := range it.Items { 306 list[i] = item.String() 307 } 308 309 return list 310 } 311 312 func (it *NameValuesCollection) JsonStrings() []string { 313 list := make([]string, it.Length()) 314 315 for i, item := range it.Items { 316 list[i] = item.JsonString() 317 } 318 319 return list 320 } 321 322 func (it *NameValuesCollection) JoinJsonStrings(joiner string) string { 323 return strings.Join(it.JsonStrings(), joiner) 324 } 325 326 func (it *NameValuesCollection) Join(joiner string) string { 327 return strings.Join(it.Strings(), joiner) 328 } 329 330 func (it *NameValuesCollection) JoinLines() string { 331 return strings.Join(it.Strings(), constants.DefaultLine) 332 } 333 334 func (it *NameValuesCollection) JoinCsv() string { 335 return strings.Join(it.CsvStrings(), constants.Comma) 336 } 337 338 func (it *NameValuesCollection) JoinCsvLine() string { 339 return strings.Join(it.CsvStrings(), constants.CommaUnixNewLine) 340 } 341 342 func (it *NameValuesCollection) IsEqual(another *NameValuesCollection) bool { 343 if it == nil && another == nil { 344 return true 345 } 346 347 if it == nil || another == nil { 348 return false 349 } 350 351 if it.Length() != another.Length() { 352 return false 353 } 354 355 return it.IsEqualItems(another.Items...) 356 } 357 358 func (it *NameValuesCollection) IsEqualItems(lines ...Instance) bool { 359 if it == nil && lines == nil { 360 return true 361 } 362 363 if it == nil || lines == nil { 364 return false 365 } 366 367 if it.Length() != len(lines) { 368 return false 369 } 370 371 for i, item := range it.Items { 372 anotherItem := lines[i] 373 374 if item != anotherItem { 375 return false 376 } 377 } 378 379 return true 380 } 381 382 func (it NameValuesCollection) JsonString() string { 383 if it.IsEmpty() { 384 return constants.EmptyString 385 } 386 387 jsonBytes, err := json.Marshal(it) 388 389 if err != nil || jsonBytes == nil { 390 return constants.EmptyString 391 } 392 393 return string(jsonBytes) 394 } 395 396 func (it *NameValuesCollection) String() string { 397 if it.IsEmpty() { 398 return constants.EmptyString 399 } 400 401 if it.HasCompiledString() { 402 return *it.lazyToString 403 } 404 405 return it.JoinLines() 406 } 407 408 func (it *NameValuesCollection) Error() error { 409 if it.IsEmpty() { 410 return nil 411 } 412 413 return errors.New(it.String()) 414 } 415 416 func (it *NameValuesCollection) ErrorUsingMessage(message string) error { 417 if it.IsEmpty() { 418 return nil 419 } 420 421 toCompiled := message + constants.Space + it.String() 422 423 return errors.New(toCompiled) 424 } 425 426 func (it *NameValuesCollection) CsvStrings() []string { 427 if it.IsEmpty() { 428 return []string{} 429 } 430 431 newSlice := make([]string, it.Length()) 432 433 for i, item := range it.Items { 434 newSlice[i] = fmt.Sprintf( 435 constants.SprintDoubleQuoteFormat, 436 item.String()) 437 } 438 439 return newSlice 440 } 441 442 func (it *NameValuesCollection) Clear() *NameValuesCollection { 443 if it == nil { 444 return it 445 } 446 447 tempItems := it.Items 448 clearFunc := func() { 449 for _, item := range tempItems { 450 item.Dispose() 451 } 452 } 453 454 go clearFunc() 455 456 it.Items = []Instance{} 457 it.lazyToString = nil 458 459 return it 460 } 461 462 func (it *NameValuesCollection) Dispose() { 463 if it == nil { 464 return 465 } 466 467 it.Clear() 468 it.Items = nil 469 } 470 471 func (it NameValuesCollection) Clone() NameValuesCollection { 472 list := NewNameValuesCollection(it.Length()) 473 474 return *list.Adds(it.Items...) 475 } 476 477 func (it *NameValuesCollection) ClonePtr() *NameValuesCollection { 478 if it == nil { 479 return nil 480 } 481 482 list := NewNameValuesCollection(it.Length()) 483 484 return list.Adds(it.Items...) 485 }