| 1 |
package swift |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
|
| 7 |
"github.com/pkg/errors" |
| 8 |
) |
| 9 |
|
| 10 |
// Unmarshal unmarshals value into m |
| 11 |
func (m *MT940) Unmarshal(value []byte) error { |
| 12 |
tagExtractor := newTagExtractor(value) |
| 13 |
tags, err := tagExtractor.Extract() |
| 14 |
if err != nil { |
| 15 |
return err |
| 16 |
} |
| 17 |
if len(tags) == 0 { |
| 18 |
return fmt.Errorf("Malformed marshaled value") |
| 19 |
} |
| 20 |
balanceTagOpen := false |
| 21 |
for _, tag := range tags { |
| 22 |
|
| 23 |
switch { |
| 24 |
case bytes.HasPrefix(tag, []byte(":20:")): |
| 25 |
m.JobReference = &AlphaNumericTag{} |
| 26 |
err = m.JobReference.Unmarshal(tag) |
| 27 |
if err != nil { |
| 28 |
return err |
| 29 |
} |
| 30 |
case bytes.HasPrefix(tag, []byte(":21:")): |
| 31 |
m.Reference = &AlphaNumericTag{} |
| 32 |
err = m.Reference.Unmarshal(tag) |
| 33 |
if err != nil { |
| 34 |
return err |
| 35 |
} |
| 36 |
case bytes.HasPrefix(tag, []byte(":25:")): |
| 37 |
m.Account = &AccountTag{} |
| 38 |
err = m.Account.Unmarshal(tag) |
| 39 |
if err != nil { |
| 40 |
return err |
| 41 |
} |
| 42 |
case bytes.HasPrefix(tag, []byte(":28C:")): |
| 43 |
m.StatementNumber = &StatementNumberTag{} |
| 44 |
err = m.StatementNumber.Unmarshal(tag) |
| 45 |
if err != nil { |
| 46 |
return err |
| 47 |
} |
| 48 |
case bytes.HasPrefix(tag, []byte(":60")): |
| 49 |
m.StartingBalance = &BalanceTag{} |
| 50 |
err = m.StartingBalance.Unmarshal(tag) |
| 51 |
if err != nil { |
| 52 |
return errors.WithMessage(err, "unmarshal starting balance tag") |
| 53 |
} |
| 54 |
balanceTagOpen = true |
| 55 |
case bytes.HasPrefix(tag, []byte(":62")): |
| 56 |
|
| 57 |
m.ClosingBalance = &BalanceTag{} |
| 58 |
err = m.ClosingBalance.Unmarshal(tag) |
| 59 |
if err != nil { |
| 60 |
return errors.WithMessage(err, "unmarshal closing balance tag") |
| 61 |
} |
| 62 |
|
| 63 |
balanceTagOpen = false |
| 64 |
case bytes.HasPrefix(tag, []byte(":64:")): |
| 65 |
m.CurrentValutaBalance = &BalanceTag{} |
| 66 |
err = m.CurrentValutaBalance.Unmarshal(tag) |
| 67 |
if err != nil { |
| 68 |
return errors.WithMessage(err, "unmarshal current valuta balance tag") |
| 69 |
} |
| 70 |
case bytes.HasPrefix(tag, []byte(":65:")): |
| 71 |
m.FutureValutaBalance = &BalanceTag{} |
| 72 |
err = m.FutureValutaBalance.Unmarshal(tag) |
| 73 |
if err != nil { |
| 74 |
return errors.WithMessage(err, "unmarshal future valuta balance tag") |
| 75 |
} |
| 76 |
case bytes.HasPrefix(tag, []byte(":61:")): |
| 77 |
|
| 78 |
transaction := &TransactionTag{} |
| 79 |
|
| 80 |
err = transaction.Unmarshal(tag) |
| 81 |
if err != nil { |
| 82 |
return err |
| 83 |
} |
| 84 |
m.Transactions = append(m.Transactions, &TransactionSequence{Transaction: transaction}) |
| 85 |
case bytes.HasPrefix(tag, []byte(":86:")): |
| 86 |
customField := &CustomFieldTag{} |
| 87 |
err = customField.Unmarshal(tag) |
| 88 |
if err != nil { |
| 89 |
return err |
| 90 |
} |
| 91 |
if balanceTagOpen { |
| 92 |
indexLastSliceitem := len(m.Transactions) - 1 |
| 93 |
if indexLastSliceitem < 0 { |
| 94 |
return errors.New("Unexpected CustomTag before first TransactionTag") |
| 95 |
} |
| 96 |
if m.Transactions[indexLastSliceitem].Description != nil { |
| 97 |
return errors.Errorf("Unexpected CustomTag: CustomTag would replace Description of %v", m.Transactions[indexLastSliceitem]) |
| 98 |
} |
| 99 |
m.Transactions[indexLastSliceitem].Description = customField |
| 100 |
} else { |
| 101 |
m.CustomField = customField |
| 102 |
} |
| 103 |
default: |
| 104 |
return fmt.Errorf("Malformed marshaled value") |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return nil |
| 109 |
} |