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

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