Debian Codegraph @

swift/element.go

raw
1 package swift
2
3 import (
4 "bytes"
5 "fmt"
6 "strconv"
7
8 "github.com/mitch000001/go-hbci/charset"
9 "github.com/mitch000001/go-hbci/internal"
10 )
11
12 // A Tag represents a S.W.I.F.T. tag
13 type Tag interface {
14 Unmarshal([]byte) error
15 Value() interface{}
16 ID() string
17 }
18
19 type tag struct {
20 id string
21 value interface{}
22 }
23
24 func (t *tag) ID() string { return t.id }
25 func (t *tag) Value() interface{} { return t.value }
26
27 // An AlphaNumericTag represents alphanumeric data in a S.W.I.F.T. tag
28 type AlphaNumericTag struct {
29 *tag
30 }
31
32 // Unmarshal unmarshals value into a
33 func (a *AlphaNumericTag) Unmarshal(value []byte) error {
34 elements, err := extractTagElements(value)
35 if err != nil {
36 return err
37 }
38 if len(elements) != 2 {
39 return fmt.Errorf("%T: Malformed marshaled value", a)
40 }
41 id := string(elements[0])
42 val := string(elements[1])
43 a.tag = &tag{id: id, value: val}
44 return nil
45 }
46
47 // Val returns the string embodied in a
48 func (a *AlphaNumericTag) Val() string {
49 return a.value.(string)
50 }
51
52 // A NumberTag represents numeric data in a S.W.I.F.T. tag
53 type NumberTag struct {
54 *tag
55 }
56
57 // Unmarshal unmarshals value into n
58 func (n *NumberTag) Unmarshal(value []byte) error {
59 elements, err := extractTagElements(value)
60 if err != nil {
61 return err
62 }
63 if len(elements) != 2 {
64 return fmt.Errorf("%T: Malformed marshaled value", n)
65 }
66 id := string(elements[0])
67 num, err := strconv.Atoi(string(elements[1]))
68 if err != nil {
69 return fmt.Errorf("%T: Error while unmarshaling: %v", n, err)
70 }
71 n.tag = &tag{id: id, value: num}
72 return nil
73 }
74
75 // Val returns the int embodied in n
76 func (n *NumberTag) Val() int {
77 return n.value.(int)
78 }
79
80 // A FloatTag represents float data in a S.W.I.F.T. tag
81 type FloatTag struct {
82 *tag
83 }
84
85 // Unmarshal unmarshals value into f
86 func (f *FloatTag) Unmarshal(value []byte) error {
87 elements, err := extractTagElements(value)
88 if err != nil {
89 return err
90 }
91 if len(elements) != 2 {
92 return fmt.Errorf("%T: Malformed marshaled value", f)
93 }
94 id := string(elements[0])
95 num, err := strconv.ParseFloat(string(elements[1]), 64)
96 if err != nil {
97 return fmt.Errorf("%T: Error while unmarshaling: %v", f, err)
98 }
99 f.tag = &tag{id: id, value: num}
100 return nil
101 }
102
103 // Val returns the value of f
104 func (f *FloatTag) Val() float64 {
105 return f.value.(float64)
106 }
107
108 // A CustomFieldTag represents holds multiple information about a transaction
109 type CustomFieldTag struct {
110 Tag string
111 TransactionID int
112 BookingText string
113 PrimanotenNumber string
114 Purpose []string
115 BankID string
116 AccountID string
117 Name string
118 MessageKeyAddition int
119 Purpose2 []string
120 }
121
122 var customFieldTagFieldKeys = [][]byte{
123 []byte{'?', '0', '0'},
124 []byte{'?', '1', '0'},
125 []byte{'?', '2', '0'},
126 []byte{'?', '2', '1'},
127 []byte{'?', '2', '2'},
128 []byte{'?', '2', '3'},
129 []byte{'?', '2', '4'},
130 []byte{'?', '2', '5'},
131 []byte{'?', '2', '6'},
132 []byte{'?', '2', '7'},
133 []byte{'?', '2', '8'},
134 []byte{'?', '2', '9'},
135 []byte{'?', '3', '0'},
136 []byte{'?', '3', '1'},
137 []byte{'?', '3', '2'},
138 []byte{'?', '3', '3'},
139 []byte{'?', '3', '4'},
140 []byte{'?', '6', '0'},
141 []byte{'?', '6', '1'},
142 []byte{'?', '6', '2'},
143 []byte{'?', '6', '3'},
144 }
145
146 // Unmarshal unmarshals the tag bytes into c
147 func (c *CustomFieldTag) Unmarshal(value []byte) error {
148 tag, err := extractRawTag(value)
149 if err != nil {
150 return err
151 }
152 c.Tag = tag.ID
153 tID, err := strconv.Atoi(charset.ToUTF8(tag.Value[:3]))
154 if err != nil {
155 return err
156 }
157 c.TransactionID = tID
158 marshaledFields := tag.Value[3:]
159 marshaledFields = bytes.Replace(
160 marshaledFields, []byte{'\r', '\n'}, []byte{}, -1,
161 )
162 var fields []fieldKeyIndex
163 for _, fieldKey := range customFieldTagFieldKeys {
164 if idx := bytes.Index(marshaledFields, fieldKey); idx != -1 {
165 fields = append(fields, fieldKeyIndex{fieldKey, idx})
166 }
167 }
168
169 getFieldValue := func(currentFieldKeyIndex, nextFieldKeyIndex int) string {
170 return charset.ToUTF8(
171 marshaledFields[currentFieldKeyIndex+3 : nextFieldKeyIndex],
172 )
173 }
174 for i, fieldKeyIndex := range fields {
175 var nextFieldKeyIndex int
176 if len(fields)-1 == i {
177 nextFieldKeyIndex = len(marshaledFields)
178 } else {
179 nextFieldKeyIndex = fields[i+1].index
180 }
181
182 fieldValue := getFieldValue(fieldKeyIndex.index, nextFieldKeyIndex)
183
184 switch fieldKey := fieldKeyIndex.fieldKey; {
185 case bytes.HasPrefix(fieldKey, []byte{'?', '0', '0'}):
186 c.BookingText = fieldValue
187 case bytes.HasPrefix(fieldKey, []byte{'?', '1', '0'}):
188 c.PrimanotenNumber = fieldValue
189 case bytes.HasPrefix(fieldKey, []byte{'?', '2'}):
190 c.Purpose = append(c.Purpose, fieldValue)
191 case bytes.HasPrefix(fieldKey, []byte{'?', '3', '0'}):
192 c.BankID = fieldValue
193 case bytes.HasPrefix(fieldKey, []byte{'?', '3', '1'}):
194 c.AccountID = fieldValue
195 case bytes.HasPrefix(fieldKey, []byte{'?', '3', '2'}):
196 c.Name = fieldValue
197 case bytes.HasPrefix(fieldKey, []byte{'?', '3', '3'}):
198 c.Name += " " + fieldValue
199 case bytes.HasPrefix(fieldKey, []byte{'?', '3', '4'}):
200 messageKeyAddition, err := strconv.Atoi(fieldValue)
201 if err != nil {
202 return err
203 }
204 c.MessageKeyAddition = messageKeyAddition
205 case bytes.HasPrefix(fieldKey, []byte{'?', '6'}):
206 c.Purpose2 = append(c.Purpose2, fieldValue)
207 default:
208 internal.Debug.Printf("Unmarshal CustomFieldTag: unknown fieldKey: %s\n", fieldKey)
209 }
210 }
211 return nil
212 }
213
214 type fieldKeyIndex struct {
215 fieldKey []byte
216 index int
217 }