gitlab.com/evatix-go/core@v1.3.55/coreimpl/enumimpl/newBasicInt8Creator.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 newBasicInt8Creator struct{} 13 14 func (it newBasicInt8Creator) CreateUsingMap( 15 typeName string, 16 actualRangesMap map[int8]string, 17 ) *BasicInt8 { 18 return it.CreateUsingMapPlusAliasMap( 19 typeName, 20 actualRangesMap, 21 nil, 22 ) 23 } 24 25 func (it newBasicInt8Creator) CreateUsingMapPlusAliasMap( 26 typeName string, 27 actualRangesMap map[int8]string, 28 aliasingMap map[string]int8, 29 ) *BasicInt8 { 30 var min, max int8 31 32 max = math.MinInt8 33 min = math.MaxInt8 34 35 actualValues := make([]int8, 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 newBasicInt8Creator) CreateUsingAliasMap( 68 typeName string, 69 actualValueRanges []int8, 70 stringRanges []string, 71 aliasingMap map[string]int8, 72 min, max int8, 73 ) *BasicInt8 { 74 enumBase := newNumberEnumBase( 75 typeName, 76 actualValueRanges, 77 stringRanges, 78 min, 79 max) 80 81 jsonDoubleQuoteNameToValueHashMap := make(map[string]int8, len(actualValueRanges)) 82 valueToJsonDoubleQuoteStringBytesHashmap := make(map[int8][]byte, len(actualValueRanges)) 83 valueNameHashmap := make(map[int8]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 &BasicInt8{ 107 numberEnumBase: enumBase, 108 minVal: min, 109 maxVal: max, 110 jsonDoubleQuoteNameToValueHashMap: jsonDoubleQuoteNameToValueHashMap, 111 valueToJsonDoubleQuoteStringBytesHashmap: valueToJsonDoubleQuoteStringBytesHashmap, 112 valueNameHashmap: valueNameHashmap, 113 } 114 } 115 116 func (it newBasicInt8Creator) CreateUsingSlicePlusAliasMapOptions( 117 isIncludeUppercaseLowercase bool, // lowercase, uppercase all 118 firstItem interface{}, 119 names []string, 120 aliasingMap map[string]int8, 121 ) *BasicInt8 { 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 newBasicInt8Creator) CreateUsingMapPlusAliasMapOptions( 137 isIncludeUppercaseLowercase bool, // lowercase, uppercase all 138 firstItem interface{}, 139 actualRangesMap map[int8]string, 140 aliasingMap map[string]int8, 141 ) *BasicInt8 { 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 newBasicInt8Creator) UsingFirstItemSliceAliasMap( 155 firstItem interface{}, 156 indexedSliceWithValues []string, 157 aliasingMap map[string]int8, 158 ) *BasicInt8 { 159 return it.UsingTypeSliceAliasMap( 160 reflect.TypeOf(firstItem).String(), 161 indexedSliceWithValues, 162 aliasingMap) 163 } 164 165 func (it newBasicInt8Creator) UsingTypeSliceAliasMap( 166 typeName string, 167 indexedSliceWithValues []string, 168 aliasingMap map[string]int8, 169 ) *BasicInt8 { 170 min := constants.Zero 171 max := len(indexedSliceWithValues) - 1 172 173 actualValues := make([]int8, max+1) 174 for i := range indexedSliceWithValues { 175 actualValues[i] = int8(i) 176 } 177 178 return it.CreateUsingAliasMap( 179 typeName, 180 actualValues, 181 indexedSliceWithValues, 182 aliasingMap, // aliasing map 183 int8(min), 184 int8(max), 185 ) 186 } 187 188 func (it newBasicInt8Creator) UsingTypeSlice( 189 typeName string, 190 indexedSliceWithValues []string, 191 ) *BasicInt8 { 192 return it.UsingTypeSliceAliasMap( 193 typeName, 194 indexedSliceWithValues, 195 nil, // aliasingMap 196 ) 197 } 198 199 func (it newBasicInt8Creator) Default( 200 firstItem interface{}, 201 indexedSliceWithValues []string, 202 ) *BasicInt8 { 203 return it.UsingTypeSliceAliasMap( 204 reflect.TypeOf(firstItem).String(), 205 indexedSliceWithValues, 206 nil, // aliasingMap 207 ) 208 } 209 210 func (it newBasicInt8Creator) DefaultWithAliasMap( 211 firstItem interface{}, 212 indexedSliceWithValues []string, 213 aliasingMap map[string]int8, 214 ) *BasicInt8 { 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 newBasicInt8Creator) DefaultAllCases( 226 firstItem interface{}, 227 indexedSliceWithValues []string, 228 ) *BasicInt8 { 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 newBasicInt8Creator) DefaultWithAliasMapAllCases( 241 firstItem interface{}, 242 indexedSliceWithValues []string, 243 aliasingMap map[string]int8, 244 ) *BasicInt8 { 245 return it.CreateUsingSlicePlusAliasMapOptions( 246 true, 247 firstItem, 248 indexedSliceWithValues[:], 249 aliasingMap, // aliasingMap 250 ) 251 } 252 253 func (it newBasicInt8Creator) generateUppercaseLowercaseAliasMap( 254 isIncludeUppercaseLowercase bool, 255 rangesMap map[int8]string, 256 aliasingMap map[string]int8, 257 ) map[string]int8 { 258 if !isIncludeUppercaseLowercase { 259 return aliasingMap 260 } 261 262 finalAliasMap := make( 263 map[string]int8, 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 newBasicInt8Creator) sliceNamesToMap( 290 names []string, 291 ) map[int8]string { 292 newMap := make( 293 map[int8]string, 294 len(names)) 295 296 for i, name := range names { 297 newMap[int8(i)] = name 298 } 299 300 return newMap 301 }