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

     1  package enumimpl
     2  
     3  import (
     4  	"reflect"
     5  	"strconv"
     6  
     7  	"gitlab.com/evatix-go/core/constants"
     8  )
     9  
    10  type newBasicUInt16Creator struct{}
    11  
    12  func (it newBasicUInt16Creator) CreateUsingMap(
    13  	typeName string,
    14  	actualRangesMap map[uint16]string,
    15  ) *BasicUInt16 {
    16  	return it.CreateUsingMapPlusAliasMap(
    17  		typeName,
    18  		actualRangesMap,
    19  		nil,
    20  	)
    21  }
    22  
    23  func (it newBasicUInt16Creator) CreateUsingMapPlusAliasMap(
    24  	typeName string,
    25  	actualRangesMap map[uint16]string,
    26  	aliasingMap map[string]uint16,
    27  ) *BasicUInt16 {
    28  	var min, max uint16
    29  	actualValues := make([]uint16, len(actualRangesMap))
    30  	actualNames := make([]string, len(actualRangesMap))
    31  
    32  	index := 0
    33  	for val, name := range actualRangesMap {
    34  		actualValues[index] = val
    35  		actualNames[index] = name
    36  
    37  		if max < val {
    38  			max = val
    39  		}
    40  
    41  		index++
    42  	}
    43  
    44  	return it.CreateUsingAliasMap(
    45  		typeName,
    46  		actualValues,
    47  		actualNames,
    48  		aliasingMap, // aliasing map
    49  		min,         // zero
    50  		max,
    51  	)
    52  }
    53  
    54  // CreateUsingAliasMap
    55  //
    56  // Length : must match stringRanges and actualRangesAnyType
    57  func (it newBasicUInt16Creator) CreateUsingAliasMap(
    58  	typeName string,
    59  	actualValueRanges []uint16,
    60  	stringRanges []string,
    61  	aliasingMap map[string]uint16,
    62  	min, max uint16,
    63  ) *BasicUInt16 {
    64  	enumBase := newNumberEnumBase(
    65  		typeName,
    66  		actualValueRanges,
    67  		stringRanges,
    68  		min,
    69  		max)
    70  
    71  	jsonDoubleQuoteNameToValueHashMap := make(map[string]uint16, len(actualValueRanges))
    72  	valueToJsonDoubleQuoteStringBytesHashmap := make(map[uint16][]byte, len(actualValueRanges))
    73  	valueNameHashmap := make(map[uint16]string, len(actualValueRanges))
    74  
    75  	for i, actualVal := range actualValueRanges {
    76  		key := stringRanges[i]
    77  		indexJson := toJsonName(i)
    78  		indexString := strconv.Itoa(i)
    79  		jsonName := toJsonName(key)
    80  		jsonDoubleQuoteNameToValueHashMap[jsonName] = actualVal
    81  		jsonDoubleQuoteNameToValueHashMap[key] = actualVal
    82  		jsonDoubleQuoteNameToValueHashMap[indexJson] = actualVal
    83  		jsonDoubleQuoteNameToValueHashMap[indexString] = actualVal
    84  		valueToJsonDoubleQuoteStringBytesHashmap[actualVal] = []byte(jsonName)
    85  		valueNameHashmap[actualVal] = key
    86  	}
    87  
    88  	if len(aliasingMap) > 0 {
    89  		for aliasName, aliasValue := range aliasingMap {
    90  			aliasJsonName := toJsonName(aliasName)
    91  			jsonDoubleQuoteNameToValueHashMap[aliasName] = aliasValue
    92  			jsonDoubleQuoteNameToValueHashMap[aliasJsonName] = aliasValue
    93  		}
    94  	}
    95  
    96  	return &BasicUInt16{
    97  		numberEnumBase:                           enumBase,
    98  		minVal:                                   min,
    99  		maxVal:                                   max,
   100  		jsonDoubleQuoteNameToValueHashMap:        jsonDoubleQuoteNameToValueHashMap,
   101  		valueToJsonDoubleQuoteStringBytesHashmap: valueToJsonDoubleQuoteStringBytesHashmap,
   102  		valueNameHashmap:                         valueNameHashmap,
   103  	}
   104  }
   105  
   106  func (it newBasicUInt16Creator) UsingFirstItemSliceAliasMap(
   107  	firstItem interface{},
   108  	indexedSliceWithValues []string,
   109  	aliasingMap map[string]uint16,
   110  ) *BasicUInt16 {
   111  	return it.UsingTypeSliceAliasMap(
   112  		reflect.TypeOf(firstItem).String(),
   113  		indexedSliceWithValues,
   114  		aliasingMap)
   115  }
   116  
   117  func (it newBasicUInt16Creator) UsingTypeSliceAliasMap(
   118  	typeName string,
   119  	indexedSliceWithValues []string,
   120  	aliasingMap map[string]uint16,
   121  ) *BasicUInt16 {
   122  	min := constants.Zero
   123  	max := len(indexedSliceWithValues) - 1
   124  
   125  	actualValues := make([]uint16, max+1)
   126  	for i := range indexedSliceWithValues {
   127  		actualValues[i] = uint16(i)
   128  	}
   129  
   130  	return it.CreateUsingAliasMap(
   131  		typeName,
   132  		actualValues,
   133  		indexedSliceWithValues,
   134  		aliasingMap, // aliasing map
   135  		uint16(min),
   136  		uint16(max),
   137  	)
   138  }
   139  
   140  func (it newBasicUInt16Creator) UsingTypeSlice(
   141  	typeName string,
   142  	indexedSliceWithValues []string,
   143  ) *BasicUInt16 {
   144  	return it.UsingTypeSliceAliasMap(
   145  		typeName,
   146  		indexedSliceWithValues,
   147  		nil, // aliasingMap
   148  	)
   149  }
   150  
   151  func (it newBasicUInt16Creator) Default(
   152  	firstItem interface{},
   153  	indexedSliceWithValues []string,
   154  ) *BasicUInt16 {
   155  	return it.UsingTypeSliceAliasMap(
   156  		reflect.TypeOf(firstItem).String(),
   157  		indexedSliceWithValues,
   158  		nil, // aliasingMap
   159  	)
   160  }
   161  
   162  func (it newBasicUInt16Creator) DefaultWithAliasMap(
   163  	firstItem interface{},
   164  	indexedSliceWithValues []string,
   165  	aliasingMap map[string]uint16,
   166  ) *BasicUInt16 {
   167  	return it.UsingTypeSliceAliasMap(
   168  		reflect.TypeOf(firstItem).String(),
   169  		indexedSliceWithValues[:],
   170  		aliasingMap, // aliasingMap
   171  	)
   172  }