gitlab.com/evatix-go/core@v1.3.55/coredata/coredynamic/KeyValCollection.go (about) 1 package coredynamic 2 3 import ( 4 "fmt" 5 "math" 6 "sort" 7 "sync" 8 9 "gitlab.com/evatix-go/core/constants" 10 "gitlab.com/evatix-go/core/coredata/corejson" 11 "gitlab.com/evatix-go/core/pagingutil" 12 ) 13 14 type KeyValCollection struct { 15 items []KeyVal 16 } 17 18 func EmptyKeyValCollection() *KeyValCollection { 19 return NewKeyValCollection(constants.Zero) 20 } 21 22 func NewKeyValCollection(capacity int) *KeyValCollection { 23 slice := make([]KeyVal, 0, capacity) 24 25 return &KeyValCollection{items: slice} 26 } 27 28 func (it *KeyValCollection) Length() int { 29 if it == nil { 30 return 0 31 } 32 33 return len(it.items) 34 } 35 36 func (it *KeyValCollection) IsEmpty() bool { 37 return it.Length() == 0 38 } 39 40 func (it *KeyValCollection) HasAnyItem() bool { 41 return it.Length() > 0 42 } 43 44 func (it *KeyValCollection) Add( 45 keyVal KeyVal, 46 ) *KeyValCollection { 47 it.items = append(it.items, keyVal) 48 49 return it 50 } 51 52 func (it *KeyValCollection) AddPtr( 53 keyVal *KeyVal, 54 ) *KeyValCollection { 55 if keyVal == nil { 56 return it 57 } 58 59 it.items = append(it.items, *keyVal) 60 61 return it 62 } 63 64 func (it *KeyValCollection) AddMany( 65 keyValues ...KeyVal, 66 ) *KeyValCollection { 67 if keyValues == nil || len(keyValues) == 0 { 68 return it 69 } 70 71 for _, keyVal := range keyValues { 72 it.items = append( 73 it.items, 74 keyVal) 75 } 76 77 return it 78 } 79 80 func (it *KeyValCollection) AddManyPtr( 81 keyValues ...*KeyVal, 82 ) *KeyValCollection { 83 if keyValues == nil || len(keyValues) == 0 { 84 return it 85 } 86 87 for _, keyVal := range keyValues { 88 if keyVal == nil { 89 continue 90 } 91 92 it.items = append( 93 it.items, 94 *keyVal) 95 } 96 97 return it 98 } 99 100 func (it *KeyValCollection) Items() []KeyVal { 101 return it.items 102 } 103 104 func (it *KeyValCollection) MapAnyItems() *MapAnyItems { 105 if it.IsEmpty() { 106 return EmptyMapAnyItems() 107 } 108 109 mapItems := make(map[string]interface{}, it.Length()) 110 for _, keyVal := range it.items { 111 mapItems[keyVal.KeyString()] = keyVal.Value 112 } 113 114 return &MapAnyItems{Items: mapItems} 115 } 116 117 func (it *KeyValCollection) JsonMapResults() (*corejson.MapResults, error) { 118 mapResults := corejson. 119 NewMapResults. 120 UsingCap(it.Length()) 121 122 if it.IsEmpty() { 123 return mapResults, nil 124 } 125 126 for _, keyVal := range it.items { 127 err := mapResults.AddAny( 128 keyVal.KeyString(), 129 keyVal.Value) 130 131 if err != nil { 132 return mapResults, err 133 } 134 } 135 136 return mapResults, nil 137 } 138 139 func (it *KeyValCollection) JsonResultsCollection() *corejson.ResultsCollection { 140 jsonResultsCollection := corejson.NewResultsCollection.UsingCap(it.Length()) 141 142 if it.IsEmpty() { 143 return jsonResultsCollection 144 } 145 146 for _, keyVal := range it.items { 147 jsonResultsCollection.AddAny( 148 keyVal.Value) 149 } 150 151 return jsonResultsCollection 152 } 153 154 func (it *KeyValCollection) JsonResultsPtrCollection() *corejson.ResultsPtrCollection { 155 jsonResultsCollection := corejson.NewResultsPtrCollection.UsingCap(it.Length()) 156 157 if it.IsEmpty() { 158 return jsonResultsCollection 159 } 160 161 for _, keyVal := range it.items { 162 jsonResultsCollection.AddAny( 163 keyVal.Value) 164 } 165 166 return jsonResultsCollection 167 } 168 169 func (it *KeyValCollection) GetPagesSize( 170 eachPageSize int, 171 ) int { 172 length := it.Length() 173 174 pagesPossibleFloat := float64(length) / float64(eachPageSize) 175 pagesPossibleCeiling := int(math.Ceil(pagesPossibleFloat)) 176 177 return pagesPossibleCeiling 178 } 179 180 func (it *KeyValCollection) GetPagedCollection( 181 eachPageSize int, 182 ) []*KeyValCollection { 183 length := it.Length() 184 185 if length < eachPageSize { 186 return []*KeyValCollection{ 187 it, 188 } 189 } 190 191 pagesPossibleFloat := float64(length) / float64(eachPageSize) 192 pagesPossibleCeiling := int(math.Ceil(pagesPossibleFloat)) 193 collectionOfCollection := make( 194 []*KeyValCollection, 195 pagesPossibleCeiling) 196 197 wg := sync.WaitGroup{} 198 addPagedItemsFunc := func(oneBasedPageIndex int) { 199 pagedCollection := it.GetSinglePageCollection( 200 eachPageSize, 201 oneBasedPageIndex, 202 ) 203 204 collectionOfCollection[oneBasedPageIndex-1] = pagedCollection 205 206 wg.Done() 207 } 208 209 wg.Add(pagesPossibleCeiling) 210 for i := 1; i <= pagesPossibleCeiling; i++ { 211 go addPagedItemsFunc(i) 212 } 213 214 wg.Wait() 215 216 return collectionOfCollection 217 } 218 219 func (it *KeyValCollection) GetPagingInfo( 220 eachPageSize int, 221 pageIndex int, 222 ) pagingutil.PagingInfo { 223 return pagingutil.GetPagingInfo(pagingutil.PagingRequest{ 224 Length: it.Length(), 225 PageIndex: pageIndex, 226 EachPageSize: eachPageSize, 227 }) 228 } 229 230 // GetSinglePageCollection PageIndex is one based index. Should be above or equal 1 231 func (it *KeyValCollection) GetSinglePageCollection( 232 eachPageSize int, 233 pageIndex int, 234 ) *KeyValCollection { 235 length := it.Length() 236 237 if length < eachPageSize { 238 return it 239 } 240 241 pageInfo := it.GetPagingInfo( 242 eachPageSize, 243 pageIndex) 244 245 list := it.items[pageInfo.SkipItems:pageInfo.EndingLength] 246 247 return &KeyValCollection{ 248 items: list, 249 } 250 } 251 252 func (it *KeyValCollection) AllKeys() []string { 253 if it.IsEmpty() { 254 return []string{} 255 } 256 257 keys := make([]string, it.Length()) 258 259 for i, keyVal := range it.items { 260 keys[i] = keyVal.KeyString() 261 } 262 263 return keys 264 } 265 266 func (it *KeyValCollection) AllKeysSorted() []string { 267 if it.IsEmpty() { 268 return []string{} 269 } 270 271 keys := it.AllKeys() 272 sort.Strings(keys) 273 274 return keys 275 } 276 277 func (it *KeyValCollection) AllValues() []interface{} { 278 if it.IsEmpty() { 279 return []interface{}{} 280 } 281 282 values := make([]interface{}, it.Length()) 283 284 for i, result := range it.items { 285 values[i] = result.Value 286 } 287 288 return values 289 } 290 291 func (it *KeyValCollection) String() string { 292 return fmt.Sprintf( 293 constants.SprintPropertyNameValueFormat, 294 it.items) 295 } 296 297 func (it KeyValCollection) JsonModel() interface{} { 298 return it 299 } 300 301 func (it KeyValCollection) JsonModelAny() interface{} { 302 return it.JsonModel() 303 } 304 305 func (it KeyValCollection) Json() corejson.Result { 306 return corejson.New(it) 307 } 308 309 func (it KeyValCollection) JsonPtr() *corejson.Result { 310 return corejson.NewPtr(it) 311 } 312 313 //goland:noinspection GoLinterLocal 314 func (it *KeyValCollection) ParseInjectUsingJson( 315 jsonResult *corejson.Result, 316 ) (*KeyValCollection, error) { 317 err := jsonResult.Unmarshal(it) 318 319 if err != nil { 320 return nil, err 321 } 322 323 return it, nil 324 } 325 326 // ParseInjectUsingJsonMust Panic if error 327 //goland:noinspection GoLinterLocal 328 func (it *KeyValCollection) ParseInjectUsingJsonMust( 329 jsonResult *corejson.Result, 330 ) *KeyValCollection { 331 newUsingJson, err := 332 it.ParseInjectUsingJson(jsonResult) 333 334 if err != nil { 335 panic(err) 336 } 337 338 return newUsingJson 339 } 340 341 func (it *KeyValCollection) JsonParseSelfInject( 342 jsonResult *corejson.Result, 343 ) error { 344 _, err := it.ParseInjectUsingJson( 345 jsonResult, 346 ) 347 348 return err 349 } 350 351 func (it *KeyValCollection) Serialize() (jsonBytesPtr []byte, err error) { 352 jsonResult := it.Json() 353 354 if jsonResult.HasError() { 355 return []byte{}, jsonResult.MeaningfulError() 356 } 357 358 return jsonResult.SafeBytes(), nil 359 } 360 361 func (it *KeyValCollection) JsonString() (jsonString string, err error) { 362 jsonResult := it.Json() 363 364 if jsonResult.HasError() { 365 return constants.EmptyString, jsonResult.MeaningfulError() 366 } 367 368 return jsonResult.JsonString(), nil 369 } 370 371 func (it *KeyValCollection) JsonStringMust() string { 372 jsonResult := it.Json() 373 jsonResult.HandleError() 374 375 return jsonResult.JsonString() 376 } 377 378 func (it KeyValCollection) Clone() KeyValCollection { 379 keyValCollection := NewKeyValCollection(it.Length()) 380 keyValCollection.AddMany(it.items...) 381 382 return keyValCollection.NonPtr() 383 } 384 385 func (it *KeyValCollection) ClonePtr() *KeyValCollection { 386 if it == nil { 387 return nil 388 } 389 390 cloned := it.Clone() 391 392 return cloned.Ptr() 393 } 394 395 func (it KeyValCollection) NonPtr() KeyValCollection { 396 return it 397 } 398 399 func (it *KeyValCollection) Ptr() *KeyValCollection { 400 return it 401 }