Skip to content

Commit

Permalink
Change Numeric value type to int64, validate tests (#294)
Browse files Browse the repository at this point in the history
Co-authored-by: Galih Fajar <[email protected]>
  • Loading branch information
GalihFajar and Galih Fajar authored Oct 10, 2023
1 parent cd4f442 commit add4df1
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 66 deletions.
4 changes: 2 additions & 2 deletions exp/emv/emv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func TestEmv(t *testing.T) {
err = emvField.Unmarshal(data)
require.NoError(t, err)

require.Equal(t, 6300, data.AmountAuthorisedNumeric.Value())
require.Equal(t, int64(6300), data.AmountAuthorisedNumeric.Value())
require.Equal(t, "5800", data.ApplicationInterchangeProfile.Value())
require.Equal(t, 2, data.ApplicationTransactionCounter.Value())
require.Equal(t, int64(2), data.ApplicationTransactionCounter.Value())
require.Equal(t, "B9B2B58202D37033", data.ApplicationCryptogram.Value())

}
24 changes: 12 additions & 12 deletions field/composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ func TestCompositePacking(t *testing.T) {

require.Equal(t, "AB", data.F1.Value())
require.Equal(t, "CD", data.F2.Value())
require.Equal(t, 12, data.F3.Value())
require.Equal(t, int64(12), data.F3.Value())
require.Nil(t, data.F11)
})

Expand All @@ -904,7 +904,7 @@ func TestCompositePacking(t *testing.T) {

require.Equal(t, "AB", data.F1.Value())
require.Equal(t, "CD", data.F2.Value())
require.Equal(t, 12, data.F3.Value())
require.Equal(t, int64(12), data.F3.Value())
require.Nil(t, data.F11)
})
}
Expand Down Expand Up @@ -1105,7 +1105,7 @@ func TestCompositePackingWithTags(t *testing.T) {

require.Equal(t, "AB", data.F1.Value())
require.Equal(t, "CD", data.F2.Value())
require.Equal(t, 12, data.F3.Value())
require.Equal(t, int64(12), data.F3.Value())
require.Equal(t, "YZ", data.F11.F1.Value())
})

Expand Down Expand Up @@ -1138,7 +1138,7 @@ func TestCompositePackingWithTags(t *testing.T) {

require.Equal(t, "AB", data.F1.Value())
require.Nil(t, data.F2)
require.Equal(t, 12, data.F3.Value())
require.Equal(t, int64(12), data.F3.Value())
})

t.Run("Unpack correctly ignores excess bytes in excess of the length described by the prefix", func(t *testing.T) {
Expand All @@ -1156,7 +1156,7 @@ func TestCompositePackingWithTags(t *testing.T) {

require.Equal(t, "AB", data.F1.Value())
require.Equal(t, "CD", data.F2.Value())
require.Equal(t, 12, data.F3.Value())
require.Equal(t, int64(12), data.F3.Value())
require.Nil(t, data.F11)
})
}
Expand Down Expand Up @@ -1365,7 +1365,7 @@ func TestCompositePackingWithBitmap(t *testing.T) {

require.Equal(t, "AB", data.F1.Value())
require.Equal(t, "CD", data.F2.Value())
require.Equal(t, 12, data.F3.Value())
require.Equal(t, int64(12), data.F3.Value())
require.Equal(t, "YZ", data.F11.F1.Value())
})

Expand All @@ -1381,7 +1381,7 @@ func TestCompositePackingWithBitmap(t *testing.T) {
require.NoError(t, composite.Unmarshal(data))

require.Equal(t, "AB", data.F1.Value())
require.Equal(t, 12, data.F3.Value())
require.Equal(t, int64(12), data.F3.Value())
require.Nil(t, data.F11)
})

Expand All @@ -1400,7 +1400,7 @@ func TestCompositePackingWithBitmap(t *testing.T) {

require.Equal(t, "AB", data.F1.Value())
require.Equal(t, "CD", data.F2.Value())
require.Equal(t, 12, data.F3.Value())
require.Equal(t, int64(12), data.F3.Value())
require.Nil(t, data.F11)
})

Expand Down Expand Up @@ -1444,7 +1444,7 @@ func TestCompositePackingWithBitmap(t *testing.T) {

require.Equal(t, "AB", data.F1.Value())
require.Equal(t, "CD", data.F2.Value())
require.Equal(t, 12, data.F3.Value())
require.Equal(t, int64(12), data.F3.Value())
require.Equal(t, "YZ", data.F11.F1.Value())
})

