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

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