Skip to content

Commit

Permalink
rename HexToASCII to BytesToASCIIHex and vice versa
Browse files Browse the repository at this point in the history
  • Loading branch information
krishishah committed Oct 4, 2021
1 parent 4aa8599 commit e3c10f3
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 104 deletions.
4 changes: 2 additions & 2 deletions encoding/bertlv.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type berTLVEncoderTag struct{}
// Encode converts ASCII Hex-digits into a byte slice e.g. []byte("AABBCC")
// would be converted into []byte{0xAA, 0xBB, 0xCC}
func (berTLVEncoderTag) Encode(data []byte) ([]byte, error) {
out, err := ASCIIToHex.Encode(data)
out, err := ASCIIHexToBytes.Encode(data)
return out, err
}

Expand Down Expand Up @@ -56,7 +56,7 @@ func (berTLVEncoderTag) Decode(data []byte, length int) ([]byte, int, error) {
}
}

out, read, err := ASCIIToHex.Decode(data[:tagLenBytes], tagLenBytes)
out, read, err := ASCIIHexToBytes.Decode(data[:tagLenBytes], tagLenBytes)
if err != nil {
return nil, 0, err
}
Expand Down
4 changes: 2 additions & 2 deletions encoding/hex.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// HEX to ASCII encoder
var HexToASCII = &hexToASCIIEncoder{}
var BytesToASCIIHex = &hexToASCIIEncoder{}

type hexToASCIIEncoder struct{}

Expand Down Expand Up @@ -45,7 +45,7 @@ func (e hexToASCIIEncoder) Decode(data []byte, length int) ([]byte, int, error)
}

// ASCII To HEX encoder
var ASCIIToHex = &asciiToHexEncoder{}
var ASCIIHexToBytes = &asciiToHexEncoder{}

type asciiToHexEncoder struct{}

