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