Open Achuthen opened 1 month ago
Hey, @Achuthen.
out VSDC example is misleading, sorry. I'll remove it. Here is how it should look like. In rare cases we should have length fields. If you see it in the spec, most probably something is wrong. Having the length field prevents the spec from unpacking the message (in most cases).:
package examples
import (
"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"
)
func TestICCField55(t *testing.T) {
field55Spec := &field.Spec{
Length: 255,
Description: "Integrated Circuit Card (ICC) Data",
Pref: prefix.Binary.L,
Tag: &field.TagSpec{
Length: 1,
Enc: encoding.ASCIIHexToBytes,
Sort: sort.StringsByHex,
},
Subfields: map[string]field.Field{
"01": field.NewComposite(&field.Spec{
Length: 252,
Description: "VSDC Data",
Pref: prefix.Binary.LL,
Tag: &field.TagSpec{
Enc: encoding.BerTLVTag,
Sort: sort.StringsByHex,
},
Subfields: map[string]field.Field{
"9A": field.NewString(&field.Spec{
Description: "Transaction Date",
Enc: encoding.Binary,
Pref: prefix.BerTLV,
}),
"9F02": field.NewString(&field.Spec{
Description: "Amount, Authorized (Numeric)",
Enc: encoding.Binary,
Pref: prefix.BerTLV,
}),
},
}),
},
}
type VSDCData struct {
TransactionDate string `iso8583:"9A"`
Amount string `iso8583:"9F02"`
}
type ICCData struct {
VSDCData *VSDCData `iso8583:"01"`
}
filed55 := field.NewComposite(field55Spec)
err := filed55.Marshal(&ICCData{
VSDCData: &VSDCData{
TransactionDate: "210720",
Amount: "000000000501",
},
})
require.NoError(t, err)
packed, err := filed55.Pack()
require.NoError(t, err)
require.Equal(t, "1A0100179A063231303732309F020C303030303030303030353031", fmt.Sprintf("%X", packed))
}
Also, you should remember that most probably for real data / spec, you will have to use SkipUnknownTLVTags
(for both TagSpec
in the example above) and PrefUnknownTLV
only for the first TagSpec
as we should tell parser what is the length of the length prefix so it can read the length of unknown tag.
Please, let me know if that worked for you.
I added the test into the examples directory: https://github.com/moov-io/iso8583/blob/master/examples/icc_test.go
Having trouble to pack the f55 if we the field was build by unpacking from bytes. ( thru marshal it works well ).
may be i am doing something wrong here. please advice