Skip to content

Commit a7cae10

Browse files
authored
Merge pull request #26 from jattento/feature/bitmap-default-length
if no length is indicated for a bitmap, 64 is assumed
2 parents 625eae5 + d93a634 commit a7cae10

File tree

6 files changed

+28
-11
lines changed

6 files changed

+28
-11
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
[![codecov](https://codecov.io/gh/jattento/go-iso8583/branch/master/graph/badge.svg)](https://codecov.io/gh/jattento/go-iso8583)
22
[![Maintainability](https://api.codeclimate.com/v1/badges/94a2058a2b0823cf31be/maintainability)](https://codeclimate.com/github/jattento/go-iso8583/maintainability)
3+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4+
[![Last release](https://img.shields.io/github/v/release/jattento/go-iso8583?style=plastic)](https://github.com/jattento/go-iso8583/releases)
5+
36

47
| Version | Build |
58
|----------|:-------------:|
@@ -43,8 +46,8 @@ import "github.com/jattento/go-iso8583/pkg/iso8583"
4346

4447
type exampleMessage struct {
4548
MessageTypeIdentifier iso8583.MTI `iso8583:"mti,length:4,encoding:ebcdic"`
46-
Bitmap iso8583.BITMAP `iso8583:"bitmap,length:64"`
47-
SecondaryBitmap iso8583.BITMAP `iso8583:"1,length:64,encoding:ebcdic,omitempty"`
49+
Bitmap iso8583.BITMAP `iso8583:"bitmap"`
50+
SecondaryBitmap iso8583.BITMAP `iso8583:"1,omitempty"`
4851
PrimaryAccountNumber iso8583.LLVAR `iso8583:"2,length:64,encoding:ebcdic,omitempty"`
4952
ProcessingCode iso8583.VAR `iso8583:"3,length:6,encoding:ebcdic,omitempty"`
5053
AmountTransaction iso8583.VAR `iso8583:"4,length:12,encoding:ebcdic,omitempty"`
@@ -67,6 +70,7 @@ type exampleMessage struct {
6770
SecurityRelatedControlInformation iso8583.VAR `iso8583:"53,length:16,encoding:ebcdic,omitempty"`
6871
AdditionalAmounts iso8583.LLLVAR `iso8583:"54,length:120,encoding:ebcdic,omitempty"`
6972
IntegratedCircuitCardSystemRelatedData iso8583.LLLBINARY `iso8583:"55,length:999,encoding:ebcdic,omitempty"`
73+
MessageSecurityCode iso8583.VAR `iso8583:"96,length:8,encoding:ebcdic,omitempty"`
7074
}
7175
```
7276

pkg/iso8583/decode.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ func unmarshalField(strct reflect.Value, fieldName string, bytes []byte) (int, t
232232
"iso8583.unmarshal: field %s is present but does not implement Unmarshaler interface", fieldName)
233233
}
234234

235+
// If bitmap length is not indicated: we assume its 64.
236+
if _, isBitmap := fieldValue.Interface().(UnmarshalerBitmap); isBitmap && tag.Length == 0 {
237+
tag.Length = 64
238+
}
239+
235240
consumed, err := executeUnmarshal(fieldInterface, bytes, tag)
236241

237242
return consumed, tag, err

pkg/iso8583/decode_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ func TestUnmarshal(t *testing.T) {
3131
[]byte("asd")...)...),
3232
InputStruct: &struct {
3333
Mti iso8583.VAR `iso8583:"mti,length:4"`
34-
Bitmap iso8583.BITMAP `iso8583:"bitmap,length:64"`
34+
Bitmap iso8583.BITMAP `iso8583:"bitmap"`
3535
Field2 iso8583.VAR `iso8583:"2,length:3"`
3636
field3 iso8583.VAR `iso8583:"3,length:3"`
3737
}{},
3838
ExpectedOutputError: "",
3939
ExpectedOutputStruct: struct {
4040
Mti iso8583.VAR `iso8583:"mti,length:4"`
41-
Bitmap iso8583.BITMAP `iso8583:"bitmap,length:64"`
41+
Bitmap iso8583.BITMAP `iso8583:"bitmap"`
4242
Field2 iso8583.VAR `iso8583:"2,length:3"`
4343
field3 iso8583.VAR `iso8583:"3,length:3"`
4444
}{
@@ -62,15 +62,15 @@ func TestUnmarshal(t *testing.T) {
6262
64: false}), bitmap.ToBytes(map[int]bool{1: false, 2: true, 64: false})...)...), []byte("asdfgh")...),
6363
InputStruct: &struct {
6464
Mti iso8583.VAR `iso8583:"mti,length:4"`
65-
Bitmap iso8583.BITMAP `iso8583:"bitmap,length:64"`
65+
Bitmap iso8583.BITMAP `iso8583:"bitmap"`
6666
Field1 iso8583.BITMAP `iso8583:"1,length:64"`
6767
Field2 iso8583.VAR `iso8583:"2,length:3"`
6868
Field66 iso8583.VAR `iso8583:"66,length:3"`
6969
}{},
7070
ExpectedOutputError: "",
7171
ExpectedOutputStruct: struct {
7272
Mti iso8583.VAR `iso8583:"mti,length:4"`
73-
Bitmap iso8583.BITMAP `iso8583:"bitmap,length:64"`
73+
Bitmap iso8583.BITMAP `iso8583:"bitmap"`
7474
Field1 iso8583.BITMAP `iso8583:"1,length:64"`
7575
Field2 iso8583.VAR `iso8583:"2,length:3"`
7676
Field66 iso8583.VAR `iso8583:"66,length:3"`
@@ -226,7 +226,7 @@ func TestUnmarshal(t *testing.T) {
226226
[]byte("asd")...)...),
227227
InputStruct: &struct {
228228
MTI VarMock `iso8583:"mti,length:3"`
229-
Bitmap iso8583.BITMAP `iso8583:"bitmap,length:64"`
229+
Bitmap iso8583.BITMAP `iso8583:"bitmap"`
230230
}{
231231
MTI: VarMock{returnUnmarshal: 999},
232232
},

pkg/iso8583/encode.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,18 @@ func (m *marshalerMessage) resolveMarshalerBitmaps(fields *[]field) (bool, error
261261

262262
sortFieldsStable(m.Bitmaps, func(index int) string { return m.Bitmaps[index].Field })
263263

264+
// If bitmap length is not indicated: we assume its 64.
265+
for n := 0; len(m.Bitmaps) > n; n++ {
266+
if m.Bitmaps[n].tags.Length == 0 {
267+
m.Bitmaps[n].tags.Length = 64
268+
}
269+
}
270+
264271
// Resolve all bitmaps starting from last one to first,
265272
// this is because every bitmaps indicates the presence of the next one.
266273
for n := len(m.Bitmaps) - 1; n >= 0; n-- {
267274
// Marshal bitmap...
275+
268276
b, err := m.Bitmaps[n].MarshalerBitmap.MarshalISO8583Bitmap(m.createBitmapMarshalerInput(*fields, n), m.Bitmaps[n].tags.Encoding)
269277
if err != nil {
270278
return false, fmt.Errorf("iso8583.marshal: field %s cant be marshaled: %w", m.Bitmaps[n].tags.Field, err)

pkg/iso8583/encode_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestMarshal(t *testing.T) {
2424
Name: "simple_one_field_ebcdic",
2525
Run: true,
2626
Input: struct {
27-
Bitmap iso8583.BITMAP `iso8583:"bitmap,length:64"`
27+
Bitmap iso8583.BITMAP `iso8583:"bitmap"`
2828
Field1 iso8583.VAR `iso8583:"mti,encoding:ebcdic"`
2929
Field2 iso8583.VAR `iso8583:"1"`
3030
}{
@@ -233,7 +233,7 @@ func TestMarshal(t *testing.T) {
233233
Input: struct {
234234
Bitmap iso8583.BITMAP `iso8583:"bitmap,length:64"`
235235
MTI iso8583.VAR `iso8583:"mti"`
236-
Field1 iso8583.BITMAP `iso8583:"1,length:64"`
236+
Field1 iso8583.BITMAP `iso8583:"1"`
237237
Field2 iso8583.VAR `iso8583:"2"`
238238
Field32 iso8583.VAR `iso8583:"32"`
239239
Field64 iso8583.VAR `iso8583:"64"`

pkg/template/mastercard.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
// MasterCardISO87 is a template of the communication settings used for MasterCard connectivity on ISO8583 on 1987 version.
88
type MasterCardISO87 struct {
99
MessageTypeIdentifier iso8583.MTI `iso8583:"mti,length:4,encoding:ebcdic"`
10-
Bitmap iso8583.BITMAP `iso8583:"bitmap,length:64"`
11-
SecondaryBitmap iso8583.BITMAP `iso8583:"1,length:64,encoding:ebcdic,omitempty"`
10+
Bitmap iso8583.BITMAP `iso8583:"bitmap"`
11+
SecondaryBitmap iso8583.BITMAP `iso8583:"1,omitempty"`
1212
PrimaryAccountNumber iso8583.LLVAR `iso8583:"2,length:64,encoding:ebcdic,omitempty"`
1313
ProcessingCode iso8583.VAR `iso8583:"3,length:6,encoding:ebcdic,omitempty"`
1414
AmountTransaction iso8583.VAR `iso8583:"4,length:12,encoding:ebcdic,omitempty"`

0 commit comments

Comments
 (0)