Skip to content

Commit

Permalink
make experiment with simplified structs work
Browse files Browse the repository at this point in the history
  • Loading branch information
alovak committed Sep 6, 2023
1 parent c20dc2c commit 5d010c2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
34 changes: 34 additions & 0 deletions exp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package iso8583_test

import (
"testing"

"github.com/moov-io/iso8583"
"github.com/moov-io/iso8583/specs"
"github.com/stretchr/testify/require"
)

func TestStructWithTypes(t *testing.T) {
type authRequestData struct {
MTI string `index:"0"`
PrimaryAccountNumber string `index:"2"`
ProcessingCode int `index:"3"`
}

t.Run("pack", func(t *testing.T) {
authRequest := &authRequestData{
MTI: "0110",
PrimaryAccountNumber: "4242424242424242",
ProcessingCode: 200000,
}

message := iso8583.NewMessage(specs.Spec87ASCII)
err := message.Marshal(authRequest)
require.NoError(t, err)

packed, err := message.Pack()
require.NoError(t, err)

require.Equal(t, "011060000000000000000000000000000000164242424242424242200000", string(packed))
})
}
26 changes: 26 additions & 0 deletions field/numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"

"github.com/moov-io/iso8583/utils"
Expand Down Expand Up @@ -143,6 +144,31 @@ func (f *Numeric) Unmarshal(v interface{}) error {
}

func (f *Numeric) SetData(data interface{}) error {
if v, ok := data.(reflect.Value); ok {
switch v.Kind() {

Check failure on line 148 in field/numeric.go

View workflow job for this annotation

GitHub Actions / Go Build (ubuntu-latest, stable)

missing cases in switch of type reflect.Kind: reflect.Invalid, reflect.Bool, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.Array, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer|reflect.Ptr, reflect.Slice, reflect.Struct, reflect.UnsafePointer (exhaustive)

Check failure on line 148 in field/numeric.go

View workflow job for this annotation

GitHub Actions / Go Build (ubuntu-latest, oldstable)

missing cases in switch of type reflect.Kind: reflect.Invalid, reflect.Bool, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.Array, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer|reflect.Ptr, reflect.Slice, reflect.Struct, reflect.UnsafePointer (exhaustive)
case reflect.Int:
f.value = int(v.Int())
case reflect.String:
val, err := strconv.Atoi(v.String())
if err != nil {
return utils.NewSafeError(err, "failed to convert into number")
}
f.value = val
default:
return fmt.Errorf("data does not match required *Numeric type")
}
return nil
}

// if v.Kind() == reflect.Int {
// }
// if v, ok := data.(reflect.Value); ok {
// if v.CanSet() {
// v.Set(reflect.ValueOf(f.value))
// return nil
// }
// }

if data == nil {
return nil
}
Expand Down
15 changes: 15 additions & 0 deletions field/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"reflect"

"github.com/moov-io/iso8583/utils"
)
Expand Down Expand Up @@ -129,6 +130,20 @@ func (f *String) Unmarshal(v interface{}) error {
}

func (f *String) SetData(data interface{}) error {
if v, ok := data.(reflect.Value); ok {
switch v.Kind() {

Check failure on line 134 in field/string.go

View workflow job for this annotation

GitHub Actions / Go Build (ubuntu-latest, stable)

missing cases in switch of type reflect.Kind: reflect.Invalid, reflect.Bool, reflect.Uintptr, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.Array, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer|reflect.Ptr, reflect.Slice, reflect.Struct, reflect.UnsafePointer (exhaustive)

Check failure on line 134 in field/string.go

View workflow job for this annotation

GitHub Actions / Go Build (ubuntu-latest, oldstable)

missing cases in switch of type reflect.Kind: reflect.Invalid, reflect.Bool, reflect.Uintptr, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.Array, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer|reflect.Ptr, reflect.Slice, reflect.Struct, reflect.UnsafePointer (exhaustive)
case reflect.String:
f.value = v.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
f.value = fmt.Sprintf("%d", v.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
f.value = fmt.Sprintf("%d", v.Uint())
default:
return fmt.Errorf("unsupported type %v", v.Kind())
}
return nil
}

if data == nil {
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ func (m *Message) Marshal(v interface{}) error {
continue
}

err = messageField.Marshal(dataField.Interface())
err = messageField.Marshal(dataField)

// err = messageField.Marshal(dataField.Interface())
if err != nil {
return fmt.Errorf("failed to set value to field %d: %w", fieldIndex, err)
}
Expand Down

0 comments on commit 5d010c2

Please sign in to comment.