From bd9c8c78908c17fde28e85961b09c09ff0eac402 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 18 Apr 2021 00:00:50 +0800 Subject: [PATCH] modify package and class name --- pkg/api/data_managements.go | 8 ++--- pkg/cli/user_data.go | 36 +++++++++---------- pkg/converters/data_converter.go | 13 +++++++ .../ezbookkeeping_csv_file.go} | 26 +++++++------- pkg/exporters/data_exporter.go | 13 ------- 5 files changed, 48 insertions(+), 48 deletions(-) create mode 100644 pkg/converters/data_converter.go rename pkg/{exporters/csv_file.go => converters/ezbookkeeping_csv_file.go} (72%) delete mode 100644 pkg/exporters/data_exporter.go diff --git a/pkg/api/data_managements.go b/pkg/api/data_managements.go index 37334fe0..8f0fbb60 100644 --- a/pkg/api/data_managements.go +++ b/pkg/api/data_managements.go @@ -5,9 +5,9 @@ import ( "strings" "time" + "github.com/mayswind/ezbookkeeping/pkg/converters" "github.com/mayswind/ezbookkeeping/pkg/core" "github.com/mayswind/ezbookkeeping/pkg/errs" - "github.com/mayswind/ezbookkeeping/pkg/exporters" "github.com/mayswind/ezbookkeeping/pkg/log" "github.com/mayswind/ezbookkeeping/pkg/models" "github.com/mayswind/ezbookkeeping/pkg/services" @@ -19,7 +19,7 @@ const pageCountForDataExport = 1000 // DataManagementsApi represents data management api type DataManagementsApi struct { - exporter *exporters.CSVFileExporter + exporter *converters.EzBookKeepingCSVFileExporter tokens *services.TokenService users *services.UserService accounts *services.AccountService @@ -31,7 +31,7 @@ type DataManagementsApi struct { // Initialize a data management api singleton instance var ( DataManagements = &DataManagementsApi{ - exporter: &exporters.CSVFileExporter{}, + exporter: &converters.EzBookKeepingCSVFileExporter{}, tokens: services.Tokens, users: services.Users, accounts: services.Accounts, @@ -97,7 +97,7 @@ func (a *DataManagementsApi) ExportDataHandler(c *core.Context) ([]byte, string, return nil, "", errs.ErrOperationFailed } - result, err := a.exporter.GetOutputContent(uid, timezone, allTransactions, accountMap, categoryMap, tagMap, tagIndexs) + result, err := a.exporter.ToExportedContent(uid, timezone, allTransactions, accountMap, categoryMap, tagMap, tagIndexs) if err != nil { log.ErrorfWithRequestId(c, "[data_managements.ExportDataHandler] failed to get csv format exported data for \"uid:%d\", because %s", uid, err.Error()) diff --git a/pkg/cli/user_data.go b/pkg/cli/user_data.go index 69ca42c1..7a11dcbd 100644 --- a/pkg/cli/user_data.go +++ b/pkg/cli/user_data.go @@ -5,8 +5,8 @@ import ( "github.com/urfave/cli/v2" + "github.com/mayswind/ezbookkeeping/pkg/converters" "github.com/mayswind/ezbookkeeping/pkg/errs" - "github.com/mayswind/ezbookkeeping/pkg/exporters" "github.com/mayswind/ezbookkeeping/pkg/log" "github.com/mayswind/ezbookkeeping/pkg/models" "github.com/mayswind/ezbookkeeping/pkg/services" @@ -18,27 +18,27 @@ const pageCountForDataExport = 1000 // UserDataCli represents user data cli type UserDataCli struct { - csvExporter *exporters.CSVFileExporter - accounts *services.AccountService - transactions *services.TransactionService - categories *services.TransactionCategoryService - tags *services.TransactionTagService - users *services.UserService - twoFactorAuthorizations *services.TwoFactorAuthorizationService - tokens *services.TokenService + ezBookKeepingCsvExporter *converters.EzBookKeepingCSVFileExporter + accounts *services.AccountService + transactions *services.TransactionService + categories *services.TransactionCategoryService + tags *services.TransactionTagService + users *services.UserService + twoFactorAuthorizations *services.TwoFactorAuthorizationService + tokens *services.TokenService } // Initialize an user data cli singleton instance var ( UserData = &UserDataCli{ - csvExporter: &exporters.CSVFileExporter{}, - accounts: services.Accounts, - transactions: services.Transactions, - categories: services.TransactionCategories, - tags: services.TransactionTags, - users: services.Users, - twoFactorAuthorizations: services.TwoFactorAuthorizations, - tokens: services.Tokens, + ezBookKeepingCsvExporter: &converters.EzBookKeepingCSVFileExporter{}, + accounts: services.Accounts, + transactions: services.Transactions, + categories: services.TransactionCategories, + tags: services.TransactionTags, + users: services.Users, + twoFactorAuthorizations: services.TwoFactorAuthorizations, + tokens: services.Tokens, } ) @@ -417,7 +417,7 @@ func (l *UserDataCli) ExportTransaction(c *cli.Context, username string) ([]byte return nil, err } - result, err := l.csvExporter.GetOutputContent(uid, time.Local, allTransactions, accountMap, categoryMap, tagMap, tagIndexs) + result, err := l.ezBookKeepingCsvExporter.ToExportedContent(uid, time.Local, allTransactions, accountMap, categoryMap, tagMap, tagIndexs) if err != nil { log.BootErrorf("[user_data.ExportTransaction] failed to get csv format exported data for \"%s\", because %s", username, err.Error()) diff --git a/pkg/converters/data_converter.go b/pkg/converters/data_converter.go new file mode 100644 index 00000000..bb5c3bd2 --- /dev/null +++ b/pkg/converters/data_converter.go @@ -0,0 +1,13 @@ +package converters + +import ( + "time" + + "github.com/mayswind/ezbookkeeping/pkg/models" +) + +// DataConverter defines the structure of data exporter +type DataConverter interface { + // ToExportedContent returns the exported data + ToExportedContent(uid int64, timezone *time.Location, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexs map[int64][]int64) ([]byte, error) +} diff --git a/pkg/exporters/csv_file.go b/pkg/converters/ezbookkeeping_csv_file.go similarity index 72% rename from pkg/exporters/csv_file.go rename to pkg/converters/ezbookkeeping_csv_file.go index a3d907a7..c779582d 100644 --- a/pkg/exporters/csv_file.go +++ b/pkg/converters/ezbookkeeping_csv_file.go @@ -1,4 +1,4 @@ -package exporters +package converters import ( "fmt" @@ -9,16 +9,16 @@ import ( "github.com/mayswind/ezbookkeeping/pkg/utils" ) -// CSVFileExporter defines the structure of csv file exporter -type CSVFileExporter struct { - DataExporter +// EzBookKeepingCSVFileExporter defines the structure of csv file exporter +type EzBookKeepingCSVFileExporter struct { + DataConverter } const csvHeaderLine = "Time,Type,Category,Sub Category,Account,Amount,Account2,Account2 Amount,Tags,Comment\n" const csvDataLineFormat = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" -// GetOutputContent returns the exported csv data -func (e *CSVFileExporter) GetOutputContent(uid int64, timezone *time.Location, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexs map[int64][]int64) ([]byte, error) { +// ToExportedContent returns the exported csv data +func (e *EzBookKeepingCSVFileExporter) ToExportedContent(uid int64, timezone *time.Location, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexs map[int64][]int64) ([]byte, error) { var ret strings.Builder ret.Grow(len(transactions) * 100) @@ -55,7 +55,7 @@ func (e *CSVFileExporter) GetOutputContent(uid int64, timezone *time.Location, t return []byte(ret.String()), nil } -func (e *CSVFileExporter) getTransactionTypeName(transactionDbType models.TransactionDbType) string { +func (e *EzBookKeepingCSVFileExporter) getTransactionTypeName(transactionDbType models.TransactionDbType) string { if transactionDbType == models.TRANSACTION_DB_TYPE_MODIFY_BALANCE { return "Balance Modification" } else if transactionDbType == models.TRANSACTION_DB_TYPE_INCOME { @@ -69,7 +69,7 @@ func (e *CSVFileExporter) getTransactionTypeName(transactionDbType models.Transa } } -func (e *CSVFileExporter) getTransactionCategoryName(categoryId int64, categoryMap map[int64]*models.TransactionCategory) string { +func (e *EzBookKeepingCSVFileExporter) getTransactionCategoryName(categoryId int64, categoryMap map[int64]*models.TransactionCategory) string { category, exists := categoryMap[categoryId] if !exists { @@ -89,7 +89,7 @@ func (e *CSVFileExporter) getTransactionCategoryName(categoryId int64, categoryM return parentCategory.Name } -func (e *CSVFileExporter) getTransactionSubCategoryName(categoryId int64, categoryMap map[int64]*models.TransactionCategory) string { +func (e *EzBookKeepingCSVFileExporter) getTransactionSubCategoryName(categoryId int64, categoryMap map[int64]*models.TransactionCategory) string { category, exists := categoryMap[categoryId] if exists { @@ -99,7 +99,7 @@ func (e *CSVFileExporter) getTransactionSubCategoryName(categoryId int64, catego } } -func (e *CSVFileExporter) getAccountName(accountId int64, accountMap map[int64]*models.Account) string { +func (e *EzBookKeepingCSVFileExporter) getAccountName(accountId int64, accountMap map[int64]*models.Account) string { account, exists := accountMap[accountId] if exists { @@ -109,7 +109,7 @@ func (e *CSVFileExporter) getAccountName(accountId int64, accountMap map[int64]* } } -func (e *CSVFileExporter) getDisplayAmount(amount int64) string { +func (e *EzBookKeepingCSVFileExporter) getDisplayAmount(amount int64) string { displayAmount := utils.Int64ToString(amount) integer := utils.SubString(displayAmount, 0, len(displayAmount)-2) decimals := utils.SubString(displayAmount, -2, 2) @@ -129,7 +129,7 @@ func (e *CSVFileExporter) getDisplayAmount(amount int64) string { return integer + "." + decimals } -func (e *CSVFileExporter) getTags(transactionId int64, allTagIndexs map[int64][]int64, tagMap map[int64]*models.TransactionTag) string { +func (e *EzBookKeepingCSVFileExporter) getTags(transactionId int64, allTagIndexs map[int64][]int64, tagMap map[int64]*models.TransactionTag) string { tagIndexs, exists := allTagIndexs[transactionId] if !exists { @@ -156,7 +156,7 @@ func (e *CSVFileExporter) getTags(transactionId int64, allTagIndexs map[int64][] return ret.String() } -func (e *CSVFileExporter) getComment(comment string) string { +func (e *EzBookKeepingCSVFileExporter) getComment(comment string) string { comment = strings.Replace(comment, ",", " ", -1) comment = strings.Replace(comment, "\r\n", " ", -1) comment = strings.Replace(comment, "\n", " ", -1) diff --git a/pkg/exporters/data_exporter.go b/pkg/exporters/data_exporter.go deleted file mode 100644 index 254efbd5..00000000 --- a/pkg/exporters/data_exporter.go +++ /dev/null @@ -1,13 +0,0 @@ -package exporters - -import ( - "time" - - "github.com/mayswind/ezbookkeeping/pkg/models" -) - -// DataExporter defines the structure of data exporter -type DataExporter interface { - // GetOutputContent returns the exported data - GetOutputContent(uid int64, timezone *time.Location, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexs map[int64][]int64) ([]byte, error) -}