mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 23:47:33 +08:00
code refactor
This commit is contained in:
+9
-9
@@ -1,7 +1,7 @@
|
||||
package datatable
|
||||
|
||||
// ImportedDataTable defines the structure of imported data table
|
||||
type ImportedDataTable interface {
|
||||
// BasicDataTable defines the structure of basic data table
|
||||
type BasicDataTable interface {
|
||||
// DataRowCount returns the total count of data row
|
||||
DataRowCount() int
|
||||
|
||||
@@ -9,11 +9,11 @@ type ImportedDataTable interface {
|
||||
HeaderColumnNames() []string
|
||||
|
||||
// DataRowIterator returns the iterator of data row
|
||||
DataRowIterator() ImportedDataRowIterator
|
||||
DataRowIterator() BasicDataTableRowIterator
|
||||
}
|
||||
|
||||
// ImportedDataRow defines the structure of imported data row
|
||||
type ImportedDataRow interface {
|
||||
// BasicDataTableRow defines the structure of basic data row
|
||||
type BasicDataTableRow interface {
|
||||
// ColumnCount returns the total count of column in this data row
|
||||
ColumnCount() int
|
||||
|
||||
@@ -21,14 +21,14 @@ type ImportedDataRow interface {
|
||||
GetData(columnIndex int) string
|
||||
}
|
||||
|
||||
// ImportedDataRowIterator defines the structure of imported data row iterator
|
||||
type ImportedDataRowIterator interface {
|
||||
// BasicDataTableRowIterator defines the structure of basic data row iterator
|
||||
type BasicDataTableRowIterator interface {
|
||||
// HasNext returns whether the iterator does not reach the end
|
||||
HasNext() bool
|
||||
|
||||
// CurrentRowId returns current row id
|
||||
CurrentRowId() string
|
||||
|
||||
// Next returns the next imported data row
|
||||
Next() ImportedDataRow
|
||||
// Next returns the next basic data row
|
||||
Next() BasicDataTableRow
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package datatable
|
||||
|
||||
// basicDataTableToCommonDataTableWrapper defines the structure of basic data table to common data table wrapper
|
||||
type basicDataTableToCommonDataTableWrapper struct {
|
||||
innerDataTable BasicDataTable
|
||||
dataColumnIndexes map[string]int
|
||||
}
|
||||
|
||||
// basicDataTableToCommonDataTableWrapperRow defines the data row structure of basic data table to common data table wrapper
|
||||
type basicDataTableToCommonDataTableWrapperRow struct {
|
||||
rowData map[string]string
|
||||
}
|
||||
|
||||
// basicDataTableToCommonDataTableWrapperRowIterator defines the data row iterator structure of basic data table to common data table wrapper
|
||||
type basicDataTableToCommonDataTableWrapperRowIterator struct {
|
||||
commonDataTable *basicDataTableToCommonDataTableWrapper
|
||||
innerIterator BasicDataTableRowIterator
|
||||
}
|
||||
|
||||
// HeaderColumnCount returns the total count of column in header row
|
||||
func (t *basicDataTableToCommonDataTableWrapper) HeaderColumnCount() int {
|
||||
return len(t.innerDataTable.HeaderColumnNames())
|
||||
}
|
||||
|
||||
// HasColumn returns whether the data table has specified column name
|
||||
func (t *basicDataTableToCommonDataTableWrapper) HasColumn(columnName string) bool {
|
||||
index, exists := t.dataColumnIndexes[columnName]
|
||||
return exists && index >= 0
|
||||
}
|
||||
|
||||
// DataRowCount returns the total count of common data row
|
||||
func (t *basicDataTableToCommonDataTableWrapper) DataRowCount() int {
|
||||
return t.innerDataTable.DataRowCount()
|
||||
}
|
||||
|
||||
// DataRowIterator returns the iterator of common data row
|
||||
func (t *basicDataTableToCommonDataTableWrapper) DataRowIterator() CommonDataTableRowIterator {
|
||||
return &basicDataTableToCommonDataTableWrapperRowIterator{
|
||||
commonDataTable: t,
|
||||
innerIterator: t.innerDataTable.DataRowIterator(),
|
||||
}
|
||||
}
|
||||
|
||||
// HasData returns whether the common data row has specified column data
|
||||
func (r *basicDataTableToCommonDataTableWrapperRow) HasData(columnName string) bool {
|
||||
_, exists := r.rowData[columnName]
|
||||
return exists
|
||||
}
|
||||
|
||||
// ColumnCount returns the total count of column in this data row
|
||||
func (r *basicDataTableToCommonDataTableWrapperRow) ColumnCount() int {
|
||||
return len(r.rowData)
|
||||
}
|
||||
|
||||
// GetData returns the data in the specified column name
|
||||
func (r *basicDataTableToCommonDataTableWrapperRow) GetData(columnName string) string {
|
||||
return r.rowData[columnName]
|
||||
}
|
||||
|
||||
// HasNext returns whether the iterator does not reach the end
|
||||
func (t *basicDataTableToCommonDataTableWrapperRowIterator) HasNext() bool {
|
||||
return t.innerIterator.HasNext()
|
||||
}
|
||||
|
||||
// CurrentRowId returns current row id
|
||||
func (t *basicDataTableToCommonDataTableWrapperRowIterator) CurrentRowId() string {
|
||||
return t.innerIterator.CurrentRowId()
|
||||
}
|
||||
|
||||
// Next returns the next common data row
|
||||
func (t *basicDataTableToCommonDataTableWrapperRowIterator) Next() CommonDataTableRow {
|
||||
basicDataRow := t.innerIterator.Next()
|
||||
|
||||
if basicDataRow == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
rowData := make(map[string]string, len(t.commonDataTable.dataColumnIndexes))
|
||||
|
||||
for column, columnIndex := range t.commonDataTable.dataColumnIndexes {
|
||||
if columnIndex < 0 || columnIndex >= basicDataRow.ColumnCount() {
|
||||
continue
|
||||
}
|
||||
|
||||
value := basicDataRow.GetData(columnIndex)
|
||||
rowData[column] = value
|
||||
}
|
||||
|
||||
return &basicDataTableToCommonDataTableWrapperRow{
|
||||
rowData: rowData,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateNewCommonDataTableFromBasicDataTable returns common data table from basic data table
|
||||
func CreateNewCommonDataTableFromBasicDataTable(dataTable BasicDataTable) CommonDataTable {
|
||||
headerLineItems := dataTable.HeaderColumnNames()
|
||||
dataColumnIndexes := make(map[string]int, len(headerLineItems))
|
||||
|
||||
for i := 0; i < len(headerLineItems); i++ {
|
||||
dataColumnIndexes[headerLineItems[i]] = i
|
||||
}
|
||||
|
||||
return &basicDataTableToCommonDataTableWrapper{
|
||||
innerDataTable: dataTable,
|
||||
dataColumnIndexes: dataColumnIndexes,
|
||||
}
|
||||
}
|
||||
+34
-34
@@ -7,30 +7,30 @@ import (
|
||||
"github.com/mayswind/ezbookkeeping/pkg/models"
|
||||
)
|
||||
|
||||
// ImportedTransactionDataTable defines the structure of imported transaction data table
|
||||
type ImportedTransactionDataTable struct {
|
||||
innerDataTable ImportedDataTable
|
||||
// basicDataTableToTransactionDataTableWrapper defines the structure of basic data table to transaction data table wrapper
|
||||
type basicDataTableToTransactionDataTableWrapper struct {
|
||||
innerDataTable BasicDataTable
|
||||
dataColumnMapping map[TransactionDataTableColumn]string
|
||||
dataColumnIndexes map[TransactionDataTableColumn]int
|
||||
rowParser TransactionDataRowParser
|
||||
addedColumns map[TransactionDataTableColumn]bool
|
||||
}
|
||||
|
||||
// ImportedTransactionDataRow defines the structure of imported transaction data row
|
||||
type ImportedTransactionDataRow struct {
|
||||
transactionDataTable *ImportedTransactionDataTable
|
||||
// basicDataTableToTransactionDataTableWrapperRow defines the data row structure of basic data table to transaction data table wrapper
|
||||
type basicDataTableToTransactionDataTableWrapperRow struct {
|
||||
transactionDataTable *basicDataTableToTransactionDataTableWrapper
|
||||
rowData map[TransactionDataTableColumn]string
|
||||
rowDataValid bool
|
||||
}
|
||||
|
||||
// ImportedTransactionDataRowIterator defines the structure of imported transaction data row iterator
|
||||
type ImportedTransactionDataRowIterator struct {
|
||||
transactionDataTable *ImportedTransactionDataTable
|
||||
innerIterator ImportedDataRowIterator
|
||||
// basicDataTableToTransactionDataTableWrapperRowIterator defines the data row iterator structure of basic data table to transaction data table wrapper
|
||||
type basicDataTableToTransactionDataTableWrapperRowIterator struct {
|
||||
transactionDataTable *basicDataTableToTransactionDataTableWrapper
|
||||
innerIterator BasicDataTableRowIterator
|
||||
}
|
||||
|
||||
// HasColumn returns whether the data table has specified column
|
||||
func (t *ImportedTransactionDataTable) HasColumn(column TransactionDataTableColumn) bool {
|
||||
func (t *basicDataTableToTransactionDataTableWrapper) HasColumn(column TransactionDataTableColumn) bool {
|
||||
index, exists := t.dataColumnIndexes[column]
|
||||
|
||||
if exists && index >= 0 {
|
||||
@@ -49,25 +49,25 @@ func (t *ImportedTransactionDataTable) HasColumn(column TransactionDataTableColu
|
||||
}
|
||||
|
||||
// TransactionRowCount returns the total count of transaction data row
|
||||
func (t *ImportedTransactionDataTable) TransactionRowCount() int {
|
||||
func (t *basicDataTableToTransactionDataTableWrapper) TransactionRowCount() int {
|
||||
return t.innerDataTable.DataRowCount()
|
||||
}
|
||||
|
||||
// TransactionRowIterator returns the iterator of transaction data row
|
||||
func (t *ImportedTransactionDataTable) TransactionRowIterator() TransactionDataRowIterator {
|
||||
return &ImportedTransactionDataRowIterator{
|
||||
func (t *basicDataTableToTransactionDataTableWrapper) TransactionRowIterator() TransactionDataRowIterator {
|
||||
return &basicDataTableToTransactionDataTableWrapperRowIterator{
|
||||
transactionDataTable: t,
|
||||
innerIterator: t.innerDataTable.DataRowIterator(),
|
||||
}
|
||||
}
|
||||
|
||||
// IsValid returns whether this row is valid data for importing
|
||||
func (r *ImportedTransactionDataRow) IsValid() bool {
|
||||
func (r *basicDataTableToTransactionDataTableWrapperRow) IsValid() bool {
|
||||
return r.rowDataValid
|
||||
}
|
||||
|
||||
// GetData returns the data in the specified column type
|
||||
func (r *ImportedTransactionDataRow) GetData(column TransactionDataTableColumn) string {
|
||||
func (r *basicDataTableToTransactionDataTableWrapperRow) GetData(column TransactionDataTableColumn) string {
|
||||
if !r.rowDataValid {
|
||||
return ""
|
||||
}
|
||||
@@ -90,28 +90,28 @@ func (r *ImportedTransactionDataRow) GetData(column TransactionDataTableColumn)
|
||||
}
|
||||
|
||||
// HasNext returns whether the iterator does not reach the end
|
||||
func (t *ImportedTransactionDataRowIterator) HasNext() bool {
|
||||
func (t *basicDataTableToTransactionDataTableWrapperRowIterator) HasNext() bool {
|
||||
return t.innerIterator.HasNext()
|
||||
}
|
||||
|
||||
// Next returns the next transaction data row
|
||||
func (t *ImportedTransactionDataRowIterator) Next(ctx core.Context, user *models.User) (daraRow TransactionDataRow, err error) {
|
||||
importedRow := t.innerIterator.Next()
|
||||
func (t *basicDataTableToTransactionDataTableWrapperRowIterator) Next(ctx core.Context, user *models.User) (daraRow TransactionDataRow, err error) {
|
||||
basicDataRow := t.innerIterator.Next()
|
||||
|
||||
if importedRow == nil {
|
||||
if basicDataRow == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if importedRow.ColumnCount() == 1 && importedRow.GetData(0) == "" {
|
||||
return &ImportedTransactionDataRow{
|
||||
if basicDataRow.ColumnCount() == 1 && basicDataRow.GetData(0) == "" {
|
||||
return &basicDataTableToTransactionDataTableWrapperRow{
|
||||
transactionDataTable: t.transactionDataTable,
|
||||
rowData: nil,
|
||||
rowDataValid: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
if importedRow.ColumnCount() < len(t.transactionDataTable.dataColumnIndexes) {
|
||||
log.Errorf(ctx, "[imported_transaction_data_table.Next] cannot parse data row, because may missing some columns (column count %d in data row is less than header column count %d)", importedRow.ColumnCount(), len(t.transactionDataTable.dataColumnIndexes))
|
||||
if basicDataRow.ColumnCount() < len(t.transactionDataTable.dataColumnIndexes) {
|
||||
log.Errorf(ctx, "[basic_data_table_to_transaction_data_table_wrapper.Next] cannot parse data row, because may missing some columns (column count %d in data row is less than header column count %d)", basicDataRow.ColumnCount(), len(t.transactionDataTable.dataColumnIndexes))
|
||||
return nil, errs.ErrFewerFieldsInDataRowThanInHeaderRow
|
||||
}
|
||||
|
||||
@@ -119,11 +119,11 @@ func (t *ImportedTransactionDataRowIterator) Next(ctx core.Context, user *models
|
||||
rowDataValid := true
|
||||
|
||||
for column, columnIndex := range t.transactionDataTable.dataColumnIndexes {
|
||||
if columnIndex < 0 || columnIndex >= importedRow.ColumnCount() {
|
||||
if columnIndex < 0 || columnIndex >= basicDataRow.ColumnCount() {
|
||||
continue
|
||||
}
|
||||
|
||||
value := importedRow.GetData(columnIndex)
|
||||
value := basicDataRow.GetData(columnIndex)
|
||||
rowData[column] = value
|
||||
}
|
||||
|
||||
@@ -131,25 +131,25 @@ func (t *ImportedTransactionDataRowIterator) Next(ctx core.Context, user *models
|
||||
rowData, rowDataValid, err = t.transactionDataTable.rowParser.Parse(rowData)
|
||||
|
||||
if err != nil {
|
||||
log.Errorf(ctx, "[imported_transaction_data_table.Next] cannot parse data row, because %s", err.Error())
|
||||
log.Errorf(ctx, "[basic_data_table_to_transaction_data_table_wrapper.Next] cannot parse data row, because %s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &ImportedTransactionDataRow{
|
||||
return &basicDataTableToTransactionDataTableWrapperRow{
|
||||
transactionDataTable: t.transactionDataTable,
|
||||
rowData: rowData,
|
||||
rowDataValid: rowDataValid,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CreateNewImportedTransactionDataTable returns transaction data table from imported data table
|
||||
func CreateNewImportedTransactionDataTable(dataTable ImportedDataTable, dataColumnMapping map[TransactionDataTableColumn]string) *ImportedTransactionDataTable {
|
||||
return CreateNewImportedTransactionDataTableWithRowParser(dataTable, dataColumnMapping, nil)
|
||||
// CreateNewTransactionDataTableFromBasicDataTable returns transaction data table from basic data table
|
||||
func CreateNewTransactionDataTableFromBasicDataTable(dataTable BasicDataTable, dataColumnMapping map[TransactionDataTableColumn]string) TransactionDataTable {
|
||||
return CreateNewTransactionDataTableFromBasicDataTableWithRowParser(dataTable, dataColumnMapping, nil)
|
||||
}
|
||||
|
||||
// CreateNewImportedTransactionDataTableWithRowParser returns transaction data table from imported data table
|
||||
func CreateNewImportedTransactionDataTableWithRowParser(dataTable ImportedDataTable, dataColumnMapping map[TransactionDataTableColumn]string, rowParser TransactionDataRowParser) *ImportedTransactionDataTable {
|
||||
// CreateNewTransactionDataTableFromBasicDataTableWithRowParser returns transaction data table from basic data table
|
||||
func CreateNewTransactionDataTableFromBasicDataTableWithRowParser(dataTable BasicDataTable, dataColumnMapping map[TransactionDataTableColumn]string, rowParser TransactionDataRowParser) TransactionDataTable {
|
||||
headerLineItems := dataTable.HeaderColumnNames()
|
||||
headerItemMap := make(map[string]int, len(headerLineItems))
|
||||
|
||||
@@ -178,7 +178,7 @@ func CreateNewImportedTransactionDataTableWithRowParser(dataTable ImportedDataTa
|
||||
}
|
||||
}
|
||||
|
||||
return &ImportedTransactionDataTable{
|
||||
return &basicDataTableToTransactionDataTableWrapper{
|
||||
innerDataTable: dataTable,
|
||||
dataColumnMapping: dataColumnMapping,
|
||||
dataColumnIndexes: dataColumnIndexes,
|
||||
@@ -12,11 +12,11 @@ type CommonDataTable interface {
|
||||
DataRowCount() int
|
||||
|
||||
// DataRowIterator returns the iterator of common data row
|
||||
DataRowIterator() CommonDataRowIterator
|
||||
DataRowIterator() CommonDataTableRowIterator
|
||||
}
|
||||
|
||||
// CommonDataRow defines the structure of common data row
|
||||
type CommonDataRow interface {
|
||||
// CommonDataTableRow defines the structure of common data row
|
||||
type CommonDataTableRow interface {
|
||||
// ColumnCount returns the total count of column in this data row
|
||||
ColumnCount() int
|
||||
|
||||
@@ -27,8 +27,8 @@ type CommonDataRow interface {
|
||||
GetData(columnName string) string
|
||||
}
|
||||
|
||||
// CommonDataRowIterator defines the structure of common data row iterator
|
||||
type CommonDataRowIterator interface {
|
||||
// CommonDataTableRowIterator defines the structure of common data row iterator
|
||||
type CommonDataTableRowIterator interface {
|
||||
// HasNext returns whether the iterator does not reach the end
|
||||
HasNext() bool
|
||||
|
||||
@@ -36,5 +36,5 @@ type CommonDataRowIterator interface {
|
||||
CurrentRowId() string
|
||||
|
||||
// Next returns the next common data row
|
||||
Next() CommonDataRow
|
||||
Next() CommonDataTableRow
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package datatable
|
||||
|
||||
import (
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/log"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/models"
|
||||
)
|
||||
|
||||
// CommonTransactionDataRowParser defines the structure of common transaction data row parser
|
||||
type CommonTransactionDataRowParser interface {
|
||||
// Parse returns the converted transaction data row
|
||||
Parse(ctx core.Context, user *models.User, dataRow CommonDataTableRow, rowId string) (rowData map[TransactionDataTableColumn]string, rowDataValid bool, err error)
|
||||
}
|
||||
|
||||
// commonDataTableToTransactionDataTableWrapper defines the structure of common data table to transaction data table wrapper
|
||||
type commonDataTableToTransactionDataTableWrapper struct {
|
||||
innerDataTable CommonDataTable
|
||||
supportedDataColumns map[TransactionDataTableColumn]bool
|
||||
rowParser CommonTransactionDataRowParser
|
||||
}
|
||||
|
||||
// commonDataTableToTransactionDataTableWrapperRow defines the data row structure of common data table to transaction data table wrapper
|
||||
type commonDataTableToTransactionDataTableWrapperRow struct {
|
||||
transactionDataTable *commonDataTableToTransactionDataTableWrapper
|
||||
rowData map[TransactionDataTableColumn]string
|
||||
rowDataValid bool
|
||||
}
|
||||
|
||||
// commonDataTableToTransactionDataTableWrapperRowIterator defines the data row iterator structure of common data table to transaction data table wrapper
|
||||
type commonDataTableToTransactionDataTableWrapperRowIterator struct {
|
||||
transactionDataTable *commonDataTableToTransactionDataTableWrapper
|
||||
innerIterator CommonDataTableRowIterator
|
||||
}
|
||||
|
||||
// HasColumn returns whether the data table has specified column
|
||||
func (t *commonDataTableToTransactionDataTableWrapper) HasColumn(column TransactionDataTableColumn) bool {
|
||||
_, exists := t.supportedDataColumns[column]
|
||||
return exists
|
||||
}
|
||||
|
||||
// TransactionRowCount returns the total count of transaction data row
|
||||
func (t *commonDataTableToTransactionDataTableWrapper) TransactionRowCount() int {
|
||||
return t.innerDataTable.DataRowCount()
|
||||
}
|
||||
|
||||
// TransactionRowIterator returns the iterator of transaction data row
|
||||
func (t *commonDataTableToTransactionDataTableWrapper) TransactionRowIterator() TransactionDataRowIterator {
|
||||
return &commonDataTableToTransactionDataTableWrapperRowIterator{
|
||||
transactionDataTable: t,
|
||||
innerIterator: t.innerDataTable.DataRowIterator(),
|
||||
}
|
||||
}
|
||||
|
||||
// IsValid returns whether this row is valid data for importing
|
||||
func (r *commonDataTableToTransactionDataTableWrapperRow) IsValid() bool {
|
||||
return r.rowDataValid
|
||||
}
|
||||
|
||||
// GetData returns the data in the specified column type
|
||||
func (r *commonDataTableToTransactionDataTableWrapperRow) GetData(column TransactionDataTableColumn) string {
|
||||
if !r.rowDataValid {
|
||||
return ""
|
||||
}
|
||||
|
||||
_, exists := r.transactionDataTable.supportedDataColumns[column]
|
||||
|
||||
if !exists {
|
||||
return ""
|
||||
}
|
||||
|
||||
return r.rowData[column]
|
||||
}
|
||||
|
||||
// HasNext returns whether the iterator does not reach the end
|
||||
func (t *commonDataTableToTransactionDataTableWrapperRowIterator) HasNext() bool {
|
||||
return t.innerIterator.HasNext()
|
||||
}
|
||||
|
||||
// Next returns the next transaction data row
|
||||
func (t *commonDataTableToTransactionDataTableWrapperRowIterator) Next(ctx core.Context, user *models.User) (daraRow TransactionDataRow, err error) {
|
||||
commonDataRow := t.innerIterator.Next()
|
||||
|
||||
if commonDataRow == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
rowId := t.innerIterator.CurrentRowId()
|
||||
rowData, rowDataValid, err := t.transactionDataTable.rowParser.Parse(ctx, user, commonDataRow, rowId)
|
||||
|
||||
if err != nil {
|
||||
log.Errorf(ctx, "[common_data_table_to_transaction_data_table_wrapper.Next] cannot parse data row, because %s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &commonDataTableToTransactionDataTableWrapperRow{
|
||||
transactionDataTable: t.transactionDataTable,
|
||||
rowData: rowData,
|
||||
rowDataValid: rowDataValid,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CreateNewTransactionDataTableFromCommonDataTable returns transaction data table from Common data table
|
||||
func CreateNewTransactionDataTableFromCommonDataTable(dataTable CommonDataTable, supportedDataColumns map[TransactionDataTableColumn]bool, rowParser CommonTransactionDataRowParser) TransactionDataTable {
|
||||
return &commonDataTableToTransactionDataTableWrapper{
|
||||
innerDataTable: dataTable,
|
||||
supportedDataColumns: supportedDataColumns,
|
||||
rowParser: rowParser,
|
||||
}
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
package datatable
|
||||
|
||||
import (
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/log"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/models"
|
||||
)
|
||||
|
||||
// CommonTransactionDataTable defines the structure of common transaction data table
|
||||
type CommonTransactionDataTable struct {
|
||||
innerDataTable CommonDataTable
|
||||
supportedDataColumns map[TransactionDataTableColumn]bool
|
||||
rowParser CommonTransactionDataRowParser
|
||||
}
|
||||
|
||||
// CommonTransactionDataRow defines the structure of common transaction data row
|
||||
type CommonTransactionDataRow struct {
|
||||
transactionDataTable *CommonTransactionDataTable
|
||||
rowData map[TransactionDataTableColumn]string
|
||||
rowDataValid bool
|
||||
}
|
||||
|
||||
// CommonTransactionDataRowIterator defines the structure of common transaction data row iterator
|
||||
type CommonTransactionDataRowIterator struct {
|
||||
transactionDataTable *CommonTransactionDataTable
|
||||
innerIterator CommonDataRowIterator
|
||||
}
|
||||
|
||||
// CommonTransactionDataRowParser defines the structure of common transaction data row parser
|
||||
type CommonTransactionDataRowParser interface {
|
||||
// Parse returns the converted transaction data row
|
||||
Parse(ctx core.Context, user *models.User, dataTable *CommonTransactionDataTable, dataRow CommonDataRow, rowId string) (rowData map[TransactionDataTableColumn]string, rowDataValid bool, err error)
|
||||
}
|
||||
|
||||
// HasColumn returns whether the data table has specified column
|
||||
func (t *CommonTransactionDataTable) HasColumn(column TransactionDataTableColumn) bool {
|
||||
_, exists := t.supportedDataColumns[column]
|
||||
return exists
|
||||
}
|
||||
|
||||
// HasOriginalColumn returns whether the original data table has specified column name
|
||||
func (t *CommonTransactionDataTable) HasOriginalColumn(columnName string) bool {
|
||||
return columnName != "" && t.innerDataTable.HasColumn(columnName)
|
||||
}
|
||||
|
||||
// TransactionRowCount returns the total count of transaction data row
|
||||
func (t *CommonTransactionDataTable) TransactionRowCount() int {
|
||||
return t.innerDataTable.DataRowCount()
|
||||
}
|
||||
|
||||
// TransactionRowIterator returns the iterator of transaction data row
|
||||
func (t *CommonTransactionDataTable) TransactionRowIterator() TransactionDataRowIterator {
|
||||
return &CommonTransactionDataRowIterator{
|
||||
transactionDataTable: t,
|
||||
innerIterator: t.innerDataTable.DataRowIterator(),
|
||||
}
|
||||
}
|
||||
|
||||
// IsValid returns whether this row is valid data for importing
|
||||
func (r *CommonTransactionDataRow) IsValid() bool {
|
||||
return r.rowDataValid
|
||||
}
|
||||
|
||||
// GetData returns the data in the specified column type
|
||||
func (r *CommonTransactionDataRow) GetData(column TransactionDataTableColumn) string {
|
||||
if !r.rowDataValid {
|
||||
return ""
|
||||
}
|
||||
|
||||
_, exists := r.transactionDataTable.supportedDataColumns[column]
|
||||
|
||||
if !exists {
|
||||
return ""
|
||||
}
|
||||
|
||||
return r.rowData[column]
|
||||
}
|
||||
|
||||
// HasNext returns whether the iterator does not reach the end
|
||||
func (t *CommonTransactionDataRowIterator) HasNext() bool {
|
||||
return t.innerIterator.HasNext()
|
||||
}
|
||||
|
||||
// Next returns the next transaction data row
|
||||
func (t *CommonTransactionDataRowIterator) Next(ctx core.Context, user *models.User) (daraRow TransactionDataRow, err error) {
|
||||
commonRow := t.innerIterator.Next()
|
||||
|
||||
if commonRow == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
rowId := t.innerIterator.CurrentRowId()
|
||||
rowData, rowDataValid, err := t.transactionDataTable.rowParser.Parse(ctx, user, t.transactionDataTable, commonRow, rowId)
|
||||
|
||||
if err != nil {
|
||||
log.Errorf(ctx, "[common_transaction_data_table.Next] cannot parse data row, because %s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &CommonTransactionDataRow{
|
||||
transactionDataTable: t.transactionDataTable,
|
||||
rowData: rowData,
|
||||
rowDataValid: rowDataValid,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CreateNewCommonTransactionDataTable returns transaction data table from Common data table
|
||||
func CreateNewCommonTransactionDataTable(dataTable CommonDataTable, supportedDataColumns map[TransactionDataTableColumn]bool, rowParser CommonTransactionDataRowParser) *CommonTransactionDataTable {
|
||||
return &CommonTransactionDataTable{
|
||||
innerDataTable: dataTable,
|
||||
supportedDataColumns: supportedDataColumns,
|
||||
rowParser: rowParser,
|
||||
}
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
package datatable
|
||||
|
||||
// ImportedCommonDataTable defines the structure of imported common data table
|
||||
type ImportedCommonDataTable struct {
|
||||
innerDataTable ImportedDataTable
|
||||
dataColumnIndexes map[string]int
|
||||
}
|
||||
|
||||
// ImportedCommonDataRow defines the structure of imported common data row
|
||||
type ImportedCommonDataRow struct {
|
||||
rowData map[string]string
|
||||
}
|
||||
|
||||
// ImportedCommonDataRowIterator defines the structure of imported common data row iterator
|
||||
type ImportedCommonDataRowIterator struct {
|
||||
commonDataTable *ImportedCommonDataTable
|
||||
innerIterator ImportedDataRowIterator
|
||||
}
|
||||
|
||||
// HeaderColumnCount returns the total count of column in header row
|
||||
func (t *ImportedCommonDataTable) HeaderColumnCount() int {
|
||||
return len(t.innerDataTable.HeaderColumnNames())
|
||||
}
|
||||
|
||||
// HasColumn returns whether the data table has specified column name
|
||||
func (t *ImportedCommonDataTable) HasColumn(columnName string) bool {
|
||||
index, exists := t.dataColumnIndexes[columnName]
|
||||
return exists && index >= 0
|
||||
}
|
||||
|
||||
// DataRowCount returns the total count of common data row
|
||||
func (t *ImportedCommonDataTable) DataRowCount() int {
|
||||
return t.innerDataTable.DataRowCount()
|
||||
}
|
||||
|
||||
// DataRowIterator returns the iterator of common data row
|
||||
func (t *ImportedCommonDataTable) DataRowIterator() CommonDataRowIterator {
|
||||
return &ImportedCommonDataRowIterator{
|
||||
commonDataTable: t,
|
||||
innerIterator: t.innerDataTable.DataRowIterator(),
|
||||
}
|
||||
}
|
||||
|
||||
// HasData returns whether the common data row has specified column data
|
||||
func (r *ImportedCommonDataRow) HasData(columnName string) bool {
|
||||
_, exists := r.rowData[columnName]
|
||||
return exists
|
||||
}
|
||||
|
||||
// ColumnCount returns the total count of column in this data row
|
||||
func (r *ImportedCommonDataRow) ColumnCount() int {
|
||||
return len(r.rowData)
|
||||
}
|
||||
|
||||
// GetData returns the data in the specified column name
|
||||
func (r *ImportedCommonDataRow) GetData(columnName string) string {
|
||||
return r.rowData[columnName]
|
||||
}
|
||||
|
||||
// HasNext returns whether the iterator does not reach the end
|
||||
func (t *ImportedCommonDataRowIterator) HasNext() bool {
|
||||
return t.innerIterator.HasNext()
|
||||
}
|
||||
|
||||
// CurrentRowId returns current row id
|
||||
func (t *ImportedCommonDataRowIterator) CurrentRowId() string {
|
||||
return t.innerIterator.CurrentRowId()
|
||||
}
|
||||
|
||||
// Next returns the next common data row
|
||||
func (t *ImportedCommonDataRowIterator) Next() CommonDataRow {
|
||||
importedRow := t.innerIterator.Next()
|
||||
|
||||
if importedRow == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
rowData := make(map[string]string, len(t.commonDataTable.dataColumnIndexes))
|
||||
|
||||
for column, columnIndex := range t.commonDataTable.dataColumnIndexes {
|
||||
if columnIndex < 0 || columnIndex >= importedRow.ColumnCount() {
|
||||
continue
|
||||
}
|
||||
|
||||
value := importedRow.GetData(columnIndex)
|
||||
rowData[column] = value
|
||||
}
|
||||
|
||||
return &ImportedCommonDataRow{
|
||||
rowData: rowData,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateNewImportedCommonDataTable returns common data table from imported data table
|
||||
func CreateNewImportedCommonDataTable(dataTable ImportedDataTable) *ImportedCommonDataTable {
|
||||
headerLineItems := dataTable.HeaderColumnNames()
|
||||
dataColumnIndexes := make(map[string]int, len(headerLineItems))
|
||||
|
||||
for i := 0; i < len(headerLineItems); i++ {
|
||||
dataColumnIndexes[headerLineItems[i]] = i
|
||||
}
|
||||
|
||||
return &ImportedCommonDataTable{
|
||||
innerDataTable: dataTable,
|
||||
dataColumnIndexes: dataColumnIndexes,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user