From 226d44651f58fe91d8238ae16da09e8ccfbb6b75 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Mon, 17 Jun 2024 00:22:32 +0800 Subject: [PATCH] code refactor --- pkg/converters/ezbookkeeping_csv_file.go | 4 +++- pkg/converters/ezbookkeeping_plain_file.go | 11 +++++++---- pkg/converters/ezbookkeeping_tsv_file.go | 4 +++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/converters/ezbookkeeping_csv_file.go b/pkg/converters/ezbookkeeping_csv_file.go index f0dc45f7..0ec7ea24 100644 --- a/pkg/converters/ezbookkeeping_csv_file.go +++ b/pkg/converters/ezbookkeeping_csv_file.go @@ -9,7 +9,9 @@ type EzBookKeepingCSVFileExporter struct { EzBookKeepingPlainFileExporter } +const csvSeparator = "," + // ToExportedContent returns the exported CSV data func (e *EzBookKeepingCSVFileExporter) ToExportedContent(uid int64, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexs map[int64][]int64) ([]byte, error) { - return e.toExportedContent(uid, ",", transactions, accountMap, categoryMap, tagMap, allTagIndexs) + return e.toExportedContent(uid, csvSeparator, transactions, accountMap, categoryMap, tagMap, allTagIndexs) } diff --git a/pkg/converters/ezbookkeeping_plain_file.go b/pkg/converters/ezbookkeeping_plain_file.go index da044154..03bfc9c9 100644 --- a/pkg/converters/ezbookkeeping_plain_file.go +++ b/pkg/converters/ezbookkeeping_plain_file.go @@ -13,8 +13,11 @@ import ( type EzBookKeepingPlainFileExporter struct { } -const headerLine = "Time,Timezone,Type,Category,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n" -const dataLineFormat = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" +const lineSeparator = "\n" +const geoLocationSeparator = " " +const transactionTagSeparator = ";" +const headerLine = "Time,Timezone,Type,Category,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description" + lineSeparator +const dataLineFormat = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s" + lineSeparator // toExportedContent returns the exported plain data func (e *EzBookKeepingPlainFileExporter) toExportedContent(uid int64, separator string, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexs map[int64][]int64) ([]byte, error) { @@ -60,7 +63,7 @@ func (e *EzBookKeepingPlainFileExporter) toExportedContent(uid int64, separator } if transaction.GeoLongitude != 0 || transaction.GeoLatitude != 0 { - geoLocation = fmt.Sprintf("%f %f", transaction.GeoLongitude, transaction.GeoLatitude) + geoLocation = fmt.Sprintf("%f%s%f", transaction.GeoLongitude, geoLocationSeparator, transaction.GeoLatitude) } tags := e.replaceDelimiters(e.getTags(transaction.TransactionId, allTagIndexs, tagMap), separator) @@ -167,7 +170,7 @@ func (e *EzBookKeepingPlainFileExporter) getTags(transactionId int64, allTagInde for i := 0; i < len(tagIndexs); i++ { if i > 0 { - ret.WriteString(";") + ret.WriteString(transactionTagSeparator) } tagIndex := tagIndexs[i] diff --git a/pkg/converters/ezbookkeeping_tsv_file.go b/pkg/converters/ezbookkeeping_tsv_file.go index daf72b51..eeb6f4d9 100644 --- a/pkg/converters/ezbookkeeping_tsv_file.go +++ b/pkg/converters/ezbookkeeping_tsv_file.go @@ -9,7 +9,9 @@ type EzBookKeepingTSVFileExporter struct { EzBookKeepingPlainFileExporter } +const tsvSeparator = "\t" + // ToExportedContent returns the exported TSV data func (e *EzBookKeepingTSVFileExporter) ToExportedContent(uid int64, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexs map[int64][]int64) ([]byte, error) { - return e.toExportedContent(uid, "\t", transactions, accountMap, categoryMap, tagMap, allTagIndexs) + return e.toExportedContent(uid, tsvSeparator, transactions, accountMap, categoryMap, tagMap, allTagIndexs) }