-
Notifications
You must be signed in to change notification settings - Fork 112
/
message.go
576 lines (463 loc) · 14.1 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package iso8583
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"sync"
iso8583errors "github.com/moov-io/iso8583/errors"
"github.com/moov-io/iso8583/field"
"github.com/moov-io/iso8583/utils"
)
var _ json.Marshaler = (*Message)(nil)
var _ json.Unmarshaler = (*Message)(nil)
const (
mtiIdx = 0
bitmapIdx = 1
)
type Message struct {
spec *MessageSpec
cachedBitmap *field.Bitmap
// stores all fields according to the spec
fields map[int]field.Field
// to guard fieldsMap
mu sync.Mutex
// tracks which fields were set
fieldsMap map[int]struct{}
}
func NewMessage(spec *MessageSpec) *Message {
// Validate the spec
if err := spec.Validate(); err != nil {
panic(err) //nolint:forbidigo,nolintlint // as specs moslty static, we panic on spec validation errors
}
fields := spec.CreateMessageFields()
return &Message{
fields: fields,
spec: spec,
fieldsMap: map[int]struct{}{},
}
}
// Deprecated. Use Marshal instead.
func (m *Message) SetData(data interface{}) error {
return m.Marshal(data)
}
func (m *Message) Bitmap() *field.Bitmap {
m.mu.Lock()
defer m.mu.Unlock()
return m.bitmap()
}
// bitmap creates and returns the bitmap field, it's not thread safe
// and should be called from a thread safe function
func (m *Message) bitmap() *field.Bitmap {
if m.cachedBitmap != nil {
return m.cachedBitmap
}
// We validate the presence and type of the bitmap field in
// spec.Validate() when we create the message so we can safely assume
// it exists and is of the correct type
m.cachedBitmap, _ = m.fields[bitmapIdx].(*field.Bitmap)
m.cachedBitmap.Reset()
m.fieldsMap[bitmapIdx] = struct{}{}
return m.cachedBitmap
}
func (m *Message) MTI(val string) {
m.mu.Lock()
defer m.mu.Unlock()
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 {
m.mu.Lock()
defer m.mu.Unlock()
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 {
m.mu.Lock()
defer m.mu.Unlock()
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) {
// we validate the presence and type of the mti field in
// spec.Validate() when we create the message so we can safely assume
// it exists
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 {
m.mu.Lock()
defer m.mu.Unlock()
return m.getFields()
}
// getFields returns the map of the set fields. It assumes that the mutex
// is already locked by the caller.
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
}
// Pack locks the message, packs its fields, and then unlocks it.
// If any errors are encountered during packing, they will be wrapped
// in a *PackError before being returned.
func (m *Message) Pack() ([]byte, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.wrapErrorPack()
}
// wrapErrorPack calls the core packing logic and wraps any errors in a
// *PackError. It assumes that the mutex is already locked by the caller.
func (m *Message) wrapErrorPack() ([]byte, error) {
data, err := m.pack()
if err != nil {
return nil, &iso8583errors.PackError{Err: err}
}
return data, nil
}
// pack contains the core logic for packing the message. This method does not
// handle locking or error wrapping and should typically be used internally
// after ensuring concurrency safety.
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
// do not pack presence bits as well
if id < 2 || m.bitmap().IsBitmapPresenceBit(id) {
continue
}
m.bitmap().Set(id)
}
// pack fields
for _, i := range ids {
// do not pack presence bits other than the first one as it's the bitmap itself
if i != 1 && m.bitmap().IsBitmapPresenceBit(i) {
continue
}
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
}
// Unpack unpacks the message from the given byte slice or returns an error
// which is of type *UnpackError and contains the raw message
func (m *Message) Unpack(src []byte) error {
m.mu.Lock()
defer m.mu.Unlock()
return m.wrapErrorUnpack(src)
}
// wrapErrorUnpack calls the core unpacking logic and wraps any
// errors in a *UnpackError. It assumes that the mutex is already
// locked by the caller.
func (m *Message) wrapErrorUnpack(src []byte) error {
if fieldID, err := m.unpack(src); err != nil {
return &iso8583errors.UnpackError{
Err: err,
FieldID: fieldID,
RawMessage: src,
}
}
return nil
}
// unpack contains the core logic for unpacking the message. This method does
// not handle locking or error wrapping and should typically be used internally
// after ensuring concurrency safety.
func (m *Message) unpack(src []byte) (string, 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 strconv.Itoa(mtiIdx), 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 strconv.Itoa(bitmapIdx), fmt.Errorf("failed to unpack bitmap: %w", err)
}
off += read
for i := 2; i <= m.bitmap().Len(); i++ {
// skip bitmap presence bits (for default bitmap length of 64 these are bits 1, 65, 129, 193, etc.)
if m.bitmap().IsBitmapPresenceBit(i) {
continue
}
if m.bitmap().IsSet(i) {
fl, ok := m.fields[i]
if !ok {
return strconv.Itoa(i), fmt.Errorf("failed to unpack field %d: no specification found", i)
}
read, err = fl.Unpack(src[off:])
if err != nil {
return strconv.Itoa(i), 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) {
m.mu.Lock()
defer m.mu.Unlock()
// by packing the message we will generate bitmap
// create HEX representation
// and validate message against the spec
if _, err := m.wrapErrorPack(); err != nil {
return nil, err
}
fieldMap := m.getFields()
strFieldMap := map[string]field.Field{}
for id, field := range fieldMap {
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 {
m.mu.Lock()
defer m.mu.Unlock()
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) {
m.mu.Lock()
defer m.mu.Unlock()
newMessage := NewMessage(m.spec)
bytes, err := m.wrapErrorPack()
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 {
m.mu.Lock()
defer m.mu.Unlock()
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++ {
indexTag := field.NewIndexTag(dataStruct.Type().Field(i))
// skip field without index or if index in tag is not defined
if indexTag.ID < 0 {
continue
}
messageField := m.GetField(indexTag.ID)
// 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", indexTag.ID)
}
dataField := dataStruct.Field(i)
// for non pointer fields we need to check if they are zero
// and we want to skip them (as specified in the field tag)
if dataField.IsZero() && !indexTag.KeepZero {
continue
}
if err := messageField.Marshal(dataField.Interface()); err != nil {
return fmt.Errorf("failed to set value to field %d: %w", indexTag.ID, err)
}
m.fieldsMap[indexTag.ID] = 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 {
m.mu.Lock()
defer m.mu.Unlock()
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++ {
indexTag := field.NewIndexTag(dataStruct.Type().Field(i))
// skip field without index or if index in tag is not defined
if indexTag.ID < 0 {
continue
}
// we can get data only if field value is set
messageField := m.GetField(indexTag.ID)
if messageField == nil {
continue
}
if _, set := m.fieldsMap[indexTag.ID]; !set {
continue
}
dataField := dataStruct.Field(i)
switch dataField.Kind() { //nolint:exhaustive
case reflect.Pointer, reflect.Interface, reflect.Slice:
if dataField.IsNil() && dataField.Kind() != reflect.Slice {
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", indexTag.ID, err)
}
default: // Native types
err := messageField.Unmarshal(dataField)
if err != nil {
return fmt.Errorf("failed to get value from field %d: %w", indexTag.ID, err)
}
}
}
return nil
}
// UnsetField marks the field with the given ID as not set and replaces it with
// a new zero-valued field. This effectively removes the field's value and excludes
// it from operations like Pack() or Marshal().
func (m *Message) UnsetField(id int) {
m.mu.Lock()
defer m.mu.Unlock()
if _, ok := m.fieldsMap[id]; ok {
delete(m.fieldsMap, id)
// re-create the field to reset its value (and subfields if it's a composite field)
if fieldSpec, ok := m.GetSpec().Fields[id]; ok {
m.fields[id] = createMessageField(fieldSpec)
}
}
}
// UnsetFields marks multiple fields identified by their paths as not set and
// replaces them with new zero-valued fields. Each path should be in the format
// "a.b.c". This effectively removes the fields' values and excludes them from
// operations like Pack() or Marshal().
func (m *Message) UnsetFields(idPaths ...string) error {
for _, idPath := range idPaths {
if idPath == "" {
continue
}
id, path, _ := strings.Cut(idPath, ".")
idx, err := strconv.Atoi(id)
if err != nil {
return fmt.Errorf("conversion of %s to int failed: %w", id, err)
}
if _, ok := m.fieldsMap[idx]; ok {
if len(path) == 0 {
m.UnsetField(idx)
continue
}
f := m.fields[idx]
if f == nil {
return fmt.Errorf("field %d does not exist", idx)
}
composite, ok := f.(*field.Composite)
if !ok {
return fmt.Errorf("field %d is not a composite field and its subfields %s cannot be unset", idx, path)
}
if err := composite.UnsetSubfields(path); err != nil {
return fmt.Errorf("failed to unset %s in composite field %d: %w", path, idx, err)
}
}
}
return nil
}