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

     1  package enumimpl
     2  
     3  import (
     4  	"reflect"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"gitlab.com/evatix-go/core/constants"
     9  )
    10  
    11  type newBasicByteCreator struct{}
    12  
    13  func (it newBasicByteCreator) CreateUsingMap(
    14  	typeName string,
    15  	actualRangesMap map[byte]string,
    16  ) *BasicByte {
    17  	return it.CreateUsingMapPlusAliasMap(
    18  		typeName,
    19  		actualRangesMap,
    20  		nil,
    21  	)
    22  }
    23  
    24  func (it newBasicByteCreator) CreateUsingMapPlusAliasMap(
    25  	typeName string,
    26  	actualRangesMap map[byte]string,
    27  	aliasingMap map[string]byte,
    28  ) *BasicByte {
    29  	var min, max byte
    30  
    31  	max = constants.MinUint
    32  	min = 0
    33  
    34  	actualValues := make([]byte, len(actualRangesMap))
    35  	actualNames := make([]string, len(actualRangesMap))
    36  
    37  	index := 0
    38  	for val, name := range actualRangesMap {
    39  		actualValues[index] = val
    40  		actualNames[index] = name
    41  
    42  		if max < val {
    43  			max = val
    44  		}
    45  
    46  		index++
    47  	}
    48  
    49  	return it.CreateUsingAliasMap(
    50  		typeName,
    51  		actualValues,
    52  		actualNames,
    53  		aliasingMap, // aliasing map
    54  		min,         // zero
    55  		max,
    56  	)
    57  }
    58  
    59  func (it newBasicByteCreator) CreateUsingSlicePlusAliasMapOptions(
    60  	isIncludeUppercaseLowercase bool, // lowercase, uppercase all
    61  	firstItem interface{},
    62  	names []string,
    63  	aliasingMap map[string]byte,
    64  ) *BasicByte {
    65  	actualRangesMap := it.sliceNamesToMap(names)
    66  
    67  	finalAliasMap := it.generateUppercaseLowercaseAliasMap(
    68  		isIncludeUppercaseLowercase,
    69  		actualRangesMap,
    70  		aliasingMap)
    71  
    72  	return it.CreateUsingMapPlusAliasMap(
    73  		reflect.TypeOf(firstItem).String(),
    74  		actualRangesMap,
    75  		finalAliasMap,
    76  	)
    77  }
    78  
    79  func (it newBasicByteCreator) CreateUsingMapPlusAliasMapOptions(
    80  	isIncludeUppercaseLowercase bool, // lowercase, uppercase all
    81  	firstItem interface{},
    82  	actualRangesMap map[byte]string,
    83  	aliasingMap map[string]byte,
    84  ) *BasicByte {
    85  	finalAliasMap := it.generateUppercaseLowercaseAliasMap(
    86  		isIncludeUppercaseLowercase,
    87  		actualRangesMap,
    88  		aliasingMap)
    89  
    90  	return it.CreateUsingMapPlusAliasMap(
    91  		reflect.TypeOf(firstItem).String(),
    92  		actualRangesMap,
    93  		finalAliasMap,
    94  	)
    95  }
    96  
    97  func (it newBasicByteCreator) Create(
    98  	typeName string,
    99  	actualValueRanges []byte,
   100  	stringRanges []string,
   101  	min, max byte,
   102  ) *BasicByte {
   103  	return it.CreateUsingAliasMap(
   104  		typeName,
   105  		actualValueRanges,
   106  		stringRanges,
   107  		nil,
   108  		min,
   109  		max)
   110  }
   111  
   112  // CreateUsingAliasMap
   113  //
   114  // Length : must match stringRanges and actualRangesAnyType
   115  func (it newBasicByteCreator) CreateUsingAliasMap(
   116  	typeName string,
   117  	actualValueRanges []byte,
   118  	stringRanges []string,
   119  	aliasingMap map[string]byte,
   120  	min, max byte,
   121  ) *BasicByte {
   122  	enumBase := newNumberEnumBase(
   123  		typeName,
   124  		actualValueRanges,
   125  		stringRanges,
   126  		min,
   127  		max)
   128  
   129  	jsonDoubleQuoteNameToValueHashMap := make(map[string]byte, len(actualValueRanges))
   130  	valueToJsonDoubleQuoteStringBytesHashmap := make(map[byte][]byte, len(actualValueRanges))
   131  	valueNameHashmap := make(map[byte]string, len(actualValueRanges))
   132  
   133  	for i, actualVal := range actualValueRanges {
   134  		key := stringRanges[i]
   135  		indexJson := toJsonName(i)
   136  		indexString := strconv.Itoa(i)
   137  		jsonName := toJsonName(key)
   138  		jsonDoubleQuoteNameToValueHashMap[jsonName] = actualVal
   139  		jsonDoubleQuoteNameToValueHashMap[key] = actualVal
   140  		jsonDoubleQuoteNameToValueHashMap[indexJson] = actualVal
   141  		jsonDoubleQuoteNameToValueHashMap[indexString] = actualVal
   142  		valueToJsonDoubleQuoteStringBytesHashmap[actualVal] = []byte(jsonName)
   143  		valueNameHashmap[actualVal] = key
   144  	}
   145  
   146  	if len(aliasingMap) > 0 {
   147  		for aliasName, aliasValue := range aliasingMap {
   148  			aliasJsonName := toJsonName(aliasName)
   149  			jsonDoubleQuoteNameToValueHashMap[aliasName] = aliasValue
   150  			jsonDoubleQuoteNameToValueHashMap[aliasJsonName] = aliasValue
   151  		}
   152  	}
   153  
   154  	return &BasicByte{
   155  		numberEnumBase:                           enumBase,
   156  		minVal:                                   min,
   157  		maxVal:                                   max,
   158  		jsonDoubleQuoteNameToValueHashMap:        jsonDoubleQuoteNameToValueHashMap,
   159  		valueToJsonDoubleQuoteStringBytesHashmap: valueToJsonDoubleQuoteStringBytesHashmap,
   160  		valueNameHashmap:                         valueNameHashmap,
   161  	}
   162  }
   163  
   164  func (it newBasicByteCreator) UsingFirstItemSliceAliasMap(
   165  	firstItem interface{},
   166  	indexedSliceWithValues []string,
   167  	aliasingMap map[string]byte,
   168  ) *BasicByte {
   169  	return it.UsingTypeSliceAliasMap(
   170  		reflect.TypeOf(firstItem).String(),
   171  		indexedSliceWithValues,
   172  		aliasingMap)
   173  }
   174  
   175  func (it newBasicByteCreator) UsingFirstItemSliceCaseOptions(
   176  	isIncludeUppercaseLowercase bool, // lowercase, uppercase all
   177  	firstItem interface{},
   178  	indexedSliceWithValues []string,
   179  ) *BasicByte {
   180  	return it.CreateUsingSlicePlusAliasMapOptions(
   181  		isIncludeUppercaseLowercase,
   182  		firstItem,
   183  		indexedSliceWithValues,
   184  		nil)
   185  }
   186  
   187  // UsingFirstItemSliceAllCases
   188  //
   189  //  Includes both cases upper, lower case unmarshalling
   190  func (it newBasicByteCreator) UsingFirstItemSliceAllCases(
   191  	firstItem interface{},
   192  	indexedSliceWithValues []string,
   193  ) *BasicByte {
   194  	return it.CreateUsingSlicePlusAliasMapOptions(
   195  		true,
   196  		firstItem,
   197  		indexedSliceWithValues,
   198  		nil)
   199  }
   200  
   201  func (it newBasicByteCreator) UsingTypeSliceAliasMap(
   202  	typeName string,
   203  	indexedSliceWithValues []string,
   204  	aliasingMap map[string]byte,
   205  ) *BasicByte {
   206  	min := constants.Zero
   207  	max := len(indexedSliceWithValues) - 1
   208  
   209  	actualValues := make([]byte, max+1)
   210  	for i := range indexedSliceWithValues {
   211  		actualValues[i] = byte(i)
   212  	}
   213  
   214  	return it.CreateUsingAliasMap(
   215  		typeName,
   216  		actualValues,
   217  		indexedSliceWithValues,
   218  		aliasingMap, // aliasing map
   219  		byte(min),
   220  		byte(max),
   221  	)
   222  }
   223  
   224  func (it newBasicByteCreator) UsingTypeSlice(
   225  	typeName string,
   226  	indexedSliceWithValues []string,
   227  ) *BasicByte {
   228  	return it.UsingTypeSliceAliasMap(
   229  		typeName,
   230  		indexedSliceWithValues,
   231  		nil, // aliasingMap
   232  	)
   233  }
   234  
   235  func (it newBasicByteCreator) Default(
   236  	firstItem interface{},
   237  	indexedSliceWithValues []string,
   238  ) *BasicByte {
   239  	return it.UsingTypeSliceAliasMap(
   240  		reflect.TypeOf(firstItem).String(),
   241  		indexedSliceWithValues,
   242  		nil, // aliasingMap
   243  	)
   244  }
   245  
   246  func (it newBasicByteCreator) DefaultWithAliasMap(
   247  	firstItem interface{},
   248  	indexedSliceWithValues []string,
   249  	aliasingMap map[string]byte,
   250  ) *BasicByte {
   251  	return it.UsingTypeSliceAliasMap(
   252  		reflect.TypeOf(firstItem).String(),
   253  		indexedSliceWithValues[:],
   254  		aliasingMap, // aliasingMap
   255  	)
   256  }
   257  
   258  // DefaultAllCases
   259  //
   260  //  includes both lowercase and uppercase parsing.
   261  func (it newBasicByteCreator) DefaultAllCases(
   262  	firstItem interface{},
   263  	indexedSliceWithValues []string,
   264  ) *BasicByte {
   265  	return it.CreateUsingSlicePlusAliasMapOptions(
   266  		true,
   267  		firstItem,
   268  		indexedSliceWithValues,
   269  		nil, // aliasingMap
   270  	)
   271  }
   272  
   273  // DefaultWithAliasMapAllCases
   274  //
   275  //  includes both lowercase and uppercase parsing.
   276  func (it newBasicByteCreator) DefaultWithAliasMapAllCases(
   277  	firstItem interface{},
   278  	indexedSliceWithValues []string,
   279  	aliasingMap map[string]byte,
   280  ) *BasicByte {
   281  	return it.CreateUsingSlicePlusAliasMapOptions(
   282  		true,
   283  		firstItem,
   284  		indexedSliceWithValues[:],
   285  		aliasingMap, // aliasingMap
   286  	)
   287  }
   288  
   289  func (it newBasicByteCreator) generateUppercaseLowercaseAliasMap(
   290  	isIncludeUppercaseLowercase bool,
   291  	rangesMap map[byte]string,
   292  	aliasingMap map[string]byte,
   293  ) map[string]byte {
   294  	if !isIncludeUppercaseLowercase {
   295  		return aliasingMap
   296  	}
   297  
   298  	finalAliasMap := make(
   299  		map[string]byte,
   300  		len(rangesMap)*3+len(aliasingMap)*3+2)
   301  
   302  	for keyAsByte, valueAsName := range rangesMap {
   303  		toUpper := strings.ToUpper(valueAsName)
   304  		toLower := strings.ToLower(valueAsName)
   305  		finalAliasMap[toUpper] = keyAsByte
   306  		finalAliasMap[toLower] = keyAsByte
   307  		finalAliasMap[valueAsName] = keyAsByte
   308  	}
   309  
   310  	if len(aliasingMap) == 0 {
   311  		return finalAliasMap
   312  	}
   313  
   314  	for keyAsName, valueAsByte := range aliasingMap {
   315  		toUpper := strings.ToUpper(keyAsName)
   316  		toLower := strings.ToLower(keyAsName)
   317  		finalAliasMap[toUpper] = valueAsByte
   318  		finalAliasMap[toLower] = valueAsByte
   319  		finalAliasMap[keyAsName] = valueAsByte
   320  	}
   321  
   322  	return finalAliasMap
   323  }
   324  
   325  func (it newBasicByteCreator) sliceNamesToMap(
   326  	names []string,
   327  ) map[byte]string {
   328  	newMap := make(
   329  		map[byte]string,
   330  		len(names))
   331  
   332  	for i, name := range names {
   333  		newMap[byte(i)] = name
   334  	}
   335  
   336  	return newMap
   337  }