gitlab.com/evatix-go/core@v1.3.55/coredata/corejson/ResultCollection.go (about)

     1  package corejson
     2  
     3  import (
     4  	"errors"
     5  	"math"
     6  	"strings"
     7  	"sync"
     8  
     9  	"gitlab.com/evatix-go/core/constants"
    10  	"gitlab.com/evatix-go/core/defaultcapacity"
    11  	"gitlab.com/evatix-go/core/errcore"
    12  )
    13  
    14  type ResultsCollection struct {
    15  	Items []Result `json:"JsonResultsCollection"`
    16  }
    17  
    18  func (it *ResultsCollection) Length() int {
    19  	if it == nil || it.Items == nil {
    20  		return 0
    21  	}
    22  
    23  	return len(it.Items)
    24  }
    25  
    26  func (it *ResultsCollection) LastIndex() int {
    27  	return it.Length() - 1
    28  }
    29  
    30  func (it *ResultsCollection) IsEmpty() bool {
    31  	return it.Length() == 0
    32  }
    33  
    34  func (it *ResultsCollection) HasAnyItem() bool {
    35  	return it.Length() > 0
    36  }
    37  
    38  func (it *ResultsCollection) FirstOrDefault() *Result {
    39  	if it.IsEmpty() {
    40  		return nil
    41  	}
    42  
    43  	return &it.Items[0]
    44  }
    45  
    46  func (it *ResultsCollection) LastOrDefault() *Result {
    47  	if it.IsEmpty() {
    48  		return nil
    49  	}
    50  
    51  	return &it.Items[it.LastIndex()]
    52  }
    53  
    54  func (it *ResultsCollection) Take(limit int) *ResultsCollection {
    55  	if it.IsEmpty() {
    56  		return Empty.ResultsCollection()
    57  	}
    58  
    59  	return &ResultsCollection{
    60  		Items: it.Items[:limit],
    61  	}
    62  }
    63  
    64  func (it *ResultsCollection) Limit(limit int) *ResultsCollection {
    65  	if it.IsEmpty() {
    66  		return Empty.ResultsCollection()
    67  	}
    68  
    69  	if limit <= constants.TakeAllMinusOne {
    70  		return it
    71  	}
    72  
    73  	limit = defaultcapacity.
    74  		MaxLimit(it.Length(), limit)
    75  
    76  	return &ResultsCollection{
    77  		Items: it.Items[:limit],
    78  	}
    79  }
    80  
    81  func (it *ResultsCollection) Skip(skip int) *ResultsCollection {
    82  	if it.IsEmpty() {
    83  		return Empty.ResultsCollection()
    84  	}
    85  
    86  	return &ResultsCollection{
    87  		Items: it.Items[skip:],
    88  	}
    89  }
    90  
    91  // AddSkipOnNil skip on nil
    92  func (it *ResultsCollection) AddSkipOnNil(
    93  	result *Result,
    94  ) *ResultsCollection {
    95  	if result == nil {
    96  		return it
    97  	}
    98  
    99  	it.Items = append(
   100  		it.Items,
   101  		*result)
   102  
   103  	return it
   104  }
   105  
   106  func (it *ResultsCollection) AddNonNilNonError(
   107  	result *Result,
   108  ) *ResultsCollection {
   109  	if result == nil || result.HasError() {
   110  		return it
   111  	}
   112  
   113  	it.Items = append(
   114  		it.Items,
   115  		*result)
   116  
   117  	return it
   118  }
   119  
   120  func (it *ResultsCollection) GetAt(
   121  	index int,
   122  ) *Result {
   123  	return &it.Items[index]
   124  }
   125  
   126  // HasError has any error
   127  func (it *ResultsCollection) HasError() bool {
   128  	for _, result := range it.Items {
   129  		if result.HasError() {
   130  			return true
   131  		}
   132  	}
   133  
   134  	return false
   135  }
   136  
   137  func (it *ResultsCollection) AllErrors() (
   138  	errListPtr []error,
   139  	hasAnyError bool,
   140  ) {
   141  	length := it.Length()
   142  	errList := make(
   143  		[]error,
   144  		0,
   145  		length)
   146  
   147  	if length == 0 {
   148  		return errList, hasAnyError
   149  	}
   150  
   151  	for i := 0; i < length; i++ {
   152  		err := it.Items[i].Error
   153  
   154  		if err != nil {
   155  			hasAnyError = true
   156  			errList = append(
   157  				errList,
   158  				err)
   159  		}
   160  	}
   161  
   162  	return errList, hasAnyError
   163  }
   164  
   165  func (it *ResultsCollection) GetErrorsStrings() []string {
   166  	length := it.Length()
   167  	errStrList := make(
   168  		[]string,
   169  		0,
   170  		length)
   171  
   172  	if length == 0 {
   173  		return errStrList
   174  	}
   175  
   176  	for _, result := range it.Items {
   177  		if result.IsEmptyError() {
   178  			continue
   179  		}
   180  
   181  		errStrList = append(
   182  			errStrList,
   183  			result.Error.Error())
   184  	}
   185  
   186  	return errStrList
   187  }
   188  
   189  func (it *ResultsCollection) GetErrorsStringsPtr() *[]string {
   190  	errStrList := it.GetErrorsStrings()
   191  
   192  	return &errStrList
   193  }
   194  
   195  func (it *ResultsCollection) GetErrorsAsSingleString() string {
   196  	errStrList := it.GetErrorsStrings()
   197  
   198  	return strings.Join(
   199  		errStrList,
   200  		constants.NewLineUnix)
   201  }
   202  
   203  func (it *ResultsCollection) GetErrorsAsSingle() error {
   204  	errorString := it.GetErrorsAsSingleString()
   205  
   206  	return errors.New(errorString)
   207  }
   208  
   209  func (it *ResultsCollection) UnmarshalAt(
   210  	index int,
   211  	any interface{},
   212  ) error {
   213  	result := it.Items[index]
   214  
   215  	return result.Unmarshal(
   216  		any)
   217  }
   218  
   219  func (it *ResultsCollection) InjectIntoAt(
   220  	index int,
   221  	injector JsonParseSelfInjector,
   222  ) error {
   223  	return injector.JsonParseSelfInject(
   224  		&it.Items[index])
   225  }
   226  
   227  // InjectIntoSameIndex any nil skip
   228  func (it *ResultsCollection) InjectIntoSameIndex(
   229  	injectors ...JsonParseSelfInjector,
   230  ) (
   231  	errListPtr []error,
   232  	hasAnyError bool,
   233  ) {
   234  	if injectors == nil {
   235  		return []error{}, false
   236  	}
   237  
   238  	length := len(injectors)
   239  	errList := make([]error, length)
   240  
   241  	for i := 0; i < length; i++ {
   242  		result := it.Items[i]
   243  		injector := injectors[i]
   244  
   245  		if result.HasError() {
   246  			hasAnyError = true
   247  
   248  			continue
   249  		}
   250  
   251  		if injector == nil {
   252  			continue
   253  		}
   254  
   255  		err := injector.
   256  			JsonParseSelfInject(
   257  				&result)
   258  
   259  		if err != nil {
   260  			hasAnyError = true
   261  		}
   262  
   263  		errList[i] = err
   264  	}
   265  
   266  	return errList, hasAnyError
   267  }
   268  
   269  // UnmarshalIntoSameIndex any nil skip
   270  func (it *ResultsCollection) UnmarshalIntoSameIndex(
   271  	anys ...interface{},
   272  ) (
   273  	errListPtr []error,
   274  	hasAnyError bool,
   275  ) {
   276  	if anys == nil {
   277  		return []error{}, false
   278  	}
   279  
   280  	length := len(anys)
   281  	errList := make([]error, length)
   282  
   283  	for i := 0; i < length; i++ {
   284  		result := it.Items[i]
   285  		any := anys[i]
   286  
   287  		if any == nil {
   288  			continue
   289  		}
   290  
   291  		if result.HasError() {
   292  			hasAnyError = true
   293  			errList[i] = result.Error
   294  
   295  			continue
   296  		}
   297  
   298  		if result.IsEmptyJsonBytes() {
   299  			continue
   300  		}
   301  
   302  		err := result.Unmarshal(
   303  			any)
   304  
   305  		if err != nil {
   306  			hasAnyError = true
   307  		}
   308  
   309  		errList[i] = err
   310  	}
   311  
   312  	return errList, hasAnyError
   313  }
   314  
   315  func (it *ResultsCollection) GetAtSafe(
   316  	index int,
   317  ) *Result {
   318  	if index > constants.InvalidNotFoundCase && index <= it.Length()-1 {
   319  		return &it.Items[index]
   320  	}
   321  
   322  	return nil
   323  }
   324  
   325  func (it *ResultsCollection) GetAtSafeUsingLength(
   326  	index, length int,
   327  ) *Result {
   328  	if index > constants.InvalidNotFoundCase && index <= length-1 {
   329  		return &it.Items[index]
   330  	}
   331  
   332  	return nil
   333  }
   334  
   335  func (it *ResultsCollection) AddPtr(
   336  	result *Result,
   337  ) *ResultsCollection {
   338  	if result == nil {
   339  		return it
   340  	}
   341  
   342  	it.Items = append(
   343  		it.Items,
   344  		*result)
   345  
   346  	return it
   347  }
   348  
   349  func (it *ResultsCollection) Add(
   350  	result Result,
   351  ) *ResultsCollection {
   352  	it.Items = append(
   353  		it.Items,
   354  		result)
   355  
   356  	return it
   357  }
   358  
   359  func (it *ResultsCollection) Adds(
   360  	results ...Result,
   361  ) *ResultsCollection {
   362  	if results == nil {
   363  		return it
   364  	}
   365  
   366  	for _, result := range results {
   367  		it.Items = append(
   368  			it.Items,
   369  			result)
   370  	}
   371  
   372  	return it
   373  }
   374  
   375  func (it *ResultsCollection) AddSerializer(
   376  	serializer bytesSerializer,
   377  ) *ResultsCollection {
   378  	if serializer == nil {
   379  		return it
   380  	}
   381  
   382  	result := NewResult.UsingSerializer(
   383  		serializer)
   384  
   385  	return it.AddSkipOnNil(result)
   386  }
   387  
   388  func (it *ResultsCollection) AddSerializers(
   389  	serializers ...bytesSerializer,
   390  ) *ResultsCollection {
   391  	if len(serializers) == 0 {
   392  		return it
   393  	}
   394  
   395  	for _, serializer := range serializers {
   396  		it.AddSerializer(serializer)
   397  	}
   398  
   399  	return it
   400  }
   401  
   402  func (it *ResultsCollection) AddSerializerFunc(
   403  	serializerFunc func() ([]byte, error),
   404  ) *ResultsCollection {
   405  	if serializerFunc == nil {
   406  		return it
   407  	}
   408  
   409  	result := NewResult.UsingSerializerFunc(
   410  		serializerFunc)
   411  
   412  	return it.AddSkipOnNil(result)
   413  }
   414  
   415  func (it *ResultsCollection) AddSerializerFunctions(
   416  	serializerFunctions ...func() ([]byte, error),
   417  ) *ResultsCollection {
   418  	if len(serializerFunctions) == 0 {
   419  		return it
   420  	}
   421  
   422  	for _, serializer := range serializerFunctions {
   423  		it.AddSerializerFunc(serializer)
   424  	}
   425  
   426  	return it
   427  }
   428  
   429  func (it *ResultsCollection) AddMapResults(
   430  	mapResults *MapResults,
   431  ) *ResultsCollection {
   432  	if mapResults.IsEmpty() {
   433  		return it
   434  	}
   435  
   436  	return it.AddRawMapResults(mapResults.Items)
   437  }
   438  
   439  func (it *ResultsCollection) AddRawMapResults(
   440  	mapResults map[string]Result,
   441  ) *ResultsCollection {
   442  	if len(mapResults) == 0 {
   443  		return it
   444  	}
   445  
   446  	for _, result := range mapResults {
   447  		it.Items = append(
   448  			it.Items,
   449  			result)
   450  	}
   451  
   452  	return it
   453  }
   454  
   455  func (it *ResultsCollection) AddsPtr(
   456  	results ...*Result,
   457  ) *ResultsCollection {
   458  	if results == nil {
   459  		return it
   460  	}
   461  
   462  	for _, result := range results {
   463  		if result == nil {
   464  			continue
   465  		}
   466  
   467  		it.Items = append(
   468  			it.Items,
   469  			*result)
   470  	}
   471  
   472  	return it
   473  }
   474  
   475  func (it *ResultsCollection) AddAny(
   476  	any interface{},
   477  ) *ResultsCollection {
   478  	if any == nil {
   479  		return it
   480  	}
   481  
   482  	it.Items = append(
   483  		it.Items,
   484  		New(any))
   485  
   486  	return it
   487  }
   488  
   489  // AddAnyItems Skip on nil
   490  func (it *ResultsCollection) AddAnyItems(
   491  	anyItems ...interface{},
   492  ) *ResultsCollection {
   493  	if anyItems == nil {
   494  		return it
   495  	}
   496  
   497  	for _, any := range anyItems {
   498  		if any == nil {
   499  			continue
   500  		}
   501  
   502  		it.Items = append(
   503  			it.Items,
   504  			New(any))
   505  	}
   506  
   507  	return it
   508  }
   509  
   510  // AddAnyItemsSlice
   511  //
   512  //  Skip on nil
   513  func (it *ResultsCollection) AddAnyItemsSlice(
   514  	anyItems []interface{},
   515  ) *ResultsCollection {
   516  	if anyItems == nil {
   517  		return it
   518  	}
   519  
   520  	for _, any := range anyItems {
   521  		if any == nil {
   522  			continue
   523  		}
   524  
   525  		it.Items = append(
   526  			it.Items,
   527  			New(any))
   528  	}
   529  
   530  	return it
   531  }
   532  
   533  // AddResultsCollection
   534  //
   535  //  skip on nil items
   536  func (it *ResultsCollection) AddResultsCollection(
   537  	collection *ResultsCollection,
   538  ) *ResultsCollection {
   539  	if collection == nil {
   540  		return it
   541  	}
   542  
   543  	return it.Adds(collection.Items...)
   544  }
   545  
   546  // AddNonNilItemsPtr skip on nil
   547  func (it *ResultsCollection) AddNonNilItemsPtr(
   548  	results ...*Result,
   549  ) *ResultsCollection {
   550  	if results == nil || len(results) == 0 {
   551  		return it
   552  	}
   553  
   554  	for _, result := range results {
   555  		if result == nil {
   556  			continue
   557  		}
   558  
   559  		it.Items = append(
   560  			it.Items,
   561  			*result)
   562  	}
   563  
   564  	return it
   565  }
   566  
   567  func (it ResultsCollection) NonPtr() ResultsCollection {
   568  	return it
   569  }
   570  
   571  func (it *ResultsCollection) Ptr() *ResultsCollection {
   572  	return it
   573  }
   574  
   575  func (it *ResultsCollection) Clear() *ResultsCollection {
   576  	if it == nil {
   577  		return it
   578  	}
   579  
   580  	temp := it.Items
   581  	clearFunc := func() {
   582  		for _, result := range temp {
   583  			result.Dispose()
   584  		}
   585  	}
   586  
   587  	go clearFunc()
   588  	it.Items = []Result{}
   589  
   590  	return it
   591  }
   592  
   593  func (it *ResultsCollection) Dispose() {
   594  	if it == nil {
   595  		return
   596  	}
   597  
   598  	it.Clear()
   599  	it.Items = nil
   600  }
   601  
   602  func (it *ResultsCollection) GetStrings() []string {
   603  	length := it.Length()
   604  	list := make([]string, length)
   605  
   606  	if length == 0 {
   607  		return list
   608  	}
   609  
   610  	for i, result := range it.Items {
   611  		list[i] = *result.JsonStringPtr()
   612  	}
   613  
   614  	return list
   615  }
   616  
   617  func (it *ResultsCollection) GetStringsPtr() *[]string {
   618  	list := it.GetStrings()
   619  
   620  	return &list
   621  }
   622  
   623  // AddJsoners skip on nil
   624  func (it *ResultsCollection) AddJsoners(
   625  	isIgnoreNilOrError bool,
   626  	jsoners ...Jsoner,
   627  ) *ResultsCollection {
   628  	if jsoners == nil {
   629  		return it
   630  	}
   631  
   632  	for _, jsoner := range jsoners {
   633  		if jsoner == nil {
   634  			continue
   635  		}
   636  
   637  		result := jsoner.Json()
   638  
   639  		if isIgnoreNilOrError && result.HasError() {
   640  			continue
   641  		}
   642  
   643  		it.Items = append(
   644  			it.Items,
   645  			result)
   646  	}
   647  
   648  	return it
   649  }
   650  
   651  func (it *ResultsCollection) GetPagesSize(
   652  	eachPageSize int,
   653  ) int {
   654  	length := it.Length()
   655  
   656  	pagesPossibleFloat := float64(length) / float64(eachPageSize)
   657  	pagesPossibleCeiling := int(math.Ceil(pagesPossibleFloat))
   658  
   659  	return pagesPossibleCeiling
   660  }
   661  
   662  func (it *ResultsCollection) GetPagedCollection(
   663  	eachPageSize int,
   664  ) []*ResultsCollection {
   665  	length := it.Length()
   666  
   667  	if length < eachPageSize {
   668  		return []*ResultsCollection{
   669  			it,
   670  		}
   671  	}
   672  
   673  	pagesPossibleFloat := float64(length) / float64(eachPageSize)
   674  	pagesPossibleCeiling := int(math.Ceil(pagesPossibleFloat))
   675  	collectionOfCollection := make([]*ResultsCollection, pagesPossibleCeiling)
   676  
   677  	wg := sync.WaitGroup{}
   678  	addPagedItemsFunc := func(oneBasedPageIndex int) {
   679  		pagedCollection := it.GetSinglePageCollection(
   680  			eachPageSize,
   681  			oneBasedPageIndex,
   682  		)
   683  
   684  		collectionOfCollection[oneBasedPageIndex-1] = pagedCollection
   685  
   686  		wg.Done()
   687  	}
   688  
   689  	wg.Add(pagesPossibleCeiling)
   690  	for i := 1; i <= pagesPossibleCeiling; i++ {
   691  		go addPagedItemsFunc(i)
   692  	}
   693  
   694  	wg.Wait()
   695  
   696  	return collectionOfCollection
   697  }
   698  
   699  // GetSinglePageCollection PageIndex is one based index. Should be above or equal 1
   700  func (it *ResultsCollection) GetSinglePageCollection(
   701  	eachPageSize int,
   702  	pageIndex int,
   703  ) *ResultsCollection {
   704  	length := it.Length()
   705  
   706  	if length < eachPageSize {
   707  		return it
   708  	}
   709  
   710  	/**
   711  	 * eachPageItems = 10
   712  	 * pageIndex = 4
   713  	 * skipItems = 10 * (4 - 1) = 30
   714  	 */
   715  	skipItems := eachPageSize * (pageIndex - 1)
   716  	if skipItems < 0 {
   717  		errcore.
   718  			CannotBeNegativeIndexType.
   719  			HandleUsingPanic(
   720  				"pageIndex cannot be negative or zero.",
   721  				pageIndex)
   722  	}
   723  
   724  	endingIndex := skipItems + eachPageSize
   725  
   726  	if endingIndex > length {
   727  		endingIndex = length
   728  	}
   729  
   730  	list := it.Items[skipItems:endingIndex]
   731  
   732  	return NewResultsCollection.UsingResults(
   733  		list...)
   734  }
   735  
   736  //goland:noinspection GoLinterLocal
   737  func (it *ResultsCollection) JsonModel() *ResultsCollection {
   738  	return it
   739  }
   740  
   741  //goland:noinspection GoLinterLocal
   742  func (it *ResultsCollection) JsonModelAny() interface{} {
   743  	return it.JsonModel()
   744  }
   745  
   746  func (it ResultsCollection) Json() Result {
   747  	return New(it)
   748  }
   749  
   750  func (it ResultsCollection) JsonPtr() *Result {
   751  	return NewPtr(it)
   752  }
   753  
   754  // ParseInjectUsingJson It will not update the self but creates a new one.
   755  func (it *ResultsCollection) ParseInjectUsingJson(
   756  	jsonResult *Result,
   757  ) (*ResultsCollection, error) {
   758  	err := jsonResult.Unmarshal(
   759  		&it,
   760  	)
   761  
   762  	if err != nil {
   763  		return Empty.ResultsCollection(), err
   764  	}
   765  
   766  	return it, nil
   767  }
   768  
   769  // ParseInjectUsingJsonMust Panic if error
   770  func (it *ResultsCollection) ParseInjectUsingJsonMust(
   771  	jsonResult *Result,
   772  ) *ResultsCollection {
   773  	resultCollection, err := it.
   774  		ParseInjectUsingJson(jsonResult)
   775  
   776  	if err != nil {
   777  		panic(err)
   778  	}
   779  
   780  	return resultCollection
   781  }
   782  
   783  func (it *ResultsCollection) AsJsonContractsBinder() JsonContractsBinder {
   784  	return it
   785  }
   786  
   787  func (it *ResultsCollection) AsJsoner() Jsoner {
   788  	return it
   789  }
   790  
   791  func (it *ResultsCollection) JsonParseSelfInject(
   792  	jsonResult *Result,
   793  ) error {
   794  	_, err := it.ParseInjectUsingJson(
   795  		jsonResult,
   796  	)
   797  
   798  	return err
   799  }
   800  
   801  func (it *ResultsCollection) AsJsonParseSelfInjector() JsonParseSelfInjector {
   802  	return it
   803  }
   804  
   805  func (it ResultsCollection) ShadowClone() ResultsCollection {
   806  	return it.Clone(false)
   807  }
   808  
   809  func (it ResultsCollection) Clone(isDeepCloneEach bool) ResultsCollection {
   810  	newResults := NewResultsCollection.
   811  		UsingCap(it.Length())
   812  
   813  	if newResults.Length() == 0 {
   814  		return *newResults
   815  	}
   816  
   817  	for _, item := range it.Items {
   818  		newResults.Add(*item.ClonePtr(isDeepCloneEach))
   819  	}
   820  
   821  	return *newResults
   822  }
   823  
   824  func (it *ResultsCollection) ClonePtr(isDeepCloneEach bool) *ResultsCollection {
   825  	if it == nil {
   826  		return nil
   827  	}
   828  
   829  	newResults := NewResultsCollection.UsingCap(
   830  		it.Length())
   831  
   832  	if newResults.Length() == 0 {
   833  		return newResults
   834  	}
   835  
   836  	for _, item := range it.Items {
   837  		newResults.Add(*item.ClonePtr(isDeepCloneEach))
   838  	}
   839  
   840  	return newResults
   841  }