gitlab.com/evatix-go/core@v1.3.55/coreimpl/enumimpl/BasicInt8.go (about) 1 package enumimpl 2 3 import ( 4 "fmt" 5 6 "gitlab.com/evatix-go/core/constants" 7 "gitlab.com/evatix-go/core/coreimpl/enumimpl/enumtype" 8 "gitlab.com/evatix-go/core/coreinterface" 9 "gitlab.com/evatix-go/core/defaulterr" 10 "gitlab.com/evatix-go/core/errcore" 11 ) 12 13 type BasicInt8 struct { 14 numberEnumBase 15 jsonDoubleQuoteNameToValueHashMap map[string]int8 // contains names double quotes to value 16 valueToJsonDoubleQuoteStringBytesHashmap map[int8][]byte // contains value to string bytes with double quotes 17 valueNameHashmap map[int8]string // contains name without double quotes 18 minVal, maxVal int8 19 } 20 21 func (it BasicInt8) IsAnyNamesOf( 22 value int8, 23 names ...string, 24 ) bool { 25 currentName := it.ToEnumString(value) 26 27 for _, name := range names { 28 if name == currentName { 29 return true 30 } 31 } 32 33 return false 34 } 35 36 func (it BasicInt8) IsAnyOf( 37 value int8, 38 checkingItems ...int8, 39 ) bool { 40 if len(checkingItems) == 0 { 41 return true 42 } 43 44 for _, givenByte := range checkingItems { 45 if value == givenByte { 46 return true 47 } 48 } 49 50 return false 51 } 52 53 func (it BasicInt8) Max() int8 { 54 return it.maxVal 55 } 56 57 func (it BasicInt8) Min() int8 { 58 return it.minVal 59 } 60 61 func (it BasicInt8) GetValueByString(valueString string) int8 { 62 return it.jsonDoubleQuoteNameToValueHashMap[valueString] 63 } 64 65 func (it BasicInt8) GetValueByName(name string) (int8, error) { 66 v, has := it.jsonDoubleQuoteNameToValueHashMap[name] 67 68 if has { 69 return v, nil 70 } 71 72 wrapped := fmt.Sprintf( 73 constants.SprintDoubleQuoteFormat, 74 name) 75 76 nextVal, isFoundByWrapped := it.jsonDoubleQuoteNameToValueHashMap[wrapped] 77 78 if isFoundByWrapped { 79 return nextVal, nil 80 } 81 82 // has error 83 return constants.InvalidValue, enumUnmarshallingMappingFailedError( 84 it.TypeName(), 85 name, 86 it.RangeNamesCsv()) 87 } 88 89 func (it BasicInt8) ExpectingEnumValueError( 90 rawString string, 91 expectedEnum interface{}, 92 ) error { 93 expectedEnumName := it.ToName(expectedEnum) 94 expectedValue := it.GetValueByString(expectedEnumName) 95 convValue, err := it.GetValueByName(rawString) 96 97 if err != nil { 98 return errcore.ExpectingErrorSimpleNoType( 99 "Expecting enum: "+expectedEnumName, 100 expectedEnumName, 101 rawString+err.Error()) 102 } 103 104 if convValue == expectedValue { 105 return nil 106 } 107 108 return errcore.ExpectingErrorSimpleNoType( 109 "Expecting enum: "+expectedEnumName, 110 expectedEnumName, 111 rawString+it.RangesInvalidMessage()) 112 } 113 114 func (it BasicInt8) GetStringValue(input int8) string { 115 return it.StringRanges()[input] 116 } 117 118 func (it BasicInt8) Ranges() []int8 { 119 return it.actualValueRanges.([]int8) 120 } 121 122 func (it BasicInt8) Hashmap() map[string]int8 { 123 return it.jsonDoubleQuoteNameToValueHashMap 124 } 125 126 func (it BasicInt8) HashmapPtr() *map[string]int8 { 127 return &it.jsonDoubleQuoteNameToValueHashMap 128 } 129 130 func (it BasicInt8) IsValidRange(value int8) bool { 131 return value >= it.minVal && value <= it.maxVal 132 } 133 134 // ToEnumJsonBytes 135 // 136 // used for MarshalJSON from map 137 func (it BasicInt8) ToEnumJsonBytes(value int8) ([]byte, error) { 138 jsonBytes, has := it.valueToJsonDoubleQuoteStringBytesHashmap[value] 139 140 if has { 141 return jsonBytes, nil 142 } 143 144 return []byte{}, it.notFoundJsonBytesError(value) 145 } 146 147 func (it BasicInt8) ToEnumString(value int8) string { 148 return it.valueNameHashmap[value] 149 } 150 151 func (it BasicInt8) AppendPrependJoinValue( 152 joiner string, 153 appendVal, prependVal int8, 154 ) string { 155 return it.ToEnumString(prependVal) + 156 joiner + 157 it.ToEnumString(appendVal) 158 } 159 160 func (it BasicInt8) AppendPrependJoinNamer( 161 joiner string, 162 appendVal, prependVal coreinterface.ToNamer, 163 ) string { 164 return prependVal.Name() + 165 joiner + 166 appendVal.Name() 167 } 168 169 func (it BasicInt8) ToNumberString(valueInRawFormat interface{}) string { 170 return fmt.Sprintf(constants.SprintValueFormat, valueInRawFormat) 171 } 172 173 // UnmarshallToValue Mostly used for UnmarshalJSON 174 // 175 // Given bytes string enum value and transpile to exact enum raw value using map 176 func (it BasicInt8) UnmarshallToValue( 177 isMappedToFirstIfEmpty bool, 178 jsonUnmarshallingValue []byte, 179 ) (int8, error) { 180 if !isMappedToFirstIfEmpty && jsonUnmarshallingValue == nil { 181 return constants.Zero, 182 defaulterr.UnmarshallingFailedDueToNilOrEmpty 183 } 184 185 if isMappedToFirstIfEmpty && jsonUnmarshallingValue == nil { 186 return it.minVal, nil 187 } 188 189 str := string(jsonUnmarshallingValue) 190 if isMappedToFirstIfEmpty && 191 (str == constants.EmptyString || str == constants.DoubleQuotationStartEnd) { 192 return it.minVal, nil 193 } 194 195 return it.GetValueByName(str) 196 } 197 198 func (it BasicInt8) EnumType() enumtype.Variant { 199 return enumtype.Integer8 200 }