forked from moov-io/iso8583
-
Notifications
You must be signed in to change notification settings - Fork 2
/
message.go
434 lines (346 loc) · 9.75 KB
/
message.go
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package iso8583
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"regexp"
"sort"
"strconv"
"github.com/mercadolibre/iso8583/field"
"github.com/mercadolibre/iso8583/utils"
)
var _ json.Marshaler = (*Message)(nil)
var _ json.Unmarshaler = (*Message)(nil)
const (
mtiIdx = 0
bitmapIdx = 1
)
type Message struct {
spec *MessageSpec
bitmap *field.Bitmap
// stores all fields according to the spec
fields map[int]field.Field
// tracks which fields were set
fieldsMap map[int]struct{}
}
func NewMessage(spec *MessageSpec) *Message {
fields := spec.CreateMessageFields()
return &Message{
fields: fields,
spec: spec,
fieldsMap: map[int]struct{}{},
}
}
// Deprecated. Use Marshal intead.
func (m *Message) SetData(data interface{}) error {
return m.Marshal(data)
}
func (m *Message) Bitmap() *field.Bitmap {
if m.bitmap != nil {
return m.bitmap
}
m.bitmap = m.fields[bitmapIdx].(*field.Bitmap)
m.bitmap.Reset()
m.fieldsMap[bitmapIdx] = struct{}{}
return m.bitmap
}
func (m *Message) MTI(val string) {
m.fieldsMap[mtiIdx] = struct{}{}
m.fields[mtiIdx].SetBytes([]byte(val))
}
func (m *Message) GetSpec() *MessageSpec {
return m.spec
}
func (m *Message) Field(id int, val string) error {
if f, ok := m.fields[id]; ok {
m.fieldsMap[id] = struct{}{}
return f.SetBytes([]byte(val))
}
return fmt.Errorf("failed to set field %d. ID does not exist", id)
}
func (m *Message) BinaryField(id int, val []byte) error {
if f, ok := m.fields[id]; ok {
m.fieldsMap[id] = struct{}{}
return f.SetBytes(val)
}
return fmt.Errorf("failed to set binary field %d. ID does not exist", id)
}
func (m *Message) GetMTI() (string, error) {
// check index
return m.fields[mtiIdx].String()
}
func (m *Message) GetString(id int) (string, error) {
if f, ok := m.fields[id]; ok {
m.fieldsMap[id] = struct{}{}
return f.String()
}
return "", fmt.Errorf("failed to get string for field %d. ID does not exist", id)
}
func (m *Message) GetBytes(id int) ([]byte, error) {
if f, ok := m.fields[id]; ok {
m.fieldsMap[id] = struct{}{}
return f.Bytes()
}
return nil, fmt.Errorf("failed to get bytes for field %d. ID does not exist", id)
}
func (m *Message) GetField(id int) field.Field {
return m.fields[id]
}
// Fields returns the map of the set fields
func (m *Message) GetFields() map[int]field.Field {
fields := map[int]field.Field{}
for i := range m.fieldsMap {
fields[i] = m.GetField(i)
}
return fields
}
func (m *Message) Pack() ([]byte, error) {
packed := []byte{}
m.Bitmap().Reset()
ids, err := m.packableFieldIDs()
if err != nil {
return nil, fmt.Errorf("failed to pack message: %w", err)
}
for _, id := range ids {
// indexes 0 and 1 are for mti and bitmap
// regular field number startd from index 2
if id < 2 {
continue
}
m.Bitmap().Set(id)
}
// pack fields
for _, i := range ids {
field, ok := m.fields[i]
if !ok {
return nil, fmt.Errorf("failed to pack field %d: no specification found", i)
}
packedField, err := field.Pack()
if err != nil {
return nil, fmt.Errorf("failed to pack field %d (%s): %w", i, field.Spec().Description, err)
}
packed = append(packed, packedField...)
}
return packed, nil
}
func (m *Message) Unpack(src []byte) error {
var off int
// reset fields that were set
m.fieldsMap = map[int]struct{}{}
// This method implicitly also sets m.fieldsMap[bitmapIdx]
m.Bitmap().Reset()
read, err := m.fields[mtiIdx].Unpack(src)
if err != nil {
return fmt.Errorf("failed to unpack MTI: %w", err)
}
m.fieldsMap[mtiIdx] = struct{}{}
off = read
// unpack Bitmap
read, err = m.fields[bitmapIdx].Unpack(src[off:])
if err != nil {
return fmt.Errorf("failed to unpack bitmap: %w", err)
}
off += read
for i := 2; i <= m.Bitmap().Len(); i++ {
if m.Bitmap().IsSet(i) {
fl, ok := m.fields[i]
if !ok {
return fmt.Errorf("failed to unpack field %d: no specification found", i)
}
read, err = fl.Unpack(src[off:])
if err != nil {
return fmt.Errorf("failed to unpack field %d (%s): %w", i, fl.Spec().Description, err)
}
m.fieldsMap[i] = struct{}{}
off += read
}
}
return nil
}
func (m *Message) MarshalJSON() ([]byte, error) {
// by packing the message we will generate bitmap
// create HEX representation
// and validate message against the spec
if _, err := m.Pack(); err != nil {
return nil, err
}
fieldMap := m.GetFields()
strFieldMap := map[string]field.Field{}
for id, field := range fieldMap {
// we don't wish to populate the bitmap in the final
// JSON since it is dynamically generated when packing
// and unpacking anyways.
if id == bitmapIdx {
continue
}
strFieldMap[fmt.Sprint(id)] = field
}
// get only fields that were set
bytes, err := json.Marshal(field.OrderedMap(strFieldMap))
if err != nil {
return nil, utils.NewSafeError(err, "failed to JSON marshal map to bytes")
}
return bytes, nil
}
func (m *Message) UnmarshalJSON(b []byte) error {
var data map[string]json.RawMessage
if err := json.Unmarshal(b, &data); err != nil {
return err
}
for id, rawMsg := range data {
i, err := strconv.Atoi(id)
if err != nil {
return fmt.Errorf("failed to unmarshal field %v: could not convert to int", i)
}
field, ok := m.fields[i]
if !ok {
return fmt.Errorf("failed to unmarshal field %d: no specification found", i)
}
if err := json.Unmarshal(rawMsg, field); err != nil {
return utils.NewSafeErrorf(err, "failed to unmarshal field %v", id)
}
m.fieldsMap[i] = struct{}{}
}
return nil
}
func (m *Message) packableFieldIDs() ([]int, error) {
// Index 1 represent bitmap which is always populated.
populatedFieldIDs := []int{1}
for id := range m.fieldsMap {
// represents the bitmap
if id == 1 {
continue
}
populatedFieldIDs = append(populatedFieldIDs, id)
}
sort.Ints(populatedFieldIDs)
return populatedFieldIDs, nil
}
// Clone clones the message by creating a new message from the binary
// representation of the original message
func (m *Message) Clone() (*Message, error) {
newMessage := NewMessage(m.spec)
bytes, err := m.Pack()
if err != nil {
return nil, err
}
mti, err := m.GetMTI()
if err != nil {
return nil, err
}
newMessage.MTI(mti)
newMessage.Unpack(bytes)
_, err = newMessage.Pack()
if err != nil {
return nil, err
}
return newMessage, nil
}
// Marshal populates message fields with v struct field values. It traverses
// through the message fields and calls Unmarshal(...) on them setting the v If
// v is not a struct or not a pointer to struct then it returns error.
func (m *Message) Marshal(v interface{}) error {
if v == nil {
return nil
}
dataStruct := reflect.ValueOf(v)
if dataStruct.Kind() == reflect.Ptr || dataStruct.Kind() == reflect.Interface {
dataStruct = dataStruct.Elem()
}
if dataStruct.Kind() != reflect.Struct {
return errors.New("data is not a struct")
}
// iterate over struct fields
for i := 0; i < dataStruct.NumField(); i++ {
fieldIndex, err := getFieldIndex(dataStruct.Type().Field(i))
if err != nil {
return fmt.Errorf("getting field %d index: %w", i, err)
}
// skip field without index
if fieldIndex < 0 {
continue
}
messageField := m.GetField(fieldIndex)
// if struct field we are usgin to populate value expects to
// set index of the field that is not described by spec
if messageField == nil {
return fmt.Errorf("no message field defined by spec with index: %d", fieldIndex)
}
dataField := dataStruct.Field(i)
if dataField.IsNil() {
continue
}
err = messageField.Marshal(dataField.Interface())
if err != nil {
return fmt.Errorf("failed to set value to field %d: %w", fieldIndex, err)
}
m.fieldsMap[fieldIndex] = struct{}{}
}
return nil
}
// Unmarshal populates v struct fields with message field values. It traverses
// through the message fields and calls Unmarshal(...) on them setting the v If
// v is nil or not a pointer it returns error.
func (m *Message) Unmarshal(v interface{}) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return errors.New("data is not a pointer or nil")
}
// get the struct from the pointer
dataStruct := rv.Elem()
if dataStruct.Kind() != reflect.Struct {
return errors.New("data is not a struct")
}
// iterate over struct fields
for i := 0; i < dataStruct.NumField(); i++ {
fieldIndex, err := getFieldIndex(dataStruct.Type().Field(i))
if err != nil {
return fmt.Errorf("getting field %d index: %w", i, err)
}
// skip field without index
if fieldIndex < 0 {
continue
}
// we can get data only if field value is set
messageField := m.GetField(fieldIndex)
if messageField == nil {
continue
}
if _, set := m.fieldsMap[fieldIndex]; !set {
continue
}
dataField := dataStruct.Field(i)
if dataField.IsNil() {
dataField.Set(reflect.New(dataField.Type().Elem()))
}
err = messageField.Unmarshal(dataField.Interface())
if err != nil {
return fmt.Errorf("failed to get value from field %d: %w", fieldIndex, err)
}
}
return nil
}
var fieldNameIndexRe = regexp.MustCompile(`^F\d+$`)
// fieldIndex returns index of the field. First, it checks field name. If it
// does not match FNN (when NN is digits), it checks value of `index` tag. If
// negative value returned (-1) then index was not found for the field.
func getFieldIndex(field reflect.StructField) (int, error) {
dataFieldName := field.Name
if indexStr := field.Tag.Get("index"); indexStr != "" {
fieldIndex, err := strconv.Atoi(indexStr)
if err != nil {
return -1, fmt.Errorf("converting field index into int: %w", err)
}
return fieldIndex, nil
}
if len(dataFieldName) > 0 && fieldNameIndexRe.MatchString(dataFieldName) {
indexStr := dataFieldName[1:]
fieldIndex, err := strconv.Atoi(indexStr)
if err != nil {
return -1, fmt.Errorf("converting field index into int: %w", err)
}
return fieldIndex, nil
}
return -1, nil
}