gitlab.com/evatix-go/core@v1.3.55/coreimpl/enumimpl/DynamicMap.go (about)

     1  package enumimpl
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"math"
     7  	"reflect"
     8  	"sort"
     9  	"strconv"
    10  	"strings"
    11  
    12  	"gitlab.com/evatix-go/core/constants"
    13  )
    14  
    15  type DynamicMap map[string]interface{}
    16  
    17  func (it DynamicMap) AddOrUpdate(key string, val interface{}) (isAddNewly bool) {
    18  	_, isAlreadyExist := it[key]
    19  	it[key] = val
    20  
    21  	return !isAlreadyExist
    22  }
    23  
    24  func (it *DynamicMap) Set(key string, val interface{}) (isAddNewly bool) {
    25  	if it == nil {
    26  		// mutating because it is part of
    27  		*it = make(map[string]interface{}, constants.Capacity5)
    28  	}
    29  
    30  	_, isAlreadyExist := (*it)[key]
    31  	(*it)[key] = val
    32  
    33  	return !isAlreadyExist
    34  }
    35  
    36  // AddNewOnly
    37  //
    38  //  Don't update existing
    39  func (it *DynamicMap) AddNewOnly(key string, val interface{}) (isAdded bool) {
    40  	if it == nil {
    41  		// mutating because it is part of
    42  		*it = make(map[string]interface{}, constants.Capacity5)
    43  	}
    44  
    45  	_, isAlreadyExist := (*it)[key]
    46  	if isAlreadyExist {
    47  		return false
    48  	}
    49  
    50  	(*it)[key] = val
    51  
    52  	return true
    53  }
    54  
    55  func (it DynamicMap) AllKeys() []string {
    56  	if it.IsEmpty() {
    57  		return []string{}
    58  	}
    59  
    60  	allKeys := make(
    61  		[]string,
    62  		it.Length())
    63  
    64  	index := 0
    65  	for key := range it {
    66  		allKeys[index] = key
    67  		index++
    68  	}
    69  
    70  	return allKeys
    71  }
    72  
    73  func (it DynamicMap) AllKeysSorted() []string {
    74  	if it.IsEmpty() {
    75  		return []string{}
    76  	}
    77  
    78  	allKeys := it.AllKeys()
    79  	sort.Strings(allKeys)
    80  
    81  	return allKeys
    82  }
    83  
    84  func (it DynamicMap) AllValuesStrings() []string {
    85  	if it.IsEmpty() {
    86  		return []string{}
    87  	}
    88  
    89  	allValues := make(
    90  		[]string,
    91  		it.Length())
    92  
    93  	index := 0
    94  	for _, value := range it {
    95  		allValues[index] = fmt.Sprintf(
    96  			constants.SprintValueFormat,
    97  			value)
    98  		index++
    99  	}
   100  
   101  	return allValues
   102  }
   103  
   104  func (it DynamicMap) AllValuesStringsSorted() []string {
   105  	if it.IsEmpty() {
   106  		return []string{}
   107  	}
   108  
   109  	allValues := it.AllValuesStrings()
   110  	sort.Strings(allValues)
   111  
   112  	return allValues
   113  }
   114  
   115  func (it DynamicMap) AllValuesIntegers() []int {
   116  	if it.IsEmpty() {
   117  		return []int{}
   118  	}
   119  
   120  	allValues := make(
   121  		[]int,
   122  		it.Length())
   123  
   124  	index := 0
   125  	for _, value := range it {
   126  		allValues[index] = ConvEnumAnyValToInteger(value)
   127  
   128  		index++
   129  	}
   130  
   131  	return allValues
   132  }
   133  
   134  func (it DynamicMap) MapIntegerString() (
   135  	rangeMap map[int]string,
   136  	allKeysSorted []int,
   137  ) {
   138  	if it.IsEmpty() {
   139  		return map[int]string{}, []int{}
   140  	}
   141  
   142  	rangeMap = make(
   143  		map[int]string,
   144  		it.Length()+2)
   145  
   146  	allKeysSorted = make(
   147  		[]int,
   148  		it.Length())
   149  
   150  	if it.IsValueString() {
   151  		return it.stringValueMapIntegerString(rangeMap, allKeysSorted)
   152  	}
   153  
   154  	index := 0
   155  	for key, value := range it {
   156  		valInt := ConvEnumAnyValToInteger(value)
   157  		rangeMap[valInt] = key
   158  		allKeysSorted[index] = valInt
   159  
   160  		index++
   161  	}
   162  
   163  	sort.Ints(allKeysSorted)
   164  
   165  	return rangeMap, allKeysSorted
   166  }
   167  
   168  func (it DynamicMap) SortedKeyValues() (
   169  	keyValues []KeyValInteger,
   170  ) {
   171  	if it.IsEmpty() {
   172  		return keyValues
   173  	}
   174  
   175  	keyValues = make(
   176  		[]KeyValInteger,
   177  		it.Length())
   178  
   179  	rangesMap, AllKeysSorted := it.MapIntegerString()
   180  
   181  	for i, keyInt := range AllKeysSorted {
   182  		name := rangesMap[keyInt]
   183  		keyValues[i] = KeyValInteger{
   184  			Key:          name,
   185  			ValueInteger: keyInt,
   186  		}
   187  	}
   188  
   189  	return keyValues
   190  }
   191  
   192  func (it DynamicMap) SortedKeyAnyValues() (
   193  	keyAnyValues []KeyAnyVal,
   194  ) {
   195  	if it.IsEmpty() {
   196  		return keyAnyValues
   197  	}
   198  
   199  	keyAnyValues = make(
   200  		[]KeyAnyVal,
   201  		it.Length())
   202  
   203  	if it.IsValueString() {
   204  		return it.sortedKeyAnyValuesString()
   205  	}
   206  
   207  	rangesMap, AllKeysSorted := it.MapIntegerString()
   208  
   209  	for i, keyInt := range AllKeysSorted {
   210  		name := rangesMap[keyInt]
   211  		keyAnyValues[i] = KeyAnyVal{
   212  			Key:      name,
   213  			AnyValue: keyInt,
   214  		}
   215  	}
   216  
   217  	return keyAnyValues
   218  }
   219  
   220  func (it DynamicMap) First() (key string, valInf interface{}) {
   221  	for key, valInf = range it {
   222  		return key, valInf
   223  	}
   224  
   225  	return "", nil
   226  }
   227  
   228  func (it DynamicMap) IsValueTypeOf(rfType reflect.Type) bool {
   229  	_, v := it.First()
   230  
   231  	return reflect.TypeOf(v) == rfType
   232  }
   233  
   234  func (it DynamicMap) IsValueString() bool {
   235  	_, v := it.First()
   236  	_, isString := v.(string)
   237  
   238  	return isString
   239  }
   240  
   241  func (it *DynamicMap) Length() int {
   242  	if it == nil {
   243  		return 0
   244  	}
   245  
   246  	return len(*it)
   247  }
   248  
   249  func (it DynamicMap) Count() int {
   250  	return it.Length()
   251  }
   252  
   253  func (it DynamicMap) IsEmpty() bool {
   254  	return it.Length() == 0
   255  }
   256  
   257  func (it DynamicMap) HasAnyItem() bool {
   258  	return it.Length() > 0
   259  }
   260  
   261  func (it DynamicMap) LastIndex() int {
   262  	return it.Length() - 1
   263  }
   264  
   265  func (it DynamicMap) HasIndex(index int) bool {
   266  	return it.LastIndex() >= index
   267  }
   268  
   269  func (it DynamicMap) HasKey(key string) bool {
   270  	_, has := it[key]
   271  
   272  	return has
   273  }
   274  
   275  func (it DynamicMap) HasAllKeys(keys ...string) bool {
   276  	for _, key := range keys {
   277  		if it.IsMissingKey(key) {
   278  			return false
   279  		}
   280  	}
   281  
   282  	return true
   283  }
   284  
   285  func (it DynamicMap) HasAnyKeys(keys ...string) bool {
   286  	for _, key := range keys {
   287  		if it.HasKey(key) {
   288  			return true
   289  		}
   290  	}
   291  
   292  	return false
   293  }
   294  
   295  func (it DynamicMap) IsMissingKey(key string) bool {
   296  	_, has := it[key]
   297  
   298  	return !has
   299  }
   300  
   301  func (it *DynamicMap) IsMismatch(
   302  	isRegardlessType bool,
   303  	rightMap *DynamicMap,
   304  ) bool {
   305  	return !it.IsEqual(isRegardlessType, rightMap)
   306  }
   307  
   308  func (it *DynamicMap) IsRawMismatch(
   309  	isRegardlessType bool,
   310  	rightMap map[string]interface{},
   311  ) bool {
   312  	return !it.IsRawEqual(isRegardlessType, rightMap)
   313  }
   314  
   315  func (it *DynamicMap) IsEqual(
   316  	isRegardlessType bool,
   317  	rightMap *DynamicMap,
   318  ) bool {
   319  	if it == nil && rightMap == nil {
   320  		return true
   321  	}
   322  
   323  	if it == nil || rightMap == nil {
   324  		return false
   325  	}
   326  
   327  	if it == rightMap {
   328  		return true
   329  	}
   330  
   331  	return it.IsRawEqual(
   332  		isRegardlessType,
   333  		*rightMap)
   334  }
   335  
   336  func (it *DynamicMap) IsRawEqual(
   337  	isRegardlessType bool,
   338  	rightMap map[string]interface{},
   339  ) bool {
   340  	if it == nil && rightMap == nil {
   341  		return true
   342  	}
   343  
   344  	if it == nil || rightMap == nil {
   345  		return false
   346  	}
   347  
   348  	if it.Length() != len(rightMap) {
   349  		return false
   350  	}
   351  
   352  	for key, leftValInf := range *it {
   353  		rightValInf, has := rightMap[key]
   354  
   355  		if !has {
   356  			return false
   357  		}
   358  
   359  		if it.isNotEqual(
   360  			isRegardlessType,
   361  			leftValInf,
   362  			rightValInf) {
   363  			return false
   364  		}
   365  	}
   366  
   367  	return true
   368  }
   369  
   370  func (it DynamicMap) Raw() map[string]interface{} {
   371  	return it
   372  }
   373  
   374  func (it *DynamicMap) DiffRaw(
   375  	isRegardlessType bool,
   376  	rightMap map[string]interface{},
   377  ) DynamicMap {
   378  	if it == nil && rightMap == nil {
   379  		return map[string]interface{}{}
   380  	}
   381  
   382  	if it == nil && rightMap != nil {
   383  		return rightMap
   384  	}
   385  
   386  	if it != nil && rightMap == nil {
   387  		return *it
   388  	}
   389  
   390  	length := it.Length() / 3
   391  	diffMap := make(
   392  		map[string]interface{},
   393  		length)
   394  
   395  	for key, leftValInf := range *it {
   396  		rightValInf, has := rightMap[key]
   397  
   398  		if !has {
   399  			diffMap[key] = leftValInf
   400  
   401  			continue
   402  		}
   403  
   404  		if it.isNotEqual(
   405  			isRegardlessType,
   406  			leftValInf,
   407  			rightValInf) {
   408  			diffMap[key] = leftValInf
   409  		}
   410  	}
   411  
   412  	if len(diffMap) == 0 && it.Length() == len(rightMap) {
   413  		return diffMap
   414  	}
   415  
   416  	leftMap := *it
   417  	for rightKey, rightAnyVal := range rightMap {
   418  		_, hasDiff := diffMap[rightKey]
   419  
   420  		if hasDiff {
   421  			// already added
   422  
   423  			continue
   424  		}
   425  
   426  		leftVal, has := leftMap[rightKey]
   427  
   428  		if !has {
   429  			diffMap[rightKey] = rightAnyVal
   430  
   431  			continue
   432  		}
   433  
   434  		if it.isNotEqual(
   435  			isRegardlessType,
   436  			rightAnyVal,
   437  			leftVal) {
   438  			diffMap[rightKey] = rightAnyVal
   439  		}
   440  	}
   441  
   442  	return diffMap
   443  }
   444  
   445  func (it *DynamicMap) DiffJsonMessage(
   446  	isRegardlessType bool,
   447  	rightMap map[string]interface{},
   448  ) string {
   449  	diffMap := it.DiffRaw(isRegardlessType, rightMap)
   450  
   451  	if diffMap.Length() == 0 {
   452  		return ""
   453  	}
   454  
   455  	slice := toStringsSliceOfDiffMap(diffMap)
   456  	compiledString := strings.Join(
   457  		slice,
   458  		constants.CommaUnixNewLine)
   459  
   460  	return fmt.Sprintf(
   461  		curlyWrapFormat,
   462  		compiledString)
   463  }
   464  
   465  func (it *DynamicMap) ShouldDiffMessage(
   466  	isRegardlessType bool,
   467  	title string,
   468  	rightMap map[string]interface{},
   469  ) string {
   470  	diffMessage := it.DiffJsonMessage(
   471  		isRegardlessType,
   472  		rightMap)
   473  
   474  	if diffMessage == "" {
   475  		return ""
   476  	}
   477  
   478  	return fmt.Sprintf(
   479  		diffBetweenMapShouldBeMessageFormat,
   480  		title,
   481  		diffMessage)
   482  }
   483  
   484  func (it *DynamicMap) LogShouldDiffMessage(
   485  	isRegardlessType bool,
   486  	title string,
   487  	rightMap map[string]interface{},
   488  ) (diffMessage string) {
   489  	diffMessage = it.ShouldDiffMessage(
   490  		isRegardlessType,
   491  		title,
   492  		rightMap)
   493  
   494  	if diffMessage == "" {
   495  		return
   496  	}
   497  
   498  	fmt.Println(diffMessage)
   499  
   500  	return diffMessage
   501  }
   502  
   503  func (it *DynamicMap) ExpectingMessage(
   504  	title string,
   505  	expected map[string]interface{},
   506  ) string {
   507  	expectedMap := DynamicMap(expected)
   508  	actualMapString := it.String()
   509  	expectedMapString := expectedMap.String()
   510  
   511  	isMapEqual := actualMapString == expectedMapString
   512  
   513  	if isMapEqual {
   514  		return ""
   515  	}
   516  
   517  	return fmt.Sprintf(
   518  		actualVsExpectingMessageFormat,
   519  		title,
   520  		actualMapString,
   521  		expectedMapString,
   522  	)
   523  }
   524  
   525  func (it *DynamicMap) LogExpectingMessage(
   526  	title string,
   527  	expected map[string]interface{},
   528  ) {
   529  	expectingMessage := it.ExpectingMessage(title, expected)
   530  
   531  	if expectingMessage == "" {
   532  		return
   533  	}
   534  
   535  	fmt.Println(expectingMessage)
   536  }
   537  
   538  func (it *DynamicMap) isNotEqual(
   539  	isRegardlessType bool,
   540  	left,
   541  	right interface{},
   542  ) bool {
   543  	if isRegardlessType {
   544  		leftString := fmt.Sprintf(
   545  			constants.SprintPropertyNameValueFormat,
   546  			left)
   547  		rightString := fmt.Sprintf(
   548  			constants.SprintPropertyNameValueFormat,
   549  			right)
   550  
   551  		return leftString != rightString
   552  	}
   553  
   554  	return !reflect.DeepEqual(left, right)
   555  }
   556  
   557  func (it *DynamicMap) IsKeysEqualOnly(
   558  	rightMap map[string]interface{},
   559  ) bool {
   560  	if it == nil && rightMap == nil {
   561  		return true
   562  	}
   563  
   564  	if it == nil || rightMap == nil {
   565  		return false
   566  	}
   567  
   568  	if it.Length() != len(rightMap) {
   569  		return false
   570  	}
   571  
   572  	for key := range *it {
   573  		_, has := rightMap[key]
   574  
   575  		if !has {
   576  			return false
   577  		}
   578  	}
   579  
   580  	return true
   581  }
   582  
   583  func (it DynamicMap) KeyValue(
   584  	key string,
   585  ) (val interface{}, isFound bool) {
   586  	val, isFound = it[key]
   587  
   588  	return val, isFound
   589  }
   590  
   591  func (it DynamicMap) KeyValueString(
   592  	key string,
   593  ) (val string, isFound bool) {
   594  	valInf, isFound := it[key]
   595  
   596  	if isFound {
   597  		convString := fmt.Sprintf(
   598  			constants.SprintValueFormat,
   599  			valInf)
   600  
   601  		return convString, isFound
   602  	}
   603  
   604  	return "", isFound
   605  }
   606  
   607  func (it DynamicMap) KeyValueIntDefault(
   608  	key string,
   609  ) (val int) {
   610  	valInt, isFound, isConvFailed := it.KeyValueInt(key)
   611  
   612  	if !isFound || isConvFailed {
   613  		return constants.InvalidValue
   614  	}
   615  
   616  	return valInt
   617  }
   618  
   619  func (it DynamicMap) KeyValueByte(
   620  	key string,
   621  ) (val byte, isFound, isConvFailed bool) {
   622  	valInf, isFound := it[key]
   623  
   624  	if !isFound {
   625  		return constants.Zero, isFound, true
   626  	}
   627  
   628  	valueByterCasted, isSuccess := valInf.(valueByter)
   629  
   630  	if isSuccess {
   631  		return valueByterCasted.Value(),
   632  			true,
   633  			false
   634  	}
   635  
   636  	exactValueByterCasted, isSuccess := valInf.(exactValueByter)
   637  
   638  	if isSuccess {
   639  		return exactValueByterCasted.ValueByte(),
   640  			true,
   641  			false
   642  	}
   643  
   644  	toByteCasted, isSuccess := valInf.(byte)
   645  
   646  	if isSuccess {
   647  		return toByteCasted,
   648  			true,
   649  			false
   650  	}
   651  
   652  	toString := fmt.Sprintf(
   653  		constants.SprintValueFormat,
   654  		valInf)
   655  
   656  	toInt, err := strconv.Atoi(toString)
   657  
   658  	if err != nil {
   659  		return constants.Zero, true, false
   660  	}
   661  
   662  	if toInt >= 0 && toInt <= 255 {
   663  		return byte(toInt), true, false
   664  	}
   665  
   666  	return constants.Zero, true, true
   667  }
   668  
   669  func (it *DynamicMap) Add(
   670  	key string,
   671  	valInf interface{},
   672  ) *DynamicMap {
   673  	(*it)[key] = valInf
   674  
   675  	return it
   676  }
   677  
   678  func (it DynamicMap) KeyValueInt(
   679  	key string,
   680  ) (val int, isFound, isConvFailed bool) {
   681  	valInf, isFound := it[key]
   682  
   683  	if !isFound {
   684  		return constants.InvalidValue, isFound, true
   685  	}
   686  
   687  	valInt, isInt := valInf.(int)
   688  	if isInt {
   689  		return valInt, isFound, false
   690  	}
   691  
   692  	valueByterCasted, isByter := valInf.(valueByter)
   693  
   694  	if isByter {
   695  		return int(valueByterCasted.Value()), isFound, false
   696  	}
   697  
   698  	exactValueByterCasted, isExactByter := valInf.(exactValueByter)
   699  
   700  	if isExactByter {
   701  		return int(exactValueByterCasted.ValueByte()), isFound, false
   702  	}
   703  
   704  	valByte, isByte := valInf.(byte)
   705  	if isByte {
   706  		return int(valByte), isFound, false
   707  	}
   708  
   709  	toString := fmt.Sprintf(
   710  		constants.SprintValueFormat,
   711  		valInf)
   712  
   713  	toInt, err := strconv.Atoi(toString)
   714  
   715  	if err != nil {
   716  		// failed
   717  		return constants.InvalidValue, true, true
   718  	}
   719  
   720  	return toInt, true, false
   721  }
   722  
   723  func (it DynamicMap) BasicByte(typeName string) *BasicByte {
   724  	return New.BasicByte.CreateUsingMap(
   725  		typeName,
   726  		it.ConvMapByteString())
   727  }
   728  
   729  func (it DynamicMap) BasicByteUsingAliasMap(
   730  	typeName string,
   731  	aliasingMap map[string]byte,
   732  ) *BasicByte {
   733  	return New.BasicByte.CreateUsingMapPlusAliasMap(
   734  		typeName,
   735  		it.ConvMapByteString(),
   736  		aliasingMap)
   737  }
   738  
   739  func (it DynamicMap) BasicInt8(typeName string) *BasicInt8 {
   740  	return New.
   741  		BasicInt8.
   742  		CreateUsingMap(
   743  			typeName,
   744  			it.ConvMapInt8String())
   745  }
   746  
   747  func (it DynamicMap) BasicInt8UsingAliasMap(
   748  	typeName string,
   749  	aliasingMap map[string]int8,
   750  ) *BasicInt8 {
   751  	return New.
   752  		BasicInt8.
   753  		CreateUsingMapPlusAliasMap(
   754  			typeName,
   755  			it.ConvMapInt8String(),
   756  			aliasingMap)
   757  }
   758  
   759  func (it DynamicMap) BasicInt16(
   760  	typeName string,
   761  ) *BasicInt16 {
   762  	return New.
   763  		BasicInt16.
   764  		CreateUsingMap(
   765  			typeName,
   766  			it.ConvMapInt16String())
   767  }
   768  
   769  func (it DynamicMap) BasicInt16UsingAliasMap(
   770  	typeName string,
   771  	aliasingMap map[string]int16,
   772  ) *BasicInt16 {
   773  	return New.BasicInt16.CreateUsingMapPlusAliasMap(
   774  		typeName,
   775  		it.ConvMapInt16String(),
   776  		aliasingMap)
   777  }
   778  
   779  func (it DynamicMap) BasicInt32(
   780  	typeName string,
   781  ) *BasicInt32 {
   782  	return New.
   783  		BasicInt32.
   784  		CreateUsingMap(
   785  			typeName,
   786  			it.ConvMapInt32String())
   787  }
   788  
   789  func (it DynamicMap) BasicInt32UsingAliasMap(
   790  	typeName string,
   791  	aliasingMap map[string]int32,
   792  ) *BasicInt32 {
   793  	return New.
   794  		BasicInt32.
   795  		CreateUsingMapPlusAliasMap(
   796  			typeName,
   797  			it.ConvMapInt32String(),
   798  			aliasingMap)
   799  }
   800  
   801  func (it DynamicMap) BasicString(
   802  	typeName string,
   803  ) *BasicString {
   804  	return New.
   805  		BasicString.
   806  		Create(
   807  			typeName,
   808  			it.AllKeysSorted())
   809  }
   810  
   811  func (it DynamicMap) BasicStringUsingAliasMap(
   812  	typeName string,
   813  	aliasingMap map[string]string,
   814  ) *BasicString {
   815  	return New.
   816  		BasicString.
   817  		CreateAliasMapOnly(
   818  			typeName,
   819  			it.AllKeysSorted(),
   820  			aliasingMap)
   821  }
   822  
   823  func (it DynamicMap) BasicUInt16(
   824  	typeName string,
   825  ) *BasicUInt16 {
   826  	return New.
   827  		BasicUInt16.
   828  		CreateUsingMap(
   829  			typeName,
   830  			it.ConvMapUInt16String())
   831  }
   832  
   833  func (it DynamicMap) BasicUInt16UsingAliasMap(
   834  	typeName string,
   835  	aliasingMap map[string]uint16,
   836  ) *BasicUInt16 {
   837  	return New.
   838  		BasicUInt16.
   839  		CreateUsingMapPlusAliasMap(
   840  			typeName,
   841  			it.ConvMapUInt16String(),
   842  			aliasingMap)
   843  }
   844  
   845  // ConvMapStringInteger
   846  //
   847  //  Conv value to key and key to value.
   848  func (it DynamicMap) ConvMapStringInteger() map[string]int {
   849  	if it.IsEmpty() {
   850  		return map[string]int{}
   851  	}
   852  
   853  	newMap := make(map[string]int, it.Length())
   854  
   855  	for key := range it {
   856  		valInt := it.KeyValueIntDefault(key)
   857  		newMap[key] = valInt
   858  	}
   859  
   860  	return newMap
   861  }
   862  
   863  // ConvMapIntegerString
   864  //
   865  //  Conv value to key and key to value.
   866  func (it DynamicMap) ConvMapIntegerString() map[int]string {
   867  	if it.IsEmpty() {
   868  		return map[int]string{}
   869  	}
   870  
   871  	newMap := make(map[int]string, it.Length())
   872  
   873  	for key := range it {
   874  		valInt := it.KeyValueIntDefault(key)
   875  		newMap[valInt] = key
   876  	}
   877  
   878  	return newMap
   879  }
   880  
   881  // ConvMapByteString
   882  //
   883  //  Conv value to key and key to value.
   884  func (it DynamicMap) ConvMapByteString() map[byte]string {
   885  	if it.IsEmpty() {
   886  		return map[byte]string{}
   887  	}
   888  
   889  	newMap := make(map[byte]string, it.Length())
   890  
   891  	for key := range it {
   892  		valByte, isFound, isFailed := it.KeyValueByte(
   893  			key)
   894  
   895  		if !isFound || isFailed {
   896  			continue
   897  		}
   898  
   899  		newMap[valByte] = key
   900  	}
   901  
   902  	return newMap
   903  }
   904  
   905  // ConvMapInt8String
   906  //
   907  //  Conv value to key and key to value.
   908  func (it DynamicMap) ConvMapInt8String() map[int8]string {
   909  	if it.IsEmpty() {
   910  		return map[int8]string{}
   911  	}
   912  
   913  	newMap := make(map[int8]string, it.Length())
   914  
   915  	for key := range it {
   916  		valInt := it.KeyValueIntDefault(
   917  			key)
   918  
   919  		if valInt < math.MinInt8 || valInt > math.MaxInt8 {
   920  			continue
   921  		}
   922  
   923  		newMap[int8(valInt)] = key
   924  	}
   925  
   926  	return newMap
   927  }
   928  
   929  // ConvMapInt16String
   930  //
   931  //  Conv value to key and key to value.
   932  func (it DynamicMap) ConvMapInt16String() map[int16]string {
   933  	if it.IsEmpty() {
   934  		return map[int16]string{}
   935  	}
   936  
   937  	newMap := make(map[int16]string, it.Length())
   938  
   939  	for key := range it {
   940  		valInt := it.KeyValueIntDefault(
   941  			key)
   942  
   943  		if valInt < math.MinInt16 || valInt > math.MaxInt16 {
   944  			continue
   945  		}
   946  
   947  		newMap[int16(valInt)] = key
   948  	}
   949  
   950  	return newMap
   951  }
   952  
   953  // ConvMapInt32String
   954  //
   955  //  Conv value to key and key to value.
   956  func (it DynamicMap) ConvMapInt32String() map[int32]string {
   957  	if it.IsEmpty() {
   958  		return map[int32]string{}
   959  	}
   960  
   961  	newMap := make(map[int32]string, it.Length())
   962  
   963  	for key := range it {
   964  		valInt := it.KeyValueIntDefault(
   965  			key)
   966  
   967  		if valInt < math.MinInt32 || valInt > math.MaxInt32 {
   968  			continue
   969  		}
   970  
   971  		newMap[int32(valInt)] = key
   972  	}
   973  
   974  	return newMap
   975  }
   976  
   977  // ConvMapUInt16String
   978  //
   979  //  Conv value to key and key to value.
   980  func (it DynamicMap) ConvMapUInt16String() map[uint16]string {
   981  	if it.IsEmpty() {
   982  		return map[uint16]string{}
   983  	}
   984  
   985  	newMap := make(map[uint16]string, it.Length())
   986  
   987  	for key := range it {
   988  		valInt := it.KeyValueIntDefault(
   989  			key)
   990  
   991  		if valInt < 0 || valInt > math.MaxInt16 {
   992  			continue
   993  		}
   994  
   995  		newMap[uint16(valInt)] = key
   996  	}
   997  
   998  	return newMap
   999  }
  1000  
  1001  // ConvMapStringString
  1002  //
  1003  //  Conv value to key and key to value.
  1004  func (it DynamicMap) ConvMapStringString() map[string]string {
  1005  	if it.IsEmpty() {
  1006  		return map[string]string{}
  1007  	}
  1008  
  1009  	newMap := make(map[string]string, it.Length())
  1010  
  1011  	for key := range it {
  1012  		valString, isFound := it.KeyValueString(
  1013  			key)
  1014  
  1015  		if !isFound {
  1016  			continue
  1017  		}
  1018  
  1019  		newMap[valString] = key
  1020  	}
  1021  
  1022  	return newMap
  1023  }
  1024  
  1025  // ConvMapInt64String
  1026  //
  1027  //  Conv value to key and key to value.
  1028  func (it DynamicMap) ConvMapInt64String() map[int64]string {
  1029  	if it.IsEmpty() {
  1030  		return map[int64]string{}
  1031  	}
  1032  
  1033  	newMap := make(map[int64]string, it.Length())
  1034  
  1035  	for key := range it {
  1036  		valInt := it.KeyValueIntDefault(
  1037  			key)
  1038  
  1039  		newMap[int64(valInt)] = key
  1040  	}
  1041  
  1042  	return newMap
  1043  }
  1044  
  1045  func (it DynamicMap) ConcatNew(
  1046  	isOverrideExisting bool,
  1047  	another DynamicMap,
  1048  ) DynamicMap {
  1049  	if it.IsEmpty() && another.IsEmpty() {
  1050  		return map[string]interface{}{}
  1051  	}
  1052  
  1053  	var newMap DynamicMap = make(
  1054  		map[string]interface{},
  1055  		it.Length()+another.Length()+1)
  1056  
  1057  	if it.HasAnyItem() {
  1058  		for key, val := range it {
  1059  			newMap[key] = val
  1060  		}
  1061  	}
  1062  
  1063  	hasAnother := another.HasAnyItem()
  1064  	if hasAnother && isOverrideExisting {
  1065  		for key, val := range another {
  1066  			newMap[key] = val
  1067  		}
  1068  	} else if hasAnother && !isOverrideExisting {
  1069  		for key, val := range another {
  1070  			newMap.AddNewOnly(key, val)
  1071  		}
  1072  	}
  1073  
  1074  	return newMap
  1075  }
  1076  
  1077  func (it DynamicMap) Strings() []string {
  1078  	if it.IsEmpty() {
  1079  		return []string{}
  1080  	}
  1081  
  1082  	slice := make([]string, it.Length())
  1083  	allKeysSorted := it.AllKeysSorted()
  1084  
  1085  	index := 0
  1086  	for _, key := range allKeysSorted {
  1087  		val := it[key]
  1088  
  1089  		slice[index] = fmt.Sprintf(
  1090  			constants.KeyValJsonFormat,
  1091  			key,
  1092  			val)
  1093  
  1094  		index++
  1095  	}
  1096  
  1097  	return slice
  1098  }
  1099  
  1100  func (it DynamicMap) StringsUsingFmt(
  1101  	formatter func(index int, key string, val interface{}) string,
  1102  ) []string {
  1103  	if it.IsEmpty() {
  1104  		return []string{}
  1105  	}
  1106  
  1107  	slice := make([]string, it.Length())
  1108  	allKeysSorted := it.AllKeysSorted()
  1109  
  1110  	for i, key := range allKeysSorted {
  1111  		val := it[key]
  1112  		slice[i] = formatter(
  1113  			i,
  1114  			key,
  1115  			val)
  1116  	}
  1117  
  1118  	return slice
  1119  }
  1120  
  1121  func (it DynamicMap) String() string {
  1122  	return strings.Join(
  1123  		it.Strings(),
  1124  		constants.DefaultLine)
  1125  }
  1126  
  1127  func (it DynamicMap) IsStringEqual(anotherMapString string) bool {
  1128  	return it.String() == anotherMapString
  1129  }
  1130  
  1131  func (it DynamicMap) Serialize() ([]byte, error) {
  1132  	return json.Marshal(it)
  1133  }
  1134  
  1135  func (it DynamicMap) sortedKeyAnyValuesString() []KeyAnyVal {
  1136  	allStringsSorted := it.AllKeysSorted()
  1137  	newSlice := make([]KeyAnyVal, len(allStringsSorted))
  1138  
  1139  	for i, keyName := range allStringsSorted {
  1140  		newSlice[i] = KeyAnyVal{
  1141  			Key:      keyName,
  1142  			AnyValue: it[keyName],
  1143  		}
  1144  	}
  1145  
  1146  	return newSlice
  1147  }
  1148  
  1149  func (it DynamicMap) stringValueMapIntegerString(
  1150  	rangeMap map[int]string, allNumberSorted []int,
  1151  ) (integerToStringMap map[int]string, sortedIntegers []int) {
  1152  	allNames := it.AllKeysSorted()
  1153  
  1154  	for i, name := range allNames {
  1155  		rangeMap[constants.MinInt] = name
  1156  		allNumberSorted[i] = constants.MinInt
  1157  	}
  1158  
  1159  	return rangeMap, allNumberSorted
  1160  }