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