File: mt940_unmarshaler.go

package info (click to toggle)
golang-github-mitch000001-go-hbci 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,468 kB
  • sloc: java: 1,092; makefile: 5
file content (109 lines) | stat: -rw-r--r-- 2,970 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package swift

import (
	"bytes"
	"fmt"

	"github.com/pkg/errors"
)

// Unmarshal unmarshals value into m
func (m *MT940) Unmarshal(value []byte) error {
	tagExtractor := newTagExtractor(value)
	tags, err := tagExtractor.Extract()
	if err != nil {
		return err
	}
	if len(tags) == 0 {
		return fmt.Errorf("Malformed marshaled value")
	}
	balanceTagOpen := false
	for _, tag := range tags {

		switch {
		case bytes.HasPrefix(tag, []byte(":20:")):
			m.JobReference = &AlphaNumericTag{}
			err = m.JobReference.Unmarshal(tag)
			if err != nil {
				return err
			}
		case bytes.HasPrefix(tag, []byte(":21:")):
			m.Reference = &AlphaNumericTag{}
			err = m.Reference.Unmarshal(tag)
			if err != nil {
				return err
			}
		case bytes.HasPrefix(tag, []byte(":25:")):
			m.Account = &AccountTag{}
			err = m.Account.Unmarshal(tag)
			if err != nil {
				return err
			}
		case bytes.HasPrefix(tag, []byte(":28C:")):
			m.StatementNumber = &StatementNumberTag{}
			err = m.StatementNumber.Unmarshal(tag)
			if err != nil {
				return err
			}
		case bytes.HasPrefix(tag, []byte(":60")):
			m.StartingBalance = &BalanceTag{}
			err = m.StartingBalance.Unmarshal(tag)
			if err != nil {
				return errors.WithMessage(err, "unmarshal starting balance tag")
			}
			balanceTagOpen = true
		case bytes.HasPrefix(tag, []byte(":62")):

			m.ClosingBalance = &BalanceTag{}
			err = m.ClosingBalance.Unmarshal(tag)
			if err != nil {
				return errors.WithMessage(err, "unmarshal closing balance tag")
			}

			balanceTagOpen = false
		case bytes.HasPrefix(tag, []byte(":64:")):
			m.CurrentValutaBalance = &BalanceTag{}
			err = m.CurrentValutaBalance.Unmarshal(tag)
			if err != nil {
				return errors.WithMessage(err, "unmarshal current valuta balance tag")
			}
		case bytes.HasPrefix(tag, []byte(":65:")):
			m.FutureValutaBalance = &BalanceTag{}
			err = m.FutureValutaBalance.Unmarshal(tag)
			if err != nil {
				return errors.WithMessage(err, "unmarshal future valuta balance tag")
			}
		case bytes.HasPrefix(tag, []byte(":61:")):

			transaction := &TransactionTag{}

			err = transaction.Unmarshal(tag)
			if err != nil {
				return err
			}
			m.Transactions = append(m.Transactions, &TransactionSequence{Transaction: transaction})
		case bytes.HasPrefix(tag, []byte(":86:")):
			customField := &CustomFieldTag{}
			err = customField.Unmarshal(tag)
			if err != nil {
				return err
			}
			if balanceTagOpen {
				indexLastSliceitem := len(m.Transactions) - 1
				if indexLastSliceitem < 0 {
					return errors.New("Unexpected CustomTag before first TransactionTag")
				}
				if m.Transactions[indexLastSliceitem].Description != nil {
					return errors.Errorf("Unexpected CustomTag: CustomTag would replace Description of %v", m.Transactions[indexLastSliceitem])
				}
				m.Transactions[indexLastSliceitem].Description = customField
			} else {
				m.CustomField = customField
			}
		default:
			return fmt.Errorf("Malformed marshaled value")
		}
	}

	return nil
}