gitlab.com/evatix-go/core@v1.3.55/coredata/coredynamic/MapAnyItems.go (about)

     1  package coredynamic
     2  
     3  import (
     4  	"encoding/json"
     5  	"math"
     6  	"reflect"
     7  	"sort"
     8  	"sync"
     9  
    10  	"gitlab.com/evatix-go/core/constants"
    11  	"gitlab.com/evatix-go/core/coredata/corejson"
    12  	"gitlab.com/evatix-go/core/defaulterr"
    13  	"gitlab.com/evatix-go/core/errcore"
    14  	"gitlab.com/evatix-go/core/internal/mapdiffinternal"
    15  	"gitlab.com/evatix-go/core/internal/reflectinternal"
    16  )
    17  
    18  type MapAnyItems struct {
    19  	Items map[string]interface{}
    20  }
    21  
    22  func EmptyMapAnyItems() *MapAnyItems {
    23  	return NewMapAnyItems(constants.Zero)
    24  }
    25  
    26  func NewMapAnyItems(capacity int) *MapAnyItems {
    27  	slice := make(map[string]interface{}, capacity)
    28  
    29  	return &MapAnyItems{Items: slice}
    30  }
    31  
    32  func NewMapAnyItemsUsingAnyTypeMap(
    33  	anyTypeOfMap interface{},
    34  ) (*MapAnyItems, error) {
    35  	if reflectinternal.IsNull(anyTypeOfMap) {
    36  		return EmptyMapAnyItems(), errcore.
    37  			CannotBeNilOrEmptyType.
    38  			ErrorNoRefs("given any map was nil, cannot process it.")
    39  	}
    40  
    41  	rv := reflect.ValueOf(anyTypeOfMap)
    42  	convertedMap, err := AnyTypeMapToMapStringAny(rv)
    43  
    44  	if err != nil {
    45  		return EmptyMapAnyItems(), err
    46  	}
    47  
    48  	return &MapAnyItems{Items: convertedMap}, nil
    49  }
    50  
    51  func NewMapAnyItemsUsingItems(
    52  	itemsMap map[string]interface{},
    53  ) *MapAnyItems {
    54  	if len(itemsMap) == 0 {
    55  		return EmptyMapAnyItems()
    56  	}
    57  
    58  	return &MapAnyItems{Items: itemsMap}
    59  }
    60  
    61  func (it *MapAnyItems) Length() int {
    62  	if it == nil {
    63  		return 0
    64  	}
    65  
    66  	return len(it.Items)
    67  }
    68  
    69  func (it *MapAnyItems) IsEmpty() bool {
    70  	return it.Length() == 0
    71  }
    72  
    73  func (it *MapAnyItems) HasAnyItem() bool {
    74  	return it.Length() > 0
    75  }
    76  
    77  func (it *MapAnyItems) HasKey(key string) bool {
    78  	_, has := it.Items[key]
    79  
    80  	return has
    81  }
    82  
    83  func (it *MapAnyItems) ReflectSetTo(
    84  	key string,
    85  	toPointerOrBytes interface{},
    86  ) error {
    87  	valInf, has := it.Items[key]
    88  
    89  	if !has {
    90  		return errcore.ErrorWithRefToError(
    91  			defaulterr.KeyNotExistInMap,
    92  			it.AllKeysSorted())
    93  	}
    94  
    95  	return ReflectSetFromTo(
    96  		valInf,
    97  		toPointerOrBytes)
    98  }
    99  
   100  func (it *MapAnyItems) ReflectSetToMust(
   101  	key string,
   102  	toPointerOrBytes interface{},
   103  ) {
   104  	err := it.ReflectSetTo(key, toPointerOrBytes)
   105  	errcore.HandleErr(err)
   106  }
   107  
   108  func (it *MapAnyItems) GetValue(
   109  	key string,
   110  ) (any interface{}) {
   111  	valInf, has := it.Items[key]
   112  
   113  	if has {
   114  		return valInf
   115  	}
   116  
   117  	return nil
   118  }
   119  
   120  func (it *MapAnyItems) GetFieldsMap(
   121  	key string,
   122  ) (
   123  	fieldMap map[string]interface{},
   124  	parsingErr error,
   125  	isFound bool,
   126  ) {
   127  	valInf, has := it.Items[key]
   128  
   129  	if has {
   130  		fieldsMap, parsingErr := corejson.
   131  			Deserialize.
   132  			AnyToFieldsMap(valInf)
   133  
   134  		return fieldsMap, parsingErr, true
   135  	}
   136  
   137  	return nil, nil, false
   138  }
   139  
   140  // GetSafeFieldsMap
   141  //
   142  // Warning:
   143  //  Swallows the parsing err if any
   144  func (it *MapAnyItems) GetSafeFieldsMap(
   145  	key string,
   146  ) (
   147  	fieldMap map[string]interface{},
   148  	isFound bool,
   149  ) {
   150  	fieldMap, _, isFound = it.GetFieldsMap(key)
   151  
   152  	return fieldMap, isFound
   153  }
   154  
   155  func (it *MapAnyItems) Get(
   156  	key string,
   157  ) (any interface{}, has bool) {
   158  	valInf, has := it.Items[key]
   159  
   160  	if has {
   161  		return valInf, has
   162  	}
   163  
   164  	return nil, false
   165  }
   166  
   167  func (it *MapAnyItems) Deserialize(
   168  	key string,
   169  	toPointer interface{},
   170  ) error {
   171  	return it.GetUsingUnmarshallAt(
   172  		key,
   173  		toPointer)
   174  }
   175  
   176  func (it *MapAnyItems) DeserializeMust(
   177  	key string,
   178  	toPointer interface{},
   179  ) {
   180  	err := it.GetUsingUnmarshallAt(
   181  		key,
   182  		toPointer)
   183  	errcore.HandleErr(err)
   184  }
   185  
   186  func (it *MapAnyItems) GetUsingUnmarshallAt(
   187  	key string,
   188  	unmarshalRef interface{},
   189  ) error {
   190  	valInf, has := it.Items[key]
   191  
   192  	if !has {
   193  		return errcore.
   194  			KeyNotExistInMapType.
   195  			ErrorRefOnly(key)
   196  	}
   197  
   198  	rawBytes, err := json.Marshal(valInf)
   199  
   200  	if err != nil {
   201  		ref := errcore.VarTwoNoType(
   202  			"key", key,
   203  			"type", TypeName(valInf))
   204  
   205  		return errcore.MarshallingFailedType.ErrorRefOnly(ref)
   206  	}
   207  
   208  	unmarshalErr := json.Unmarshal(rawBytes, unmarshalRef)
   209  
   210  	if unmarshalErr != nil {
   211  		ref := errcore.VarThreeNoType(
   212  			"key", key,
   213  			"StoreType", TypeName(valInf),
   214  			"RequestedType", TypeName(unmarshalRef))
   215  
   216  		return errcore.UnMarshallingFailedType.ErrorRefOnly(ref)
   217  	}
   218  
   219  	return nil
   220  }
   221  
   222  func (it *MapAnyItems) GetUsingUnmarshallManyAt(
   223  	keyAnyItems ...corejson.KeyAny,
   224  ) error {
   225  	for _, keyAny := range keyAnyItems {
   226  		err := it.GetUsingUnmarshallAt(
   227  			keyAny.Key,
   228  			keyAny.AnyInf)
   229  
   230  		if err != nil {
   231  			return err
   232  		}
   233  	}
   234  
   235  	return nil
   236  }
   237  
   238  func (it *MapAnyItems) GetManyItemsRefs(
   239  	keyAnyItems ...corejson.KeyAny,
   240  ) error {
   241  	if len(keyAnyItems) == 0 {
   242  		return nil
   243  	}
   244  
   245  	for _, keyAny := range keyAnyItems {
   246  		err := it.GetItemRef(
   247  			keyAny.Key,
   248  			keyAny.AnyInf)
   249  
   250  		if err != nil {
   251  			return err
   252  		}
   253  	}
   254  
   255  	return nil
   256  }
   257  
   258  func (it *MapAnyItems) GetItemRef(
   259  	key string,
   260  	referenceOut interface{},
   261  ) error {
   262  	valInf, has := it.Items[key]
   263  
   264  	if !has {
   265  		return errcore.
   266  			KeyNotExistInMapType.
   267  			Error("key", key)
   268  	}
   269  
   270  	if referenceOut == nil {
   271  		reference := errcore.VarTwoNoType(
   272  			"key", key,
   273  			"referenceOutType", TypeName(referenceOut))
   274  
   275  		return errcore.
   276  			CannotBeNilType.
   277  			Error(
   278  				"referenceOut cannot be nil",
   279  				reference)
   280  	}
   281  
   282  	outInfRv := reflect.ValueOf(referenceOut)
   283  	foundItemRv := reflect.ValueOf(valInf)
   284  
   285  	if outInfRv.Kind() != reflect.Ptr {
   286  		reference := errcore.VarTwoNoType(
   287  			"key", key,
   288  			"referenceOutType", TypeName(referenceOut))
   289  
   290  		return errcore.
   291  			ShouldBePointerType.
   292  			Error(
   293  				"referenceOut is not a pointer!",
   294  				reference)
   295  	}
   296  
   297  	if outInfRv.IsNil() || foundItemRv.IsNil() {
   298  		reference := errcore.VarThreeNoType(
   299  			"key", key,
   300  			"referenceOutType", TypeName(referenceOut),
   301  			"foundItemType", TypeName(valInf))
   302  
   303  		return errcore.
   304  			CannotBeNilType.
   305  			Error(
   306  				"referenceOut or found item is nil",
   307  				reference)
   308  	}
   309  
   310  	foundTypeName := foundItemRv.Type().String()
   311  	refOutTypeName := outInfRv.Type().String()
   312  	isTypeNotEqual := foundTypeName != refOutTypeName
   313  	if isTypeNotEqual {
   314  		reference := errcore.VarThreeNoType(
   315  			"key", key,
   316  			"referenceOutType", refOutTypeName,
   317  			"foundItemType", foundTypeName)
   318  
   319  		return errcore.
   320  			TypeMismatchType.
   321  			Error(
   322  				"Use UnmarshalAt method to get generic data to specific type.",
   323  				reference)
   324  	}
   325  
   326  	if foundItemRv.Kind() != reflect.Ptr {
   327  		outInfRv.Elem().Set(foundItemRv)
   328  
   329  		return nil
   330  	}
   331  
   332  	if foundItemRv.Kind() == reflect.Ptr {
   333  		outInfRv.Elem().Set(foundItemRv.Elem())
   334  
   335  		return nil
   336  	}
   337  
   338  	reference := errcore.VarThreeNoType(
   339  		"key", key,
   340  		"referenceOutType", TypeName(referenceOut),
   341  		"foundItemType", TypeName(valInf))
   342  
   343  	return errcore.
   344  		UnexpectedValueType.
   345  		Error(
   346  			"unknown error",
   347  			reference)
   348  }
   349  
   350  func (it *MapAnyItems) Add(
   351  	key string,
   352  	valInf interface{},
   353  ) (isNewlyAdded bool) {
   354  	_, isAlreadyExist := it.Items[key]
   355  
   356  	it.Items[key] = valInf
   357  
   358  	return !isAlreadyExist
   359  }
   360  
   361  func (it *MapAnyItems) Set(
   362  	key string,
   363  	valInf interface{},
   364  ) (isNewlyAdded bool) {
   365  	_, isAlreadyExist := it.Items[key]
   366  
   367  	it.Items[key] = valInf
   368  
   369  	return !isAlreadyExist
   370  }
   371  
   372  func (it *MapAnyItems) AddKeyAny(
   373  	keyAny corejson.KeyAny,
   374  ) (isNewlyAdded bool) {
   375  	return it.Add(
   376  		keyAny.Key,
   377  		keyAny.AnyInf)
   378  }
   379  
   380  func (it *MapAnyItems) AddKeyAnyWithValidation(
   381  	typeVerify reflect.Type,
   382  	keyAny corejson.KeyAny,
   383  ) error {
   384  	actualTypeOf := reflect.TypeOf(keyAny.AnyInf)
   385  	if actualTypeOf != typeVerify {
   386  		return errcore.
   387  			TypeMismatchType.
   388  			Expecting(
   389  				typeVerify.String(),
   390  				actualTypeOf.String())
   391  	}
   392  
   393  	it.AddKeyAny(keyAny)
   394  
   395  	return nil
   396  }
   397  
   398  func (it *MapAnyItems) AddWithValidation(
   399  	typeVerify reflect.Type,
   400  	key string,
   401  	anyInf interface{},
   402  ) error {
   403  	actualTypeOf := reflect.TypeOf(anyInf)
   404  	if actualTypeOf != typeVerify {
   405  		return errcore.
   406  			TypeMismatchType.
   407  			Expecting(
   408  				typeVerify.String(),
   409  				actualTypeOf.String())
   410  	}
   411  
   412  	it.Add(key, anyInf)
   413  
   414  	return nil
   415  }
   416  
   417  func (it *MapAnyItems) AddJsonResultPtr(
   418  	key string,
   419  	jsonResult *corejson.Result,
   420  ) *MapAnyItems {
   421  	if jsonResult == nil {
   422  		return it
   423  	}
   424  
   425  	it.Items[key] = jsonResult
   426  
   427  	return it
   428  }
   429  
   430  func (it *MapAnyItems) GetPagesSize(
   431  	eachPageSize int,
   432  ) int {
   433  	length := it.Length()
   434  
   435  	pagesPossibleFloat := float64(length) / float64(eachPageSize)
   436  	pagesPossibleCeiling := int(math.Ceil(pagesPossibleFloat))
   437  
   438  	return pagesPossibleCeiling
   439  }
   440  
   441  func (it *MapAnyItems) GetPagedCollection(
   442  	eachPageSize int,
   443  ) []*MapAnyItems {
   444  	length := it.Length()
   445  
   446  	if length < eachPageSize {
   447  		return []*MapAnyItems{
   448  			it,
   449  		}
   450  	}
   451  
   452  	allKeys := it.AllKeysSorted()
   453  	pagesPossibleFloat := float64(length) / float64(eachPageSize)
   454  	pagesPossibleCeiling := int(math.Ceil(pagesPossibleFloat))
   455  	collectionOfCollection := make([]*MapAnyItems, pagesPossibleCeiling)
   456  
   457  	wg := sync.WaitGroup{}
   458  	addPagedItemsFunc := func(oneBasedPageIndex int) {
   459  		pagedCollection := it.GetSinglePageCollection(
   460  			eachPageSize,
   461  			oneBasedPageIndex,
   462  			allKeys)
   463  
   464  		collectionOfCollection[oneBasedPageIndex-1] = pagedCollection
   465  
   466  		wg.Done()
   467  	}
   468  
   469  	wg.Add(pagesPossibleCeiling)
   470  	for i := 1; i <= pagesPossibleCeiling; i++ {
   471  		go addPagedItemsFunc(i)
   472  	}
   473  
   474  	wg.Wait()
   475  
   476  	return collectionOfCollection
   477  }
   478  
   479  // AddMapResult
   480  //
   481  //  apply override on existing result
   482  func (it *MapAnyItems) AddMapResult(
   483  	mapResults map[string]interface{},
   484  ) *MapAnyItems {
   485  	if len(mapResults) == 0 {
   486  		return it
   487  	}
   488  
   489  	for key, result := range mapResults {
   490  		it.Items[key] = result
   491  	}
   492  
   493  	return it
   494  }
   495  
   496  func (it *MapAnyItems) AddMapResultOption(
   497  	isOverride bool,
   498  	mapResults map[string]interface{},
   499  ) *MapAnyItems {
   500  	if len(mapResults) == 0 {
   501  		return it
   502  	}
   503  
   504  	if isOverride {
   505  		return it.AddMapResult(mapResults)
   506  	}
   507  
   508  	// no override
   509  	for key, result := range mapResults {
   510  		_, isFound := it.Items[key]
   511  
   512  		if !isFound {
   513  			continue
   514  		}
   515  
   516  		it.Items[key] = result
   517  	}
   518  
   519  	return it
   520  }
   521  
   522  func (it *MapAnyItems) AddManyMapResultsUsingOption(
   523  	isOverridingExisting bool,
   524  	mapsOfMapsResults ...map[string]interface{},
   525  ) *MapAnyItems {
   526  	if len(mapsOfMapsResults) == 0 {
   527  		return it
   528  	}
   529  
   530  	for _, mapResult := range mapsOfMapsResults {
   531  		it.AddMapResultOption(
   532  			isOverridingExisting,
   533  			mapResult)
   534  	}
   535  
   536  	return it
   537  }
   538  
   539  // GetSinglePageCollection PageIndex is one based index. Should be above or equal 1
   540  func (it *MapAnyItems) GetSinglePageCollection(
   541  	eachPageSize int,
   542  	pageIndex int,
   543  	allKeys []string,
   544  ) *MapAnyItems {
   545  	length := it.Length()
   546  
   547  	if length < eachPageSize {
   548  		return it
   549  	}
   550  
   551  	if length != len(allKeys) {
   552  		reference := errcore.VarTwoNoType(
   553  			"MapLength", it.Length(),
   554  			"AllKeysLength", len(allKeys))
   555  
   556  		errcore.
   557  			LengthShouldBeEqualToType.
   558  			HandleUsingPanic(
   559  				"allKeys length should be exact same as the map length, "+
   560  					"use AllKeys method to get the keys.",
   561  				reference)
   562  	}
   563  
   564  	/**
   565  	 * eachPageItems = 10
   566  	 * pageIndex = 4
   567  	 * skipItems = 10 * (4 - 1) = 30
   568  	 */
   569  	skipItems := eachPageSize * (pageIndex - 1)
   570  	if skipItems < 0 {
   571  		errcore.
   572  			CannotBeNegativeIndexType.
   573  			HandleUsingPanic(
   574  				"pageIndex cannot be negative or zero.",
   575  				pageIndex)
   576  	}
   577  
   578  	endingIndex := skipItems + eachPageSize
   579  
   580  	if endingIndex > length {
   581  		endingIndex = length
   582  	}
   583  
   584  	list := allKeys[skipItems:endingIndex]
   585  
   586  	return it.GetNewMapUsingKeys(
   587  		true,
   588  		list...)
   589  }
   590  
   591  func (it *MapAnyItems) GetNewMapUsingKeys(
   592  	isPanicOnMissing bool,
   593  	keys ...string,
   594  ) *MapAnyItems {
   595  	if len(keys) == 0 {
   596  		return EmptyMapAnyItems()
   597  	}
   598  
   599  	mapResults := make(map[string]interface{}, len(keys))
   600  
   601  	for _, key := range keys {
   602  		item, has := it.Items[key]
   603  
   604  		if isPanicOnMissing && !has {
   605  			errcore.
   606  				KeyNotExistInMapType.
   607  				HandleUsingPanic(
   608  					"given key is not found in the map, key ="+key,
   609  					it.AllKeys())
   610  		}
   611  
   612  		if has {
   613  			mapResults[key] = item
   614  		}
   615  	}
   616  
   617  	return &MapAnyItems{Items: mapResults}
   618  }
   619  
   620  func (it *MapAnyItems) JsonString() (jsonString string, err error) {
   621  	toBytes, err := json.Marshal(it.Items)
   622  
   623  	if err != nil {
   624  		return constants.EmptyString, err
   625  	}
   626  
   627  	return string(toBytes), err
   628  }
   629  
   630  func (it *MapAnyItems) JsonStringMust() string {
   631  	toString, err := it.JsonString()
   632  
   633  	if err != nil {
   634  		errcore.
   635  			MarshallingFailedType.
   636  			HandleUsingPanic(err.Error(), it.Items)
   637  	}
   638  
   639  	return toString
   640  }
   641  
   642  func (it *MapAnyItems) JsonResultOfKey(
   643  	key string,
   644  ) *corejson.Result {
   645  	item, has := it.Get(key)
   646  
   647  	if has {
   648  		return corejson.NewPtr(item)
   649  	}
   650  
   651  	err := errcore.
   652  		KeyNotExistInMapType.
   653  		Error("Key", key)
   654  
   655  	return corejson.
   656  		Empty.
   657  		ResultPtrWithErr(
   658  			reflectinternal.TypeName(it),
   659  			err)
   660  }
   661  
   662  func (it *MapAnyItems) JsonResultOfKeys(
   663  	keys ...string,
   664  ) *corejson.MapResults {
   665  	mapResults := corejson.NewMapResults.UsingCap(len(keys))
   666  
   667  	if len(keys) == 0 {
   668  		return mapResults
   669  	}
   670  
   671  	for _, key := range keys {
   672  		mapResults.AddPtr(
   673  			key,
   674  			it.JsonResultOfKey(key))
   675  	}
   676  
   677  	return mapResults
   678  }
   679  
   680  func (it *MapAnyItems) AllKeys() []string {
   681  	if it.IsEmpty() {
   682  		return []string{}
   683  	}
   684  
   685  	keys := make([]string, it.Length())
   686  
   687  	index := 0
   688  	for key := range it.Items {
   689  		keys[index] = key
   690  		index++
   691  	}
   692  
   693  	return keys
   694  }
   695  
   696  func (it *MapAnyItems) AllKeysSorted() []string {
   697  	if it.IsEmpty() {
   698  		return []string{}
   699  	}
   700  
   701  	keys := it.AllKeys()
   702  	sort.Strings(keys)
   703  
   704  	return keys
   705  }
   706  
   707  func (it *MapAnyItems) AllValues() []interface{} {
   708  	if it.IsEmpty() {
   709  		return []interface{}{}
   710  	}
   711  
   712  	values := make([]interface{}, it.Length())
   713  
   714  	index := 0
   715  	for _, result := range it.Items {
   716  		values[index] = result
   717  		index++
   718  	}
   719  
   720  	return values
   721  }
   722  
   723  func (it *MapAnyItems) DiffRaw(
   724  	isRegardlessType bool,
   725  	rightMap map[string]interface{},
   726  ) map[string]interface{} {
   727  	mapDiffer := mapdiffinternal.MapStringAnyDiff(
   728  		rightMap)
   729  
   730  	return mapDiffer.DiffRaw(
   731  		isRegardlessType,
   732  		rightMap)
   733  }
   734  
   735  func (it *MapAnyItems) Diff(
   736  	isRegardlessType bool,
   737  	rightMap *MapAnyItems,
   738  ) *MapAnyItems {
   739  	rawMap := it.DiffRaw(
   740  		isRegardlessType,
   741  		rightMap.Items)
   742  
   743  	return NewMapAnyItemsUsingItems(rawMap)
   744  }
   745  
   746  func (it *MapAnyItems) IsRawEqual(
   747  	isRegardlessType bool,
   748  	rightMap map[string]interface{},
   749  ) bool {
   750  	differ := it.RawMapStringAnyDiff()
   751  
   752  	return differ.
   753  		IsRawEqual(
   754  			isRegardlessType,
   755  			rightMap)
   756  }
   757  
   758  func (it *MapAnyItems) HashmapDiffUsingRaw(
   759  	isRegardlessType bool,
   760  	rightMap map[string]interface{},
   761  ) MapAnyItemDiff {
   762  	diffMap := it.DiffRaw(
   763  		isRegardlessType,
   764  		rightMap)
   765  
   766  	if len(diffMap) == 0 {
   767  		return map[string]interface{}{}
   768  	}
   769  
   770  	return diffMap
   771  }
   772  
   773  func (it *MapAnyItems) MapAnyItems() *MapAnyItems {
   774  	return it
   775  }
   776  
   777  func (it *MapAnyItems) HasAnyChanges(
   778  	isRegardlessType bool,
   779  	rightMap map[string]interface{},
   780  ) bool {
   781  	return !it.IsRawEqual(
   782  		isRegardlessType,
   783  		rightMap)
   784  }
   785  
   786  func (it *MapAnyItems) MapStringAnyDiff() mapdiffinternal.MapStringAnyDiff {
   787  	return it.Items
   788  }
   789  
   790  func (it *MapAnyItems) DiffJsonMessage(
   791  	isRegardlessType bool,
   792  	rightMap map[string]interface{},
   793  ) string {
   794  	differ := it.RawMapStringAnyDiff()
   795  
   796  	return differ.DiffJsonMessage(
   797  		isRegardlessType,
   798  		rightMap)
   799  }
   800  
   801  func (it *MapAnyItems) ToStringsSliceOfDiffMap(
   802  	diffMap map[string]interface{},
   803  ) (diffSlice []string) {
   804  	differ := it.RawMapStringAnyDiff()
   805  
   806  	return differ.ToStringsSliceOfDiffMap(
   807  		diffMap)
   808  }
   809  
   810  func (it *MapAnyItems) ShouldDiffMessage(
   811  	isRegardlessType bool,
   812  	title string,
   813  	rightMap map[string]interface{},
   814  ) string {
   815  	differ := it.RawMapStringAnyDiff()
   816  
   817  	return differ.ShouldDiffMessage(
   818  		isRegardlessType,
   819  		title,
   820  		rightMap)
   821  }
   822  
   823  func (it *MapAnyItems) LogShouldDiffMessage(
   824  	isRegardlessType bool,
   825  	title string,
   826  	rightMap map[string]interface{},
   827  ) (diffMessage string) {
   828  	differ := it.RawMapStringAnyDiff()
   829  
   830  	return differ.LogShouldDiffMessage(
   831  		isRegardlessType,
   832  		title,
   833  		rightMap)
   834  }
   835  
   836  func (it *MapAnyItems) JsonMapResults() (*corejson.MapResults, error) {
   837  	mapResults := corejson.NewMapResults.UsingCap(it.Length())
   838  
   839  	if it.IsEmpty() {
   840  		return mapResults, nil
   841  	}
   842  
   843  	for key, anyInf := range it.Items {
   844  		err := mapResults.AddAny(
   845  			key,
   846  			anyInf)
   847  
   848  		if err != nil {
   849  			return mapResults, err
   850  		}
   851  	}
   852  
   853  	return mapResults, nil
   854  }
   855  
   856  func (it *MapAnyItems) JsonResultsCollection() *corejson.ResultsCollection {
   857  	jsonResultsCollection := corejson.
   858  		NewResultsCollection.
   859  		UsingCap(it.Length())
   860  
   861  	if it.IsEmpty() {
   862  		return jsonResultsCollection
   863  	}
   864  
   865  	for _, anyInf := range it.Items {
   866  		jsonResultsCollection.AddAny(
   867  			anyInf)
   868  	}
   869  
   870  	return jsonResultsCollection
   871  }
   872  
   873  func (it *MapAnyItems) JsonResultsPtrCollection() *corejson.ResultsPtrCollection {
   874  	jsonResultsCollection := corejson.NewResultsPtrCollection.UsingCap(it.Length())
   875  
   876  	if it.IsEmpty() {
   877  		return jsonResultsCollection
   878  	}
   879  
   880  	for _, anyInf := range it.Items {
   881  		jsonResultsCollection.AddAny(
   882  			anyInf)
   883  	}
   884  
   885  	return jsonResultsCollection
   886  }
   887  
   888  func (it *MapAnyItems) JsonModel() *corejson.MapResults {
   889  	mapResults := corejson.NewMapResults.UsingCap(
   890  		it.Length() +
   891  			constants.Capacity3)
   892  
   893  	if it.IsEmpty() {
   894  		return mapResults
   895  	}
   896  
   897  	for key, anyInf := range it.Items {
   898  		mapResults.AddAnyNonEmpty(key, anyInf)
   899  	}
   900  
   901  	return mapResults
   902  }
   903  
   904  func (it *MapAnyItems) JsonModelAny() interface{} {
   905  	return it.JsonModel()
   906  }
   907  
   908  func (it MapAnyItems) Json() corejson.Result {
   909  	return corejson.New(it)
   910  }
   911  
   912  func (it MapAnyItems) JsonPtr() *corejson.Result {
   913  	return corejson.NewPtr(it)
   914  }
   915  
   916  //goland:noinspection GoLinterLocal
   917  func (it *MapAnyItems) ParseInjectUsingJson(
   918  	jsonResult *corejson.Result,
   919  ) (*MapAnyItems, error) {
   920  	err := jsonResult.Unmarshal(it)
   921  
   922  	if err != nil {
   923  		return EmptyMapAnyItems(), err
   924  	}
   925  
   926  	return it, nil
   927  }
   928  
   929  // ParseInjectUsingJsonMust Panic if error
   930  //goland:noinspection GoLinterLocal
   931  func (it *MapAnyItems) ParseInjectUsingJsonMust(
   932  	jsonResult *corejson.Result,
   933  ) *MapAnyItems {
   934  	newUsingJson, err :=
   935  		it.ParseInjectUsingJson(jsonResult)
   936  
   937  	if err != nil {
   938  		panic(err)
   939  	}
   940  
   941  	return newUsingJson
   942  }
   943  
   944  func (it *MapAnyItems) JsonParseSelfInject(
   945  	jsonResult *corejson.Result,
   946  ) error {
   947  	_, err := it.ParseInjectUsingJson(
   948  		jsonResult,
   949  	)
   950  
   951  	return err
   952  }
   953  
   954  func (it *MapAnyItems) Strings() []string {
   955  	return errcore.VarMapStrings(it.Items)
   956  }
   957  
   958  func (it *MapAnyItems) Clear() {
   959  	if it == nil {
   960  		return
   961  	}
   962  
   963  	it.Items = map[string]interface{}{}
   964  }
   965  
   966  func (it *MapAnyItems) DeepClear() {
   967  	if it == nil {
   968  		return
   969  	}
   970  
   971  	tempItems := it.Items
   972  
   973  	tempClearFunc := func() {
   974  		for key := range tempItems {
   975  			delete(tempItems, key)
   976  		}
   977  	}
   978  
   979  	go tempClearFunc()
   980  
   981  	it.Items = map[string]interface{}{}
   982  }
   983  
   984  func (it *MapAnyItems) Dispose() {
   985  	if it == nil {
   986  		return
   987  	}
   988  
   989  	it.DeepClear()
   990  	it.Items = nil
   991  }
   992  
   993  func (it *MapAnyItems) String() string {
   994  	return errcore.VarMap(it.Items)
   995  }
   996  
   997  func (it *MapAnyItems) IsEqualRaw(
   998  	rightMappedItems map[string]interface{},
   999  ) bool {
  1000  	if it == nil && rightMappedItems == nil {
  1001  		return true
  1002  	}
  1003  
  1004  	if it == nil || rightMappedItems == nil {
  1005  		return false
  1006  	}
  1007  
  1008  	leftLength := it.Length()
  1009  	rightLength := len(rightMappedItems)
  1010  
  1011  	if leftLength != rightLength {
  1012  		return false
  1013  	}
  1014  
  1015  	for key := range it.Items {
  1016  		rightElem, has := rightMappedItems[key]
  1017  
  1018  		if !has {
  1019  			return false
  1020  		}
  1021  
  1022  		leftElem := it.Items[key]
  1023  		if !reflectinternal.IsAnyEqual(leftElem, rightElem) {
  1024  			return false
  1025  		}
  1026  	}
  1027  
  1028  	return true
  1029  }
  1030  
  1031  func (it *MapAnyItems) IsEqual(
  1032  	right *MapAnyItems,
  1033  ) bool {
  1034  	if it == nil && right == nil {
  1035  		return true
  1036  	}
  1037  
  1038  	if it == nil || right == nil {
  1039  		return false
  1040  	}
  1041  
  1042  	leftLength := it.Length()
  1043  	rightLength := right.Length()
  1044  
  1045  	if leftLength != rightLength {
  1046  		return false
  1047  	}
  1048  
  1049  	return it.IsEqualRaw(right.Items)
  1050  }
  1051  
  1052  func (it *MapAnyItems) ClonePtr() (*MapAnyItems, error) {
  1053  	if it == nil {
  1054  		return nil, defaulterr.NilResult
  1055  	}
  1056  
  1057  	jsonResult := it.Json()
  1058  	if jsonResult.HasError() {
  1059  		return EmptyMapAnyItems(), jsonResult.MeaningfulError()
  1060  	}
  1061  
  1062  	bytesConv := NewBytesConverter(
  1063  		jsonResult.Bytes)
  1064  
  1065  	return bytesConv.ToMapAnyItems()
  1066  }
  1067  
  1068  func (it *MapAnyItems) RawMapStringAnyDiff() mapdiffinternal.MapStringAnyDiff {
  1069  	if it == nil {
  1070  		return map[string]interface{}{}
  1071  	}
  1072  
  1073  	return it.Items
  1074  }