// minInt returns minimum signed integer value for specified bit size
funcminInt(bitsint)int64{
switchbits{
case8:
returnmath.MinInt8
case16:
returnmath.MinInt16
case32:
returnmath.MinInt32
case64:
returnmath.MinInt64
default:
returnmath.MinInt64// fallback
}
}
// maxInt returns maximum signed integer value for specified bit size
funcmaxInt(bitsint)int64{
switchbits{
case8:
returnmath.MaxInt8
case16:
returnmath.MaxInt16
case32:
returnmath.MaxInt32
case64:
returnmath.MaxInt64
default:
returnmath.MaxInt64// fallback
}
}
// toInt converts the given interface value into an int64. The function accepts
// a parameter v of any type and an integer bits representing the size of the
// desired integer type (8, 16, 32 or 64). If successful, it returns the
// converted integer and nil for error. Otherwise, it returns zero for int64 and
// an appropriate error message.
//
// The function checks whether v is nil. If true, it returns an error stating
// "cannot convert nil to int".
//
// It then identifies the type of v using a type switch statement. Depending on
// the type, the function performs different actions:
// - For integer types (int, int8, int16, int32 and int64), it uses checkIntBounds() to ensure the value fits within the specified bits range and returns the result.
// - For unsigned types (uint, uint8, uint16, uint32 and uint64), it checks for overflow before converting to an int64 and calls checkIntBounds().
// - For float types (float32 and float64), it converts them to int64 directly.
// - For string type, it attempts to parse the string as a base-10 integer using strconv.ParseInt() with specified bits range.
//
// If no match is found in these cases or v is of unsupported type, it returns
// an error stating "unsupported type for toInt".
functoInt(vinterface{},bitsint)(int64,error){
ifv==nil{
return0,errors.New("cannot convert nil to int")
}
switchval:=v.(type){
caseint:
returncheckIntBounds(int64(val),bits)
caseint8:
returncheckIntBounds(int64(val),bits)
caseint16:
returncheckIntBounds(int64(val),bits)
caseint32:
returncheckIntBounds(int64(val),bits)
caseint64:
returncheckIntBounds(val,bits)
caseuint,uint8,uint16,uint32,uint64:
u:=reflect.ValueOf(val).Uint()
ifu>uint64(math.MaxInt64){
return0,errors.New("unsigned value overflows int64")
}
returncheckIntBounds(int64(u),bits)
casefloat32:
returncheckIntBounds(int64(val),bits)
casefloat64:
returncheckIntBounds(int64(val),bits)
casestring:
i,err:=strconv.ParseInt(val,10,bits)
iferr!=nil{
return0,err
}
returncheckIntBounds(i,bits)
default:
return0,errors.New("unsupported type for toInt")
}
}
// CheckIntBounds checks if a given int64 value is within the valid range for a specific number of bits.
// It returns an error and 0 if the value is out of bounds, else it returns the input value and nil.
//
// Parameters:
// - v: The integer value to check.
// - bits: The number of bits that determine the valid range for 'v'.
//
// Returns:
// - int64: The original input value if within bounds, else 0.
// - error: An error object if the value is out of bounds; nil otherwise.