diff --git a/pkg/api/accounts.go b/pkg/api/accounts.go index fe5f3066..373eff3e 100644 --- a/pkg/api/accounts.go +++ b/pkg/api/accounts.go @@ -403,7 +403,7 @@ func (a *AccountsApi) AccountDeleteHandler(c *core.Context) (interface{}, *errs. return true, nil } -func (a *AccountsApi) createNewAccountModel(uid int64, accountCreateReq *models.AccountCreateRequest, order int) *models.Account { +func (a *AccountsApi) createNewAccountModel(uid int64, accountCreateReq *models.AccountCreateRequest, order int32) *models.Account { return &models.Account{ Uid: uid, Name: accountCreateReq.Name, @@ -425,7 +425,7 @@ func (a *AccountsApi) createSubAccountModels(uid int64, accountCreateReq *models childrenAccounts := make([]*models.Account, len(accountCreateReq.SubAccounts)) - for i := 0; i < len(accountCreateReq.SubAccounts); i++ { + for i := int32(0); i < int32(len(accountCreateReq.SubAccounts)); i++ { childrenAccounts[i] = a.createNewAccountModel(uid, accountCreateReq.SubAccounts[i], i+1) } diff --git a/pkg/api/transaction_categories.go b/pkg/api/transaction_categories.go index bcd8eb1d..1cd145b7 100644 --- a/pkg/api/transaction_categories.go +++ b/pkg/api/transaction_categories.go @@ -102,7 +102,7 @@ func (a *TransactionCategoriesApi) CategoryCreateHandler(c *core.Context) (inter } } - var maxOrderId int + var maxOrderId int32 if categoryCreateReq.ParentId <= 0 { maxOrderId, err = a.categories.GetMaxDisplayOrder(uid, categoryCreateReq.Type) @@ -143,7 +143,7 @@ func (a *TransactionCategoriesApi) CategoryCreateBatchHandler(c *core.Context) ( uid := c.GetCurrentUid() - categoryTypeMaxOrderMap := make(map[models.TransactionCategoryType]int) + categoryTypeMaxOrderMap := make(map[models.TransactionCategoryType]int32) categoriesMap := make(map[*models.TransactionCategory][]*models.TransactionCategory) categoriesMap[nil] = make([]*models.TransactionCategory, len(categoryCreateBatchReq.Categories)) totalCount := 0 @@ -170,7 +170,7 @@ func (a *TransactionCategoriesApi) CategoryCreateBatchHandler(c *core.Context) ( categoriesMap[category] = make([]*models.TransactionCategory, len(categoryCreateReq.SubCategories)) - for j := 0; j < len(categoryCreateReq.SubCategories); j++ { + for j := int32(0); j < int32(len(categoryCreateReq.SubCategories)); j++ { subCategory := a.createNewCategoryModel(uid, categoryCreateReq.SubCategories[j], j+1) categoriesMap[category][j] = subCategory totalCount++ @@ -325,7 +325,7 @@ func (a *TransactionCategoriesApi) CategoryDeleteHandler(c *core.Context) (inter return true, nil } -func (a *TransactionCategoriesApi) createNewCategoryModel(uid int64, categoryCreateReq *models.TransactionCategoryCreateRequest, order int) *models.TransactionCategory { +func (a *TransactionCategoriesApi) createNewCategoryModel(uid int64, categoryCreateReq *models.TransactionCategoryCreateRequest, order int32) *models.TransactionCategory { return &models.TransactionCategory{ Uid: uid, Name: categoryCreateReq.Name, diff --git a/pkg/api/transaction_tags.go b/pkg/api/transaction_tags.go index b4965dbc..10fa5b6d 100644 --- a/pkg/api/transaction_tags.go +++ b/pkg/api/transaction_tags.go @@ -223,7 +223,7 @@ func (a *TransactionTagsApi) TagDeleteHandler(c *core.Context) (interface{}, *er return true, nil } -func (a *TransactionTagsApi) createNewTagModel(uid int64, tagCreateReq *models.TransactionTagCreateRequest, order int) *models.TransactionTag { +func (a *TransactionTagsApi) createNewTagModel(uid int64, tagCreateReq *models.TransactionTagCreateRequest, order int32) *models.TransactionTag { return &models.TransactionTag{ Uid: uid, Name: tagCreateReq.Name, diff --git a/pkg/api/transactions.go b/pkg/api/transactions.go index e3a58aaf..80a0982a 100644 --- a/pkg/api/transactions.go +++ b/pkg/api/transactions.go @@ -121,7 +121,7 @@ func (a *TransactionsApi) TransactionListHandler(c *core.Context) (interface{}, hasMore := false var nextTimeSequenceId *int64 - if len(transactions) > transactionListReq.Count { + if len(transactions) > int(transactionListReq.Count) { hasMore = true nextTimeSequenceId = &transactions[transactionListReq.Count].TransactionTime transactions = transactions[:transactionListReq.Count] diff --git a/pkg/errs/error.go b/pkg/errs/error.go index 1d78be07..e8a9b5e3 100644 --- a/pkg/errs/error.go +++ b/pkg/errs/error.go @@ -1,7 +1,7 @@ package errs // ErrorCategory represents error category -type ErrorCategory int +type ErrorCategory int32 // Error categories const ( @@ -32,8 +32,8 @@ const ( // Error represents the specific error returned to user type Error struct { Category ErrorCategory - SubCategory int - Index int + SubCategory int32 + Index int32 HttpStatusCode int Message string BaseError []error @@ -45,12 +45,12 @@ func (err *Error) Error() string { } // Code returns the error code -func (err *Error) Code() int { - return int(err.Category)*100000 + err.SubCategory*1000 + err.Index +func (err *Error) Code() int32 { + return int32(err.Category)*100000 + err.SubCategory*1000 + err.Index } // New returns a new error instance -func New(category ErrorCategory, subCategory int, index int, httpStatusCode int, message string, baseError ...error) *Error { +func New(category ErrorCategory, subCategory int32, index int32, httpStatusCode int, message string, baseError ...error) *Error { return &Error{ Category: category, SubCategory: subCategory, @@ -62,12 +62,12 @@ func New(category ErrorCategory, subCategory int, index int, httpStatusCode int, } // NewSystemError returns a new system error instance -func NewSystemError(subCategory int, index int, httpStatusCode int, message string) *Error { +func NewSystemError(subCategory int32, index int32, httpStatusCode int, message string) *Error { return New(CATEGORY_SYSTEM, subCategory, index, httpStatusCode, message) } // NewNormalError returns a new normal error instance -func NewNormalError(subCategory int, index int, httpStatusCode int, message string) *Error { +func NewNormalError(subCategory int32, index int32, httpStatusCode int, message string) *Error { return New(CATEGORY_NORMAL, subCategory, index, httpStatusCode, message) } diff --git a/pkg/middlewares/request_log.go b/pkg/middlewares/request_log.go index 7c573edc..138770cf 100644 --- a/pkg/middlewares/request_log.go +++ b/pkg/middlewares/request_log.go @@ -18,7 +18,7 @@ func RequestLog(c *core.Context) { now := time.Now() statusCode := c.Writer.Status() - errorCode := 0 + errorCode := int32(0) userId := "-" claims := c.GetTokenClaims() diff --git a/pkg/models/account.go b/pkg/models/account.go index 6de46549..d3af4cab 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -55,7 +55,7 @@ type Account struct { Type AccountType `xorm:"NOT NULL"` ParentAccountId int64 `xorm:"INDEX(IDX_account_uid_deleted_parent_account_id_order) NOT NULL"` Name string `xorm:"VARCHAR(32) NOT NULL"` - DisplayOrder int `xorm:"INDEX(IDX_account_uid_deleted_parent_account_id_order) NOT NULL"` + DisplayOrder int32 `xorm:"INDEX(IDX_account_uid_deleted_parent_account_id_order) NOT NULL"` Icon int64 `xorm:"NOT NULL"` Color string `xorm:"VARCHAR(6) NOT NULL"` Currency string `xorm:"VARCHAR(3) NOT NULL"` @@ -116,7 +116,7 @@ type AccountMoveRequest struct { // AccountNewDisplayOrderRequest represents a data pair of id and display order type AccountNewDisplayOrderRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` - DisplayOrder int `json:"displayOrder"` + DisplayOrder int32 `json:"displayOrder"` } // AccountDeleteRequest represents all parameters of account deleting request @@ -136,7 +136,7 @@ type AccountInfoResponse struct { Currency string `json:"currency"` Balance int64 `json:"balance"` Comment string `json:"comment"` - DisplayOrder int `json:"displayOrder"` + DisplayOrder int32 `json:"displayOrder"` IsAsset bool `json:"isAsset,omitempty"` IsLiability bool `json:"isLiability,omitempty"` Hidden bool `json:"hidden"` diff --git a/pkg/models/transaction.go b/pkg/models/transaction.go index 40f13d81..ac5bdb78 100644 --- a/pkg/models/transaction.go +++ b/pkg/models/transaction.go @@ -102,7 +102,7 @@ type TransactionListByMaxTimeRequest struct { Keyword string `form:"keyword"` MaxTime int64 `form:"max_time" binding:"min=0"` MinTime int64 `form:"min_time" binding:"min=0"` - Count int `form:"count" binding:"required,min=1,max=50"` + Count int32 `form:"count" binding:"required,min=1,max=50"` TrimAccount bool `form:"trim_account"` TrimCategory bool `form:"trim_category"` TrimTag bool `form:"trim_tag"` @@ -110,14 +110,14 @@ type TransactionListByMaxTimeRequest struct { // TransactionListInMonthByPageRequest represents all parameters of transaction listing by month request type TransactionListInMonthByPageRequest struct { - Year int `form:"year" binding:"required,min=1"` - Month int `form:"month" binding:"required,min=1"` + Year int32 `form:"year" binding:"required,min=1"` + Month int32 `form:"month" binding:"required,min=1"` Type TransactionDbType `form:"type" binding:"min=0,max=4"` CategoryId int64 `form:"category_id" binding:"min=0"` AccountId int64 `form:"account_id" binding:"min=0"` Keyword string `form:"keyword"` - Page int `form:"page" binding:"required,min=1"` - Count int `form:"count" binding:"required,min=1,max=50"` + Page int32 `form:"page" binding:"required,min=1"` + Count int32 `form:"count" binding:"required,min=1,max=50"` TrimAccount bool `form:"trim_account"` TrimCategory bool `form:"trim_category"` TrimTag bool `form:"trim_tag"` @@ -232,8 +232,8 @@ type TransactionAmountsResponseItem struct { // TransactionMonthAmountsResponseItem represents an item of transaction month amounts type TransactionMonthAmountsResponseItem struct { - Year int `json:"year"` - Month int `json:"month"` + Year int32 `json:"year"` + Month int32 `json:"month"` Amounts []*TransactionAmountsResponseItemAmountInfo `json:"amounts"` } diff --git a/pkg/models/transaction_category.go b/pkg/models/transaction_category.go index 09096228..5d5c37e0 100644 --- a/pkg/models/transaction_category.go +++ b/pkg/models/transaction_category.go @@ -21,7 +21,7 @@ type TransactionCategory struct { Type TransactionCategoryType `xorm:"INDEX(IDX_category_uid_deleted_type_parent_category_id_order) NOT NULL"` ParentCategoryId int64 `xorm:"INDEX(IDX_category_uid_deleted_type_parent_category_id_order) NOT NULL"` Name string `xorm:"VARCHAR(32) NOT NULL"` - DisplayOrder int `xorm:"INDEX(IDX_category_uid_deleted_type_parent_category_id_order) NOT NULL"` + DisplayOrder int32 `xorm:"INDEX(IDX_category_uid_deleted_type_parent_category_id_order) NOT NULL"` Icon int64 `xorm:"NOT NULL"` Color string `xorm:"VARCHAR(6) NOT NULL"` Hidden bool `xorm:"NOT NULL"` @@ -91,7 +91,7 @@ type TransactionCategoryMoveRequest struct { // TransactionCategoryNewDisplayOrderRequest represents a data pair of id and display order type TransactionCategoryNewDisplayOrderRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` - DisplayOrder int `json:"displayOrder"` + DisplayOrder int32 `json:"displayOrder"` } // TransactionCategoryDeleteRequest represents all parameters of transaction category deleting request @@ -108,7 +108,7 @@ type TransactionCategoryInfoResponse struct { Icon int64 `json:"icon,string"` Color string `json:"color"` Comment string `json:"comment"` - DisplayOrder int `json:"displayOrder"` + DisplayOrder int32 `json:"displayOrder"` Hidden bool `json:"hidden"` SubCategories TransactionCategoryInfoResponseSlice `json:"subCategories,omitempty"` } diff --git a/pkg/models/transaction_tag.go b/pkg/models/transaction_tag.go index 9beaac9d..9d3dc86b 100644 --- a/pkg/models/transaction_tag.go +++ b/pkg/models/transaction_tag.go @@ -6,7 +6,7 @@ type TransactionTag struct { Uid int64 `xorm:"INDEX(IDX_tag_uid_deleted_name) NOT NULL"` Deleted bool `xorm:"INDEX(IDX_tag_uid_deleted_name) NOT NULL"` Name string `xorm:"INDEX(IDX_tag_uid_deleted_name) VARCHAR(32) NOT NULL"` - DisplayOrder int `xorm:"NOT NULL"` + DisplayOrder int32 `xorm:"NOT NULL"` Hidden bool `xorm:"NOT NULL"` CreatedUnixTime int64 UpdatedUnixTime int64 @@ -43,7 +43,7 @@ type TransactionTagMoveRequest struct { // TransactionTagNewDisplayOrderRequest represents a data pair of id and display order type TransactionTagNewDisplayOrderRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` - DisplayOrder int `json:"displayOrder"` + DisplayOrder int32 `json:"displayOrder"` } // TransactionTagDeleteRequest represents all parameters of transaction tag deleting request @@ -55,7 +55,7 @@ type TransactionTagDeleteRequest struct { type TransactionTagInfoResponse struct { Id int64 `json:"id,string"` Name string `json:"name"` - DisplayOrder int `json:"displayOrder"` + DisplayOrder int32 `json:"displayOrder"` Hidden bool `json:"hidden"` } diff --git a/pkg/services/accounts.go b/pkg/services/accounts.go index 023cd3b7..1adbedc6 100644 --- a/pkg/services/accounts.go +++ b/pkg/services/accounts.go @@ -96,7 +96,7 @@ func (s *AccountService) GetAccountsByAccountIds(uid int64, accountIds []int64) } // GetMaxDisplayOrder returns the max display order according to account category -func (s *AccountService) GetMaxDisplayOrder(uid int64, category models.AccountCategory) (int, error) { +func (s *AccountService) GetMaxDisplayOrder(uid int64, category models.AccountCategory) (int32, error) { if uid <= 0 { return 0, errs.ErrUserIdInvalid } @@ -116,7 +116,7 @@ func (s *AccountService) GetMaxDisplayOrder(uid int64, category models.AccountCa } // GetMaxSubAccountDisplayOrder returns the max display order of sub account according to account category and parent account id -func (s *AccountService) GetMaxSubAccountDisplayOrder(uid int64, category models.AccountCategory, parentAccountId int64) (int, error) { +func (s *AccountService) GetMaxSubAccountDisplayOrder(uid int64, category models.AccountCategory, parentAccountId int64) (int32, error) { if uid <= 0 { return 0, errs.ErrUserIdInvalid } diff --git a/pkg/services/transaction_categories.go b/pkg/services/transaction_categories.go index d28ddc00..75764adc 100644 --- a/pkg/services/transaction_categories.go +++ b/pkg/services/transaction_categories.go @@ -100,7 +100,7 @@ func (s *TransactionCategoryService) GetCategoriesByCategoryIds(uid int64, categ } // GetMaxDisplayOrder returns the max display order according to transaction category type -func (s *TransactionCategoryService) GetMaxDisplayOrder(uid int64, categoryType models.TransactionCategoryType) (int, error) { +func (s *TransactionCategoryService) GetMaxDisplayOrder(uid int64, categoryType models.TransactionCategoryType) (int32, error) { if uid <= 0 { return 0, errs.ErrUserIdInvalid } @@ -120,7 +120,7 @@ func (s *TransactionCategoryService) GetMaxDisplayOrder(uid int64, categoryType } // GetMaxSubCategoryDisplayOrder returns the max display order of sub transaction category according to transaction category type and parent transaction category id -func (s *TransactionCategoryService) GetMaxSubCategoryDisplayOrder(uid int64, categoryType models.TransactionCategoryType, parentCategoryId int64) (int, error) { +func (s *TransactionCategoryService) GetMaxSubCategoryDisplayOrder(uid int64, categoryType models.TransactionCategoryType, parentCategoryId int64) (int32, error) { if uid <= 0 { return 0, errs.ErrUserIdInvalid } diff --git a/pkg/services/transaction_tags.go b/pkg/services/transaction_tags.go index d1ff3095..cbfcda56 100644 --- a/pkg/services/transaction_tags.go +++ b/pkg/services/transaction_tags.go @@ -85,7 +85,7 @@ func (s *TransactionTagService) GetTagsByTagIds(uid int64, tagIds []int64) (map[ } // GetMaxDisplayOrder returns the max display order -func (s *TransactionTagService) GetMaxDisplayOrder(uid int64) (int, error) { +func (s *TransactionTagService) GetMaxDisplayOrder(uid int64) (int32, error) { if uid <= 0 { return 0, errs.ErrUserIdInvalid } diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index 1c2bf375..80bcdd35 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -33,7 +33,7 @@ var ( ) // GetAllTransactions returns all transactions -func (s *TransactionService) GetAllTransactions(uid int64, pageCount int, noDuplicated bool) ([]*models.Transaction, error) { +func (s *TransactionService) GetAllTransactions(uid int64, pageCount int32, noDuplicated bool) ([]*models.Transaction, error) { maxTransactionTime := utils.GetMaxTransactionTimeFromUnixTime(time.Now().Unix()) var allTransactions []*models.Transaction @@ -46,7 +46,7 @@ func (s *TransactionService) GetAllTransactions(uid int64, pageCount int, noDupl allTransactions = append(allTransactions, transactions...) - if len(transactions) < pageCount { + if len(transactions) < int(pageCount) { maxTransactionTime = 0 break } @@ -58,12 +58,12 @@ func (s *TransactionService) GetAllTransactions(uid int64, pageCount int, noDupl } // GetAllTransactionsByMaxTime returns all transactions before given time -func (s *TransactionService) GetAllTransactionsByMaxTime(uid int64, maxTransactionTime int64, count int, noDuplicated bool) ([]*models.Transaction, error) { +func (s *TransactionService) GetAllTransactionsByMaxTime(uid int64, maxTransactionTime int64, count int32, noDuplicated bool) ([]*models.Transaction, error) { return s.GetTransactionsByMaxTime(uid, maxTransactionTime, 0, 0, nil, nil, "", count, noDuplicated) } // GetTransactionsByMaxTime returns transactions before given time -func (s *TransactionService) GetTransactionsByMaxTime(uid int64, maxTransactionTime int64, minTransactionTime int64, transactionType models.TransactionDbType, categoryIds []int64, accountIds []int64, keyword string, count int, noDuplicated bool) ([]*models.Transaction, error) { +func (s *TransactionService) GetTransactionsByMaxTime(uid int64, maxTransactionTime int64, minTransactionTime int64, transactionType models.TransactionDbType, categoryIds []int64, accountIds []int64, keyword string, count int32, noDuplicated bool) ([]*models.Transaction, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid } @@ -76,13 +76,13 @@ func (s *TransactionService) GetTransactionsByMaxTime(uid int64, maxTransactionT var err error condition, conditionParams := s.getTransactionQueryCondition(uid, maxTransactionTime, minTransactionTime, transactionType, categoryIds, accountIds, keyword, noDuplicated) - err = s.UserDataDB(uid).Where(condition, conditionParams...).Limit(count, 0).OrderBy("transaction_time desc").Find(&transactions) + err = s.UserDataDB(uid).Where(condition, conditionParams...).Limit(int(count), 0).OrderBy("transaction_time desc").Find(&transactions) return transactions, err } // GetTransactionsInMonthByPage returns transactions in given year and month -func (s *TransactionService) GetTransactionsInMonthByPage(uid int64, year int, month int, transactionType models.TransactionDbType, categoryIds []int64, accountIds []int64, keyword string, page int, count int, utcOffset int16) ([]*models.Transaction, error) { +func (s *TransactionService) GetTransactionsInMonthByPage(uid int64, year int32, month int32, transactionType models.TransactionDbType, categoryIds []int64, accountIds []int64, keyword string, page int32, count int32, utcOffset int16) ([]*models.Transaction, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid } @@ -109,7 +109,7 @@ func (s *TransactionService) GetTransactionsInMonthByPage(uid int64, year int, m var transactions []*models.Transaction condition, conditionParams := s.getTransactionQueryCondition(uid, maxTransactionTime, minTransactionTime, transactionType, categoryIds, accountIds, keyword, true) - err = s.UserDataDB(uid).Where(condition, conditionParams...).Limit(count, count*(page-1)).OrderBy("transaction_time desc").Find(&transactions) + err = s.UserDataDB(uid).Where(condition, conditionParams...).Limit(int(count), int(count*(page-1))).OrderBy("transaction_time desc").Find(&transactions) return transactions, err } @@ -142,7 +142,7 @@ func (s *TransactionService) GetAllTransactionCount(uid int64) (int64, error) { } // GetMonthTransactionCount returns total count of transactions in given year and month -func (s *TransactionService) GetMonthTransactionCount(uid int64, year int, month int, transactionType models.TransactionDbType, categoryIds []int64, accountIds []int64, keyword string, utcOffset int16) (int64, error) { +func (s *TransactionService) GetMonthTransactionCount(uid int64, year int32, month int32, transactionType models.TransactionDbType, categoryIds []int64, accountIds []int64, keyword string, utcOffset int16) (int64, error) { if uid <= 0 { return 0, errs.ErrUserIdInvalid } diff --git a/pkg/utils/converter.go b/pkg/utils/converter.go index a8739118..2633b7d5 100644 --- a/pkg/utils/converter.go +++ b/pkg/utils/converter.go @@ -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) diff --git a/pkg/utils/converter_test.go b/pkg/utils/converter_test.go index c410a5a4..7443a23a 100644 --- a/pkg/utils/converter_test.go +++ b/pkg/utils/converter_test.go @@ -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) } diff --git a/pkg/utils/datetimes.go b/pkg/utils/datetimes.go index 2b0bd4e1..70b15c5a 100644 --- a/pkg/utils/datetimes.go +++ b/pkg/utils/datetimes.go @@ -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