Expand Down
4 changes: 2 additions & 2 deletions encoding/hex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestHexToASCIIEncoder(t *testing.T) {
enc := HexToASCII
enc := BytesToASCIIHex

got, read, err := enc.Decode([]byte("aabbcc"), 3)
require.NoError(t, err)
Expand All @@ -20,7 +20,7 @@ func TestHexToASCIIEncoder(t *testing.T) {
}

func TestASCIIToHexEncoder(t *testing.T) {
enc := ASCIIToHex
enc := ASCIIHexToBytes

got, read, err := enc.Decode([]byte{0xAA, 0xBB, 0xCC}, 3)
require.NoError(t, err)
Expand Down
8 changes: 2 additions & 6 deletions field/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,7 @@ func (f *Binary) SetData(data interface{}) error {
}

func (f *Binary) MarshalJSON() ([]byte, error) {
str, err := f.String()
if err != nil {
return nil, err
}
return json.Marshal(str)
return json.Marshal(f.Value)
}

func (f *Binary) UnmarshalJSON(b []byte) error {
Expand All @@ -127,7 +123,7 @@ func (f *Binary) UnmarshalJSON(b []byte) error {
return fmt.Errorf("failed to JSON unmarshal bytes to string: %v", err)
}

hex, err := encoding.ASCIIToHex.Encode([]byte(v))
hex, err := encoding.ASCIIHexToBytes.Encode([]byte(v))
if err != nil {
return fmt.Errorf("failed to convert ASCII Hex string to bytes")
}
Expand Down
18 changes: 9 additions & 9 deletions field/bitmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestHexBitmap(t *testing.T) {
t.Run("Read only first bitmap", func(t *testing.T) {
bitmap := NewBitmap(&Spec{
Description: "Bitmap",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
})

Expand All @@ -28,7 +28,7 @@ func TestHexBitmap(t *testing.T) {
t.Run("Read two bitmaps", func(t *testing.T) {
bitmap := NewBitmap(&Spec{
Description: "Bitmap",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
})

Expand All @@ -45,7 +45,7 @@ func TestHexBitmap(t *testing.T) {
t.Run("Read three bitmaps", func(t *testing.T) {
bitmap := NewBitmap(&Spec{
Description: "Bitmap",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
})

Expand All @@ -62,7 +62,7 @@ func TestHexBitmap(t *testing.T) {
t.Run("When not enough data to unpack", func(t *testing.T) {
bitmap := NewBitmap(&Spec{
Description: "Bitmap",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
})

Expand All @@ -75,7 +75,7 @@ func TestHexBitmap(t *testing.T) {
t.Run("When bit for secondary bitmap is set but not enough data to read", func(t *testing.T) {
bitmap := NewBitmap(&Spec{
Description: "Bitmap",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
})

Expand All @@ -89,7 +89,7 @@ func TestHexBitmap(t *testing.T) {
t.Run("With primary bitmap only it returns signle bitmap length", func(t *testing.T) {
bitmap := NewBitmap(&Spec{
Description: "Bitmap",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
})

Expand All @@ -104,7 +104,7 @@ func TestHexBitmap(t *testing.T) {
t.Run("With secondary bitmap it returns length of two bitmaps", func(t *testing.T) {
bitmap := NewBitmap(&Spec{
Description: "Bitmap",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
})

Expand All @@ -120,7 +120,7 @@ func TestHexBitmap(t *testing.T) {
t.Run("With third bitmap it returns length of three bitmaps", func(t *testing.T) {
bitmap := NewBitmap(&Spec{
Description: "Bitmap",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
})

Expand Down Expand Up @@ -188,7 +188,7 @@ func TestBinaryBitmap(t *testing.T) {
func TestBitmap_SetData(t *testing.T) {
spec := &Spec{
Description: "Bitmap",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
}
bitmapBytes := []byte("004000000000000000000000000000000000000000000000")
Expand Down
4 changes: 2 additions & 2 deletions field/composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ var (
Subfields: map[string]Field{
"9A": NewString(&Spec{
Description: "Transaction Date",
Enc: encoding.ASCIIToHex,
Enc: encoding.ASCIIHexToBytes,
Pref: prefix.BerTLV,
}),
"9F02": NewString(&Spec{
Description: "Amount, Authorized (Numeric)",
Enc: encoding.ASCIIToHex,
Enc: encoding.ASCIIHexToBytes,
Pref: prefix.BerTLV,
}),
},
Expand Down
2 changes: 1 addition & 1 deletion message_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestMessageSpec_CreateMessageFields(t *testing.T) {
}),
1: field.NewBitmap(&field.Spec{
Description: "Bitmap",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
}),
},
Expand Down
4 changes: 2 additions & 2 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestMessage(t *testing.T) {
}),
1: field.NewBitmap(&field.Spec{
Description: "Bitmap",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
}),
2: field.NewString(&field.Spec{
Expand Down Expand Up @@ -544,7 +544,7 @@ func TestMessageJSON(t *testing.T) {
}),
1: field.NewBitmap(&field.Spec{
Description: "Bitmap",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
}),
2: field.NewString(&field.Spec{
Expand Down
4 changes: 2 additions & 2 deletions prefix/hex.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (p *hexVarPrefixer) EncodeLength(maxLen, dataLen int) ([]byte, error) {
}

strLen := strconv.Itoa(dataLen)
res, err := encoding.HexToASCII.Encode([]byte(strLen))
res, err := encoding.BytesToASCIIHex.Encode([]byte(strLen))
if err != nil {
return nil, err
}
Expand All @@ -65,7 +65,7 @@ func (p *hexVarPrefixer) DecodeLength(maxLen int, data []byte) (int, int, error)
return 0, 0, fmt.Errorf("length mismatch: want to read %d bytes, get only %d", length, len(data))
}

bDigits, _, err := encoding.HexToASCII.Decode(data[:length], p.Digits)
bDigits, _, err := encoding.BytesToASCIIHex.Decode(data[:length], p.Digits)
if err != nil {
return 0, 0, err
}
Expand Down
4 changes: 2 additions & 2 deletions sort/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func StringsByInt(x []string) {
// of even length.
func StringsByHex(x []string) {
sort.Slice(x, func(i, j int) bool {
valI, err := encoding.ASCIIToHex.Encode([]byte(x[i]))
valI, err := encoding.ASCIIHexToBytes.Encode([]byte(x[i]))
if err != nil {
panic(fmt.Sprintf("failed to sort strings by hex: %v", err))
}
valJ, err := encoding.ASCIIToHex.Encode([]byte(x[j]))
valJ, err := encoding.ASCIIHexToBytes.Encode([]byte(x[j]))
if err != nil {
panic(fmt.Sprintf("failed to sort strings by hex: %v", err))
}
Expand Down
6 changes: 3 additions & 3 deletions spec87.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var Spec87 *MessageSpec = &MessageSpec{
1: field.NewBitmap(&field.Spec{
Length: 16,
Description: "Bitmap",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
}),
2: field.NewString(&field.Spec{
Expand Down Expand Up @@ -330,7 +330,7 @@ var Spec87 *MessageSpec = &MessageSpec{
52: field.NewString(&field.Spec{
Length: 8,
Description: "PIN Data",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
}),
53: field.NewString(&field.Spec{
Expand Down Expand Up @@ -402,7 +402,7 @@ var Spec87 *MessageSpec = &MessageSpec{
64: field.NewString(&field.Spec{
Length: 8,
Description: "Message Authentication Code (MAC)",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
}),
70: field.NewString(&field.Spec{
Expand Down
4 changes: 2 additions & 2 deletions specs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ var (
"BCD": encoding.BCD,
"EBCDIC": encoding.EBCDIC,
"Binary": encoding.Binary,
"HexToASCII": encoding.HexToASCII,
"ASCIIToHex": encoding.ASCIIToHex,
"HexToASCII": encoding.BytesToASCIIHex,
"ASCIIToHex": encoding.ASCIIHexToBytes,
"LBCD": encoding.LBCD,
"BerTLVTag": encoding.BerTLVTag,
}
Expand Down
6 changes: 3 additions & 3 deletions specs/spec87ascii.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var Spec87ASCII *iso8583.MessageSpec = &iso8583.MessageSpec{
1: field.NewBitmap(&field.Spec{
Length: 16,
Description: "Bitmap",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
}),
2: field.NewString(&field.Spec{
Expand Down Expand Up @@ -331,7 +331,7 @@ var Spec87ASCII *iso8583.MessageSpec = &iso8583.MessageSpec{
52: field.NewString(&field.Spec{
Length: 8,
Description: "PIN Data",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
}),
53: field.NewString(&field.Spec{
Expand Down Expand Up @@ -403,7 +403,7 @@ var Spec87ASCII *iso8583.MessageSpec = &iso8583.MessageSpec{
64: field.NewString(&field.Spec{
Length: 8,
Description: "Message Authentication Code (MAC)",
Enc: encoding.HexToASCII,
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
}),
90: field.NewString(&field.Spec{
Expand Down
Loading

0 comments on commit e3c10f3

Please sign in to comment.