Skip to content

Commit

Permalink
[PR] remove the options and just use params in the constructor. Updat…
Browse files Browse the repository at this point in the history
…e test to use constructor. Cleanup unpacker and make it simple
  • Loading branch information
bpross committed Nov 12, 2024
1 parent aa525e9 commit 351809e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 48 deletions.
14 changes: 4 additions & 10 deletions field/packer_unpacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,10 @@ func (p Track2Unpacker) Unpack(packedFieldValue []byte, spec *Spec) ([]byte, int
return nil, 0, fmt.Errorf("failed to decode length: %w", err)
}

// Peek and see if the first value is equal to the padding rune if set
// if it is, we need to increase the length to decode by 1
// This is because the packer will pad the value, but set the value length
// equal to the unpadded value length
if spec.Pad != nil {
b := spec.Pad.Inspect()
r := b[0]
if packedFieldValue[prefBytes] == r {
valueLength++
}
// if valueLength is odd we need to make it even to adjust for
// the padding in our Packer
if valueLength%2 != 0 {
valueLength++
}

// decode the value
Expand Down
8 changes: 7 additions & 1 deletion field/packer_unpacker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,13 @@ func TestTrack2Packer(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fd := field.NewTrack2Value(tc.primaryAccountNumber, &tc.expirationDate, tc.serviceCode, tc.discretionaryData, field.WithSeparator(tc.separator))
fd := field.NewTrack2Value(
tc.primaryAccountNumber,
&tc.expirationDate,
tc.serviceCode,
tc.discretionaryData,
tc.separator,
)
fd.SetSpec(s)

packed, err := fd.Pack()
Expand Down
19 changes: 5 additions & 14 deletions field/track1.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ const (

var track1Regex = regexp.MustCompile(`^([A-Z]{1})([0-9]{1,19})\^([^\^]{2,26})\^([0-9]{4}|\^)([0-9]{3}|\^)([^\?]+)$`)

type Track1Option func(*Track1)

func WithFormatCode(code string) Track1Option {
return func(t *Track1) {
t.FormatCode = code
}
}

func NewTrack1(spec *Spec) *Track1 {
return &Track1{
spec: spec,
Expand All @@ -49,19 +41,18 @@ func NewTrack1Value(
name string,
expirationDate *time.Time,
serviceCode,
discretionaryData string,
opts ...Track1Option,
discretionaryData,
formatCode string,
fixedLength bool,
) *Track1 {
t := &Track1{
PrimaryAccountNumber: primaryAccountNumber,
Name: name,
ExpirationDate: expirationDate,
ServiceCode: serviceCode,
DiscretionaryData: discretionaryData,
}

for _, opt := range opts {
opt(t)
FormatCode: formatCode,
FixedLength: fixedLength,
}

return t
Expand Down
17 changes: 3 additions & 14 deletions field/track2.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ const (

var track2Regex = regexp.MustCompile(`^([0-9]{1,19})(=|D)([0-9]{4})([0-9]{3})([^?]+)$`)

type Track2Option func(*Track2)

func WithSeparator(sep string) Track2Option {
return func(t *Track2) {
t.Separator = sep
}
}

func NewTrack2(spec *Spec) *Track2 {
return &Track2{
spec: spec,
Expand All @@ -47,20 +39,17 @@ func NewTrack2Value(
primaryAccountNumber string,
expirationDate *time.Time,
serviceCode,
discretionaryData string,
opts ...Track2Option,
discretionaryData,
separator string,
) *Track2 {
t := &Track2{
PrimaryAccountNumber: primaryAccountNumber,
Separator: separator,
ExpirationDate: expirationDate,
ServiceCode: serviceCode,
DiscretionaryData: discretionaryData,
}

for _, opt := range opts {
opt(t)
}

return t
}

Expand Down
20 changes: 11 additions & 9 deletions field/track_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,18 @@ func TestTrack1(t *testing.T) {
expDate, err := time.Parse("0601", "9901")
require.NoError(t, err)

fd := field.NewTrack1Value(
"1234567890123445",
"PADILLA/L.",
&expDate,
"120",
"0000000000000**XXX******",
"B",
true,
)

track := field.NewTrack1(track1Spec)
err = track.Marshal(&field.Track1{
FixedLength: true,
FormatCode: "B",
PrimaryAccountNumber: "1234567890123445",
ServiceCode: "120",
DiscretionaryData: "0000000000000**XXX******",
ExpirationDate: &expDate,
Name: "PADILLA/L.",
})
err = track.Marshal(fd)
require.NoError(t, err)

data := &field.Track1{}
Expand Down

0 comments on commit 351809e

Please sign in to comment.