Skip to content

Commit

Permalink
sort fields properly (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
alovak authored Jul 31, 2023
1 parent 36606f8 commit a0db8ac
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 23 deletions.
42 changes: 21 additions & 21 deletions describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,30 +153,30 @@ func DescribeFieldContainer(container FieldContainer, w io.Writer, filters ...Fi
}

func sortFieldIDs(fields map[string]field.Field) []string {
numericKeys := make([]int, 0)
nonNumericKeys := make([]string, 0)

for k := range fields {
if id, err := strconv.Atoi(k); err == nil {
numericKeys = append(numericKeys, id)
} else {
nonNumericKeys = append(nonNumericKeys, k)
}
}

// Sorting numeric and non-numeric keys separately
sort.Ints(numericKeys)
sort.Strings(nonNumericKeys)

keys := make([]string, 0, len(fields))

// Appending numeric keys first (as strings)
for _, key := range numericKeys {
keys = append(keys, strconv.Itoa(key))
for k := range fields {
keys = append(keys, k)
}

// Appending non-numeric keys
keys = append(keys, nonNumericKeys...)
sort.Slice(keys, func(i, j int) bool {
// check if both keys are numeric
ni, ei := strconv.Atoi(keys[i])
nj, ej := strconv.Atoi(keys[j])
if ei == nil && ej == nil {
// if both keys are numeric, compare as integers
return ni < nj
}
if ei == nil {
// if only i is numeric, it goes first
return true
}
if ej == nil {
// if only j is numeric, it goes first
return false
}
// if neither key is numeric, compare as strings
return keys[i] < keys[j]
})

return keys
}
Expand Down
43 changes: 41 additions & 2 deletions describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package iso8583

import (
"bytes"
"fmt"
"testing"

"github.com/moov-io/iso8583/encoding"
"github.com/moov-io/iso8583/field"
"github.com/moov-io/iso8583/prefix"
"github.com/moov-io/iso8583/sort"
"github.com/stretchr/testify/require"
)

Expand All @@ -30,27 +32,64 @@ func TestDescribe(t *testing.T) {
Enc: encoding.ASCII,
Pref: prefix.ASCII.LL,
}),
3: field.NewComposite(&field.Spec{
Length: 6,
Description: "Processing Code",
Pref: prefix.ASCII.Fixed,
Tag: &field.TagSpec{
Sort: sort.StringsByInt,
},
Subfields: map[string]field.Field{
"01": field.NewString(&field.Spec{
Length: 2,
Description: "Transaction Type",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
"02": field.NewString(&field.Spec{
Length: 2,
Description: "From Account",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
"03": field.NewString(&field.Spec{
Length: 2,
Description: "To Account",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
},
}),
},
}

message := NewMessage(spec)
message.MTI("0100")
message.Field(2, "4242424242424242")
message.Field(3, "123456")
message.Pack() // to generate bitmap

out := bytes.NewBuffer([]byte{})
require.NotPanics(t, func() {
Describe(message, out, DoNotFilterFields()...)
})

fmt.Println(out.String())

expectedOutput := `ISO 8583 Message:
MTI..........: 0100
Bitmap HEX...: 4000000000000000
Bitmap HEX...: 6000000000000000
Bitmap bits..:
[1-8]01000000 [9-16]00000000 [17-24]00000000 [25-32]00000000
[1-8]01100000 [9-16]00000000 [17-24]00000000 [25-32]00000000
[33-40]00000000 [41-48]00000000 [49-56]00000000 [57-64]00000000
F0 Message Type Indicator..: 0100
F2 Primary Account Number..: 4242424242424242
F3 Processing Code SUBFIELDS:
-------------------------------------------
F01 Transaction Type..: 12
F02 From Account......: 34
F03 To Account........: 56
------------------------------------------
`
require.Equal(t, expectedOutput, out.String())
}
Expand Down

0 comments on commit a0db8ac

Please sign in to comment.