use unambiguous numeric variable type
This commit is contained in:
+18
-7
@@ -2,20 +2,20 @@ package utils
|
||||
|
||||
import "strconv"
|
||||
|
||||
// Int32ToString returns the textual representation of this number
|
||||
func Int32ToString(num int) string {
|
||||
// IntToString returns the textual representation of this number
|
||||
func IntToString(num int) string {
|
||||
return strconv.Itoa(num)
|
||||
}
|
||||
|
||||
// StringToInt32 parses a textual representation of the number to int32
|
||||
func StringToInt32(str string) (int, error) {
|
||||
// StringToInt parses a textual representation of the number to int
|
||||
func StringToInt(str string) (int, error) {
|
||||
return strconv.Atoi(str)
|
||||
}
|
||||
|
||||
// StringTryToInt32 parses a textual representation of the number to int32 if str is valid,
|
||||
// StringTryToInt parses a textual representation of the number to int if str is valid,
|
||||
// or returns the default value
|
||||
func StringTryToInt32(str string, defaultValue int) int {
|
||||
num, err := StringToInt32(str)
|
||||
func StringTryToInt(str string, defaultValue int) int {
|
||||
num, err := StringToInt(str)
|
||||
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
@@ -24,6 +24,17 @@ func StringTryToInt32(str string, defaultValue int) int {
|
||||
return num
|
||||
}
|
||||
|
||||
// StringToInt32 parses a textual representation of the number to int32
|
||||
func StringToInt32(str string) (int32, error) {
|
||||
val, err := strconv.ParseInt(str, 10, 32)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return int32(val), nil
|
||||
}
|
||||
|
||||
// Int64ToString returns the textual representation of this number
|
||||
func Int64ToString(num int64) string {
|
||||
return strconv.FormatInt(num, 10)
|
||||
|
||||
@@ -6,19 +6,42 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestInt32ToString(t *testing.T) {
|
||||
func TestIntToString(t *testing.T) {
|
||||
expectedValue := "-123456789"
|
||||
actualValue := Int32ToString(-123456789)
|
||||
actualValue := IntToString(-123456789)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
}
|
||||
|
||||
func TestStringToInt32(t *testing.T) {
|
||||
func TestStringToInt(t *testing.T) {
|
||||
expectedValue := -123456789
|
||||
actualValue, err := StringToInt("-123456789")
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
}
|
||||
|
||||
func TestStringToInt_InvalidNumber(t *testing.T) {
|
||||
_, err := StringToInt("")
|
||||
assert.NotEqual(t, nil, err)
|
||||
|
||||
_, err = StringToInt("null")
|
||||
assert.NotEqual(t, nil, err)
|
||||
}
|
||||
|
||||
func TestStringToInt32(t *testing.T) {
|
||||
expectedValue := int32(-123456789)
|
||||
actualValue, err := StringToInt32("-123456789")
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
}
|
||||
|
||||
func TestStringToInt32_OutOfRange(t *testing.T) {
|
||||
_, err := StringToInt32("2147483648")
|
||||
assert.NotEqual(t, nil, err)
|
||||
|
||||
_, err = StringToInt32("-2147483649")
|
||||
assert.NotEqual(t, nil, err)
|
||||
}
|
||||
|
||||
func TestStringToInt32_InvalidNumber(t *testing.T) {
|
||||
_, err := StringToInt32("")
|
||||
assert.NotEqual(t, nil, err)
|
||||
@@ -29,10 +52,10 @@ func TestStringToInt32_InvalidNumber(t *testing.T) {
|
||||
|
||||
func TestStringTryToInt32_InvalidNumber(t *testing.T) {
|
||||
expectedValue := -1
|
||||
actualValue := StringTryToInt32("", -1)
|
||||
actualValue := StringTryToInt("", -1)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
actualValue = StringTryToInt32("null", -1)
|
||||
actualValue = StringTryToInt("null", -1)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
}
|
||||
|
||||
|
||||
@@ -102,13 +102,13 @@ func ParseFromTimezoneOffset(tzOffset string) (*time.Location, error) {
|
||||
return nil, errs.ErrFormatInvalid
|
||||
}
|
||||
|
||||
hourAbsOffset, err := StringToInt32(offsets[0])
|
||||
hourAbsOffset, err := StringToInt(offsets[0])
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
minuteAbsOffset, err := StringToInt32(offsets[1])
|
||||
minuteAbsOffset, err := StringToInt(offsets[1])
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user