gitlab.com/evatix-go/core@v1.3.55/coreimpl/enumimpl/ConvAnyValToInteger.go (about)

     1  package enumimpl
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"gitlab.com/evatix-go/core/constants"
     8  )
     9  
    10  func ConvEnumAnyValToInteger(val interface{}) int {
    11  	_, isStr := val.(string)
    12  
    13  	if isStr {
    14  		// already a string represents string type enum
    15  		return constants.MinInt
    16  	}
    17  
    18  	valInt, isInt := val.(int)
    19  
    20  	if isInt {
    21  		return valInt
    22  	}
    23  
    24  	switch casted := val.(type) {
    25  	case valueByter:
    26  		return int(casted.Value())
    27  	case exactValueByter:
    28  		return int(casted.ValueByte())
    29  	case valueInter:
    30  		return casted.Value()
    31  	case exactValueInter:
    32  		return casted.ValueInt()
    33  	case valueInt8er:
    34  		return int(casted.Value())
    35  	case exactValueInt8er:
    36  		return int(casted.ValueInt8())
    37  	case valueUInt16er:
    38  		return int(casted.Value())
    39  	case exactValueUInt16er:
    40  		return int(casted.ValueUInt16())
    41  	}
    42  
    43  	str := fmt.Sprintf(
    44  		constants.SprintValueFormat,
    45  		val)
    46  
    47  	convValueInt, err := strconv.Atoi(str)
    48  
    49  	if err != nil {
    50  		return constants.MinInt
    51  	}
    52  
    53  	return convValueInt
    54  }