| 1 |
package swift |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"sort" |
| 6 |
"strings" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/mitch000001/go-hbci/domain" |
| 10 |
) |
| 11 |
|
| 12 |
func NewMT940Messages(data []byte) *MT940Messages { |
| 13 |
return &MT940Messages{ |
| 14 |
Data: data, |
| 15 |
timestamp: time.Now().UnixNano(), |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
type MT940Messages struct { |
| 20 |
Data []byte |
| 21 |
timestamp int64 |
| 22 |
} |
| 23 |
|
| 24 |
func MergeMT940Messages(messages ...*MT940Messages) *MT940Messages { |
| 25 |
sortedMessages := sortedByTimestamp(messages) |
| 26 |
sort.Sort(sortedMessages) |
| 27 |
merged := &MT940Messages{ |
| 28 |
Data: []byte{}, |
| 29 |
timestamp: time.Now().UnixNano(), |
| 30 |
} |
| 31 |
for _, msg := range sortedMessages { |
| 32 |
merged.Data = append(merged.Data, msg.Data...) |
| 33 |
} |
| 34 |
return merged |
| 35 |
} |
| 36 |
|
| 37 |
type sortedByTimestamp []*MT940Messages |
| 38 |
|
| 39 |
// Len is the number of elements in the collection. |
| 40 |
func (s sortedByTimestamp) Len() int { |
| 41 |
return len(s) |
| 42 |
} |
| 43 |
|
| 44 |
// Less reports whether the element with index i |
| 45 |
// must sort before the element with index j. |
| 46 |
// |
| 47 |
// If both Less(i, j) and Less(j, i) are false, |
| 48 |
// then the elements at index i and j are considered equal. |
| 49 |
// Sort may place equal elements in any order in the final result, |
| 50 |
// while Stable preserves the original input order of equal elements. |
| 51 |
// |
| 52 |
// Less must describe a transitive ordering: |
| 53 |
// - if both Less(i, j) and Less(j, k) are true, then Less(i, k) must be true as well. |
| 54 |
// - if both Less(i, j) and Less(j, k) are false, then Less(i, k) must be false as well. |
| 55 |
// |
| 56 |
// Note that floating-point comparison (the < operator on float32 or float64 values) |
| 57 |
// is not a transitive ordering when not-a-number (NaN) values are involved. |
| 58 |
// See Float64Slice.Less for a correct implementation for floating-point values. |
| 59 |
func (s sortedByTimestamp) Less(i int, j int) bool { |
| 60 |
return s[i].timestamp < s[j].timestamp |
| 61 |
} |
| 62 |
|
| 63 |
// Swap swaps the elements with indexes i and j. |
| 64 |
func (s sortedByTimestamp) Swap(i int, j int) { |
| 65 |
s[i], s[j] = s[j], s[i] |
| 66 |
} |
| 67 |
|
| 68 |
type MT940Unmarshaler interface { |
| 69 |
UnmarshalMT940([]byte) ([]domain.AccountTransaction, error) |
| 70 |
} |
| 71 |
|
| 72 |
func NewMT940MessagesUnmarshaler() MT940Unmarshaler { |
| 73 |
return &mt940MessagesUnmarshaler{} |
| 74 |
} |
| 75 |
|
| 76 |
type mt940MessagesUnmarshaler struct{} |
| 77 |
|
| 78 |
func (m *mt940MessagesUnmarshaler) UnmarshalMT940(value []byte) ([]domain.AccountTransaction, error) { |
| 79 |
messageExtractor := NewMessageExtractor(value) |
| 80 |
messages, err := messageExtractor.Extract() |
| 81 |
if err != nil { |
| 82 |
return nil, fmt.Errorf("error extracting messages: %w", err) |
| 83 |
} |
| 84 |
var errors errorList |
| 85 |
var transactions []domain.AccountTransaction |
| 86 |
for _, message := range messages { |
| 87 |
tr := &MT940{} |
| 88 |
err = tr.Unmarshal(message) |
| 89 |
if err != nil { |
| 90 |
errors = append(errors, fmt.Errorf("error unmarshaling MT940: %w", err)) |
| 91 |
} |
| 92 |
transactions = append(transactions, tr.AccountTransactions()...) |
| 93 |
} |
| 94 |
if len(errors) != 0 { |
| 95 |
return nil, errors |
| 96 |
} |
| 97 |
return transactions, nil |
| 98 |
} |
| 99 |
|
| 100 |
type errorList []error |
| 101 |
|
| 102 |
func (e errorList) Error() string { |
| 103 |
errs := make([]string, len(e)) |
| 104 |
for i, err := range e { |
| 105 |
errs[i] = err.Error() |
| 106 |
} |
| 107 |
return strings.Join(errs, ",") |
| 108 |
} |