Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mfdeveloper508 committed Oct 2, 2023
1 parent 0293e3a commit 90dd1d9
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 64 deletions.
17 changes: 7 additions & 10 deletions field/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,8 @@ func (f *Binary) Unmarshal(v interface{}) error {

switch val.Kind() { //nolint:exhaustive
case reflect.String:
if !val.CanSet() {
return fmt.Errorf("reflect.Value of the data can not be change")
}

str := hex.EncodeToString(f.value)
val.SetString(str)
val.SetString(hex.EncodeToString(f.value))
case reflect.Slice:
if !val.CanSet() {
return fmt.Errorf("reflect.Value of the data can not be change")
}

val.SetBytes(f.value)
default:
return fmt.Errorf("unsupported reflect.Value type: %s", val.Kind())
Expand Down Expand Up @@ -185,12 +176,18 @@ func (f *Binary) Marshal(v interface{}) error {

f.value = buf
case []byte:
if v == nil || len(v) == 0 {
f.value = nil
return nil
}

f.SetBytes(v)
case *[]byte:
if v == nil {
f.value = nil
return nil
}

f.SetBytes(*v)
default:
return fmt.Errorf("data does not match required *Binary or (string, *string, []byte, *[]byte) type")
Expand Down
6 changes: 3 additions & 3 deletions field/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,16 @@ func TestBinaryFieldUnmarshal(t *testing.T) {
val6 := reflect.ValueOf(val2)
err = str.Unmarshal(val6)
require.Error(t, err)
require.Equal(t, "reflect.Value of the data can not be change", err.Error())
require.Equal(t, "cannot set reflect.Value of type string", err.Error())

val7 := reflect.ValueOf(&val2)
err = str.Unmarshal(val7)
require.Error(t, err)
require.Equal(t, "data does not match required reflect.Value type", err.Error())
require.Equal(t, "cannot set reflect.Value of type ptr", err.Error())

err = str.Unmarshal(nil)
require.Error(t, err)
require.Equal(t, "data does not match required *Binary or (*string, *[]byte) type", err.Error())
require.Equal(t, "unsupported type: expected *Binary, *string, *[]byte, or reflect.Value, got <nil>", err.Error())
}

func TestBinaryFieldMarshal(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion field/bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (f *Bitmap) Unmarshal(v interface{}) error {

bmap, ok := v.(*Bitmap)
if !ok {
return fmt.Errorf("data does not match required *Bitmap type")
return fmt.Errorf("unsupported type: expected *Bitmap, got %T", v)
}

bmap.data = f.data
Expand Down
2 changes: 1 addition & 1 deletion field/composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ func TestCompositePacking(t *testing.T) {
err = composite.Unmarshal(data)

require.Error(t, err)
require.EqualError(t, err, "failed to get data from field 1: data does not match required *String or *string type")
require.EqualError(t, err, "failed to get data from field 1: unsupported type: expected *String, *string, or reflect.Value, got *field.Numeric")
})

t.Run("Unpack returns an error on failure of subfield to unpack bytes", func(t *testing.T) {
Expand Down
22 changes: 11 additions & 11 deletions field/hex.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,19 @@ func (f *Hex) SetData(data interface{}) error {
func (f *Hex) Unmarshal(v interface{}) error {
switch val := v.(type) {
case reflect.Value:
if !val.CanSet() {
return fmt.Errorf("cannot set reflect.Value of type %s", val.Kind())
}

switch val.Kind() { //nolint:exhaustive
case reflect.String:
if !val.CanSet() {
return fmt.Errorf("reflect.Value of the data can not be change")
}

str, _ := f.String()
val.SetString(str)
case reflect.Slice:
if !val.CanSet() {
return fmt.Errorf("reflect.Value of the data can not be change")
}

buf, _ := f.Bytes()
val.SetBytes(buf)
default:
return fmt.Errorf("data does not match required reflect.Value type")
return fmt.Errorf("unsupported reflect.Value type: %s", val.Kind())
}
case *string:
*val, _ = f.String()
Expand All @@ -155,7 +151,7 @@ func (f *Hex) Unmarshal(v interface{}) error {
case *Hex:
val.value = f.value
default:
return fmt.Errorf("data does not match required *Hex or (*string, *[]byte) type")
return fmt.Errorf("unsupported type: expected *Hex, *string, *[]byte, or reflect.Value, got %T", v)
}

return nil
Expand All @@ -175,7 +171,6 @@ func (f *Hex) Marshal(v interface{}) error {
}

f.value = v
hex.EncodeToString([]byte(v))
case *string:
if v == nil {
f.value = ""
Expand All @@ -184,6 +179,11 @@ func (f *Hex) Marshal(v interface{}) error {

f.value = *v
case []byte:
if v == nil || len(v) == 0 {
f.value = ""
return nil
}

f.SetBytes(v)
case *[]byte:
if v == nil {
Expand Down
6 changes: 3 additions & 3 deletions field/hex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,16 @@ func TestHexFieldUnmarshal(t *testing.T) {
val6 := reflect.ValueOf(val2)
err = str.Unmarshal(val6)
require.Error(t, err)
require.Equal(t, "reflect.Value of the data can not be change", err.Error())
require.Equal(t, "cannot set reflect.Value of type string", err.Error())

val7 := reflect.ValueOf(&val2)
err = str.Unmarshal(val7)
require.Error(t, err)
require.Equal(t, "data does not match required reflect.Value type", err.Error())
require.Equal(t, "cannot set reflect.Value of type ptr", err.Error())

err = str.Unmarshal(nil)
require.Error(t, err)
require.Equal(t, "data does not match required *Hex or (*string, *[]byte) type", err.Error())
require.Equal(t, "unsupported type: expected *Hex, *string, *[]byte, or reflect.Value, got <nil>", err.Error())
}

func TestHexFieldMarshal(t *testing.T) {
Expand Down
16 changes: 6 additions & 10 deletions field/numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,18 @@ func (f *Numeric) SetData(data interface{}) error {
func (f *Numeric) Unmarshal(v interface{}) error {
switch val := v.(type) {
case reflect.Value:
if !val.CanSet() {
return fmt.Errorf("cannot set reflect.Value of type %s", val.Kind())
}

switch val.Kind() { //nolint:exhaustive
case reflect.String:
if !val.CanSet() {
return fmt.Errorf("reflect.Value of the data can not be change")
}

str := strconv.Itoa(f.value)
val.SetString(str)
case reflect.Int:
if !val.CanSet() {
return fmt.Errorf("reflect.Value of the data can not be change")
}

val.SetInt(int64(f.value))
default:
return fmt.Errorf("data does not match required reflect.Value type")
return fmt.Errorf("unsupported reflect.Value type: %s", val.Kind())
}
case *string:
str := strconv.Itoa(f.value)
Expand All @@ -157,7 +153,7 @@ func (f *Numeric) Unmarshal(v interface{}) error {
case *Numeric:
val.value = f.value
default:
return fmt.Errorf("data does not match required *Numeric or *int type")
return fmt.Errorf("unsupported type: expected *Numeric, *int, or reflect.Value, got %T", v)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions field/numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ func TestNumericFieldUnmarshal(t *testing.T) {
val6 := reflect.ValueOf(val2)
err = str.Unmarshal(val6)
require.Error(t, err)
require.Equal(t, "reflect.Value of the data can not be change", err.Error())
require.Equal(t, "cannot set reflect.Value of type string", err.Error())

val7 := reflect.ValueOf(&val2)
err = str.Unmarshal(val7)
require.Error(t, err)
require.Equal(t, "data does not match required reflect.Value type", err.Error())
require.Equal(t, "cannot set reflect.Value of type ptr", err.Error())

err = str.Unmarshal(nil)
require.Error(t, err)
require.Equal(t, "data does not match required *Numeric or *int type", err.Error())
require.Equal(t, "unsupported type: expected *Numeric, *int, or reflect.Value, got <nil>", err.Error())
}

func TestNumericFieldMarshal(t *testing.T) {
Expand Down
16 changes: 6 additions & 10 deletions field/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,22 @@ func (f *String) SetData(data interface{}) error {
func (f *String) Unmarshal(v interface{}) error {
switch val := v.(type) {
case reflect.Value:
if !val.CanSet() {
return fmt.Errorf("cannot set reflect.Value of type %s", val.Kind())
}

switch val.Kind() { //nolint:exhaustive
case reflect.String:
if !val.CanSet() {
return fmt.Errorf("reflect.Value of the data can not be change")
}

val.SetString(f.value)
case reflect.Int:
if !val.CanSet() {
return fmt.Errorf("reflect.Value of the data can not be change")
}

i, err := strconv.Atoi(f.value)
if err != nil {
return fmt.Errorf("failed to convert string to int: %w", err)
}

val.SetInt(int64(i))
default:
return fmt.Errorf("data does not match required reflect.Value type")
return fmt.Errorf("unsupported reflect.Value type: %s", val.Kind())
}
case *string:
*val = f.value
Expand All @@ -150,7 +146,7 @@ func (f *String) Unmarshal(v interface{}) error {
case *String:
val.value = f.value
default:
return fmt.Errorf("data does not match required *String or *string type")
return fmt.Errorf("unsupported type: expected *String, *string, or reflect.Value, got %T", v)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions field/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,16 @@ func TestStringFieldUnmarshal(t *testing.T) {
val6 := reflect.ValueOf(val2)
err = str.Unmarshal(val6)
require.Error(t, err)
require.Equal(t, "reflect.Value of the data can not be change", err.Error())
require.Equal(t, "cannot set reflect.Value of type string", err.Error())

val7 := reflect.ValueOf(&val2)
err = str.Unmarshal(val7)
require.Error(t, err)
require.Equal(t, "data does not match required reflect.Value type", err.Error())
require.Equal(t, "cannot set reflect.Value of type ptr", err.Error())

err = str.Unmarshal(nil)
require.Error(t, err)
require.Equal(t, "data does not match required *String or *string type", err.Error())
require.Equal(t, "unsupported type: expected *String, *string, or reflect.Value, got <nil>", err.Error())
}

func TestStringFieldMarshal(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions field/track1.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (f *Track1) Unmarshal(v interface{}) error {

track, ok := v.(*Track1)
if !ok {
return fmt.Errorf("data does not match required *Track1 type")
return fmt.Errorf("unsupported type: expected *Track1, got %T", v)
}

track.FixedLength = f.FixedLength
Expand All @@ -144,7 +144,7 @@ func (f *Track1) Marshal(v interface{}) error {

track, ok := v.(*Track1)
if !ok {
return fmt.Errorf("data does not match required *Track type")
return fmt.Errorf("unsupported type: expected *Track1, got %T", v)
}

f.FixedLength = track.FixedLength
Expand Down
4 changes: 2 additions & 2 deletions field/track2.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (f *Track2) Unmarshal(v interface{}) error {

track, ok := v.(*Track2)
if !ok {
return fmt.Errorf("data does not match required *Track2 type")
return fmt.Errorf("unsupported type: expected *Track2, got %T", v)
}

track.PrimaryAccountNumber = f.PrimaryAccountNumber
Expand All @@ -140,7 +140,7 @@ func (f *Track2) Marshal(v interface{}) error {

track, ok := v.(*Track2)
if !ok {
return fmt.Errorf("data does not match required *Track type")
return fmt.Errorf("unsupported type: expected *Track2, got %T", v)
}

f.PrimaryAccountNumber = track.PrimaryAccountNumber
Expand Down
4 changes: 2 additions & 2 deletions field/track3.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (f *Track3) Unmarshal(v interface{}) error {

track, ok := v.(*Track3)
if !ok {
return fmt.Errorf("data does not match required *Track3 type")
return fmt.Errorf("unsupported type: expected *Track3, got %T", v)
}

track.PrimaryAccountNumber = f.PrimaryAccountNumber
Expand All @@ -137,7 +137,7 @@ func (f *Track3) Marshal(v interface{}) error {

track, ok := v.(*Track3)
if !ok {
return fmt.Errorf("data does not match required *Track type")
return fmt.Errorf("unsupported type: expected *Track3, got %T", v)
}

f.FormatCode = track.FormatCode
Expand Down
6 changes: 3 additions & 3 deletions field/track_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestTrack1(t *testing.T) {
t.Run("Returns an error on mismatch of track type", func(t *testing.T) {
track := NewTrack1(track1Spec)
err := track.Marshal(NewStringValue("hello"))
require.EqualError(t, err, "data does not match required *Track type")
require.EqualError(t, err, "unsupported type: expected *Track1, got *field.String")
})

t.Run("Unmarshal gets track values into data parameter", func(t *testing.T) {
Expand Down Expand Up @@ -305,7 +305,7 @@ func TestTrack2TypedAPI(t *testing.T) {
t.Run("Returns an error on mismatch of track type", func(t *testing.T) {
track := NewTrack2(track2Spec)
err := track.Marshal(NewStringValue("hello"))
require.EqualError(t, err, "data does not match required *Track type")
require.EqualError(t, err, "unsupported type: expected *Track2, got *field.String")
})

t.Run("Unmarshal gets track values into data parameter", func(t *testing.T) {
Expand Down Expand Up @@ -516,7 +516,7 @@ func TestTrack3TypedAPI(t *testing.T) {
t.Run("Returns an error on mismatch of track type", func(t *testing.T) {
track := NewTrack3(track3Spec)
err := track.Marshal(NewStringValue("hello"))
require.EqualError(t, err, "data does not match required *Track type")
require.EqualError(t, err, "unsupported type: expected *Track3, got *field.String")
})

t.Run("Unmarshal gets track values into data parameter", func(t *testing.T) {
Expand Down

0 comments on commit 90dd1d9

Please sign in to comment.