Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mayswind committed Jun 16, 2024
1 parent db71ac5 commit 226d446
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 3 additions & 1 deletion pkg/converters/ezbookkeeping_csv_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 7 additions & 4 deletions pkg/converters/ezbookkeeping_plain_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion pkg/converters/ezbookkeeping_tsv_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 226d446

Please sign in to comment.