Skip to content

Commit

Permalink
do not pack fields that represent bitmap indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
alovak committed Jul 25, 2023
1 parent 47788f0 commit 6353047
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
8 changes: 7 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,20 @@ func (m *Message) pack() ([]byte, error) {
for _, id := range ids {
// indexes 0 and 1 are for mti and bitmap
// regular field number startd from index 2
if id < 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)
Expand Down
41 changes: 41 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ func TestMessage(t *testing.T) {
Pref: prefix.ASCII.Fixed,
Pad: padding.Left('0'),
}),

// this field will be ignored when packing and
// unpacking, as bit 65 is a bitmap presence indicator
65: field.NewString(&field.Spec{
Length: 1,
Description: "Settlement Code",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
// this is a field of the third bitmap
130: field.NewString(&field.Spec{
Length: 1,
Description: "Additional Data",
Expand Down Expand Up @@ -115,6 +125,37 @@ func TestMessage(t *testing.T) {
require.Equal(t, "100", s)
})

t.Run("Do not pack fields that match the bitmap presence indicator", func(t *testing.T) {
message := NewMessage(spec)
message.MTI("0100")
require.NoError(t, message.Field(65, "1"))
require.NoError(t, message.Field(130, "1")) // field of third bitmap

got, err := message.Pack()

want := "01008000000000000000800000000000000040000000000000001"
require.NoError(t, err)
require.NotNil(t, got)
require.Equal(t, want, string(got))

message = NewMessage(spec)

err = message.Unpack([]byte(want))
require.NoError(t, err)

s, err := message.GetMTI()
require.NoError(t, err)
require.Equal(t, "0100", s)

s, err = message.GetString(65)
require.NoError(t, err)
require.Equal(t, "", s)

s, err = message.GetString(130)
require.NoError(t, err)
require.Equal(t, "1", s)
})

t.Run("Does not fail when packing and unpacking message with three bitmaps", func(t *testing.T) {
message := NewMessage(spec)
message.MTI("0100")
Expand Down

0 comments on commit 6353047

Please sign in to comment.