Expand All @@ -1460,7 +1460,7 @@ func TestCompositePackingWithBitmap(t *testing.T) {
require.NoError(t, composite.Unmarshal(data))

require.Equal(t, "AB", data.F1.Value())
require.Equal(t, 12, data.F3.Value())
require.Equal(t, int64(12), data.F3.Value())
require.Nil(t, data.F11)
})

Expand All @@ -1479,7 +1479,7 @@ func TestCompositePackingWithBitmap(t *testing.T) {

require.Equal(t, "AB", data.F1.Value())
require.Equal(t, "CD", data.F2.Value())
require.Equal(t, 12, data.F3.Value())
require.Equal(t, int64(12), data.F3.Value())
require.Nil(t, data.F11)
})
}
Expand Down Expand Up @@ -1743,7 +1743,7 @@ func TestCompositeJSONConversion(t *testing.T) {

require.Equal(t, "AB", data.F1.Value())
require.Nil(t, data.F2)
require.Equal(t, 12, data.F3.Value())
require.Equal(t, int64(12), data.F3.Value())
require.Equal(t, "YZ", data.F11.F1.Value())
})

Expand Down
34 changes: 17 additions & 17 deletions field/numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var _ json.Marshaler = (*Numeric)(nil)
var _ json.Unmarshaler = (*Numeric)(nil)

type Numeric struct {
value int
value int64
spec *Spec
}

Expand All @@ -24,7 +24,7 @@ func NewNumeric(spec *Spec) *Numeric {
}
}

func NewNumericValue(val int) *Numeric {
func NewNumericValue(val int64) *Numeric {
return &Numeric{
value: val,
}
Expand All @@ -46,7 +46,7 @@ func (f *Numeric) SetBytes(b []byte) error {
f.value = 0
} else {
// otherwise parse the raw to an int
val, err := strconv.Atoi(string(b))
val, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return utils.NewSafeError(err, "failed to convert into number")
}
Expand All @@ -60,29 +60,29 @@ func (f *Numeric) Bytes() ([]byte, error) {
if f == nil {
return nil, nil
}
return []byte(strconv.Itoa(f.value)), nil
return []byte(strconv.FormatInt(f.value, 10)), nil
}

func (f *Numeric) String() (string, error) {
if f == nil {
return "", nil
}
return strconv.Itoa(f.value), nil
return strconv.FormatInt(f.value, 10), nil
}

func (f *Numeric) Value() int {
func (f *Numeric) Value() int64 {
if f == nil {
return 0
}
return f.value
}

func (f *Numeric) SetValue(v int) {
func (f *Numeric) SetValue(v int64) {
f.value = v
}

func (f *Numeric) Pack() ([]byte, error) {
data := []byte(strconv.Itoa(f.value))
data := []byte(strconv.FormatInt(f.value, 10))

if f.spec.Pad != nil {
data = f.spec.Pad.Pad(data, f.spec.Length)
Expand Down Expand Up @@ -138,17 +138,17 @@ func (f *Numeric) Unmarshal(v interface{}) error {

switch val.Kind() { //nolint:exhaustive
case reflect.String:
str := strconv.Itoa(f.value)
str := strconv.FormatInt(f.value, 10)
val.SetString(str)
case reflect.Int:
case reflect.Int64:
val.SetInt(int64(f.value))
default:
return fmt.Errorf("unsupported reflect.Value type: %s", val.Kind())
}
case *string:
str := strconv.Itoa(f.value)
str := strconv.FormatInt(f.value, 10)
*val = str
case *int:
case *int64:
*val = f.value
case *Numeric:
val.value = f.value
Expand All @@ -159,7 +159,7 @@ func (f *Numeric) Unmarshal(v interface{}) error {
return nil
}

func (f *Numeric) Marshal(v interface{}) error {
func (f *Numeric) Marshal(v any) error {
if v == nil || reflect.ValueOf(v).IsZero() {
f.value = 0
return nil
Expand All @@ -168,18 +168,18 @@ func (f *Numeric) Marshal(v interface{}) error {
switch v := v.(type) {
case *Numeric:
f.value = v.value
case int:
case int64:
f.value = v
case *int:
case *int64:
f.value = *v
case string:
val, err := strconv.Atoi(v)
val, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return utils.NewSafeError(err, "failed to convert sting value into number")
}
f.value = val
case *string:
val, err := strconv.Atoi(*v)
val, err := strconv.ParseInt(*v, 10, 64)
if err != nil {
return utils.NewSafeError(err, "failed to convert sting value into number")
}
Expand Down
Loading

0 comments on commit add4df1

Please sign in to comment.