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

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