From 024960f7671a2b5f0fc6d1df701aeac60696f5cb Mon Sep 17 00:00:00 2001 From: MaysWind Date: Tue, 22 Dec 2020 22:59:50 +0800 Subject: [PATCH] format code --- pkg/api/authorizations.go | 6 +++--- pkg/api/default.go | 2 +- pkg/api/exchange_rates.go | 4 ++-- pkg/api/transaction_categories.go | 8 ++++---- pkg/api/transaction_tags.go | 6 +++--- pkg/api/transactions.go | 4 ++-- pkg/api/users.go | 20 ++++++++++---------- pkg/datastore/datastore.go | 4 ++-- pkg/datastore/datastore_container.go | 4 ++-- pkg/datastore/query_log.go | 4 ++-- pkg/log/formatter.go | 2 +- pkg/middlewares/request_id.go | 2 +- pkg/models/transaction_tag.go | 4 ++-- pkg/services/tokens.go | 22 +++++++++++----------- pkg/services/transaction_categories.go | 4 ++-- pkg/services/transaction_tags.go | 2 +- pkg/services/transactions.go | 10 +++++----- pkg/services/twofactor_authorizations.go | 2 +- pkg/utils/converter.go | 2 +- pkg/utils/datetimes.go | 2 +- pkg/utils/regexps.go | 4 ++-- pkg/utils/strings.go | 2 +- pkg/validators/currency.go | 2 +- 23 files changed, 61 insertions(+), 61 deletions(-) diff --git a/pkg/api/authorizations.go b/pkg/api/authorizations.go index c492ded1..b745817b 100644 --- a/pkg/api/authorizations.go +++ b/pkg/api/authorizations.go @@ -187,10 +187,10 @@ func (a *AuthorizationsApi) TwoFactorAuthorizeByRecoveryCodeHandler(c *core.Cont return authResp, nil } -func (a *AuthorizationsApi) getAuthResponse(token string, need2FA bool, user *models.User) (*models.AuthResponse) { +func (a *AuthorizationsApi) getAuthResponse(token string, need2FA bool, user *models.User) *models.AuthResponse { return &models.AuthResponse{ - Token : token, + Token: token, Need2FA: need2FA, - User: user.ToUserBasicInfo(), + User: user.ToUserBasicInfo(), } } diff --git a/pkg/api/default.go b/pkg/api/default.go index ae45bb17..97b8ef66 100644 --- a/pkg/api/default.go +++ b/pkg/api/default.go @@ -5,7 +5,7 @@ import ( "github.com/mayswind/lab/pkg/errs" ) -type DefaultApi struct {} +type DefaultApi struct{} var ( Default = &DefaultApi{} diff --git a/pkg/api/exchange_rates.go b/pkg/api/exchange_rates.go index abeb2570..6c071648 100644 --- a/pkg/api/exchange_rates.go +++ b/pkg/api/exchange_rates.go @@ -13,7 +13,7 @@ import ( const EuroCentralBankExchangeRateUrl = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml" -type ExchangeRatesApi struct {} +type ExchangeRatesApi struct{} var ( ExchangeRates = &ExchangeRatesApi{} @@ -52,7 +52,7 @@ func (a *ExchangeRatesApi) LatestExchangeRateHandler(c *core.Context) (interface latestExchangeRateResponse.ExchangeRates = append(latestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{ Currency: "EUR", - Rate: "1", + Rate: "1", }) return latestExchangeRateResponse, nil diff --git a/pkg/api/transaction_categories.go b/pkg/api/transaction_categories.go index a277c52d..89b62c73 100644 --- a/pkg/api/transaction_categories.go +++ b/pkg/api/transaction_categories.go @@ -156,9 +156,9 @@ func (a *TransactionCategoriesApi) CategoryCreateBatchHandler(c *core.Context) ( } category := a.createNewCategoryModel(uid, &models.TransactionCategoryCreateRequest{ - Name: categoryCreateReq.Name, - Type: categoryCreateReq.Type, - Icon: categoryCreateReq.Icon, + Name: categoryCreateReq.Name, + Type: categoryCreateReq.Type, + Icon: categoryCreateReq.Icon, Color: categoryCreateReq.Color, }, maxOrderId+1) @@ -171,7 +171,7 @@ func (a *TransactionCategoriesApi) CategoryCreateBatchHandler(c *core.Context) ( } categoriesMap[nil][i] = category - categoryTypeMaxOrderMap[categoryCreateReq.Type] = maxOrderId+1 + categoryTypeMaxOrderMap[categoryCreateReq.Type] = maxOrderId + 1 totalCount++ } diff --git a/pkg/api/transaction_tags.go b/pkg/api/transaction_tags.go index de0f9262..ea0777fd 100644 --- a/pkg/api/transaction_tags.go +++ b/pkg/api/transaction_tags.go @@ -114,9 +114,9 @@ func (a *TransactionTagsApi) TagModifyHandler(c *core.Context) (interface{}, *er } newTag := &models.TransactionTag{ - TagId: tag.TagId, - Uid: uid, - Name: tagModifyReq.Name, + TagId: tag.TagId, + Uid: uid, + Name: tagModifyReq.Name, } if newTag.Name == tag.Name { diff --git a/pkg/api/transactions.go b/pkg/api/transactions.go index 399eab2c..b3ef8a1d 100644 --- a/pkg/api/transactions.go +++ b/pkg/api/transactions.go @@ -33,7 +33,7 @@ func (a *TransactionsApi) TransactionListHandler(c *core.Context) (interface{}, } uid := c.GetCurrentUid() - transactions, err := a.transactions.GetTransactionsByMaxTime(uid, transactionListReq.MaxTime, transactionListReq.Count + 1) + transactions, err := a.transactions.GetTransactionsByMaxTime(uid, transactionListReq.MaxTime, transactionListReq.Count+1) if err != nil { log.ErrorfWithRequestId(c, "[transactions.TransactionListHandler] failed to get transactions earlier than \"%d\" for user \"uid:%d\", because %s", transactionListReq.MaxTime, uid, err.Error()) @@ -236,7 +236,7 @@ func (a *TransactionsApi) TransactionModifyHandler(c *core.Context) (interface{} } if newTransaction.CategoryId == transaction.CategoryId && - newTransaction.TransactionTime / 1000 == transaction.TransactionTime / 1000 && + newTransaction.TransactionTime/1000 == transaction.TransactionTime/1000 && newTransaction.SourceAccountId == transaction.SourceAccountId && newTransaction.DestinationAccountId == transaction.DestinationAccountId && newTransaction.SourceAmount == transaction.SourceAmount && diff --git a/pkg/api/users.go b/pkg/api/users.go index 6f5459fd..f2f47865 100644 --- a/pkg/api/users.go +++ b/pkg/api/users.go @@ -12,13 +12,13 @@ import ( ) type UsersApi struct { - users *services.UserService + users *services.UserService tokens *services.TokenService } var ( Users = &UsersApi{ - users: services.Users, + users: services.Users, tokens: services.Tokens, } ) @@ -37,10 +37,10 @@ func (a *UsersApi) UserRegisterHandler(c *core.Context) (interface{}, *errs.Erro userRegisterReq.Nickname = strings.TrimSpace(userRegisterReq.Nickname) user := &models.User{ - Username: userRegisterReq.Username, - Email: userRegisterReq.Email, - Nickname: userRegisterReq.Nickname, - Password: userRegisterReq.Password, + Username: userRegisterReq.Username, + Email: userRegisterReq.Email, + Nickname: userRegisterReq.Nickname, + Password: userRegisterReq.Password, DefaultCurrency: userRegisterReq.DefaultCurrency, } @@ -55,7 +55,7 @@ func (a *UsersApi) UserRegisterHandler(c *core.Context) (interface{}, *errs.Erro authResp := &models.AuthResponse{ Need2FA: false, - User: user.ToUserBasicInfo(), + User: user.ToUserBasicInfo(), } token, claims, err := a.tokens.CreateToken(user, c) @@ -114,8 +114,8 @@ func (a *UsersApi) UserUpdateProfileHandler(c *core.Context) (interface{}, *errs anythingUpdate := false userNew := &models.User{ - Uid: user.Uid, - Salt: user.Salt, + Uid: user.Uid, + Salt: user.Salt, Rands: user.Rands, } @@ -161,7 +161,7 @@ func (a *UsersApi) UserUpdateProfileHandler(c *core.Context) (interface{}, *errs log.InfofWithRequestId(c, "[users.UserUpdateProfileHandler] user \"uid:%d\" has updated successfully", user.Uid) - resp := &models.UserProfileUpdateResponse { + resp := &models.UserProfileUpdateResponse{ User: user.ToUserBasicInfo(), } diff --git a/pkg/datastore/datastore.go b/pkg/datastore/datastore.go index e10a802b..1d9fa6ae 100644 --- a/pkg/datastore/datastore.go +++ b/pkg/datastore/datastore.go @@ -22,7 +22,7 @@ func (s *DataStore) DoTransaction(key int64, fn func(sess *xorm.Session) error) return s.Choose(key).DoTransaction(fn) } -func (s *DataStore) SyncStructs(beans... interface{}) error { +func (s *DataStore) SyncStructs(beans ...interface{}) error { var err error for i := 0; i < len(s.databases); i++ { @@ -36,7 +36,7 @@ func (s *DataStore) SyncStructs(beans... interface{}) error { return err } -func NewDataStore(databases... *Database) (*DataStore, error) { +func NewDataStore(databases ...*Database) (*DataStore, error) { if len(databases) < 1 { return nil, errs.ErrDatabaseIsNull } diff --git a/pkg/datastore/datastore_container.go b/pkg/datastore/datastore_container.go index 9d64b664..86b05ea6 100644 --- a/pkg/datastore/datastore_container.go +++ b/pkg/datastore/datastore_container.go @@ -17,8 +17,8 @@ import ( ) type DataStoreContainer struct { - UserStore *DataStore - TokenStore *DataStore + UserStore *DataStore + TokenStore *DataStore UserDataStore *DataStore } diff --git a/pkg/datastore/query_log.go b/pkg/datastore/query_log.go index 66ba4c6d..c8be8fa2 100644 --- a/pkg/datastore/query_log.go +++ b/pkg/datastore/query_log.go @@ -8,7 +8,7 @@ import ( ) type XOrmLoggerAdapter struct { - enable bool + enable bool logLevel settings.Level } @@ -82,7 +82,7 @@ func (logger XOrmLoggerAdapter) IsShowSQL() bool { func NewXOrmLoggerAdapter(showSql bool, logLevel settings.Level) xorm.Logger { return XOrmLoggerAdapter{ - enable: showSql, + enable: showSql, logLevel: logLevel, } } diff --git a/pkg/log/formatter.go b/pkg/log/formatter.go index 22e26d94..509b42f5 100644 --- a/pkg/log/formatter.go +++ b/pkg/log/formatter.go @@ -12,7 +12,7 @@ import ( ) type LogFormatter struct { - Prefix string + Prefix string DisableLevel bool } diff --git a/pkg/middlewares/request_id.go b/pkg/middlewares/request_id.go index b8f01f9b..a12f649a 100644 --- a/pkg/middlewares/request_id.go +++ b/pkg/middlewares/request_id.go @@ -9,7 +9,7 @@ import ( const REQUEST_ID_HEADER = "X-Request-ID" func RequestId(config *settings.Config) core.MiddlewareHandlerFunc { - return func (c *core.Context) { + return func(c *core.Context) { if requestid.Container.Current == nil { c.Next() return diff --git a/pkg/models/transaction_tag.go b/pkg/models/transaction_tag.go index 4575d5ca..e5b4665a 100644 --- a/pkg/models/transaction_tag.go +++ b/pkg/models/transaction_tag.go @@ -19,8 +19,8 @@ type TransactionTagCreateRequest struct { } type TransactionTagModifyRequest struct { - Id int64 `json:"id,string" binding:"required,min=1"` - Name string `json:"name" binding:"required,notBlank,max=32"` + Id int64 `json:"id,string" binding:"required,min=1"` + Name string `json:"name" binding:"required,notBlank,max=32"` } type TransactionTagHideRequest struct { diff --git a/pkg/services/tokens.go b/pkg/services/tokens.go index c74c75bd..e3672c7f 100644 --- a/pkg/services/tokens.go +++ b/pkg/services/tokens.go @@ -63,7 +63,7 @@ func (s *TokenService) ParseToken(c *core.Context) (*jwt.Token, *core.UserTokenC claims := &core.UserTokenClaims{} token, err := request.ParseFromRequest(c.Request, request.AuthorizationHeaderExtractor, - func (token *jwt.Token) (interface{}, error) { + func(token *jwt.Token) (interface{}, error) { uid, err := utils.StringToInt64(claims.Id) now := time.Now().Unix() @@ -205,8 +205,8 @@ func (s *TokenService) ParseFromTokenId(tokenId string) (*models.TokenRecord, er } tokenRecord := &models.TokenRecord{ - Uid: uid, - UserTokenId: userTokenId, + Uid: uid, + UserTokenId: userTokenId, CreatedUnixTime: createdUnixTime, } @@ -222,10 +222,10 @@ func (s *TokenService) createToken(user *models.User, tokenType core.TokenType, now := time.Now() tokenRecord := &models.TokenRecord{ - Uid: user.Uid, - UserTokenId: s.getUserTokenId(), - TokenType: tokenType, - UserAgent: userAgent, + Uid: user.Uid, + UserTokenId: s.getUserTokenId(), + TokenType: tokenType, + UserAgent: userAgent, CreatedUnixTime: now.Unix(), ExpiredUnixTime: now.Add(expiryDate).Unix(), } @@ -236,11 +236,11 @@ func (s *TokenService) createToken(user *models.User, tokenType core.TokenType, claims := &core.UserTokenClaims{ UserTokenId: utils.Int64ToString(tokenRecord.UserTokenId), - Username: user.Username, - Type: tokenRecord.TokenType, + Username: user.Username, + Type: tokenRecord.TokenType, StandardClaims: jwt.StandardClaims{ - Id: utils.Int64ToString(tokenRecord.Uid), - IssuedAt: tokenRecord.CreatedUnixTime, + Id: utils.Int64ToString(tokenRecord.Uid), + IssuedAt: tokenRecord.CreatedUnixTime, ExpiresAt: tokenRecord.ExpiredUnixTime, }, } diff --git a/pkg/services/transaction_categories.go b/pkg/services/transaction_categories.go index 004520d4..748c1f2a 100644 --- a/pkg/services/transaction_categories.go +++ b/pkg/services/transaction_categories.go @@ -214,7 +214,7 @@ func (s *TransactionCategoryService) HideCategory(uid int64, ids []int64, hidden now := time.Now().Unix() updateModel := &models.TransactionCategory{ - Hidden: hidden, + Hidden: hidden, UpdatedUnixTime: now, } @@ -264,7 +264,7 @@ func (s *TransactionCategoryService) DeleteCategory(uid int64, categoryId int64) now := time.Now().Unix() updateModel := &models.TransactionCategory{ - Deleted: true, + Deleted: true, DeletedUnixTime: now, } diff --git a/pkg/services/transaction_tags.go b/pkg/services/transaction_tags.go index 7998c6d0..0f320dac 100644 --- a/pkg/services/transaction_tags.go +++ b/pkg/services/transaction_tags.go @@ -164,7 +164,7 @@ func (s *TransactionTagService) HideTag(uid int64, ids []int64, hidden bool) err now := time.Now().Unix() updateModel := &models.TransactionTag{ - Hidden: hidden, + Hidden: hidden, UpdatedUnixTime: now, } diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index d2c9efbb..db36c691 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -69,7 +69,7 @@ func (s *TransactionService) GetTransactionsInMonthByPage(uid int64, year int, m endUnixTime := endTime.Unix() var transactions []*models.Transaction - err = s.UserDataDB(uid).Where("uid=? AND deleted=? AND transaction_time>=? AND transaction_time=? AND transaction_time=? AND transaction_time<=?", transaction.Uid, false, minTransactionTime, maxTransactionTime).OrderBy("transaction_time desc").Limit(1).Get(sameSecondLatestTransaction) - if has && sameSecondLatestTransaction.TransactionTime < maxTransactionTime - 1 { + if has && sameSecondLatestTransaction.TransactionTime < maxTransactionTime-1 { transaction.TransactionTime = sameSecondLatestTransaction.TransactionTime + 1 - } else if has && sameSecondLatestTransaction.TransactionTime == maxTransactionTime - 1 { + } else if has && sameSecondLatestTransaction.TransactionTime == maxTransactionTime-1 { return errs.ErrTooMuchTransactionInOneSecond } @@ -634,7 +634,7 @@ func (s *TransactionService) DeleteTransaction(uid int64, transactionId int64) e now := time.Now().Unix() updateModel := &models.Transaction{ - Deleted: true, + Deleted: true, DeletedUnixTime: now, } diff --git a/pkg/services/twofactor_authorizations.go b/pkg/services/twofactor_authorizations.go index 53bba0fb..6fd60c2d 100644 --- a/pkg/services/twofactor_authorizations.go +++ b/pkg/services/twofactor_authorizations.go @@ -3,9 +3,9 @@ package services import ( "time" - "xorm.io/xorm" "github.com/pquerna/otp" "github.com/pquerna/otp/totp" + "xorm.io/xorm" "github.com/mayswind/lab/pkg/datastore" "github.com/mayswind/lab/pkg/errs" diff --git a/pkg/utils/converter.go b/pkg/utils/converter.go index ee385df9..8ce2c93b 100644 --- a/pkg/utils/converter.go +++ b/pkg/utils/converter.go @@ -10,7 +10,7 @@ func StringToInt32(str string) (int, error) { return strconv.Atoi(str) } -func StringTryToInt32(str string, defaultValue int) int { +func StringTryToInt32(str string, defaultValue int) int { num, err := StringToInt32(str) if err != nil { diff --git a/pkg/utils/datetimes.go b/pkg/utils/datetimes.go index 718b75c2..a00ba8c5 100644 --- a/pkg/utils/datetimes.go +++ b/pkg/utils/datetimes.go @@ -17,7 +17,7 @@ func GetMinTransactionTimeFromUnixTime(unixTime int64) int64 { } func GetMaxTransactionTimeFromUnixTime(unixTime int64) int64 { - return unixTime * 1000 + 999 + return unixTime*1000 + 999 } func GetUnixTimeFromTransactionTime(transactionTime int64) int64 { diff --git a/pkg/utils/regexps.go b/pkg/utils/regexps.go index bac76866..30dea05e 100644 --- a/pkg/utils/regexps.go +++ b/pkg/utils/regexps.go @@ -3,8 +3,8 @@ package utils import "regexp" var ( - UsernamePattern = regexp.MustCompile("^(?i)[a-z0-9_-]+$") - EmailPattern = regexp.MustCompile("^(?i)(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])$") + UsernamePattern = regexp.MustCompile("^(?i)[a-z0-9_-]+$") + EmailPattern = regexp.MustCompile("^(?i)(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])$") HexRGBColorPattern = regexp.MustCompile("^(?i)([0-9a-f]{6}|[0-9a-f]{3})$") ) diff --git a/pkg/utils/strings.go b/pkg/utils/strings.go index a2cf04f6..3a51efe0 100644 --- a/pkg/utils/strings.go +++ b/pkg/utils/strings.go @@ -148,7 +148,7 @@ func AESGCMDecrypt(key []byte, ciphertext []byte) ([]byte, error) { nonceSize := aesgcm.NonceSize() - if len(ciphertext) - nonceSize <= 0 { + if len(ciphertext)-nonceSize <= 0 { return nil, errs.ErrCiphertextInvalid } diff --git a/pkg/validators/currency.go b/pkg/validators/currency.go index 5b6b4e8b..02ba41cb 100644 --- a/pkg/validators/currency.go +++ b/pkg/validators/currency.go @@ -7,7 +7,7 @@ import ( const PARENT_ACCOUNT_CURRENCY_PLACEHODLER = "---" // ISO 4217 -var ALL_CURRENCY_NAMES = map[string]bool { +var ALL_CURRENCY_NAMES = map[string]bool{ "AED": true, //UAE Dirham "AFN": true, //Afghani "ALL": true, //Lek