WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit a062592

Browse files
committed
rename files
1 parent 8978e34 commit a062592

26 files changed

+365
-361
lines changed

pkg/converters/alipay/alipay_app_transaction_data_csv_file_importer.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package alipay
22

3-
// alipayAppTransactionDataCsvImporter defines the structure of alipay app csv importer for transaction data
4-
type alipayAppTransactionDataCsvImporter struct {
5-
alipayTransactionDataCsvImporter
3+
// alipayAppTransactionDataCsvFileImporter defines the structure of alipay app csv importer for transaction data
4+
type alipayAppTransactionDataCsvFileImporter struct {
5+
alipayTransactionDataCsvFileImporter
66
}
77

88
// Initialize a alipay app transaction data csv file importer singleton instance
99
var (
10-
AlipayAppTransactionDataCsvImporter = &alipayAppTransactionDataCsvImporter{
11-
alipayTransactionDataCsvImporter{
10+
AlipayAppTransactionDataCsvFileImporter = &alipayAppTransactionDataCsvFileImporter{
11+
alipayTransactionDataCsvFileImporter{
1212
fileHeaderLine: "------------------------------------------------------------------------------------",
1313
dataHeaderStartContent: "支付宝(中国)网络技术有限公司 电子客户回单",
1414
originalColumnNames: alipayTransactionColumnNames{

pkg/converters/alipay/alipay_transaction_data_plain_text_data_table.go renamed to pkg/converters/alipay/alipay_transaction_csv_data_table.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (t *alipayTransactionDataRowIterator) Next(ctx core.Context, user *models.U
139139
rowItems[t.dataTable.originalTypeColumnIndex] != alipayTransactionTypeNameMapping[models.TRANSACTION_TYPE_INCOME] &&
140140
rowItems[t.dataTable.originalTypeColumnIndex] != alipayTransactionTypeNameMapping[models.TRANSACTION_TYPE_EXPENSE] &&
141141
rowItems[t.dataTable.originalTypeColumnIndex] != alipayTransactionTypeNameMapping[models.TRANSACTION_TYPE_TRANSFER] {
142-
log.Warnf(ctx, "[alipay_transaction_data_plain_text_data_table.Next] skip parsing transaction in row \"index:%d\", because type is \"%s\"", t.currentIndex, rowItems[t.dataTable.originalTypeColumnIndex])
142+
log.Warnf(ctx, "[alipay_transaction_csv_data_table.Next] skip parsing transaction in row \"index:%d\", because type is \"%s\"", t.currentIndex, rowItems[t.dataTable.originalTypeColumnIndex])
143143
isValid = false
144144
}
145145

@@ -150,7 +150,7 @@ func (t *alipayTransactionDataRowIterator) Next(ctx core.Context, user *models.U
150150
rowItems[t.dataTable.originalStatusColumnIndex] != alipayTransactionDataStatusClosedName &&
151151
rowItems[t.dataTable.originalStatusColumnIndex] != alipayTransactionDataStatusRefundSuccessName &&
152152
rowItems[t.dataTable.originalStatusColumnIndex] != alipayTransactionDataStatusTaxRefundSuccessName {
153-
log.Warnf(ctx, "[alipay_transaction_data_plain_text_data_table.Next] skip parsing transaction in row \"index:%d\", because status is \"%s\"", t.currentIndex, rowItems[t.dataTable.originalStatusColumnIndex])
153+
log.Warnf(ctx, "[alipay_transaction_csv_data_table.Next] skip parsing transaction in row \"index:%d\", because status is \"%s\"", t.currentIndex, rowItems[t.dataTable.originalStatusColumnIndex])
154154
isValid = false
155155
}
156156

@@ -161,7 +161,7 @@ func (t *alipayTransactionDataRowIterator) Next(ctx core.Context, user *models.U
161161
finalItems, errMsg = t.dataTable.parseTransactionData(ctx, user, rowItems)
162162

163163
if finalItems == nil {
164-
log.Warnf(ctx, "[alipay_transaction_data_plain_text_data_table.Next] skip parsing transaction in row \"index:%d\", because %s", t.currentIndex, errMsg)
164+
log.Warnf(ctx, "[alipay_transaction_csv_data_table.Next] skip parsing transaction in row \"index:%d\", because %s", t.currentIndex, errMsg)
165165
isValid = false
166166
}
167167
}
@@ -302,7 +302,7 @@ func createNewAlipayTransactionDataTable(ctx core.Context, reader io.Reader, fil
302302
}
303303

304304
if len(allOriginalLines) < 2 {
305-
log.Errorf(ctx, "[alipay_transaction_data_plain_text_data_table.createNewAlipayTransactionPlainTextDataTable] cannot parse import data, because data table row count is less 1")
305+
log.Errorf(ctx, "[alipay_transaction_csv_data_table.createNewAlipayTransactionPlainTextDataTable] cannot parse import data, because data table row count is less 1")
306306
return nil, errs.ErrNotFoundTransactionDataInFile
307307
}
308308

@@ -324,7 +324,7 @@ func createNewAlipayTransactionDataTable(ctx core.Context, reader io.Reader, fil
324324
descriptionColumnIdx, descriptionColumnExists := originalHeaderItemMap[originalColumnNames.descriptionColumnName]
325325

326326
if !timeColumnExists || !amountColumnExists || !typeColumnExists || !statusColumnExists {
327-
log.Errorf(ctx, "[alipay_transaction_data_plain_text_data_table.createNewAlipayTransactionPlainTextDataTable] cannot parse alipay csv data, because missing essential columns in header row")
327+
log.Errorf(ctx, "[alipay_transaction_csv_data_table.createNewAlipayTransactionPlainTextDataTable] cannot parse alipay csv data, because missing essential columns in header row")
328328
return nil, errs.ErrMissingRequiredFieldInHeaderRow
329329
}
330330

@@ -379,7 +379,7 @@ func parseAllLinesFromAlipayTransactionPlainText(ctx core.Context, reader io.Rea
379379
}
380380

381381
if err != nil {
382-
log.Errorf(ctx, "[alipay_transaction_data_plain_text_data_table.parseAllLinesFromAlipayTransactionPlainText] cannot parse alipay csv data, because %s", err.Error())
382+
log.Errorf(ctx, "[alipay_transaction_csv_data_table.parseAllLinesFromAlipayTransactionPlainText] cannot parse alipay csv data, because %s", err.Error())
383383
return nil, errs.ErrInvalidCSVFile
384384
}
385385

@@ -390,7 +390,7 @@ func parseAllLinesFromAlipayTransactionPlainText(ctx core.Context, reader io.Rea
390390
hasFileHeader = true
391391
continue
392392
} else {
393-
log.Warnf(ctx, "[alipay_transaction_data_plain_text_data_table.parseAllLinesFromAlipayTransactionPlainText] read unexpected line before read file header, line content is %s", strings.Join(items, ","))
393+
log.Warnf(ctx, "[alipay_transaction_csv_data_table.parseAllLinesFromAlipayTransactionPlainText] read unexpected line before read file header, line content is %s", strings.Join(items, ","))
394394
continue
395395
}
396396
}
@@ -418,7 +418,7 @@ func parseAllLinesFromAlipayTransactionPlainText(ctx core.Context, reader io.Rea
418418
}
419419

420420
if len(allOriginalLines) > 0 && len(items) < len(allOriginalLines[0]) {
421-
log.Errorf(ctx, "[alipay_transaction_data_plain_text_data_table.parseAllLinesFromAlipayTransactionPlainText] cannot parse row \"index:%d\", because may missing some columns (column count %d in data row is less than header column count %d)", len(allOriginalLines), len(items), len(allOriginalLines[0]))
421+
log.Errorf(ctx, "[alipay_transaction_csv_data_table.parseAllLinesFromAlipayTransactionPlainText] cannot parse row \"index:%d\", because may missing some columns (column count %d in data row is less than header column count %d)", len(allOriginalLines), len(items), len(allOriginalLines[0]))
422422
return nil, errs.ErrFewerFieldsInDataRowThanInHeaderRow
423423
}
424424

pkg/converters/alipay/alipay_transaction_data_csv_file_importer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ var alipayTransactionTypeNameMapping = map[models.TransactionType]string{
1717
models.TRANSACTION_TYPE_TRANSFER: "不计收支",
1818
}
1919

20-
// alipayTransactionDataCsvImporter defines the structure of alipay csv importer for transaction data
21-
type alipayTransactionDataCsvImporter struct {
20+
// alipayTransactionDataCsvFileImporter defines the structure of alipay csv importer for transaction data
21+
type alipayTransactionDataCsvFileImporter struct {
2222
fileHeaderLine string
2323
dataHeaderStartContent string
2424
dataBottomEndLineRune rune
2525
originalColumnNames alipayTransactionColumnNames
2626
}
2727

2828
// ParseImportedData returns the imported data by parsing the alipay transaction csv data
29-
func (c *alipayTransactionDataCsvImporter) ParseImportedData(ctx core.Context, user *models.User, data []byte, defaultTimezoneOffset int16, accountMap map[string]*models.Account, expenseCategoryMap map[string]*models.TransactionCategory, incomeCategoryMap map[string]*models.TransactionCategory, transferCategoryMap map[string]*models.TransactionCategory, tagMap map[string]*models.TransactionTag) (models.ImportedTransactionSlice, []*models.Account, []*models.TransactionCategory, []*models.TransactionCategory, []*models.TransactionCategory, []*models.TransactionTag, error) {
29+
func (c *alipayTransactionDataCsvFileImporter) ParseImportedData(ctx core.Context, user *models.User, data []byte, defaultTimezoneOffset int16, accountMap map[string]*models.Account, expenseCategoryMap map[string]*models.TransactionCategory, incomeCategoryMap map[string]*models.TransactionCategory, transferCategoryMap map[string]*models.TransactionCategory, tagMap map[string]*models.TransactionTag) (models.ImportedTransactionSlice, []*models.Account, []*models.TransactionCategory, []*models.TransactionCategory, []*models.TransactionCategory, []*models.TransactionTag, error) {
3030
enc := simplifiedchinese.GB18030
3131
reader := transform.NewReader(bytes.NewReader(data), enc.NewDecoder())
3232

pkg/converters/alipay/alipay_transaction_data_csv_file_importer_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
func TestAlipayCsvFileImporterParseImportedData_MinimumValidData(t *testing.T) {
18-
converter := AlipayWebTransactionDataCsvImporter
18+
converter := AlipayWebTransactionDataCsvFileImporter
1919
context := core.NewNullContext()
2020

2121
user := &models.User{
@@ -94,7 +94,7 @@ func TestAlipayCsvFileImporterParseImportedData_MinimumValidData(t *testing.T) {
9494
}
9595

9696
func TestAlipayCsvFileImporterParseImportedData_ParseRefundTransaction(t *testing.T) {
97-
converter := AlipayWebTransactionDataCsvImporter
97+
converter := AlipayWebTransactionDataCsvFileImporter
9898
context := core.NewNullContext()
9999

100100
user := &models.User{
@@ -142,7 +142,7 @@ func TestAlipayCsvFileImporterParseImportedData_ParseRefundTransaction(t *testin
142142
}
143143

144144
func TestAlipayCsvFileImporterParseImportedData_ParseInvalidTime(t *testing.T) {
145-
converter := AlipayWebTransactionDataCsvImporter
145+
converter := AlipayWebTransactionDataCsvFileImporter
146146
context := core.NewNullContext()
147147

148148
user := &models.User{
@@ -176,7 +176,7 @@ func TestAlipayCsvFileImporterParseImportedData_ParseInvalidTime(t *testing.T) {
176176
}
177177

178178
func TestAlipayCsvFileImporterParseImportedData_ParseInvalidType(t *testing.T) {
179-
converter := AlipayWebTransactionDataCsvImporter
179+
converter := AlipayWebTransactionDataCsvFileImporter
180180
context := core.NewNullContext()
181181

182182
user := &models.User{
@@ -198,7 +198,7 @@ func TestAlipayCsvFileImporterParseImportedData_ParseInvalidType(t *testing.T) {
198198
}
199199

200200
func TestAlipayCsvFileImporterParseImportedData_ParseAccountName(t *testing.T) {
201-
converter := AlipayWebTransactionDataCsvImporter
201+
converter := AlipayWebTransactionDataCsvFileImporter
202202
context := core.NewNullContext()
203203

204204
user := &models.User{
@@ -325,7 +325,7 @@ func TestAlipayCsvFileImporterParseImportedData_ParseAccountName(t *testing.T) {
325325
}
326326

327327
func TestAlipayCsvFileImporterParseImportedData_ParseCategory(t *testing.T) {
328-
converter := AlipayAppTransactionDataCsvImporter
328+
converter := AlipayAppTransactionDataCsvFileImporter
329329
context := core.NewNullContext()
330330

331331
user := &models.User{
@@ -365,7 +365,7 @@ func TestAlipayCsvFileImporterParseImportedData_ParseCategory(t *testing.T) {
365365
}
366366

367367
func TestAlipayCsvFileImporterParseImportedData_ParseDescription(t *testing.T) {
368-
converter := AlipayWebTransactionDataCsvImporter
368+
converter := AlipayWebTransactionDataCsvFileImporter
369369
context := core.NewNullContext()
370370

371371
user := &models.User{
@@ -405,7 +405,7 @@ func TestAlipayCsvFileImporterParseImportedData_ParseDescription(t *testing.T) {
405405
}
406406

407407
func TestAlipayCsvFileImporterParseImportedData_MissingFileHeader(t *testing.T) {
408-
converter := AlipayWebTransactionDataCsvImporter
408+
converter := AlipayWebTransactionDataCsvFileImporter
409409
context := core.NewNullContext()
410410

411411
user := &models.User{
@@ -427,7 +427,7 @@ func TestAlipayCsvFileImporterParseImportedData_MissingFileHeader(t *testing.T)
427427
}
428428

429429
func TestAlipayCsvFileImporterParseImportedData_MissingRequiredColumn(t *testing.T) {
430-
converter := AlipayWebTransactionDataCsvImporter
430+
converter := AlipayWebTransactionDataCsvFileImporter
431431
context := core.NewNullContext()
432432

433433
user := &models.User{

pkg/converters/alipay/alipay_web_transaction_data_csv_file_importer.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package alipay
22

3-
// alipayWebTransactionDataCsvImporter defines the structure of alipay (web) csv importer for transaction data
4-
type alipayWebTransactionDataCsvImporter struct {
5-
alipayTransactionDataCsvImporter
3+
// alipayWebTransactionDataCsvFileImporter defines the structure of alipay (web) csv importer for transaction data
4+
type alipayWebTransactionDataCsvFileImporter struct {
5+
alipayTransactionDataCsvFileImporter
66
}
77

88
// Initialize a alipay (web) transaction data csv file importer singleton instance
99
var (
10-
AlipayWebTransactionDataCsvImporter = &alipayWebTransactionDataCsvImporter{
11-
alipayTransactionDataCsvImporter{
10+
AlipayWebTransactionDataCsvFileImporter = &alipayWebTransactionDataCsvFileImporter{
11+
alipayTransactionDataCsvFileImporter{
1212
fileHeaderLine: "支付宝交易记录明细查询",
1313
dataHeaderStartContent: "交易记录明细列表",
1414
dataBottomEndLineRune: '-',
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package csv
2+
3+
import (
4+
"encoding/csv"
5+
"io"
6+
7+
"github.com/mayswind/ezbookkeeping/pkg/converters/datatable"
8+
"github.com/mayswind/ezbookkeeping/pkg/core"
9+
"github.com/mayswind/ezbookkeeping/pkg/errs"
10+
"github.com/mayswind/ezbookkeeping/pkg/log"
11+
)
12+
13+
// CsvFileImportedDataTable defines the structure of csv data table
14+
type CsvFileImportedDataTable struct {
15+
allLines [][]string
16+
}
17+
18+
// CsvFileImportedDataRow defines the structure of csv data table row
19+
type CsvFileImportedDataRow struct {
20+
dataTable *CsvFileImportedDataTable
21+
allItems []string
22+
}
23+
24+
// CsvFileImportedDataRowIterator defines the structure of csv data table row iterator
25+
type CsvFileImportedDataRowIterator struct {
26+
dataTable *CsvFileImportedDataTable
27+
currentIndex int
28+
}
29+
30+
// DataRowCount returns the total count of data row
31+
func (t *CsvFileImportedDataTable) DataRowCount() int {
32+
if len(t.allLines) < 1 {
33+
return 0
34+
}
35+
36+
return len(t.allLines) - 1
37+
}
38+
39+
// HeaderColumnNames returns the header column name list
40+
func (t *CsvFileImportedDataTable) HeaderColumnNames() []string {
41+
if len(t.allLines) < 1 {
42+
return nil
43+
}
44+
45+
return t.allLines[0]
46+
}
47+
48+
// DataRowIterator returns the iterator of data row
49+
func (t *CsvFileImportedDataTable) DataRowIterator() datatable.ImportedDataRowIterator {
50+
return &CsvFileImportedDataRowIterator{
51+
dataTable: t,
52+
currentIndex: 0,
53+
}
54+
}
55+
56+
// ColumnCount returns the total count of column in this data row
57+
func (r *CsvFileImportedDataRow) ColumnCount() int {
58+
return len(r.allItems)
59+
}
60+
61+
// GetData returns the data in the specified column index
62+
func (r *CsvFileImportedDataRow) GetData(columnIndex int) string {
63+
if columnIndex >= len(r.allItems) {
64+
return ""
65+
}
66+
67+
return r.allItems[columnIndex]
68+
}
69+
70+
// HasNext returns whether the iterator does not reach the end
71+
func (t *CsvFileImportedDataRowIterator) HasNext() bool {
72+
return t.currentIndex+1 < len(t.dataTable.allLines)
73+
}
74+
75+
// Next returns the next imported data row
76+
func (t *CsvFileImportedDataRowIterator) Next() datatable.ImportedDataRow {
77+
if t.currentIndex+1 >= len(t.dataTable.allLines) {
78+
return nil
79+
}
80+
81+
t.currentIndex++
82+
83+
rowItems := t.dataTable.allLines[t.currentIndex]
84+
85+
return &CsvFileImportedDataRow{
86+
dataTable: t.dataTable,
87+
allItems: rowItems,
88+
}
89+
}
90+
91+
// CreateNewCsvDataTable returns comma separated values data table by io readers
92+
func CreateNewCsvDataTable(ctx core.Context, reader io.Reader) (*CsvFileImportedDataTable, error) {
93+
return createNewCsvFileDataTable(ctx, reader, ',')
94+
}
95+
96+
func createNewCsvFileDataTable(ctx core.Context, reader io.Reader, separator rune) (*CsvFileImportedDataTable, error) {
97+
csvReader := csv.NewReader(reader)
98+
csvReader.Comma = separator
99+
csvReader.FieldsPerRecord = -1
100+
101+
allLines := make([][]string, 0)
102+
103+
for {
104+
items, err := csvReader.Read()
105+
106+
if err == io.EOF {
107+
break
108+
}
109+
110+
if err != nil {
111+
log.Errorf(ctx, "[csv_file_imported_data_table.createNewCsvFileDataTable] cannot parse csv data, because %s", err.Error())
112+
return nil, errs.ErrInvalidCSVFile
113+
}
114+
115+
if len(items) == 0 && items[0] == "" {
116+
continue
117+
}
118+
119+
allLines = append(allLines, items)
120+
}
121+
122+
return &CsvFileImportedDataTable{
123+
allLines: allLines,
124+
}, nil
125+
}

0 commit comments

Comments
 (0)