Skip to content
Snippets Groups Projects
Select Git revision
  • feature/v1.3.60
  • develop default protected
  • main protected
  • release/v1.0.7
  • release/v1.0.0
  • feature/v0.9.5
  • release/v0.6.2
  • hotfix/v0.5.7
  • release/v0.5.6
  • release/v0.5.5
  • hotfix/v0.5.4
  • hotfix/v0.5.3
  • hotfix/v0.5.2
  • release/v0.5.1
  • hotfix/v0.5.0
  • hotfix/v0.4.9
  • hotfix/v0.4.8
  • hotfix/v0.4.7
  • release/v0.4.6
  • hotfix/v0.4.5
  • v1.3.55
  • v1.3.52
  • v1.3.50
  • v1.3.45
  • v1.3.42
  • v1.3.41
  • v1.3.40
  • v1.3.38
  • v1.3.37
  • v1.3.36
  • v1.3.35
  • v1.3.34
  • v1.3.33
  • v1.3.32
  • v1.3.31
  • v1.3.30
  • v1.3.29
  • v1.3.28
  • v1.3.27
  • v1.3.26
40 results

BasicInt8.go

  • Md. Alim Ul Karim's avatar
    43e01a83
    Merge branch 'featue/improvements-v4' into 'develop' · 43e01a83
    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
    
    ![image](/uploads/4847fcac93a4dfd385b2c0a85cdb8a7f/image.png)
    
    - [x] Run `Go Install` on root
    
    ![image](/uploads/7cb48e3f9f02b3dafc6d60c08c6bae04/image.png)
    
    - [x] Run `go tests/test` successfully [no failing tests]
    
    ![image](/uploads/cb64dfa3080008d73d5c81f26b3d8a88/image.png)
    - [x] Run `go run cmd/main/main.go` successfully [no build fail]
    
    ![image](/uploads/5ba43615da38078163b1593a1c67fd1b/image.png)
    
    See merge request !48
    43e01a83
    History
    Merge branch 'featue/improvements-v4' into 'develop'
    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
    
    ![image](/uploads/4847fcac93a4dfd385b2c0a85cdb8a7f/image.png)
    
    - [x] Run `Go Install` on root
    
    ![image](/uploads/7cb48e3f9f02b3dafc6d60c08c6bae04/image.png)
    
    - [x] Run `go tests/test` successfully [no failing tests]
    
    ![image](/uploads/cb64dfa3080008d73d5c81f26b3d8a88/image.png)
    - [x] Run `go run cmd/main/main.go` successfully [no build fail]
    
    ![image](/uploads/5ba43615da38078163b1593a1c67fd1b/image.png)
    
    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
}