Select Git revision
-
Md. Alim Ul Karim authored
"Enums improved" "Request Type" ## Introduction "Enums improved" "Request Type" Note: [Template Reference](https://hackmd.io/@akarimevatix/H1Qsmq23w) ## Worked on Items - [x] "Enums improved" - [x] "Request Type" ## TODOs - [ ] .... ## References - N/A ## Notes - N/A ## Performed full package - [x] Code reformat ## Analysis CheckList - [x] Run + add screenshot of CodeInspect  - [x] Run `Go Install` on root  - [x] Run `go tests/test` successfully [no failing tests]  - [x] Run `go run cmd/main/main.go` successfully [no build fail]  See merge request !48
Md. Alim Ul Karim authored"Enums improved" "Request Type" ## Introduction "Enums improved" "Request Type" Note: [Template Reference](https://hackmd.io/@akarimevatix/H1Qsmq23w) ## Worked on Items - [x] "Enums improved" - [x] "Request Type" ## TODOs - [ ] .... ## References - N/A ## Notes - N/A ## Performed full package - [x] Code reformat ## Analysis CheckList - [x] Run + add screenshot of CodeInspect  - [x] Run `Go Install` on root  - [x] Run `go tests/test` successfully [no failing tests]  - [x] Run `go run cmd/main/main.go` successfully [no build fail]  See merge request !48
BasicInt8.go 2.97 KiB
package enumimpl
import (
"fmt"
"gitlab.com/evatix-go/core/constants"
"gitlab.com/evatix-go/core/converters"
"gitlab.com/evatix-go/core/defaulterr"
"gitlab.com/evatix-go/core/msgtype"
)
type BasicInt8 struct {
*numberEnumBase
hashMap map[string]int8
jsonBytesHashmap map[int8][]byte
minVal, maxVal int8
}
func NewBasicInt8(
actualValueRanges []int8,
stringRanges []string,
min, max int8,
) *BasicInt8 {
enumBase := newNumberEnumBase(
actualValueRanges,
stringRanges,
min,
max)
hashMap := make(map[string]int8, len(actualValueRanges))
jsonBytesHashmap := make(map[int8][]byte, len(actualValueRanges))
for i, actualVal := range actualValueRanges {
key := stringRanges[i]
hashMap[key] = actualVal
jsonBytesHashmap[actualVal] = []byte(key)
}
return &BasicInt8{
numberEnumBase: enumBase,
minVal: min,
maxVal: max,
hashMap: hashMap,
jsonBytesHashmap: jsonBytesHashmap,
}
}
func (receiver *BasicInt8) IsAnyOf(value int8, checkingItems ...int8) bool {
if len(checkingItems) == 0 {
return true
}
for _, givenByte := range checkingItems {
if value == givenByte {
return true
}
}
return false
}
func (receiver *BasicInt8) Max() int8 {
return receiver.maxVal
}
func (receiver *BasicInt8) Min() int8 {
return receiver.minVal
}
func (receiver *BasicInt8) GetValueByString(valueString string) int8 {
return receiver.hashMap[valueString]
}
func (receiver *BasicInt8) GetStringValue(input int8) string {
return receiver.StringRanges()[input]
}
func (receiver *BasicInt8) Ranges() []int8 {
return receiver.actualValueRanges.([]int8)
}
func (receiver *BasicInt8) Hashmap() map[string]int8 {
return receiver.hashMap
}
func (receiver *BasicInt8) HashmapPtr() *map[string]int8 {
return &receiver.hashMap
}
func (receiver *BasicInt8) IsValidRange(value int8) bool {
return value >= receiver.minVal && value <= receiver.maxVal
}
// ToEnumJsonBytes used for MarshalJSON from map
func (receiver *BasicInt8) ToEnumJsonBytes(value int8) []byte {
return receiver.jsonBytesHashmap[value]
}
func (receiver *BasicInt8) ToEnumString(value int8) string {
return *converters.UnsafeBytesToStringPtr(receiver.jsonBytesHashmap[value])
}
func (receiver *BasicInt8) ToNumberString(valueInRawFormat interface{}) string {
return fmt.Sprintf(constants.SprintValueFormat, valueInRawFormat)
}
// UnmarshallEnumToValue Mostly used for UnmarshalJSON
//
// Given bytes string enum value and transpile to exact enum raw value using map
func (receiver *BasicInt8) UnmarshallEnumToValue(
jsonUnmarshallingValue []byte,
) (int8, error) {
if jsonUnmarshallingValue == nil {
return constants.Zero,
defaulterr.UnMarshallingFailedDueToNilOrEmpty
}
str := string(jsonUnmarshallingValue)
v, has := receiver.hashMap[str]
if !has {
return constants.Zero, msgtype.MeaningFulErrorWithData(
msgtype.UnMarshallingFailed,
"UnmarshallEnumToValue",
defaulterr.UnMarshallingPlusCannotFindingEnumMap,
string(jsonUnmarshallingValue))
}
return v, nil
}